Search in sources :

Example 1 with TransactionReceiptWithProof

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

the class TransactionResourceTest method main.

public static void main(String[] args) throws Exception {
    // init the Service
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.setGroupId(Integer.parseInt(args[0]));
    // run the daemon service
    service.run();
    // init the client keys
    keyPair = Keys.createEcKeyPair();
    credentials = Credentials.create(keyPair);
    logger.info("-----> start TransactionResourceTest !");
    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);
    try {
        web3j = Web3j.build(channelEthereumService, Integer.parseInt(args[0]));
    } catch (Exception e) {
        System.out.println("Please provide groupID in the first paramters");
        System.exit(1);
    }
    try {
        if (args.length == 4) {
            BigInteger blockNumber = new BigInteger(args[2]);
            BigInteger transactionIndex = new BigInteger(args[3]);
            DefaultBlockParameter defaultBlockParameter = DefaultBlockParameter.valueOf(blockNumber);
            BcosTransaction bcosTransaction = web3j.getTransactionByBlockNumberAndIndex(defaultBlockParameter, transactionIndex).send();
            String transactionHash = bcosTransaction.getTransaction().get().getHash();
            BcosBlock block = web3j.getBlockByNumber(defaultBlockParameter, true).send();
            String transactionsRootHash = block.getBlock().getTransactionsRoot();
            System.out.println("transactionsRoot: " + transactionsRootHash);
            String receiptRootHash = block.getBlock().getReceiptsRoot();
            System.out.println("receiptRootHash : " + receiptRootHash);
            TransactionResource transactionResource = new TransactionResource(web3j);
            if ("getTrans".equals(args[1])) {
                TransactionWithProof transactionWithProof = web3j.getTransactionByHashWithProof(transactionHash).send();
                if (transactionWithProof == null) {
                    System.out.println("transactionWithProof == null");
                    System.exit(1);
                }
                System.out.println("***********Test getTransactionByHashWithProof************");
                List<MerkleProofUnit> transactionProof = transactionWithProof.getTransactionWithProof().getTxProof();
                System.out.println("transactionProof:" + transactionProof);
                TransactionWithProof newTransactionWithProof = transactionResource.getTransactionWithProof(transactionHash, transactionsRootHash);
                if (newTransactionWithProof == null) {
                    System.out.println("Test getTransactionByHashWithProof failed!");
                } else {
                    System.out.println(newTransactionWithProof.getTransactionWithProof().toString());
                    System.out.println("Test getTransactionByHashWithProof successfully!");
                }
            } else if ("getReceipt".equals(args[1])) {
                TransactionReceiptWithProof transactionReceiptWithProof = web3j.getTransactionReceiptByHashWithProof(transactionHash).send();
                if (transactionReceiptWithProof == null) {
                    System.out.println("transactionReceiptWithProof == null");
                    System.exit(1);
                }
                List<MerkleProofUnit> transactionReceiptProof = transactionReceiptWithProof.getTransactionReceiptWithProof().getReceiptProof();
                System.out.println("receiptProof:" + transactionReceiptProof);
                System.out.println("***********Test getReceiptByHashWithProof************");
                TransactionReceiptWithProof newTransactionReceiptWithProof = transactionResource.getTransactionReceiptWithProof(transactionHash, receiptRootHash);
                if (newTransactionReceiptWithProof == null) {
                    System.out.println("Test getReceiptByHashWithProof failed!");
                } else {
                    System.out.println(newTransactionReceiptWithProof.getTransactionReceiptWithProof().toString());
                    System.out.println("Test getReceiptByHashWithProof successfully!");
                }
            } else if ("getAll".equals(args[1])) {
                System.out.println("***********Test getTransactionAndReceiptWithProof************");
                ImmutablePair<TransactionWithProof, TransactionReceiptWithProof> pair = transactionResource.getTransactionAndReceiptWithProof(transactionHash, transactionsRootHash, receiptRootHash);
                if (pair == null) {
                    System.out.println("Test getAll failed!");
                } else {
                    System.out.println("Test getAll successful!");
                }
            } else {
                System.out.println("Command not found!");
            }
        } else {
            System.out.println("Please choose follow commands:\n getTrans or getReceipt!");
        }
    } catch (Exception e) {
        System.out.println(e.getLocalizedMessage());
        System.exit(1);
    }
    System.exit(0);
}
Also used : BcosTransaction(org.fisco.bcos.web3j.protocol.core.methods.response.BcosTransaction) TransactionReceiptWithProof(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceiptWithProof) TransactionWithProof(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionWithProof) MerkleProofUnit(org.fisco.bcos.web3j.protocol.core.methods.response.MerkleProofUnit) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) DefaultBlockParameter(org.fisco.bcos.web3j.protocol.core.DefaultBlockParameter) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BcosBlock(org.fisco.bcos.web3j.protocol.core.methods.response.BcosBlock) BigInteger(java.math.BigInteger) List(java.util.List)

