Search in sources :

Example 1 with MessageDecodingException

use of org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException in project web3sdk by FISCO-BCOS.

the class ChannelEthereumService method send.

@Override
public <T extends Response> T send(Request request, Class<T> responseType) throws IOException {
    byte[] payload = objectMapper.writeValueAsBytes(request);
    BcosRequest bcosRequest = new BcosRequest();
    if (channelService.getOrgID() != null) {
        bcosRequest.setKeyID(channelService.getOrgID());
    } else {
        bcosRequest.setKeyID(channelService.getAgencyName());
    }
    bcosRequest.setBankNO("");
    bcosRequest.setContent(new String(payload));
    bcosRequest.setMessageID(channelService.newSeq());
    if (timeout != 0) {
        bcosRequest.setTimeout(timeout);
    }
    BcosResponse response;
    if (!request.isNeedTransCallback()) {
        response = channelService.sendEthereumMessage(bcosRequest);
    } else {
        response = channelService.sendEthereumMessage(bcosRequest, request.getTransactionSucCallback());
    }
    logger.debug("bcos request, seq:{}, method:{}", bcosRequest.getMessageID(), request.getMethod());
    logger.debug("bcos request:{} {}", bcosRequest.getMessageID(), objectMapper.writeValueAsString(request));
    logger.trace("bcos request:{} {}", bcosRequest.getMessageID(), objectMapper.writeValueAsString(request));
    logger.trace("bcos response:{} {} {}", bcosRequest.getMessageID(), response.getErrorCode(), response.getContent());
    if (response.getErrorCode() == 0) {
        try {
            T t = objectMapper.readValue(response.getContent(), responseType);
            if (t.getError() != null) {
                throw new IOException(t.getError().getMessage());
            }
            if (t.getResult() instanceof CallOutput) {
                CallOutput callResult = (CallOutput) t.getResult();
                Tuple2<Boolean, String> revertMessage = RevertResolver.tryResolveRevertMessage(callResult.getStatus(), callResult.getOutput());
                if (revertMessage.getValue1()) {
                    logger.debug(" revert message: {}", revertMessage.getValue2());
                // throw new ContractCallException(revertMessage.getValue2());
                }
                if (StatusCode.RevertInstruction.equals(callResult.getStatus())) {
                    ContractCallException contractCallException = new ContractCallException("The execution of the contract rolled back" + (revertMessage.getValue1() ? ", " + revertMessage.getValue2() : "") + ".");
                    contractCallException.setCallOutput(callResult);
                    throw contractCallException;
                }
                if (StatusCode.CallAddressError.equals(callResult.getStatus())) {
                    ContractCallException contractCallException = new ContractCallException("The contract address is incorrect.");
                    contractCallException.setCallOutput(callResult);
                    throw contractCallException;
                }
                if (!StatusCode.Success.equals(callResult.getStatus())) {
                    ContractCallException contractCallException = new ContractCallException(StatusCode.getStatusMessage(callResult.getStatus()));
                    contractCallException.setCallOutput(callResult);
                    throw contractCallException;
                }
            }
            return t;
        } catch (ContractCallException e) {
            throw e;
        } catch (Exception e) {
            logger.error("e: ", e);
            throw new MessageDecodingException(response.getContent());
        }
    } else {
        throw new IOException(response.getErrorMessage());
    }
}
Also used : BcosResponse(org.fisco.bcos.channel.dto.BcosResponse) CallOutput(org.fisco.bcos.web3j.protocol.core.methods.response.Call.CallOutput) MessageDecodingException(org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException) ContractCallException(org.fisco.bcos.web3j.tx.exceptions.ContractCallException) IOException(java.io.IOException) BcosRequest(org.fisco.bcos.channel.dto.BcosRequest) IOException(java.io.IOException) MessageDecodingException(org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException) ContractCallException(org.fisco.bcos.web3j.tx.exceptions.ContractCallException)

Example 2 with MessageDecodingException

use of org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException 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)

Aggregations

IOException (java.io.IOException)2 BcosResponse (org.fisco.bcos.channel.dto.BcosResponse)2 MessageDecodingException (org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ByteBuf (io.netty.buffer.ByteBuf)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 BigInteger (java.math.BigInteger)1 InetSocketAddress (java.net.InetSocketAddress)1 BcosResponseCallback (org.fisco.bcos.channel.client.BcosResponseCallback)1 BcosMessage (org.fisco.bcos.channel.dto.BcosMessage)1 BcosRequest (org.fisco.bcos.channel.dto.BcosRequest)1 ChannelEthereumService (org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService)1 Request (org.fisco.bcos.web3j.protocol.core.Request)1 BlockNumber (org.fisco.bcos.web3j.protocol.core.methods.response.BlockNumber)1 CallOutput (org.fisco.bcos.web3j.protocol.core.methods.response.Call.CallOutput)1 ContractCallException (org.fisco.bcos.web3j.tx.exceptions.ContractCallException)1