org.web3j.protocol.core.methods.response.EthBlock Java Examples

The following examples show how to use org.web3j.protocol.core.methods.response.EthBlock. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonRpc2_0Rx.java    From web3j with Apache License 2.0 6 votes vote down vote up
private Flowable<EthBlock> replayBlocksFlowableSync(
        DefaultBlockParameter startBlock,
        DefaultBlockParameter endBlock,
        boolean containsFullTransactionObjects,
        boolean isAscending) {
    BigInteger startBlockNumber;
    BigInteger endBlockNumber;
    try {
        startBlockNumber = getBlockNumber(startBlock);
        endBlockNumber = getBlockNumber(endBlock);
    } catch (IOException e) {
        return Flowable.error(e);
    }

    return Flowables.range(startBlockNumber, endBlockNumber, isAscending)
            .map(DefaultBlockParameterNumber::new)
            .map(number -> web3j.ethGetBlockByNumber(number, containsFullTransactionObjects))
            .flatMap(Request::flowable);
}
 
Example #2
Source File: ResponseTest.java    From web3j with Apache License 2.0 6 votes vote down vote up
@Test
public void testErrorResponseComplexData() {
    buildResponse(
            "{"
                    + "  \"jsonrpc\":\"2.0\","
                    + "  \"id\":1,"
                    + "  \"error\":{"
                    + "    \"code\":-32602,"
                    + "    \"message\":\"Invalid address length, expected 40 got 64 bytes\","
                    + "    \"data\":{\"foo\":\"bar\"}"
                    + "  }"
                    + "}");

    EthBlock ethBlock = deserialiseResponse(EthBlock.class);
    assertTrue(ethBlock.hasError());
    assertEquals(ethBlock.getError().getData(), ("{\"foo\":\"bar\"}"));
}
 
Example #3
Source File: AssetDefinitionService.java    From alpha-wallet-android with MIT License 6 votes vote down vote up
private void processLogs(EventDefinition ev, List<EthLog.LogResult> logs)
{
    if (logs.size() == 0) return; //early return
    int chainId = ev.contract.addresses.keySet().iterator().next();
    Web3j web3j = getWeb3jService(chainId);

    for (EthLog.LogResult ethLog : logs)
    {
        String selectVal = eventUtils.getSelectVal(ev, ethLog);
        EthBlock txBlock = eventUtils.getTransactionDetails(((Log)ethLog.get()).getBlockHash(), web3j).blockingGet();

        long blockTime = txBlock.getBlock().getTimestamp().longValue();
        if (eventCallback != null) eventCallback.receivedEvent(ev.attributeName, ev.parentAttribute.getSyntaxVal(selectVal), blockTime, chainId);
        storeEventValue(ev, ethLog, ev.parentAttribute, blockTime, selectVal);
    }
}
 
Example #4
Source File: Eth1DepositManager.java    From teku with Apache License 2.0 6 votes vote down vote up
private SafeFuture<Void> processStart(
    final EthBlock.Block headBlock, final ReplayDepositsResult replayDepositsResult) {
  BigInteger startBlockNumber = replayDepositsResult.getFirstUnprocessedBlockNumber();
  if (headBlock.getNumber().compareTo(startBlockNumber) >= 0) {
    if (isBlockAfterMinGenesis(headBlock)) {
      return headAfterMinGenesisMode(headBlock, replayDepositsResult);
    } else {
      return headBeforeMinGenesisMode(headBlock, startBlockNumber);
    }
  }

  if (replayDepositsResult.isPastMinGenesisBlock()) {
    depositProcessingController.startSubscription(startBlockNumber);
  } else {
    // preGenesisSubscription starts processing from the next block
    preGenesisSubscription(replayDepositsResult.getLastProcessedBlockNumber());
  }
  return SafeFuture.COMPLETE;
}
 
Example #5
Source File: Web3jBlock.java    From eventeum with Apache License 2.0 6 votes vote down vote up
public Web3jBlock(EthBlock.Block web3jBlock, String nodeName) {
    final ModelMapper modelMapper = ModelMapperFactory.getInstance().createModelMapper();
    modelMapper.typeMap(
            EthBlock.Block.class, Web3jBlock.class)
            .addMappings(mapper -> {
                mapper.skip(Web3jBlock::setTransactions);

                //Nonce can be null which throws exception in web3j when
                //calling getNonce (because of attempted hex conversion)
                if (web3jBlock.getNonceRaw() == null) {
                    mapper.skip(Web3jBlock::setNonce);
                }
            });

    modelMapper.map(web3jBlock, this);

    transactions = convertTransactions(web3jBlock.getTransactions());

    this.nodeName = nodeName;
}
 
