Search in sources :

Example 1 with GetTxGroupParam

use of io.nuls.protocol.model.GetTxGroupParam 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);
}
Also used : SmallBlock(io.nuls.protocol.model.SmallBlock) HashMap(java.util.HashMap) GetTxGroupRequest(io.nuls.protocol.message.GetTxGroupRequest) ValidateResult(io.nuls.kernel.validate.ValidateResult) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ValidateResult(io.nuls.kernel.validate.ValidateResult) GetTxGroupParam(io.nuls.protocol.model.GetTxGroupParam) SmallBlock(io.nuls.protocol.model.SmallBlock)

Example 2 with GetTxGroupParam

use of io.nuls.protocol.model.GetTxGroupParam in project nuls by nuls-io.

the class GetTxGroupHandler method onMessage.

@Override
public void onMessage(GetTxGroupRequest message, Node fromNode) {
    if (message == null || fromNode == null) {
        return;
    }
    GetTxGroupParam getTxGroupParam = message.getMsgBody();
    if (getTxGroupParam == null || getTxGroupParam.getTxHashList() == null || getTxGroupParam.getTxHashList().size() > 10000) {
        return;
    }
    NulsDigestData requestHash = null;
    try {
        requestHash = NulsDigestData.calcDigestData(getTxGroupParam.serialize());
    } catch (IOException e) {
        Log.error(e);
        return;
    }
    TxGroupMessage txGroupMessage = new TxGroupMessage();
    TxGroup txGroup = new TxGroup();
    List<Transaction> txList = new ArrayList<>();
    for (NulsDigestData hash : getTxGroupParam.getTxHashList()) {
        Transaction tx = transactionService.getTx(hash);
        if (tx != null) {
            txList.add(tx);
        } else {
            Log.error("GetTxGroupHandler NULL TX=========================================hash: " + hash.getDigestHex());
            return;
        }
    }
    if (txList.isEmpty()) {
        Log.error("ASK:{}, {}", fromNode, getTxGroupParam.getTxHashList().get(0));
        return;
    }
    txGroup.setTxList(txList);
    txGroup.setRequestHash(requestHash);
    txGroupMessage.setMsgBody(txGroup);
    messageBusService.sendToNode(txGroupMessage, fromNode, true);
}
Also used : TxGroup(io.nuls.protocol.model.TxGroup) Transaction(io.nuls.kernel.model.Transaction) GetTxGroupParam(io.nuls.protocol.model.GetTxGroupParam) ArrayList(java.util.ArrayList) NulsDigestData(io.nuls.kernel.model.NulsDigestData) IOException(java.io.IOException) TxGroupMessage(io.nuls.protocol.message.TxGroupMessage)

Aggregations

GetTxGroupParam (io.nuls.protocol.model.GetTxGroupParam)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 NulsDigestData (io.nuls.kernel.model.NulsDigestData)1 Transaction (io.nuls.kernel.model.Transaction)1 ValidateResult (io.nuls.kernel.validate.ValidateResult)1 GetTxGroupRequest (io.nuls.protocol.message.GetTxGroupRequest)1 TxGroupMessage (io.nuls.protocol.message.TxGroupMessage)1 SmallBlock (io.nuls.protocol.model.SmallBlock)1 TxGroup (io.nuls.protocol.model.TxGroup)1 HashMap (java.util.HashMap)1