Search in sources :

Example 1 with JSONRPC2SessionException

use of com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException in project starcoin-search by starcoinorg.

the class MarketCapHandle method getResult.

private Result<TokenMarketCap> getResult(SearchResponse searchResponse) {
    SearchHit[] searchHit = searchResponse.getHits().getHits();
    Result<TokenMarketCap> result = new Result<>();
    result.setTotal(searchResponse.getHits().getTotalHits().value);
    List<TokenMarketCap> tokens = new ArrayList<>();
    for (SearchHit hit : searchHit) {
        TokenMarketCap marketCap = JSON.parseObject(hit.getSourceAsString(), TokenMarketCap.class);
        try {
            if (marketCap.getTypeTag().equals(STCTypeTag)) {
                marketCap.setMarketCap(tokenContractRPCClient.getSTCCurrentSupply());
            } else {
                marketCap.setMarketCap(tokenContractRPCClient.getTokenCurrentSupply(marketCap.getTypeTag()));
            }
            tokens.add(marketCap);
        } catch (JSONRPC2SessionException e) {
            logger.error("get market cap err:", e);
        }
    }
    result.setContents(tokens);
    return result;
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) TokenMarketCap(org.starcoin.bean.TokenMarketCap) JSONRPC2SessionException(com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException) ArrayList(java.util.ArrayList) Result(org.starcoin.api.Result)

Example 2 with JSONRPC2SessionException

use of com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException in project starcoin-search by starcoinorg.

the class RepairHandle method repair.

public void repair(long blockNumber) {
    try {
        // get block from chain
        Block blockOnChain = blockRPCClient.getBlockByHeight(blockNumber);
        if (blockOnChain == null) {
            logger.warn("block not exist on chain: {}", blockNumber);
            return;
        }
        // get block from es
        Block blockOnEs = elasticSearchHandler.getBlockId(blockNumber);
        if (blockOnEs != null) {
            if (!blockOnChain.getHeader().getBlockHash().equals(blockOnEs.getHeader().getBlockHash())) {
                // update block
                List<Block> blockList = new ArrayList<>();
                ServiceUtils.addBlockToList(transactionRPCClient, blockList, blockOnChain);
                elasticSearchHandler.updateBlock(blockList);
                logger.info("repair ok: {}", blockNumber);
            } else {
                logger.info("not repair");
            }
        } else {
            logger.warn("es not exist block:{}", blockNumber);
        }
    } catch (JSONRPC2SessionException e) {
        logger.error("repair error:", e);
    }
}
Also used : JSONRPC2SessionException(com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException) ArrayList(java.util.ArrayList) Block(org.starcoin.bean.Block)

Example 3 with JSONRPC2SessionException

use of com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException in project starcoin-search by starcoinorg.

the class RepairHandle method autoRepair.

public boolean autoRepair(long startNumber, int count) {
    // read current master height
    try {
        BlockHeader chainHeader = blockRPCClient.getChainHeader();
        long currentMasterNumber = chainHeader.getHeight();
        if (startNumber > currentMasterNumber - 200) {
            logger.info("repair too fast: {}", startNumber);
            return false;
        }
    } catch (JSONRPC2SessionException e) {
        logger.error("get master error: ", e);
        return false;
    }
    List<Block> blocksOnChain;
    try {
        blocksOnChain = blockRPCClient.getBlockListFromHeight(startNumber, count);
    } catch (JSONRPC2SessionException e) {
        logger.error("get blocks on chain error:", e);
        return false;
    }
    if (blocksOnChain.isEmpty()) {
        logger.warn("blocks on chain is empty!");
        return false;
    }
    Result<Block> blockResult = elasticSearchHandler.getBlockIds(startNumber - count, count);
    if (blockResult == null || blockResult.getContents().isEmpty()) {
        logger.warn("blocks on es is empty!");
        return false;
    }
    Map<Long, Block> blockHashMap = new HashMap<>();
    for (Block block : blockResult.getContents()) {
        blockHashMap.put(block.getHeader().getHeight(), block);
    }
    List<Block> blockList = new ArrayList<>();
    long newStartNumber = startNumber;
    for (Block block : blocksOnChain) {
        Block esBlock = blockHashMap.get(block.getHeader().getHeight());
        if (esBlock == null) {
            logger.warn("es block not exist: {}", block.getHeader().getHeight());
            try {
                ServiceUtils.addBlockToList(transactionRPCClient, blockList, block);
            } catch (JSONRPC2SessionException e) {
                logger.error("add block err:", e);
            }
            continue;
        }
        if (!block.getHeader().getBlockHash().equals(esBlock.getHeader().getBlockHash())) {
            // fork block
            try {
                ServiceUtils.addBlockToList(transactionRPCClient, blockList, block);
            } catch (JSONRPC2SessionException e) {
                logger.error("add fix block err:", e);
            }
        } else {
            logger.info("normal block: {}", block.getHeader().getHeight());
        }
        newStartNumber = block.getHeader().getHeight();
    }
    if (!blockList.isEmpty()) {
        elasticSearchHandler.updateBlock(blockList);
        logger.info("repair ok: {}", newStartNumber);
    } else {
        logger.info("not repair:{}", startNumber);
    }
    return true;
}
Also used : HashMap(java.util.HashMap) JSONRPC2SessionException(com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException) ArrayList(java.util.ArrayList) Block(org.starcoin.bean.Block) BlockHeader(org.starcoin.bean.BlockHeader)

