Search in sources :

Example 1 with Response

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

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

the class RawResponseDeserializer method deserialize.

@Override
public Response deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
    Response deserializedResponse = (Response) defaultDeserializer.deserialize(jp, ctxt);
    deserializedResponse.setRawResponse(getRawResponse(jp));
    return deserializedResponse;
}
Also used : Response(org.fisco.bcos.web3j.protocol.core.Response)

Example 3 with Response

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

Response (org.fisco.bcos.web3j.protocol.core.Response)3 RateLimiter (com.google.common.util.concurrent.RateLimiter)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 SecureRandom (java.security.SecureRandom)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Service (org.fisco.bcos.channel.client.Service)1 BcosRequest (org.fisco.bcos.channel.dto.BcosRequest)1 BcosResponse (org.fisco.bcos.channel.dto.BcosResponse)1 Web3j (org.fisco.bcos.web3j.protocol.Web3j)1 ChannelEthereumService (org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService)1 DefaultBlockParameter (org.fisco.bcos.web3j.protocol.core.DefaultBlockParameter)1 Error (org.fisco.bcos.web3j.protocol.core.Response.Error)1 ApplicationContext (org.springframework.context.ApplicationContext)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 ThreadPoolTaskExecutor (org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor)1