Java Code Examples for org.fisco.bcos.web3j.protocol.core.methods.response.BcosBlock#getBlock()

The following examples show how to use org.fisco.bcos.web3j.protocol.core.methods.response.BcosBlock#getBlock() . 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: MockBlockTest.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void getBlockNumber() throws IOException {

    BcosBlock block = objectMapper.readValue(rawResponse, BcosBlock.class);
    block.setRawResponse(rawResponse);

    Web3j web3j = Web3j.build(web3jService);
    when(web3jService.send(any(Request.class), eq(BcosBlock.class))).thenReturn(block);

    BcosBlock mockBlocks =
            web3j.getBlockByNumber(DefaultBlockParameter.valueOf(new BigInteger("1")), true)
                    .send();
    BcosBlock.Block mockBlock = mockBlocks.getBlock();
    assertEquals(mockBlock.getNonce(), new BigInteger("0"));
    assertTrue(mockBlock.getNumber().intValue() == 1);
}
 
Example 2
Source File: Web3SDK2Wrapper.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
private static void generateTbTransHashListPage(Integer pageIndex, Integer pageSize, ListPage<TbTransHash> tbTransHashListPage, List<TbTransHash> tbTransHashes, BcosBlock bcosBlock) throws BrokerException {
    BcosBlock.Block block = bcosBlock.getBlock();
    if (block == null || CollectionUtils.isEmpty(block.getTransactions())) {
        log.error("query transaction from block failed. transaction in block is empty");
        throw new BrokerException(ErrorCode.WEB3SDK_RPC_ERROR);
    }

    Integer transCount = block.getTransactions().size();

    if (pageIndex < 1 || (pageIndex - 1) * pageSize > transCount) {
        log.error("pageIndex error.");
        throw new BrokerException("pageIndex error.");
    }
    Integer transSize = (transCount <= pageIndex * pageSize) ? (transCount - ((pageIndex - 1) * pageSize)) : pageSize;
    Integer transIndexStart = (pageIndex - 1) * pageSize;

    List<Transaction> transactionHashList = block.getTransactions().stream()
            .map(transactionResult -> (Transaction) transactionResult.get()).collect(Collectors.toList()).subList(transIndexStart, transSize + transIndexStart);
    transactionHashList.forEach(tx -> {
        TbTransHash tbTransHash = new TbTransHash(tx.getHash(), tx.getFrom(), tx.getTo(),
                tx.getBlockNumber(), DataTypeUtils.getTimestamp(bcosBlock.getBlock().getTimestamp().longValue()));
        tbTransHashes.add(tbTransHash);
    });
    tbTransHashListPage.setPageSize(transSize);
    tbTransHashListPage.setTotal(transCount);
    tbTransHashListPage.setPageData(tbTransHashes);
}