use of io.nuls.protocol.message.GetTxGroupRequest in project nuls by nuls-io.
the class SmallBlockHandler method onMessage.
@Override
public void onMessage(SmallBlockMessage event, Node fromNode) {
SmallBlock smallBlock = event.getMsgBody();
if (null == smallBlock) {
Log.warn("recieved a null smallBlock!");
return;
}
BlockHeader header = smallBlock.getHeader();
// 阻止恶意节点提前出块
if (header.getTime() > (TimeService.currentTimeMillis() + ProtocolConstant.BLOCK_TIME_INTERVAL_SECOND * 1000)) {
// Log.info("---------------" + TimeService.currentTimeMillis());
return;
}
if (!SmallBlockDuplicateRemoval.needProcess(header.getHash())) {
// Log.warn("block header 重复");
return;
}
BlockHeader theBlockHeader = blockService.getBlockHeader(header.getHash()).getData();
if (null != theBlockHeader) {
// Log.warn("这个区块已存在");
return;
}
ValidateResult result = header.verify();
boolean isOrphan = result.getErrorCode() == TransactionErrorCode.ORPHAN_TX || result.getErrorCode() == TransactionErrorCode.ORPHAN_BLOCK;
BlockLog.debug("recieve new block from(" + fromNode.getId() + "), tx count : " + header.getTxCount() + " , tx pool count : " + consensusService.getMemoryTxs().size() + " , header height:" + header.getHeight() + ", preHash:" + header.getPreHash() + " , hash:" + header.getHash() + ", addressHex:" + AddressTool.getStringAddressByBytes(header.getPackingAddress()) + "\n and verify block result: " + result.isSuccess() + " , verify message : " + result.getMsg() + " , isOrphan : " + isOrphan);
if (result.isFailed() && !isOrphan) {
BlockLog.debug("discard a SmallBlock:" + smallBlock.getHeader().getHash() + ", from:" + fromNode.getId() + " ,reason:" + result.getMsg());
return;
}
Map<NulsDigestData, Transaction> txMap = new HashMap<>();
for (Transaction tx : smallBlock.getSubTxList()) {
txMap.put(tx.getHash(), tx);
}
List<NulsDigestData> needHashList = new ArrayList<>();
for (NulsDigestData hash : smallBlock.getTxHashList()) {
Transaction tx = txMap.get(hash);
if (null == tx) {
tx = transactionService.getTx(hash);
if (tx != null) {
smallBlock.getSubTxList().add(tx);
txMap.put(hash, tx);
}
}
if (null == tx) {
needHashList.add(hash);
}
}
if (!needHashList.isEmpty()) {
// Log.info("block height : " + header.getHeight() + ", tx count : " + header.getTxCount() + " , get group tx of " + needHashList.size());
GetTxGroupRequest request = new GetTxGroupRequest();
GetTxGroupParam param = new GetTxGroupParam();
param.setTxHashList(needHashList);
request.setMsgBody(param);
Result sendResult = this.messageBusService.sendToNode(request, fromNode, true);
if (sendResult.isFailed()) {
Log.warn("get tx group failed,height:" + header.getHeight());
} else {
NulsDigestData requestHash = null;
try {
requestHash = NulsDigestData.calcDigestData(request.getMsgBody().serialize());
} catch (IOException e) {
Log.error(e);
return;
}
temporaryCacheManager.cacheSmallBlockWithRequest(requestHash, smallBlock);
}
return;
}
Block block = AssemblyBlockUtil.assemblyBlock(header, txMap, smallBlock.getTxHashList());
consensusService.newBlock(block, fromNode);
}
Aggregations