Example 4 with JSONRPC2SessionException

use of com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException in project junit-plugin by kiwitcms.

the class BaseRpcClient method executeViaPositionalParams.

protected Object executeViaPositionalParams(String serviceMethod, List<Object> params) {
    JSONRPC2Session mySession = prepareSession();
    // Construct new request
    int requestID = 1;
    JSONRPC2Request request = new JSONRPC2Request(serviceMethod, requestID);
    request.setPositionalParams(params);
    // Send request
    try {
        return getResponse(mySession.send(request));
    } catch (JSONRPC2SessionException e) {
        System.err.println(e.getMessage());
        return null;
    }
}
Also used : JSONRPC2SessionException(com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException) JSONRPC2Session(com.thetransactioncompany.jsonrpc2.client.JSONRPC2Session) JSONRPC2Request(com.thetransactioncompany.jsonrpc2.JSONRPC2Request)

Example 5 with JSONRPC2SessionException

use of com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException in project starcoin-java by starcoinorg.

the class JsonRPCClient method getObjectArray.

/**
 * 获取对象列表的接口
 *
 * @param session
 * @param method    rpc接口的方法名
 * @param params    rpc接口的参数列表
 * @param requestId 请求id
 * @param clazz     返回对象的封装类
 * @return
 * @throws JSONRPC2SessionException
 */
protected List<T> getObjectArray(JSONRPC2Session session, String method, List<Object> params, int requestId, Class<T> clazz) throws JSONRPC2SessionException {
    JSONRPC2Request request = new JSONRPC2Request(method, params, requestId);
    JSONRPC2Response response = session.send(request);
    if (response.indicatesSuccess()) {
        Object result = response.getResult();
        if (result != null) {
            return JSON.parseArray(result.toString(), clazz);
        }
    }
    return null;
}
Also used : JSONRPC2Response(com.thetransactioncompany.jsonrpc2.JSONRPC2Response) JSONObject(com.alibaba.fastjson.JSONObject) JSONRPC2Request(com.thetransactioncompany.jsonrpc2.JSONRPC2Request)

Aggregations

JSONRPC2SessionException (com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException)18 ArrayList (java.util.ArrayList)10 JSONRPC2Request (com.thetransactioncompany.jsonrpc2.JSONRPC2Request)7 Block (org.starcoin.bean.Block)5 ContractCall (org.starcoin.bean.ContractCall)5 JSONObject (com.alibaba.fastjson.JSONObject)4 JSONRPC2Response (com.thetransactioncompany.jsonrpc2.JSONRPC2Response)4 List (java.util.List)4 JSONRPC2Session (com.thetransactioncompany.jsonrpc2.client.JSONRPC2Session)3 IOException (java.io.IOException)2 BigInteger (java.math.BigInteger)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 BlockHeader (org.starcoin.bean.BlockHeader)2 BlockOffset (org.starcoin.bean.BlockOffset)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DeserializationError (com.novi.serde.DeserializationError)1 BigDecimal (java.math.BigDecimal)1 ConnectException (java.net.ConnectException)1