use of io.nuls.consensus.poc.model.Chain in project nuls by nuls-io.
the class BlockProcess method checkForkChainFromMasterChain.
/**
* When a block cannot be connected to the main chain, it is checked whether it is a branch of the main chain.
* If it is a branch of the main chain, a bifurcation chain is generated, and then the bifurcation chain is added into the forked chain pool to be verified.
* <p>
* 当一个区块不能与主链相连时,检查是否是主链的分支,如果是主链的分支,则产生一条分叉链,然后把该分叉链添加进待验证的分叉链池里
*
* @return boolean
*/
protected boolean checkForkChainFromMasterChain(Block block) {
BlockHeader blockHeader = block.getHeader();
Chain masterChain = chainManager.getMasterChain().getChain();
List<BlockHeader> headerList = masterChain.getAllBlockHeaderList();
for (int i = headerList.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(blockHeader.getPreHash())) {
if (header.getHeight() + 1L != blockHeader.getHeight()) {
// 丢弃数据不正确的区块
return true;
}
Chain newForkChain = new Chain();
newForkChain.addBlock(block);
chainManager.getChains().add(new ChainContainer(newForkChain));
return true;
}
if (header.getHeight() < blockHeader.getHeight()) {
break;
}
}
return false;
}
use of io.nuls.consensus.poc.model.Chain in project nuls by nuls-io.
the class ChainContainerTest method testGetBeforeTheForkChain.
@Test
public void testGetBeforeTheForkChain() {
Block forkBlock = null;
for (int i = 0; i < 20; i++) {
Block bestBlock = chainContainer.getBestBlock();
Block newBlock = newBlock(bestBlock);
Result success = chainContainer.verifyAndAddBlock(newBlock, false, true);
assertTrue(success.isSuccess());
bestBlock = chainContainer.getBestBlock();
assertEquals(bestBlock.getHeader().getHeight(), 1L + i);
if (i == 10) {
forkBlock = bestBlock;
}
}
Chain chain = new Chain();
chain.addBlock(forkBlock);
Block newBlock = newBlock(forkBlock);
chain.addBlock(newBlock);
ChainContainer otherChainContainer = new ChainContainer(chain);
ChainContainer newForkChainContainer = chainContainer.getBeforeTheForkChain(otherChainContainer);
assertEquals(newForkChainContainer.getBestBlock().getHeader().getHeight(), 10L);
}
use of io.nuls.consensus.poc.model.Chain in project nuls by nuls-io.
the class ChainManagerTest method testGetBestBlockHeight.
@Test
public void testGetBestBlockHeight() {
assertNotNull(chainManager);
Block block = createBlock();
ChainContainer masterChain = new ChainContainer(new Chain());
chainManager.setMasterChain(masterChain);
masterChain.getChain().addBlock(block);
assertEquals(0L, chainManager.getBestBlockHeight());
}
use of io.nuls.consensus.poc.model.Chain in project nuls by nuls-io.
the class ChainManagerTest method testCheckIsAfterOrphanChainAndAdd.
@Test
public void testCheckIsAfterOrphanChainAndAdd() {
testGetBestBlockHeight();
Block block = createBlock();
Block block1 = createBlock();
block1.getHeader().setHeight(1L);
block1.getHeader().setPreHash(block.getHeader().getHash());
ChainContainer orphanChain = new ChainContainer(new Chain());
orphanChain.getChain().addBlock(block);
chainManager.getOrphanChains().add(orphanChain);
assertEquals(1, chainManager.getOrphanChains().size());
boolean success = chainManager.checkIsAfterOrphanChainAndAdd(block1);
assertTrue(success);
}
use of io.nuls.consensus.poc.model.Chain in project nuls by nuls-io.
the class RoundManagerTest method testInitRound.
@Test
public void testInitRound() {
assertNotNull(roundManager);
assertNotNull(roundManager.getChain());
Chain chain = roundManager.getChain();
assertNotNull(chain.getEndBlockHeader());
assert (chain.getAllBlockList().size() > 0);
MeetingRound round = roundManager.initRound();
assertNotNull(round);
assertEquals(round.getIndex(), 2L);
Assert.assertEquals(round.getStartTime(), ProtocolConstant.BLOCK_TIME_INTERVAL_MILLIS + 1L);
MeetingRound round2 = roundManager.getNextRound(null, false);
assertNotNull(round2);
assertEquals(round.getIndex(), round2.getIndex());
assertEquals(round.getStartTime(), round2.getStartTime());
round2 = roundManager.getNextRound(null, true);
assertNotNull(round2);
assert (round.getIndex() < round2.getIndex());
assert (round.getStartTime() < round2.getStartTime());
assertEquals("", 0d, round2.getTotalWeight(), 2200000d);
}
Aggregations