Example 2 with TransactionReceiptWithProof

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

the class TransactionResource method getTransactionReceiptWithProof.

public TransactionReceiptWithProof getTransactionReceiptWithProof(String transactionHash, String rootHash) throws IOException {
    TransactionReceiptWithProof transactionReceiptWithProof = web3j.getTransactionReceiptByHashWithProof(transactionHash).send();
    if (transactionReceiptWithProof.getTransactionReceiptWithProof() == null) {
        return null;
    }
    TransactionReceipt transactionReceipt = transactionReceiptWithProof.getTransactionReceiptWithProof().getTransactionReceipt();
    logger.debug("Receipt {}", transactionReceipt.toString());
    // transaction index
    String index = transactionReceipt.getTransactionIndexRaw();
    BigInteger indexValue = Numeric.toBigInt(index);
    byte[] byteIndex = RlpEncoder.encode(RlpString.create(indexValue));
    String receiptRlp = ReceiptEncoder.encode(transactionReceipt);
    logger.debug("ReceiptRlp:{}", receiptRlp);
    String rlpHash = Hash.sha3(receiptRlp);
    logger.debug("ReceiptRlpHash:{}", rlpHash);
    String input = Numeric.toHexString(byteIndex) + rlpHash.substring(2);
    logger.info("ReceiptWithIndex:{}", input);
    String proof = Merkle.calculateMerkleRoot(transactionReceiptWithProof.getTransactionReceiptWithProof().getReceiptProof(), input);
    // System.out.println("MerkleRoot: " + proof);
    if (!proof.equals(rootHash)) {
        logger.debug("MerkleRoot:{}", proof);
        logger.debug("TransRoot :{}", rootHash);
        return null;
    }
    return transactionReceiptWithProof;
}
Also used : TransactionReceiptWithProof(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceiptWithProof) TransactionReceipt(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt) BigInteger(java.math.BigInteger) RlpString(org.fisco.bcos.web3j.rlp.RlpString)

Example 3 with TransactionReceiptWithProof

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

the class TransactionResource method getTransactionAndReceiptWithProof.

public ImmutablePair<TransactionWithProof, TransactionReceiptWithProof> getTransactionAndReceiptWithProof(String transactionHash, String transactionsRoot, String receiptsRoot) throws IOException {
    TransactionWithProof transactionWithProof = getTransactionWithProof(transactionHash, transactionsRoot);
    if (transactionWithProof == null) {
        return null;
    }
    TransactionReceiptWithProof transactionReceiptWithProof = getTransactionReceiptWithProof(transactionHash, receiptsRoot);
    if (transactionReceiptWithProof == null) {
        return null;
    }
    String indexFromTransaction = transactionWithProof.getTransactionWithProof().getTransaction().getTransactionIndexRaw();
    String indexFromReceipt = transactionReceiptWithProof.getTransactionReceiptWithProof().getTransactionReceipt().getTransactionIndexRaw();
    logger.debug("indexFromTransaction:{}, indexFromReceipt:{}", indexFromTransaction, indexFromReceipt);
    if (!indexFromTransaction.equals(indexFromReceipt)) {
        return null;
    }
    return new ImmutablePair<>(transactionWithProof, transactionReceiptWithProof);
}
Also used : TransactionReceiptWithProof(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceiptWithProof) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) TransactionWithProof(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionWithProof) RlpString(org.fisco.bcos.web3j.rlp.RlpString)

Aggregations

TransactionReceiptWithProof (org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceiptWithProof)3 BigInteger (java.math.BigInteger)2 TransactionWithProof (org.fisco.bcos.web3j.protocol.core.methods.response.TransactionWithProof)2 RlpString (org.fisco.bcos.web3j.rlp.RlpString)2 List (java.util.List)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 ChannelEthereumService (org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService)1 DefaultBlockParameter (org.fisco.bcos.web3j.protocol.core.DefaultBlockParameter)1 BcosBlock (org.fisco.bcos.web3j.protocol.core.methods.response.BcosBlock)1 BcosTransaction (org.fisco.bcos.web3j.protocol.core.methods.response.BcosTransaction)1 MerkleProofUnit (org.fisco.bcos.web3j.protocol.core.methods.response.MerkleProofUnit)1 TransactionReceipt (org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1