Search in sources :

Example 1 with TransactionException

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

the class Service method asyncSendEthereumMessage.

public void asyncSendEthereumMessage(BcosRequest request, BcosResponseCallback callback) {
    BcosMessage bcosMessage = new BcosMessage();
    bcosMessage.setSeq(request.getMessageID());
    bcosMessage.setResult(0);
    bcosMessage.setType((short) 0x12);
    bcosMessage.setData(request.getContent().getBytes());
    // select node
    try {
        ChannelConnections channelConnections = allChannelConnections.getAllChannelConnections().stream().filter(x -> x.getGroupId() == groupId).findFirst().get();
        if (channelConnections == null) {
            if (orgID != null) {
                logger.error("not found:{}", orgID);
                throw new TransactionException("not found orgID");
            } else {
                logger.error("not found:{}", agencyName);
                throw new TransactionException("not found agencyName");
            }
        }
        ChannelHandlerContext ctx = channelConnections.randomNetworkConnection(nodeToBlockNumberMap);
        ByteBuf out = ctx.alloc().buffer();
        bcosMessage.writeHeader(out);
        bcosMessage.writeExtra(out);
        seq2Callback.put(request.getMessageID(), callback);
        if (request.getTimeout() > 0) {
            final BcosResponseCallback callbackInner = callback;
            callback.setTimeout(timeoutHandler.newTimeout(new TimerTask() {

                BcosResponseCallback _callback = callbackInner;

                @Override
                public void run(Timeout timeout) throws Exception {
                    logger.error("process bcos message timeout, seq: {}, timeout: {}", bcosMessage.getSeq(), request.getTimeout());
                    // handle timer
                    _callback.onTimeout();
                }
            }, request.getTimeout(), TimeUnit.MILLISECONDS));
        }
        ctx.writeAndFlush(out);
        SocketChannel socketChannel = (SocketChannel) ctx.channel();
        InetSocketAddress socketAddress = socketChannel.remoteAddress();
        logger.debug("selected node {}:{} bcos request, seq:{}", socketAddress.getAddress().getHostAddress(), socketAddress.getPort(), bcosMessage.getSeq());
    } catch (Exception e) {
        logger.error(" error message:{}, error: {} ", e.getMessage(), e);
        BcosResponse response = new BcosResponse();
        response.setErrorCode(-1);
        response.setErrorMessage(e.getMessage() + " requset send failed! please check the log file content for reasons.");
        response.setContent("");
        response.setMessageID(request.getMessageID());
        if (callback.getTimeout() != null) {
            callback.getTimeout().cancel();
        }
        callback.onResponse(response);
    }
}
Also used : ChannelConnections(org.fisco.bcos.channel.handler.ChannelConnections) BcosMessage(org.fisco.bcos.channel.dto.BcosMessage) SocketChannel(io.netty.channel.socket.SocketChannel) TransactionException(org.fisco.bcos.web3j.protocol.exceptions.TransactionException) BcosResponse(org.fisco.bcos.channel.dto.BcosResponse) TimerTask(io.netty.util.TimerTask) Timeout(io.netty.util.Timeout) InetSocketAddress(java.net.InetSocketAddress) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TransactionException(org.fisco.bcos.web3j.protocol.exceptions.TransactionException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 2 with TransactionException

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

the class OkClient method deployOk.

/* deploy the contract,get address from blockchain */
@SuppressWarnings("deprecation")
public static void deployOk() {
    RemoteCall<Ok> deploy = Ok.deploy(web3j, credentials, gasPrice, gasLimit);
    Ok ok;
    try {
        ok = deploy.send();
        contractAddress = ok.getContractAddress();
        System.out.println("deploy contract address: " + contractAddress);
        logger.info("deploy contract address: " + contractAddress);
        final Resource contractResource = new ClassPathResource("contract.properties");
        PropertiesConfiguration prop = new PropertiesConfiguration(contractResource.getFile());
        prop.setProperty("ok_address", contractAddress);
        prop.save();
        System.out.println("deploy contract successful!");
    } catch (TransactionException e) {
        if ("0x19".equals(e.getStatus())) {
            System.out.println("non-authorized to deploy contracts!");
        } else {
            System.out.println(e.getMessage());
        }
    } catch (Exception e) {
        System.out.println("deploy failed! " + e.getMessage());
    }
}
Also used : TransactionException(org.fisco.bcos.web3j.protocol.exceptions.TransactionException) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ClassPathResource(org.springframework.core.io.ClassPathResource) TransactionException(org.fisco.bcos.web3j.protocol.exceptions.TransactionException)

Example 3 with TransactionException

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

the class TableTestClient method deployTableTest.

/* deploy the contract,get address from blockchain */
@SuppressWarnings("deprecation")
public static void deployTableTest() {
    RemoteCall<TableTest> deploy = TableTest.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit));
    TableTest tabletest;
    try {
        tabletest = deploy.send();
        contractAddress = tabletest.getContractAddress();
        System.out.println("deploy contract address: " + contractAddress);
        logger.info("deploy contract address: " + contractAddress);
        final Resource contractResource = new ClassPathResource("contract.properties");
        PropertiesConfiguration prop = new PropertiesConfiguration(contractResource.getFile());
        prop.setProperty("crud_address", contractAddress);
        prop.save();
        System.out.println("deploy contract successful!");
    } catch (TransactionException e) {
        if ("0x19".equals(e.getStatus())) {
            System.out.println("non-authorized to deploy contracts!");
        } else {
            System.out.println("deploy transaction is abnormal, please check the environment msg:" + e.getMessage());
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("deploy transaction is abnormal, please check the environment");
    }
}
Also used : StaticGasProvider(org.fisco.bcos.web3j.tx.gas.StaticGasProvider) TransactionException(org.fisco.bcos.web3j.protocol.exceptions.TransactionException) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ClassPathResource(org.springframework.core.io.ClassPathResource) TransactionException(org.fisco.bcos.web3j.protocol.exceptions.TransactionException)

