Search in sources :

Example 1 with BlockNumber

use of org.fisco.bcos.web3j.protocol.core.methods.response.BlockNumber in project web3sdk by FISCO-BCOS.

the class JsonRpc2_0Web3j method getBlockNumberCache.

@Override
public BigInteger getBlockNumberCache() {
    if (getLocalBlockNumber().intValue() == 1) {
        try {
            BlockNumber blockNumber = getBlockNumber().sendAsync().get();
            setBlockNumber(blockNumber.getBlockNumber());
        } catch (Exception e) {
            logger.error("Exception: " + e);
        }
    }
    return getLocalBlockNumber().add(new BigInteger(BlockLimit.blockLimit.toString()));
}
Also used : BigInteger(java.math.BigInteger) BlockNumber(org.fisco.bcos.web3j.protocol.core.methods.response.BlockNumber) IOException(java.io.IOException)

Example 2 with BlockNumber

use of org.fisco.bcos.web3j.protocol.core.methods.response.BlockNumber in project web3sdk by FISCO-BCOS.

the class ManagedTransactionTester method prepareBlockNumberRequest.

@SuppressWarnings("unchecked")
void prepareBlockNumberRequest() throws IOException {
    BlockNumber ethBlockNumber = new BlockNumber();
    ethBlockNumber.setResult("0x1");
    Request<?, BlockNumber> ethBlockNumberRequest = mock(Request.class);
    when(ethBlockNumberRequest.send()).thenReturn(ethBlockNumber);
    when(web3j.getBlockNumber()).thenReturn((Request) ethBlockNumberRequest);
    when(web3j.getBlockLimit()).thenReturn(new BigInteger("1"));
}
Also used : BigInteger(java.math.BigInteger) BlockNumber(org.fisco.bcos.web3j.protocol.core.methods.response.BlockNumber)

Example 3 with BlockNumber

use of org.fisco.bcos.web3j.protocol.core.methods.response.BlockNumber in project web3sdk by FISCO-BCOS.

the class ConnectionCallback method queryBlockNumber.

private void queryBlockNumber(ChannelHandlerContext ctx) throws JsonProcessingException {
    final String host = ChannelHandlerContextHelper.getPeerHost(ctx);
    String seq = channelService.newSeq();
    BcosMessage bcosMessage = new BcosMessage();
    bcosMessage.setType((short) ChannelMessageType.CHANNEL_RPC_REQUEST.getType());
    bcosMessage.setSeq(seq);
    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(channelService);
    Request<Integer, BlockNumber> request = new Request<>("getBlockNumber", Arrays.asList(channelService.getGroupId()), channelEthereumService, BlockNumber.class);
    bcosMessage.setData(ObjectMapperFactory.getObjectMapper().writeValueAsBytes(request));
    ByteBuf byteBuf = ctx.alloc().buffer();
    bcosMessage.writeHeader(byteBuf);
    bcosMessage.writeExtra(byteBuf);
    String content = new String(bcosMessage.getData());
    logger.info(" query block number host: {}, seq: {}, content: {}", host, seq, content);
    channelService.getSeq2Callback().put(seq, new BcosResponseCallback() {

        @Override
        public void onResponse(BcosResponse response) {
            try {
                BlockNumber blockNumber = ObjectMapperFactory.getObjectMapper().readValue(response.getContent(), BlockNumber.class);
                SocketChannel socketChannel = (SocketChannel) ctx.channel();
                InetSocketAddress socketAddress = socketChannel.remoteAddress();
                channelService.getNodeToBlockNumberMap().put(socketAddress.getAddress().getHostAddress() + socketAddress.getPort(), blockNumber.getBlockNumber());
                logger.info(" query blocknumer, host:{}, blockNumber: {} ", host, blockNumber.getBlockNumber());
            } catch (Exception e) {
                logger.error(" query blocknumer failed, host: {}, message: {} ", host, e.getMessage());
                throw new MessageDecodingException(response.getContent());
            }
        }
    });
    ctx.writeAndFlush(byteBuf);
}
Also used : BcosResponseCallback(org.fisco.bcos.channel.client.BcosResponseCallback) SocketChannel(io.netty.channel.socket.SocketChannel) BcosResponse(org.fisco.bcos.channel.dto.BcosResponse) InetSocketAddress(java.net.InetSocketAddress) Request(org.fisco.bcos.web3j.protocol.core.Request) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) ByteBuf(io.netty.buffer.ByteBuf) MessageDecodingException(org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) BigInteger(java.math.BigInteger) BcosMessage(org.fisco.bcos.channel.dto.BcosMessage) MessageDecodingException(org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException) BlockNumber(org.fisco.bcos.web3j.protocol.core.methods.response.BlockNumber)

Example 4 with BlockNumber

use of org.fisco.bcos.web3j.protocol.core.methods.response.BlockNumber in project web3sdk by FISCO-BCOS.

the class Web3jApITest method getBlockNumber.

@Test
public void getBlockNumber() throws IOException, InterruptedException, ExecutionException {
    BlockNumber blockNumber = web3j.getBlockNumber().send();
    assertNotNull(blockNumber.getBlockNumber());
    blockNumber = web3j.getBlockNumber().sendAsync().get();
    assertNotNull(blockNumber.getBlockNumber());
}
Also used : BlockNumber(org.fisco.bcos.web3j.protocol.core.methods.response.BlockNumber) Test(org.junit.Test)

Example 5 with BlockNumber

use of org.fisco.bcos.web3j.protocol.core.methods.response.BlockNumber in project web3sdk by FISCO-BCOS.

the class JsonRpc2_0Web3j method getBlockLimit.

@Override
public BigInteger getBlockLimit() {
    if (getLocalBlockNumber().intValue() == 1) {
        try {
            BlockNumber blockNumber = getBlockNumber().sendAsync().get();
            setBlockNumber(blockNumber.getBlockNumber());
        } catch (Exception e) {
            logger.error("Exception: " + e);
        }
    }
    return getLocalBlockNumber().add(new BigInteger(BlockLimit.blockLimit.toString()));
}
Also used : BigInteger(java.math.BigInteger) BlockNumber(org.fisco.bcos.web3j.protocol.core.methods.response.BlockNumber) IOException(java.io.IOException)

Aggregations

BlockNumber (org.fisco.bcos.web3j.protocol.core.methods.response.BlockNumber)5 BigInteger (java.math.BigInteger)4 IOException (java.io.IOException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ByteBuf (io.netty.buffer.ByteBuf)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 InetSocketAddress (java.net.InetSocketAddress)1 BcosResponseCallback (org.fisco.bcos.channel.client.BcosResponseCallback)1 BcosMessage (org.fisco.bcos.channel.dto.BcosMessage)1 BcosResponse (org.fisco.bcos.channel.dto.BcosResponse)1 ChannelEthereumService (org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService)1 Request (org.fisco.bcos.web3j.protocol.core.Request)1 MessageDecodingException (org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException)1 Test (org.junit.Test)1