Search in sources :

Example 1 with TxGroup

use of io.nuls.protocol.model.TxGroup 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)

Example 2 with TxGroup

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

the class TxGroupHandler method onMessage.

@Override
public void onMessage(TxGroupMessage message, Node fromNode) throws NulsException {
    TxGroup txGroup = message.getMsgBody();
    if (null == txGroup) {
        Log.warn("recieved a null txGroup form " + fromNode.getId());
        return;
    }
    SmallBlock smallBlock = temporaryCacheManager.getSmallBlockByRequest(txGroup.getRequestHash());
    if (null == smallBlock) {
        return;
    }
    BlockHeader header = smallBlock.getHeader();
    Map<NulsDigestData, Transaction> txMap = new HashMap<>();
    for (Transaction tx : smallBlock.getSubTxList()) {
        txMap.put(tx.getHash(), tx);
    }
    for (Transaction tx : txGroup.getTxList()) {
        txMap.put(tx.getHash(), tx);
    }
    for (NulsDigestData hash : smallBlock.getTxHashList()) {
        Transaction tx = txMap.get(hash);
        if (null == tx) {
            tx = temporaryCacheManager.getTx(hash);
        }
        if (tx != null) {
            smallBlock.getSubTxList().add(tx);
            txMap.put(hash, tx);
        }
    }
    Block block = AssemblyBlockUtil.assemblyBlock(header, txMap, smallBlock.getTxHashList());
    consensusService.newBlock(block, fromNode);
}
Also used : SmallBlock(io.nuls.protocol.model.SmallBlock) TxGroup(io.nuls.protocol.model.TxGroup) HashMap(java.util.HashMap) SmallBlock(io.nuls.protocol.model.SmallBlock)

Aggregations

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