Example 4 with TransactionException

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

the class PrecompiledCommon method handleTransactionReceiptForCRUD.

public static int handleTransactionReceiptForCRUD(TransactionReceipt receipt) throws TransactionException {
    String status = receipt.getStatus();
    if (!"0x0".equals(status)) {
        throw new TransactionException(StatusCode.getStatusMessage(status, receipt.getMessage()));
    }
    String output = receipt.getOutput();
    if (!"0x".equals(output)) {
        return new BigInteger(output.substring(2, output.length()), 16).intValue();
    } else {
        throw new TransactionException("Transaction is handled failure.");
    }
}
Also used : TransactionException(org.fisco.bcos.web3j.protocol.exceptions.TransactionException) BigInteger(java.math.BigInteger)

Example 5 with TransactionException

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

the class MixContractClient method deploymixContract.

/* deploy the contract,get address from blockchain */
@SuppressWarnings("deprecation")
public static void deploymixContract() {
    RemoteCall<MixContract> deploy = MixContract.deploy(web3j, credentials, gasPrice, gasLimit);
    MixContract mixContract;
    try {
        mixContract = deploy.send();
        contractAddress = mixContract.getContractAddress();
        System.out.println("deploy contract address: " + contractAddress);
        logger.info("deploy contract address: " + contractAddress);
        final Resource contractResource = new ClassPathResource("contract.properties");
        PropertiesConfiguration prop = new PropertiesConfiguration(contractResource.getFile());
        prop.setProperty("mix_address", contractAddress);
        prop.save();
        System.out.println("deploy contract successful!");
    } catch (TransactionException e) {
        if ("0x19".equals(e.getStatus())) {
            System.out.println("non-authorized to deploy contracts!");
        } else {
            System.out.println(e.getMessage());
        }
    } catch (Exception e) {
        System.out.println("deploy failed! " + e.getMessage());
    }
}
Also used : TransactionException(org.fisco.bcos.web3j.protocol.exceptions.TransactionException) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) ClassPathResource(org.springframework.core.io.ClassPathResource) TransactionException(org.fisco.bcos.web3j.protocol.exceptions.TransactionException)

Aggregations

TransactionException (org.fisco.bcos.web3j.protocol.exceptions.TransactionException)7 PropertiesConfiguration (org.apache.commons.configuration.PropertiesConfiguration)4 ClassPathResource (org.springframework.core.io.ClassPathResource)4 Resource (org.springframework.core.io.Resource)4 BigInteger (java.math.BigInteger)2 StaticGasProvider (org.fisco.bcos.web3j.tx.gas.StaticGasProvider)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ByteBuf (io.netty.buffer.ByteBuf)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 Timeout (io.netty.util.Timeout)1 TimerTask (io.netty.util.TimerTask)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InetSocketAddress (java.net.InetSocketAddress)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 CertificateException (java.security.cert.CertificateException)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1