Example #6
Source File: ResponseTest.java    From web3j with Apache License 2.0 6 votes vote down vote up
@Test
public void testErrorResponse() {
    buildResponse(
            "{"
                    + "  \"jsonrpc\":\"2.0\","
                    + "  \"id\":1,"
                    + "  \"error\":{"
                    + "    \"code\":-32602,"
                    + "    \"message\":\"Invalid address length, expected 40 got 64 bytes\","
                    + "    \"data\":null"
                    + "  }"
                    + "}");

    EthBlock ethBlock = deserialiseResponse(EthBlock.class);
    assertTrue(ethBlock.hasError());
    assertEquals(
            ethBlock.getError(),
            (new Response.Error(-32602, "Invalid address length, expected 40 got 64 bytes")));
}
 
Example #7
Source File: GetAllPrivacyMarkerTransactionHashes.java    From besu with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> execute(final NodeRequests node) {
  final Besu besu = node.privacy().getBesuClient();
  final List<String> toReturn = new ArrayList<>();
  try {
    final long blockchainHeight = besu.ethBlockNumber().send().getBlockNumber().longValueExact();
    for (int i = 0; i <= blockchainHeight; i++) {
      besu.ethGetBlockByNumber(DefaultBlockParameter.valueOf(BigInteger.valueOf(i)), true)
          .send()
          .getBlock()
          .getTransactions()
          .forEach(
              t -> {
                if (((EthBlock.TransactionObject) t)
                    .getTo()
                    .equals(Address.DEFAULT_PRIVACY.toString())) {
                  toReturn.add(((EthBlock.TransactionObject) t).getHash());
                }
              });
    }
  } catch (IOException e) {
    throw new RuntimeException(e);
  }

  return toReturn;
}
 
Example #8
Source File: JsonRpc2_0RxTest.java    From web3j with Apache License 2.0 6 votes vote down vote up
@Test
void testReplayBlocksFlowableWhenIOExceptionOnBlockResolving() throws IOException {
    Web3j web3j = mock(Web3j.class, RETURNS_DEEP_STUBS);
    when(web3j.ethGetBlockByNumber(any(), anyBoolean()).send())
            .thenThrow(new IOException("fail"));

    JsonRpc2_0Rx rpc = new JsonRpc2_0Rx(web3j, Executors.newSingleThreadScheduledExecutor());

    Flowable<EthBlock> flowable =
            rpc.replayBlocksFlowable(
                    mock(DefaultBlockParameter.class),
                    mock(DefaultBlockParameter.class),
                    false,
                    false);
    EthBlock expected = mock(EthBlock.class);
    EthBlock actual = flowable.onErrorReturnItem(expected).blockingFirst();

    assertSame(expected, actual, "unexpected returned block");
}
 
Example #9
Source File: EthereumRecordCursor.java    From presto-ethereum with Apache License 2.0 6 votes vote down vote up
public EthereumRecordCursor(List<EthereumColumnHandle> columnHandles, EthBlock block, EthereumTable table, Web3j web3j) {
    this.columnHandles = columnHandles;
    this.table = table;
    this.web3j = web3j;
    this.suppliers = Collections.emptyList();

    fieldToColumnIndex = new int[columnHandles.size()];
    for (int i = 0; i < columnHandles.size(); i++) {
        EthereumColumnHandle columnHandle = columnHandles.get(i);
        fieldToColumnIndex[i] = columnHandle.getOrdinalPosition();
    }

    // TODO: handle failure upstream
    this.block = requireNonNull(block, "block is null");
    this.blockIter = ImmutableList.of(block).iterator();
    this.txIter = block.getBlock().getTransactions().iterator();
    this.logIter = new EthereumLogLazyIterator(block, web3j);
}
 
