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;
}
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);
}
}
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;
}
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;
}
}
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;
}
Aggregations