Search in sources :

Example 6 with BcosResponse

use of org.fisco.bcos.channel.dto.BcosResponse 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 7 with BcosResponse

use of org.fisco.bcos.channel.dto.BcosResponse in project web3sdk by FISCO-BCOS.

the class ChannelEthereumService method sendSpecial.

public String sendSpecial(Request request) 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.trace("bcos request:{} {}", bcosRequest.getMessageID(), objectMapper.writeValueAsString(request));
    logger.trace("bcos response:{} {} {}", bcosRequest.getMessageID(), response.getErrorCode(), response.getContent());
    if (response.getErrorCode() == 0) {
        if (response.getContent().contains("error")) {
            Response t = objectMapper.readValue(response.getContent(), Response.class);
            throw new ResponseExcepiton(t.getError().getCode(), t.getError().getMessage());
        } else {
            String[] resultArray = response.getContent().split("result");
            String resultStr = resultArray[1];
            if ("\"".equals(resultStr.substring(2, 3)))
                return resultStr.substring(3, resultStr.length() - 3);
            else
                return resultStr.substring(2, resultStr.length() - 2);
        }
    } else {
        throw new IOException(response.getErrorMessage());
    }
}
Also used : Response(org.fisco.bcos.web3j.protocol.core.Response) BcosResponse(org.fisco.bcos.channel.dto.BcosResponse) BcosResponse(org.fisco.bcos.channel.dto.BcosResponse) IOException(java.io.IOException) BcosRequest(org.fisco.bcos.channel.dto.BcosRequest)

Example 8 with BcosResponse

use of org.fisco.bcos.channel.dto.BcosResponse in project web3sdk by FISCO-BCOS.

the class BcosResponseCallback method onTimeout.

public void onTimeout() {
    // logger.error("Processing bcos message timeout:{}");
    BcosResponse response = new BcosResponse();
    response.setErrorCode(ChannelMessageError.MESSAGE_TIMEOUT.getError());
    response.setErrorMessage("Processing bcos message timeout");
    response.setContent("");
    onResponse(response);
}
Also used : BcosResponse(org.fisco.bcos.channel.dto.BcosResponse)

Example 9 with BcosResponse

use of org.fisco.bcos.channel.dto.BcosResponse in project web3sdk by FISCO-BCOS.

the class Service method onReceiveEthereumMessage.

public void onReceiveEthereumMessage(ChannelHandlerContext ctx, BcosMessage message) {
    BcosResponseCallback callback = (BcosResponseCallback) seq2Callback.get(message.getSeq());
    if (callback != null) {
        if (callback.getTimeout() != null) {
            callback.getTimeout().cancel();
        }
        logger.trace(" receive ethereum response, seq: {}, result: {}, content: {}", message.getSeq(), message.getResult(), new String(message.getData()));
        BcosResponse response = new BcosResponse();
        if (message.getResult() != 0) {
            response.setErrorMessage("BcosResponse error");
        }
        response.setErrorCode(message.getResult());
        response.setMessageID(message.getSeq());
        response.setContent(new String(message.getData()));
        callback.onResponse(response);
        seq2Callback.remove(message.getSeq());
    } else {
        logger.debug("no callback push message, seq: {}", message.getSeq());
    }
}
Also used : BcosResponse(org.fisco.bcos.channel.dto.BcosResponse)

Example 10 with BcosResponse

use of org.fisco.bcos.channel.dto.BcosResponse in project web3sdk by FISCO-BCOS.

the class Service method sendEthereumMessage.

public BcosResponse sendEthereumMessage(BcosRequest request, TransactionSucCallback transactionSucCallback) {
    class Callback extends BcosResponseCallback {

        private transient BcosResponse ethereumResponse;

        private transient Semaphore semaphore = new Semaphore(1, true);

        Callback() {
            try {
                semaphore.acquire(1);
            } catch (InterruptedException e) {
                logger.error("error:", e);
                Thread.currentThread().interrupt();
            }
        }

        @Override
        public void onResponse(BcosResponse response) {
            ethereumResponse = response;
            semaphore.release();
        }
    }
    Callback callback = new Callback();
    asyncSendEthereumMessage(request, callback, transactionSucCallback);
    try {
        callback.semaphore.acquire(1);
    } catch (InterruptedException e) {
        logger.error("system error:", e);
        Thread.currentThread().interrupt();
    }
    return callback.ethereumResponse;
}
Also used : BcosResponse(org.fisco.bcos.channel.dto.BcosResponse) ConnectionCallback(org.fisco.bcos.channel.handler.ConnectionCallback) EventLogPushCallback(org.fisco.bcos.channel.event.filter.EventLogPushCallback) Semaphore(java.util.concurrent.Semaphore)

Aggregations

BcosResponse (org.fisco.bcos.channel.dto.BcosResponse)11 IOException (java.io.IOException)7 MessageDecodingException (org.fisco.bcos.web3j.protocol.exceptions.MessageDecodingException)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 ByteBuf (io.netty.buffer.ByteBuf)4 BcosResponseCallback (org.fisco.bcos.channel.client.BcosResponseCallback)4 BcosMessage (org.fisco.bcos.channel.dto.BcosMessage)4 BcosRequest (org.fisco.bcos.channel.dto.BcosRequest)3 SocketChannel (io.netty.channel.socket.SocketChannel)2 Timeout (io.netty.util.Timeout)2 TimerTask (io.netty.util.TimerTask)2 InetSocketAddress (java.net.InetSocketAddress)2 Semaphore (java.util.concurrent.Semaphore)2 EventLogPushCallback (org.fisco.bcos.channel.event.filter.EventLogPushCallback)2 ConnectionCallback (org.fisco.bcos.channel.handler.ConnectionCallback)2 ChannelPrococolExceiption (org.fisco.bcos.channel.protocol.ChannelPrococolExceiption)2 EnumChannelProtocolVersion (org.fisco.bcos.channel.protocol.EnumChannelProtocolVersion)2 Request (org.fisco.bcos.web3j.protocol.core.Request)2 ContractCallException (org.fisco.bcos.web3j.tx.exceptions.ContractCallException)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1