Search in sources :

Example 6 with BlockRoundData

use of io.nuls.consensus.entity.block.BlockRoundData 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);
}
Also used : ValidateResult(io.nuls.core.validate.ValidateResult) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) BlockHeaderEvent(io.nuls.consensus.event.BlockHeaderEvent) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) Transaction(io.nuls.core.chain.entity.Transaction) RedPunishTransaction(io.nuls.consensus.entity.tx.RedPunishTransaction) YellowPunishTransaction(io.nuls.consensus.entity.tx.YellowPunishTransaction) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData) Block(io.nuls.core.chain.entity.Block) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) PackedBlockNotice(io.nuls.consensus.event.notice.PackedBlockNotice) BlockData(io.nuls.consensus.entity.block.BlockData)

Example 7 with BlockRoundData

use of io.nuls.consensus.entity.block.BlockRoundData in project nuls by nuls-io.

the class ConsensusTool method toPojo.

public static final BlockHeaderPo toPojo(BlockHeader header) {
    BlockHeaderPo po = new BlockHeaderPo();
    po.setTxCount(header.getTxCount());
    po.setPreHash(header.getPreHash().getDigestHex());
    po.setMerkleHash(header.getMerkleHash().getDigestHex());
    po.setHeight(header.getHeight());
    po.setCreateTime(header.getTime());
    po.setHash(header.getHash().getDigestHex());
    po.setSize(header.getSize());
    if (null != header.getScriptSig()) {
        try {
            po.setScriptSig(header.getScriptSig().serialize());
        } catch (IOException e) {
            Log.error(e);
        }
    }
    po.setTxCount(header.getTxCount());
    po.setConsensusAddress(header.getPackingAddress());
    po.setExtend(header.getExtend());
    BlockRoundData data = new BlockRoundData();
    try {
        data.parse(header.getExtend());
    } catch (NulsException e) {
        Log.error(e);
    }
    po.setRoundIndex(data.getRoundIndex());
    return po;
}
Also used : NulsException(io.nuls.core.exception.NulsException) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData) IOException(java.io.IOException) BlockHeaderPo(io.nuls.db.entity.BlockHeaderPo)

Example 8 with BlockRoundData

use of io.nuls.consensus.entity.block.BlockRoundData in project nuls by nuls-io.

the class CoinbaseValidator method validate.

@Override
public ValidateResult validate(Block block) {
    if (null == block || block.getHeader() == null || null == block.getTxs() || block.getTxs().isEmpty()) {
        return ValidateResult.getFailedResult(ErrorCode.DATA_FIELD_CHECK_ERROR);
    }
    Transaction tx = block.getTxs().get(0);
    if (tx.getType() != TransactionConstant.TX_TYPE_COIN_BASE) {
        return ValidateResult.getFailedResult("Coinbase transaction order wrong!");
    }
    for (int i = 1; i < block.getTxs().size(); i++) {
        Transaction transaction = block.getTxs().get(i);
        if (transaction.getType() == TransactionConstant.TX_TYPE_COIN_BASE) {
            ValidateResult result = ValidateResult.getFailedResult("Coinbase transaction more than one!");
            result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
            return result;
        }
    }
    BlockRoundData blockRound = null;
    try {
        blockRound = new BlockRoundData(block.getHeader().getExtend());
    } catch (NulsException e) {
        Log.error(e);
    }
    if (null == blockRound) {
        return ValidateResult.getFailedResult("Cann't get the round data!");
    }
    return ValidateResult.getSuccessResult();
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) NulsException(io.nuls.core.exception.NulsException) ValidateResult(io.nuls.core.validate.ValidateResult) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData)

Aggregations

BlockRoundData (io.nuls.consensus.entity.block.BlockRoundData)8 NulsException (io.nuls.core.exception.NulsException)7 IOException (java.io.IOException)4 PocMeetingRound (io.nuls.consensus.entity.meeting.PocMeetingRound)3 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)3 ValidateResult (io.nuls.core.validate.ValidateResult)3 YellowPunishTransaction (io.nuls.consensus.entity.tx.YellowPunishTransaction)2 Block (io.nuls.core.chain.entity.Block)2 BlockHeader (io.nuls.core.chain.entity.BlockHeader)2 Transaction (io.nuls.core.chain.entity.Transaction)2 CoinBaseTransaction (io.nuls.ledger.entity.tx.CoinBaseTransaction)2 Address (io.nuls.account.entity.Address)1 YellowPunishData (io.nuls.consensus.entity.YellowPunishData)1 BlockData (io.nuls.consensus.entity.block.BlockData)1 PocMeetingMember (io.nuls.consensus.entity.meeting.PocMeetingMember)1 RedPunishTransaction (io.nuls.consensus.entity.tx.RedPunishTransaction)1 BlockHeaderEvent (io.nuls.consensus.event.BlockHeaderEvent)1 PackedBlockNotice (io.nuls.consensus.event.notice.PackedBlockNotice)1 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)1 P2PKHScriptSig (io.nuls.core.script.P2PKHScriptSig)1