Search in sources :

Example 1 with DefaultBlockParameter

use of org.fisco.bcos.web3j.protocol.core.DefaultBlockParameter 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 DefaultBlockParameter

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

the class PerformanceRPC method main.

public static void main(String[] args) throws Exception {
    try {
        if (args.length < 3) {
            Usage();
        }
        int groupId = Integer.valueOf(args[0]);
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Service service = context.getBean(Service.class);
        service.setGroupId(groupId);
        service.run();
        System.out.println("Start test...");
        System.out.println("===================================================================");
        ChannelEthereumService channelEthereumService = new ChannelEthereumService();
        channelEthereumService.setChannelService(service);
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(500);
        Web3j web3j = Web3j.build(channelEthereumService, 15 * 100, scheduledExecutorService, groupId);
        Integer count = Integer.parseInt(args[1]);
        Integer qps = Integer.parseInt(args[2]);
        ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
        threadPool.setCorePoolSize(200);
        threadPool.setMaxPoolSize(500);
        threadPool.setQueueCapacity(count);
        threadPool.initialize();
        PerformanceRpcCollector collector = new PerformanceRpcCollector();
        collector.setTotal(count);
        RateLimiter limiter = RateLimiter.create(qps);
        Integer area = count / 10;
        final Integer total = count;
        System.out.println("Start test,total:" + count);
        for (Integer i = 0; i < count; ++i) {
            threadPool.execute(new Runnable() {

                @Override
                public void run() {
                    limiter.acquire();
                    Response response = new Response();
                    try {
                        int random = new SecureRandom().nextInt(50000);
                        int methodNum = 10;
                        Long startTime = System.nanoTime();
                        switch(random % methodNum) {
                            // 1. call getPendingTxSize
                            case 0:
                                response = web3j.getPendingTxSize().send();
                                break;
                            // 2. call getBlockNumber
                            case 1:
                                response = web3j.getBlockNumber().send();
                                break;
                            // 3. call getSyncStatus
                            case 2:
                                response = web3j.getSyncStatus().send();
                                break;
                            // 5. call getSealerList
                            case 4:
                                response = web3j.getSealerList().send();
                                break;
                            // 6. call getTotalTransactionCount
                            case 5:
                                response = web3j.getTotalTransactionCount().send();
                                break;
                            // 7. call getObserverList
                            case 6:
                                response = web3j.getObserverList().send();
                                break;
                            // 8. call getBlockHashByNumber
                            case 7:
                                BigInteger blockNumber = web3j.getBlockNumber().send().getBlockNumber();
                                DefaultBlockParameter blockParam = DefaultBlockParameter.valueOf(blockNumber);
                                response = web3j.getBlockHashByNumber(blockParam).send();
                                break;
                            // 9. call getSystemConfigByKey
                            case 8:
                                response = web3j.getSystemConfigByKey("tx_count_limit").send();
                                break;
                            // 10. call getPbftView
                            case 9:
                                response = web3j.getPbftView().send();
                                break;
                            default:
                                // default call getPbftView
                                response = web3j.getPbftView().send();
                        }
                        Long cost = System.nanoTime() - startTime;
                        collector.onMessage(response, cost);
                    } catch (Exception e) {
                        logger.error("test rpc interface failed, error info: {}", e.getMessage());
                        Error error = new Error();
                        error.setCode(1);
                        response.setError(error);
                        collector.onMessage(response, 0L);
                    }
                    int current = sended.incrementAndGet();
                    if (current >= area && ((current % area) == 0)) {
                        System.out.println("Already sended: " + current + "/" + total + " RPC Requests");
                    }
                }
            });
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Service(org.fisco.bcos.channel.client.Service) SecureRandom(java.security.SecureRandom) Error(org.fisco.bcos.web3j.protocol.core.Response.Error) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) RateLimiter(com.google.common.util.concurrent.RateLimiter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) Response(org.fisco.bcos.web3j.protocol.core.Response) DefaultBlockParameter(org.fisco.bcos.web3j.protocol.core.DefaultBlockParameter) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Web3j(org.fisco.bcos.web3j.protocol.Web3j) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BigInteger(java.math.BigInteger) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)

Aggregations

BigInteger (java.math.BigInteger)2 ChannelEthereumService (org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService)2 DefaultBlockParameter (org.fisco.bcos.web3j.protocol.core.DefaultBlockParameter)2 ApplicationContext (org.springframework.context.ApplicationContext)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2 RateLimiter (com.google.common.util.concurrent.RateLimiter)1 SecureRandom (java.security.SecureRandom)1 List (java.util.List)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Service (org.fisco.bcos.channel.client.Service)1 Web3j (org.fisco.bcos.web3j.protocol.Web3j)1 Response (org.fisco.bcos.web3j.protocol.core.Response)1 Error (org.fisco.bcos.web3j.protocol.core.Response.Error)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 TransactionReceiptWithProof (org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceiptWithProof)1 TransactionWithProof (org.fisco.bcos.web3j.protocol.core.methods.response.TransactionWithProof)1 ThreadPoolTaskExecutor (org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)1