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()));
}
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"));
}
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);
}
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());
}
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()));
}
Aggregations