Example #10
Source File: DepositFetcher.java    From teku with Apache License 2.0 5 votes vote down vote up
private SafeFuture<Void> postDepositEvents(
    List<SafeFuture<EthBlock.Block>> blockRequests,
    Map<BlockNumberAndHash, List<DepositContract.DepositEventEventResponse>> depositEventsByBlock,
    BigInteger fromBlock,
    BigInteger toBlock) {
  BigInteger from = fromBlock;
  // First process completed requests using iteration.
  // Avoid StackOverflowException when there is a long string of requests already completed.
  while (!blockRequests.isEmpty() && blockRequests.get(0).isDone()) {
    final EthBlock.Block block = blockRequests.remove(0).join();

    // Fetch any empty blocks between this deposit block and the previous one (or start of range)
    final BigInteger to = block.getNumber().subtract(BigInteger.ONE);
    eth1BlockFetcher.fetch(from, to);
    from = block.getNumber().add(BigInteger.ONE);

    postEventsForBlock(block, depositEventsByBlock);
  }

  // All requests have completed and been processed.
  if (blockRequests.isEmpty()) {
    // Fetch any empty blocks between the last deposit and end of the range
    eth1BlockFetcher.fetch(from, toBlock);
    return SafeFuture.COMPLETE;
  }

  BigInteger remainingRangeStart = from;
  // Reached a block request that isn't complete so wait for it and recurse back into this method.
  return blockRequests
      .get(0)
      .thenCompose(
          block ->
              postDepositEvents(
                  blockRequests, depositEventsByBlock, remainingRangeStart, toBlock));
}
 
Example #11
Source File: EthQueryExecutor.java    From eth-jdbc-connector with Apache License 2.0 5 votes vote down vote up
private Block getBlockByNumber(String blockNumber) throws IOException, Exception {
    LOGGER.info("Getting block - " + blockNumber + " Information ");
    EthBlock block = web3jClient
            .ethGetBlockByNumber(DefaultBlockParameter.valueOf(new BigInteger(blockNumber)), true).send();

    if (block == null || block.hasError())
        throw new Exception("blockNumber not found : " + blockNumber);

    return block.getBlock();
}
 
Example #12
Source File: ResponseTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testEthBlockNull() {
    buildResponse(
            "{\n"
                    + "  \"result\": null\n"
                    + "}"
    );

    EthBlock ethBlock = deserialiseResponse(EthBlock.class);
    assertNull(ethBlock.getBlock());
}
 
Example #13
Source File: PubSubBlockSubscriptionStrategy.java    From eventeum with Apache License 2.0 5 votes vote down vote up
private EthBlock getEthBlock(String blockHash) {
    return getRetryTemplate().execute((context) -> {
        try {
            final EthBlock block = web3j.ethGetBlockByHash(blockHash, true).send();

            if (block == null || block.getBlock() == null) {
                throw new BlockchainException(String.format("Block not found. Hash: %s", blockHash));
            }

            return block;
        } catch (IOException e) {
            throw new BlockchainException("Unable to retrieve block details", e);
        }
    });
}
 
Example #14
Source File: CoreIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testEthGetBlockByNumberReturnTransactionObjects() throws Exception {
    EthBlock ethBlock =
            web3j.ethGetBlockByNumber(DefaultBlockParameter.valueOf(config.validBlock()), true)
                    .send();

    EthBlock.Block block = ethBlock.getBlock();
    assertNotNull(ethBlock.getBlock());
    assertEquals(block.getNumber(), (config.validBlock()));
    assertEquals(
            block.getTransactions().size(), (config.validBlockTransactionCount().intValue()));
}
 
Example #15
Source File: Web3jEth1Provider.java    From teku with Apache License 2.0 5 votes vote down vote up
@Override
public SafeFuture<EthBlock.Block> getGuaranteedEth1Block(final UnsignedLong blockNumber) {
  return getEth1Block(blockNumber)
      .exceptionallyCompose(
          (err) -> {
            LOG.debug("Retrying Eth1 request for block: {}", blockNumber, err);
            return asyncRunner
                .getDelayedFuture(
                    Constants.ETH1_INDIVIDUAL_BLOCK_RETRY_TIMEOUT, TimeUnit.MILLISECONDS)
                .thenCompose(__ -> getGuaranteedEth1Block(blockNumber));
          });
}
 
Example #16
Source File: MinimumGenesisTimeBlockFinder.java    From teku with Apache License 2.0 5 votes vote down vote up
private SafeFuture<EthBlock.Block> binarySearchLoop(final SearchContext searchContext) {
  if (searchContext.low.compareTo(searchContext.high) <= 0) {
    final UnsignedLong mid = searchContext.low.plus(searchContext.high).dividedBy(TWO);
    return eth1Provider
        .getEth1Block(mid)
        .thenCompose(
            midBlock -> {
              final int cmp = compareBlockTimestampToMinGenesisTime(midBlock);
              if (cmp < 0) {
                searchContext.low = mid.plus(ONE);
                return binarySearchLoop(searchContext);
              } else if (cmp > 0) {
                if (mid.equals(ZERO)) {
                  // The very first eth1 block is after eth2 min genesis so just use it
                  return SafeFuture.completedFuture(midBlock);
                }
                searchContext.high = mid.minus(ONE);
                return binarySearchLoop(searchContext);
              } else {
                // This block has exactly the min genesis time
                return SafeFuture.completedFuture(midBlock);
              }
            });
  } else {
    // Completed search
    return eth1Provider.getEth1Block(searchContext.low);
  }
}
 
