use of io.nuls.consensus.event.notice.PackedBlockNotice in project nuls by nuls-io.
the class ConsensusMeetingRunner method packing.
private void packing(PocMeetingMember self) throws NulsException, IOException {
Block bestBlock = context.getBestBlock();
List<Transaction> txList = txCacheManager.getTxList();
txList.sort(TxTimeComparator.getInstance());
BlockData bd = new BlockData();
bd.setHeight(bestBlock.getHeader().getHeight() + 1);
bd.setPreHash(bestBlock.getHeader().getHash());
BlockRoundData roundData = new BlockRoundData();
roundData.setRoundIndex(consensusManager.getCurrentRound().getIndex());
roundData.setConsensusMemberCount(consensusManager.getCurrentRound().getMemberCount());
roundData.setPackingIndexOfRound(self.getIndexOfRound());
roundData.setRoundStartTime(consensusManager.getCurrentRound().getStartTime());
bd.setRoundData(roundData);
List<Integer> outTxList = new ArrayList<>();
List<NulsDigestData> outHashList = new ArrayList<>();
List<NulsDigestData> hashList = new ArrayList<>();
long totalSize = 0L;
for (int i = 0; i < txList.size(); i++) {
if ((self.getPackTime() - TimeService.currentTimeMillis()) <= 100) {
break;
}
Transaction tx = txList.get(i);
totalSize += tx.size();
if (totalSize >= PocConsensusConstant.MAX_BLOCK_SIZE) {
break;
}
outHashList.add(tx.getHash());
ValidateResult result = tx.verify();
if (result.isFailed()) {
Log.error(result.getMessage());
outTxList.add(i);
continue;
}
try {
ledgerService.approvalTx(tx);
} catch (Exception e) {
Log.error(e);
outTxList.add(i);
continue;
}
confirmingTxCacheManager.putTx(tx);
}
txCacheManager.removeTx(hashList);
for (int i = outTxList.size() - 1; i >= 0; i--) {
txList.remove(i);
}
txCacheManager.removeTx(outHashList);
if (totalSize < PocConsensusConstant.MAX_BLOCK_SIZE) {
addOrphanTx(txList, totalSize, self);
}
addConsensusTx(bestBlock, txList, self);
bd.setTxList(txList);
Log.info("txCount:" + txList.size());
Block newBlock = ConsensusTool.createBlock(bd, consensusManager.getConsensusStatusInfo().getAccount());
ValidateResult result = newBlock.verify();
if (result.isFailed()) {
Log.warn("packing block error:" + result.getMessage());
for (Transaction tx : newBlock.getTxs()) {
if (tx.getType() == TransactionConstant.TX_TYPE_COIN_BASE) {
continue;
}
ledgerService.rollbackTx(tx);
}
return;
}
confirmingTxCacheManager.putTx(newBlock.getTxs().get(0));
blockManager.addBlock(newBlock, false, null);
BlockHeaderEvent event = new BlockHeaderEvent();
event.setEventBody(newBlock.getHeader());
eventBroadcaster.broadcastAndCache(event, false);
PackedBlockNotice notice = new PackedBlockNotice();
notice.setEventBody(newBlock.getHeader());
eventBroadcaster.publishToLocal(notice);
}
Aggregations