Search in sources :

Example 1 with NodeVersion

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

the class ConnectionCallback method queryNodeVersion.

private void queryNodeVersion(ChannelHandlerContext ctx) throws JsonProcessingException {
    final String host = ChannelHandlerContextHelper.getPeerHost(ctx);
    String seq = UUID.randomUUID().toString().replaceAll("-", "");
    Request<?, NodeVersion> request = new Request<>("getClientVersion", Arrays.asList(), null, NodeVersion.class);
    byte[] payload = ObjectMapperFactory.getObjectMapper().writeValueAsBytes(request);
    String content = new String(payload);
    logger.info(" query node version host: {}, seq: {}, content: {}", host, seq, content);
    BcosMessage bcosMessage = new BcosMessage();
    bcosMessage.setType((short) ChannelMessageType.CHANNEL_RPC_REQUEST.getType());
    bcosMessage.setSeq(seq);
    bcosMessage.setResult(0);
    bcosMessage.setData(payload);
    ByteBuf byteBuf = ctx.alloc().buffer();
    bcosMessage.writeHeader(byteBuf);
    bcosMessage.writeExtra(byteBuf);
    BcosResponseCallback callback = new BcosResponseCallback() {

        @Override
        public void onResponse(BcosResponse response) {
            try {
                if (response.getErrorCode() == ChannelMessageError.MESSAGE_TIMEOUT.getError()) {
                    // The fisco node version number is below 2.1.0 when request timeout
                    ChannelHandlerContextHelper.setProtocolVersion(ctx, EnumChannelProtocolVersion.VERSION_1, "below-2.1.0-timeout");
                    logger.info(" query node version timeout, content: {}", response.getContent());
                    sendUpdateTopicMessage(ctx);
                    queryBlockNumber(ctx);
                    return;
                } else if (response.getErrorCode() != 0) {
                    logger.error(" fisco node version response, code: {}, message: {}", response.getErrorCode(), response.getErrorMessage());
                    throw new ChannelPrococolExceiption(" query node version failed, code: " + response.getErrorCode() + ", message: " + response.getErrorMessage());
                }
                Response<NodeVersion.Version> nodeVersion = ObjectMapperFactory.getObjectMapper().readValue(response.getContent(), NodeVersion.class);
                logger.info(" node: {}, content: {}", nodeVersion.getResult(), response.getContent());
                if (EnumNodeVersion.channelProtocolHandleShakeSupport(nodeVersion.getResult().getSupportedVersion())) {
                    // fisco node support channel protocol handshake, start it
                    logger.info(" support channel handshake node: {}, content: {}", nodeVersion.getResult(), response.getContent());
                    queryChannelProtocolVersion(ctx);
                } else {
                    // default channel protocol
                    ChannelHandlerContextHelper.setProtocolVersion(ctx, EnumChannelProtocolVersion.VERSION_1, nodeVersion.getResult().getSupportedVersion());
                    logger.info(" not support channel handshake set default ,node: {}, content: {}", nodeVersion.getResult(), response.getContent());
                    sendUpdateTopicMessage(ctx);
                    queryBlockNumber(ctx);
                // channelService.getEventLogFilterManager().sendFilter();
                }
            } catch (Exception e) {
                logger.error(" query node version failed, message: {}", e.getMessage());
                ctx.writeAndFlush("").addListener(ChannelFutureListener.CLOSE);
            }
        }
    };
    final BcosResponseCallback callbackInner = callback;
    callback.setTimeout(channelService.getTimeoutHandler().newTimeout(new TimerTask() {

        BcosResponseCallback _callback = callbackInner;

        @Override
        public void run(Timeout timeout) throws Exception {
            // handle timer
            _callback.onTimeout();
            logger.error("queryNodeVersion timeout, seq: {}", bcosMessage.getSeq());
        }
    }, queryNodeVersionTimeoutMS, TimeUnit.MILLISECONDS));
    channelService.getSeq2Callback().put(seq, callback);
    ctx.writeAndFlush(byteBuf);
}
Also used : BcosResponseCallback(org.fisco.bcos.channel.client.BcosResponseCallback) BcosResponse(org.fisco.bcos.channel.dto.BcosResponse) ChannelPrococolExceiption(org.fisco.bcos.channel.protocol.ChannelPrococolExceiption) Timeout(io.netty.util.Timeout) Request(org.fisco.bcos.web3j.protocol.core.Request) ByteBuf(io.netty.buffer.ByteBuf) MessageDecodingException(org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) EnumNodeVersion(org.fisco.bcos.fisco.EnumNodeVersion) NodeVersion(org.fisco.bcos.web3j.protocol.core.methods.response.NodeVersion) BcosMessage(org.fisco.bcos.channel.dto.BcosMessage) TimerTask(io.netty.util.TimerTask) EnumNodeVersion(org.fisco.bcos.fisco.EnumNodeVersion) EnumChannelProtocolVersion(org.fisco.bcos.channel.protocol.EnumChannelProtocolVersion) NodeVersion(org.fisco.bcos.web3j.protocol.core.methods.response.NodeVersion)