Example #17
Source File: JsonRpc2_0Web3j.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Override
public Flowable<EthBlock> replayPastBlocksFlowable(
        DefaultBlockParameter startBlock,
        DefaultBlockParameter endBlock,
        boolean fullTransactionObjects,
        boolean ascending) {
    return web3jRx.replayBlocksFlowable(
            startBlock, endBlock, fullTransactionObjects, ascending);
}
 
Example #18
Source File: PubSubBlockchainSubscriptionStrategyTest.java    From eventeum with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws IOException {
    this.mockWeb3j = mock(Web3j.class);

    mockNewHeadsNotification = mock(NewHeadsNotification.class);
    mockEventStoreService = mock(EventStoreService.class);
    when(mockNewHeadsNotification.getParams()).thenReturn(new NewHeadNotificationParameter());

    mockNewHead = mock(NewHead.class);
    when(mockNewHead.getHash()).thenReturn(BLOCK_HASH);

    blockPublishProcessor = PublishProcessor.create();
    when(mockWeb3j.newHeadsNotifications()).thenReturn(blockPublishProcessor);

    mockEthBlock = mock(EthBlock.class);
    final EthBlock.Block mockBlock = mock(EthBlock.Block.class);

    when(mockBlock.getNumber()).thenReturn(BLOCK_NUMBER);
    when(mockBlock.getHash()).thenReturn(BLOCK_HASH);
    when(mockBlock.getTimestamp()).thenReturn(Numeric.toBigInt(BLOCK_TIMESTAMP));
    when(mockEthBlock.getBlock()).thenReturn(mockBlock);

    final Request<?, EthBlock> mockRequest = mock(Request.class);
    doReturn(mockRequest).when(mockWeb3j).ethGetBlockByHash(BLOCK_HASH, true);

    when(mockRequest.send()).thenReturn(mockEthBlock);

    underTest = new PubSubBlockSubscriptionStrategy(mockWeb3j, NODE_NAME,
            mockEventStoreService, new DummyAsyncTaskService());
}
 
Example #19
Source File: JsonRpc2_0Rx.java    From web3j with Apache License 2.0 5 votes vote down vote up
public Flowable<EthBlock> replayBlocksFlowable(
        DefaultBlockParameter startBlock,
        DefaultBlockParameter endBlock,
        boolean fullTransactionObjects,
        boolean ascending) {
    // We use a scheduler to ensure this Flowable runs asynchronously for users to be
    // consistent with the other Flowables
    return replayBlocksFlowableSync(startBlock, endBlock, fullTransactionObjects, ascending)
            .subscribeOn(scheduler);
}
 
Example #20
Source File: MinimumGenesisTimeBlockFinderTest.java    From teku with Apache License 2.0 5 votes vote down vote up
private Block[] withBlockTimestamps(final long... timestamps) {
  final EthBlock.Block[] blocks = new EthBlock.Block[timestamps.length];
  for (int blockNumber = 0; blockNumber < timestamps.length; blockNumber++) {
    blocks[blockNumber] = block(blockNumber, timestamps[blockNumber]);
  }
  return blocks;
}
 
Example #21
Source File: JsonRpc2_0RxTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
private EthBlock createBlockWithTransactions(int blockNumber, List<Transaction> transactions) {
    EthBlock ethBlock = new EthBlock();
    EthBlock.Block block = new EthBlock.Block();
    block.setNumber(Numeric.encodeQuantity(BigInteger.valueOf(blockNumber)));

    List<EthBlock.TransactionResult> transactionResults =
            transactions.stream()
                    .map(it -> (EthBlock.TransactionResult<Transaction>) () -> it)
                    .collect(Collectors.toList());
    block.setTransactions(transactionResults);

    ethBlock.setResult(block);
    return ethBlock;
}
 
