Java Code Examples for org.fisco.bcos.web3j.protocol.core.methods.response.BcosBlock#Block

The following examples show how to use org.fisco.bcos.web3j.protocol.core.methods.response.BcosBlock#Block . 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: Web3ApiService.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
/**
 * getBlockByNumber.
 *
 * @param blockNumber blockNumber
 */
public BcosBlock.Block getBlockByNumber(int groupId, BigInteger blockNumber) {
    if (blockNumberCheck(groupId, blockNumber)) {
        throw new FrontException(ConstantCode.BLOCK_NUMBER_ERROR);
    }
    BcosBlock.Block block;
    try {
        block = getWeb3j(groupId)
                .getBlockByNumber(DefaultBlockParameter.valueOf(blockNumber), true)
                .send()
                .getBlock();
    } catch (IOException e) {
        log.info("get blocknumber failed" + e.getMessage());
        log.error("getBlAockByNumber fail. blockNumber:{} , groupID: {}", blockNumber, groupId);
        throw new FrontException(ConstantCode.NODE_REQUEST_FAILED);
    }
    return block;
}
 
Example 2
Source File: Web3ApiService.java    From WeBASE-Front with Apache License 2.0 6 votes vote down vote up
/**
 * getBlockTransCntByNumber.
 *
 * @param blockNumber blockNumber
 */
public int getBlockTransCntByNumber(int groupId, BigInteger blockNumber) {
    int transCnt;
    try {
        if (blockNumberCheck(groupId, blockNumber)) {
            throw new FrontException("ConstantCode.NODE_REQUEST_FAILED");
        }
        BcosBlock.Block block = getWeb3j(groupId)
                .getBlockByNumber(DefaultBlockParameter.valueOf(blockNumber), true)
                .send()
                .getBlock();
        transCnt = block.getTransactions().size();
    } catch (IOException e) {
        log.error("getBlockTransCntByNumber fail. blockNumber:{} ", blockNumber);
        throw new FrontException(ConstantCode.NODE_REQUEST_FAILED);
    }
    return transCnt;
}
 
Example 3
Source File: Web3SDK2Wrapper.java    From WeEvent with Apache License 2.0 6 votes vote down vote up
private static void getTbBlockList(List<TbBlock> tbBlocks, BcosBlock.Block block) throws BrokerException {
    if (block == null) {
        log.error("query block failed, block is null.");
        throw new BrokerException(ErrorCode.WEB3SDK_RPC_ERROR);
    }

    String blockTimestamp = DataTypeUtils.getTimestamp(block.getTimestamp().longValue());

    int transactions = 0;
    if (!block.getTransactions().isEmpty()) {
        transactions = block.getTransactions().size();
    }
    int sealerIndex = Integer.parseInt(block.getSealer().substring(2), 16);
    TbBlock tbBlock = new TbBlock(block.getHash(), block.getNumber(), blockTimestamp,
            transactions, sealerIndex);
    tbBlock.setSealer(block.getSealer());
    tbBlocks.add(tbBlock);
}
 
Example 4
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 5
Source File: Web3ApiController.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
@ApiOperation(value = "getBlockByNumber", notes = "Get block information based on block height")
@ApiImplicitParam(name = "blockNumber", value = "blockNumber", required = true,
        dataType = "BigInteger", paramType = "path")
@GetMapping("/blockByNumber/{blockNumber}")
public BcosBlock.Block getBlockByNumber(@PathVariable int groupId,
        @PathVariable BigInteger blockNumber) {
    return web3ApiService.getBlockByNumber(groupId, blockNumber);
}
 
Example 6
Source File: Web3ApiController.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
@ApiOperation(value = "getBlockByHash", notes = "Get block information based on block hash")
@ApiImplicitParam(name = "blockHash", value = "blockHash", required = true, dataType = "String",
        paramType = "path")
@GetMapping("/blockByHash/{blockHash}")
public BcosBlock.Block getBlockByHash(@PathVariable int groupId,
        @PathVariable String blockHash) {
    return web3ApiService.getBlockByHash(groupId, blockHash);
}
 
Example 7
Source File: Web3ApiService.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
/**
 * getBlockByHash.
 *
 * @param blockHash blockHash
 */
public BcosBlock.Block getBlockByHash(int groupId, String blockHash) {
    BcosBlock.Block block;
    try {

        block = getWeb3j(groupId).getBlockByHash(blockHash, true)
                .send()
                .getBlock();
    } catch (IOException e) {
        log.error("getBlockByHash fail. blockHash:{} ", blockHash);
        throw new FrontException(ConstantCode.NODE_REQUEST_FAILED);
    }
    return block;
}
 
Example 8
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);
}