Example 2 with NodeVersion

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

the class Web3jApITest method getNodeVersion.

@Test
public void getNodeVersion() throws IOException {
    NodeVersion nodeVersion = web3j.getNodeVersion().send();
    assertNotNull(nodeVersion.getNodeVersion().getBuildTime());
    assertNotNull(nodeVersion.getNodeVersion().getBuildType());
    assertNotNull(nodeVersion.getNodeVersion().getGitBranch());
    assertNotNull(nodeVersion.getNodeVersion().getGitCommit());
    assertNotNull(nodeVersion.getNodeVersion().getVersion());
}
Also used : NodeVersion(org.fisco.bcos.web3j.protocol.core.methods.response.NodeVersion) Test(org.junit.Test)

Example 3 with NodeVersion

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

the class Contract method getTheTransactionManager.

public static TransactionManager getTheTransactionManager(Web3j web3j, Credentials credentials) {
    JsonRpc2_0Web3j jsonRpc2_0Web3j = (JsonRpc2_0Web3j) web3j;
    String chainId = "1";
    int groupId = 1;
    String version = "";
    String supportedVersion = "";
    NodeVersion.Version nodeVersion = null;
    try {
        groupId = jsonRpc2_0Web3j.getGroupId();
        nodeVersion = web3j.getNodeVersion().send().getNodeVersion();
        version = nodeVersion.getVersion();
        supportedVersion = nodeVersion.getSupportedVersion();
        if (EnumNodeVersion.BCOS_2_0_0_RC1.getVersion().equals(version) || EnumNodeVersion.BCOS_2_0_0_RC1.getVersion().equals(supportedVersion)) {
            version = EnumNodeVersion.BCOS_2_0_0_RC1.getVersion();
            logger.debug("fisco-bcos version:{}", version);
        } else {
            chainId = nodeVersion.getChainID();
            logger.debug("fisco-bcos version:{}, supported version:{}", version, supportedVersion);
        }
    } catch (Exception e) {
        logger.error("Query fisco-bcos version failed", e);
    }
    TransactionManager transactionManager = EnumNodeVersion.BCOS_2_0_0_RC1.getVersion().equals(version) ? new RawTransactionManager(web3j, credentials) : new ExtendedRawTransactionManager(web3j, credentials, BigInteger.valueOf(groupId), new BigInteger(chainId));
    transactionManager.setNodeVersion(nodeVersion);
    return transactionManager;
}
Also used : EnumNodeVersion(org.fisco.bcos.fisco.EnumNodeVersion) NodeVersion(org.fisco.bcos.web3j.protocol.core.methods.response.NodeVersion) BigInteger(java.math.BigInteger) JsonRpc2_0Web3j(org.fisco.bcos.web3j.protocol.core.JsonRpc2_0Web3j) ContractCallException(org.fisco.bcos.web3j.tx.exceptions.ContractCallException) IOException(java.io.IOException) TransactionException(org.fisco.bcos.web3j.protocol.exceptions.TransactionException)

Aggregations

NodeVersion (org.fisco.bcos.web3j.protocol.core.methods.response.NodeVersion)3 IOException (java.io.IOException)2 EnumNodeVersion (org.fisco.bcos.fisco.EnumNodeVersion)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ByteBuf (io.netty.buffer.ByteBuf)1 Timeout (io.netty.util.Timeout)1 TimerTask (io.netty.util.TimerTask)1 BigInteger (java.math.BigInteger)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 ChannelPrococolExceiption (org.fisco.bcos.channel.protocol.ChannelPrococolExceiption)1 EnumChannelProtocolVersion (org.fisco.bcos.channel.protocol.EnumChannelProtocolVersion)1 JsonRpc2_0Web3j (org.fisco.bcos.web3j.protocol.core.JsonRpc2_0Web3j)1 Request (org.fisco.bcos.web3j.protocol.core.Request)1 MessageDecodingException (org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException)1 TransactionException (org.fisco.bcos.web3j.protocol.exceptions.TransactionException)1 ContractCallException (org.fisco.bcos.web3j.tx.exceptions.ContractCallException)1 Test (org.junit.Test)1