Search in sources :

Example 11 with Chain

use of io.nuls.consensus.poc.model.Chain in project nuls by nuls-io.

the class ChainManager method newOrphanChain.

public void newOrphanChain(Block block) {
    Chain orphanChain = new Chain();
    orphanChain.initData(block);
    ChainContainer orphanChainContainer = new ChainContainer(orphanChain);
    orphanChains.add(orphanChainContainer);
}
Also used : Chain(io.nuls.consensus.poc.model.Chain) ChainContainer(io.nuls.consensus.poc.container.ChainContainer)

Example 12 with Chain

use of io.nuls.consensus.poc.model.Chain in project nuls by nuls-io.

the class ChainManager method checkIsBeforeOrphanChainAndAdd.

public boolean checkIsBeforeOrphanChainAndAdd(Block block) {
    BlockHeader header = block.getHeader();
    boolean success = false;
    for (ChainContainer chainContainer : orphanChains) {
        Chain chain = chainContainer.getChain();
        if (header.getHash().equals(chain.getStartBlockHeader().getPreHash())) {
            success = true;
            chain.addPreBlock(block);
        }
    }
    return success;
}
Also used : Chain(io.nuls.consensus.poc.model.Chain) ChainContainer(io.nuls.consensus.poc.container.ChainContainer) BlockHeader(io.nuls.kernel.model.BlockHeader)

Example 13 with Chain

use of io.nuls.consensus.poc.model.Chain in project nuls by nuls-io.

the class BlockProcess method checkForkChainFromForkChains.

/**
 * When a block cannot be connected to the main chain, it checks whether it is a branch of a forked chain,
 * or is connected to a forked chain, and if so, it produces a forked chain, and then adds the forked chain into the fork to be verified. Chain pool;
 * or add the block directly to the corresponding branch chain
 * <p>
 * 当一个区块不能与主链相连时,检查是否是分叉链的分支,或者与分叉链相连,如果是,则产生一条分叉链,然后把该分叉链添加进待验证的分叉链池里;或者把该块直接添加到对应的分叉链上
 *
 * @return boolean
 */
protected boolean checkForkChainFromForkChains(Block block) {
    BlockHeader blockHeader = block.getHeader();
    NulsDigestData preHash = blockHeader.getPreHash();
    // check the preHash is in the waitVerifyChainList
    Iterator<ChainContainer> iterator = chainManager.getChains().iterator();
    while (iterator.hasNext()) {
        Chain forkChain = iterator.next().getChain();
        List<BlockHeader> headerList = forkChain.getAllBlockHeaderList();
        int size = headerList.size();
        for (int i = size - 1; i >= 0; i--) {
            BlockHeader header = headerList.get(i);
            if (header.getHash().equals(blockHeader.getHash())) {
                // found a same block , return true
                return true;
            } else if (header.getHash().equals(preHash)) {
                if (header.getHeight() + 1L != blockHeader.getHeight()) {
                    // 丢弃数据不正确的区块
                    return true;
                }
                // 检查是分叉还是连接,如果是连接,则加上即可
                if (i == size - 1) {
                    forkChain.addBlock(block);
                    return true;
                }
                // The block is again forked in the forked chain
                // 该块是在分叉链中再次进行的分叉
                List<Block> blockList = forkChain.getAllBlockList();
                Chain newForkChain = new Chain();
                newForkChain.initData(forkChain.getStartBlockHeader(), new ArrayList<>(headerList.subList(0, i + 1)), new ArrayList<>(blockList.subList(0, i + 1)));
                newForkChain.addBlock(block);
                return chainManager.getChains().add(new ChainContainer(newForkChain));
            } else if (header.getHeight() < blockHeader.getHeight()) {
                break;
            }
        }
    }
    return false;
}
Also used : Chain(io.nuls.consensus.poc.model.Chain) ChainContainer(io.nuls.consensus.poc.container.ChainContainer)

Aggregations

Chain (io.nuls.consensus.poc.model.Chain)13 ChainContainer (io.nuls.consensus.poc.container.ChainContainer)9 Test (org.junit.Test)6 Block (io.nuls.kernel.model.Block)4 BaseChainTest (io.nuls.consensus.poc.BaseChainTest)3 BaseTest (io.nuls.consensus.poc.BaseTest)3 Agent (io.nuls.consensus.poc.protocol.entity.Agent)2 Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)2 PunishLogPo (io.nuls.consensus.poc.storage.po.PunishLogPo)2 BlockHeader (io.nuls.kernel.model.BlockHeader)2 BlockExtendsData (io.nuls.consensus.poc.model.BlockExtendsData)1 MeetingRound (io.nuls.consensus.poc.model.MeetingRound)1 PunishLogStorageService (io.nuls.consensus.poc.storage.service.PunishLogStorageService)1 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)1