use of io.nuls.consensus.poc.container.ChainContainer 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;
}
use of io.nuls.consensus.poc.container.ChainContainer 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;
}
Aggregations