Example #22
Source File: JsonRpc2_0RxTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplayBlocksDescendingFlowable() throws Exception {

    List<EthBlock> ethBlocks = Arrays.asList(createBlock(2), createBlock(1), createBlock(0));

    OngoingStubbing<EthBlock> stubbing =
            when(web3jService.send(any(Request.class), eq(EthBlock.class)));
    for (EthBlock ethBlock : ethBlocks) {
        stubbing = stubbing.thenReturn(ethBlock);
    }

    Flowable<EthBlock> flowable =
            web3j.replayPastBlocksFlowable(
                    new DefaultBlockParameterNumber(BigInteger.ZERO),
                    new DefaultBlockParameterNumber(BigInteger.valueOf(2)),
                    false,
                    false);

    CountDownLatch transactionLatch = new CountDownLatch(ethBlocks.size());
    CountDownLatch completedLatch = new CountDownLatch(1);

    List<EthBlock> results = new ArrayList<>(ethBlocks.size());
    Disposable subscription =
            flowable.subscribe(
                    result -> {
                        results.add(result);
                        transactionLatch.countDown();
                    },
                    throwable -> fail(throwable.getMessage()),
                    () -> completedLatch.countDown());

    transactionLatch.await(1, TimeUnit.SECONDS);
    assertEquals(results, (ethBlocks));

    subscription.dispose();

    completedLatch.await(1, TimeUnit.SECONDS);
    assertTrue(subscription.isDisposed());
}
 
Example #23
Source File: EthGetBlockTransaction.java    From besu with Apache License 2.0 5 votes vote down vote up
@Override
public Block execute(final NodeRequests node) {
  try {
    final EthBlock result =
        node.eth().ethGetBlockByNumber(blockParameter, fullTransactionObjects).send();
    assertThat(result).isNotNull();
    assertThat(result.hasError()).isFalse();
    return result.getBlock();
  } catch (final IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example #24
Source File: EnsResolverTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private void configureLatestBlock(long timestamp) throws IOException {
    EthBlock.Block block = new EthBlock.Block();
    block.setTimestamp(Numeric.encodeQuantity(BigInteger.valueOf(timestamp)));
    EthBlock ethBlock = new EthBlock();
    ethBlock.setResult(block);

    when(web3jService.send(any(Request.class), eq(EthBlock.class)))
            .thenReturn(ethBlock);
}
 
Example #25
Source File: JsonRpc2_0Web3j.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Observable<EthBlock> catchUpToLatestBlockObservable(
        DefaultBlockParameter startBlock, boolean fullTransactionObjects,
        Observable<EthBlock> onCompleteObservable) {
    return web3jRx.catchUpToLatestBlockObservable(
            startBlock, fullTransactionObjects, onCompleteObservable);
}
 
Example #26
Source File: JsonRpc2_0Rx.java    From web3j with Apache License 2.0 5 votes vote down vote up
public Flowable<EthBlock> replayPastAndFutureBlocksFlowable(
        DefaultBlockParameter startBlock,
        boolean fullTransactionObjects,
        long pollingInterval) {

    return replayPastBlocksFlowable(
            startBlock,
            fullTransactionObjects,
            blockFlowable(fullTransactionObjects, pollingInterval));
}
 
Example #27
Source File: JsonRpc2_0Web3j.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Observable<EthBlock> replayBlocksObservable(
        DefaultBlockParameter startBlock, DefaultBlockParameter endBlock,
        boolean fullTransactionObjects, boolean ascending) {
    return web3jRx.replayBlocksObservable(startBlock, endBlock,
            fullTransactionObjects, ascending);
}
 
Example #28
Source File: EthereumRecordSet.java    From presto-ethereum with Apache License 2.0 5 votes vote down vote up
@Override
public RecordCursor cursor() {
    EthBlock block = null;
    try {
        block = web3j.ethGetBlockByNumber(DefaultBlockParameter.valueOf(BigInteger.valueOf(split.getBlockId())), true).send();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new EthereumRecordCursor(columnHandles, block, split.getTable(), web3j);
}
 
Example #29
Source File: EnsResolver.java    From web3j with Apache License 2.0 5 votes vote down vote up
boolean isSynced() throws Exception {
    EthSyncing ethSyncing = web3j.ethSyncing().send();
    if (ethSyncing.isSyncing()) {
        return false;
    } else {
        EthBlock ethBlock =
                web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false).send();
        long timestamp = ethBlock.getBlock().getTimestamp().longValueExact() * 1000;

        return System.currentTimeMillis() - syncThreshold < timestamp;
    }
}
 
Example #30
Source File: FlowableIT.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testReplayPastAndFutureBlocksFlowable() throws Exception {
    EthBlock ethBlock =
            web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false).send();
    BigInteger latestBlockNumber = ethBlock.getBlock().getNumber();
    run(
            web3j.replayPastAndFutureBlocksFlowable(
                    new DefaultBlockParameterNumber(latestBlockNumber.subtract(BigInteger.ONE)),
                    false));
}