use of io.nuls.consensus.poc.model.Chain in project nuls by nuls-io.
the class CacheManager method load.
public void load() throws NulsException {
// load storage data to memory
List<BlockHeader> blockHeaderList = cacheLoader.loadBlockHeaders(PocConsensusConstant.INIT_HEADERS_OF_ROUND_COUNT);
List<Block> blockList = cacheLoader.loadBlocks(PocConsensusConstant.INIT_BLOCKS_COUNT);
if (blockHeaderList == null || blockHeaderList.size() == 0 || blockList == null || blockList.size() == 0) {
Log.error("load cache error ,not find the block info!");
throw new NulsRuntimeException(KernelErrorCode.DATA_ERROR);
}
List<Agent> agentList = cacheLoader.loadAgents();
List<Deposit> depositList = cacheLoader.loadDepositList();
List<PunishLogPo> allPunishList = NulsContext.getServiceBean(PunishLogStorageService.class).getPunishList();
List<PunishLogPo> yellowPunishList = cacheLoader.loadYellowPunishList(allPunishList, PocConsensusConstant.INIT_HEADERS_OF_ROUND_COUNT);
List<PunishLogPo> redPunishList = cacheLoader.loadRedPunishList(allPunishList);
Chain masterChain = new Chain();
masterChain.initData(blockList.get(0).getHeader(), blockHeaderList, blockList);
masterChain.setAgentList(agentList);
masterChain.setDepositList(depositList);
masterChain.setYellowPunishList(yellowPunishList);
masterChain.setRedPunishList(redPunishList);
ChainContainer masterChainContainer = new ChainContainer(masterChain);
chainManager.setMasterChain(masterChainContainer);
chainManager.getMasterChain().initRound();
}
use of io.nuls.consensus.poc.model.Chain in project nuls by nuls-io.
the class ForkChainProcess method checkOrphanChainHasConnection.
private boolean checkOrphanChainHasConnection(ChainContainer orphanChain) {
// Determine whether the orphan chain is connected to the main chain
// 判断该孤立链是否和主链相连
BlockHeader startBlockHeader = orphanChain.getChain().getStartBlockHeader();
List<BlockHeader> blockHeaderList = chainManager.getMasterChain().getChain().getAllBlockHeaderList();
int count = blockHeaderList.size() > PocConsensusConstant.MAX_ISOLATED_BLOCK_COUNT ? PocConsensusConstant.MAX_ISOLATED_BLOCK_COUNT : blockHeaderList.size();
for (int i = blockHeaderList.size() - 1; i >= blockHeaderList.size() - count; i--) {
BlockHeader header = blockHeaderList.get(i);
if (startBlockHeader.getPreHash().equals(header.getHash()) && startBlockHeader.getHeight() == header.getHeight() + 1) {
// yes connectioned
orphanChain.getChain().setPreChainId(chainManager.getMasterChain().getChain().getId());
chainManager.getChains().add(orphanChain);
ChainLog.debug("discover the OrphanChain {} : start {} - {} , end {} - {} , connection the master chain of {} - {} - {}, move into the fork chians", orphanChain.getChain().getId(), startBlockHeader.getHeight(), startBlockHeader.getHash().getDigestHex(), orphanChain.getChain().getEndBlockHeader().getHeight(), orphanChain.getChain().getEndBlockHeader().getHash(), chainManager.getMasterChain().getChain().getId(), chainManager.getMasterChain().getChain().getBestBlock().getHeader().getHeight(), chainManager.getMasterChain().getChain().getBestBlock().getHeader().getHash());
return true;
} else if (startBlockHeader.getHeight() > header.getHeight()) {
break;
}
}
// 判断该孤链是否和待验证的分叉链相连
for (ChainContainer forkChain : chainManager.getChains()) {
Chain chain = forkChain.getChain();
if (startBlockHeader.getHeight() > chain.getEndBlockHeader().getHeight() + 1 || startBlockHeader.getHeight() <= chain.getStartBlockHeader().getHeight()) {
continue;
}
blockHeaderList = chain.getAllBlockHeaderList();
for (int i = 0; i < blockHeaderList.size(); i++) {
BlockHeader header = blockHeaderList.get(i);
if (startBlockHeader.getPreHash().equals(header.getHash()) && startBlockHeader.getHeight() == header.getHeight() + 1) {
// yes connectioned
orphanChain.getChain().setPreChainId(chain.getPreChainId());
// orphanChain.getChain().initData(chain.getStartBlockHeader(), blockHeaderList.subList(0, i + 1), chain.getAllBlockList().subList(0, i + 1));
List<Block> blockList = chain.getAllBlockList().subList(0, i + 1);
for (int m = blockList.size() - 1; m >= 0; m--) {
orphanChain.getChain().addPreBlock(blockList.get(m));
}
chainManager.getChains().add(orphanChain);
if (i == blockHeaderList.size() - 1) {
chainManager.getChains().remove(forkChain);
}
ChainLog.debug("discover the OrphanChain {} : start {} - {} , end {} - {} , connection the fork chain of : start {} - {} , end {} - {}, move into the fork chians", orphanChain.getChain().getId(), startBlockHeader.getHeight(), startBlockHeader.getHash().getDigestHex(), orphanChain.getChain().getEndBlockHeader().getHeight(), orphanChain.getChain().getEndBlockHeader().getHash(), chainManager.getMasterChain().getChain().getId(), chain.getStartBlockHeader().getHeight(), chain.getStartBlockHeader().getHash(), chain.getEndBlockHeader().getHeight(), chain.getEndBlockHeader().getHash());
return true;
} else if (startBlockHeader.getHeight() == header.getHeight() + 1) {
break;
}
}
}
// 判断孤立链之间是否相连
for (ChainContainer orphan : chainManager.getOrphanChains()) {
if (orphan.getChain().getEndBlockHeader().getHash().equals(orphanChain.getChain().getStartBlockHeader().getPreHash()) && orphan.getChain().getEndBlockHeader().getHeight() + 1 == orphanChain.getChain().getStartBlockHeader().getHeight()) {
// Chain chain = orphan.getChain();
// chain.initData(orphanChain.getChain().getEndBlockHeader(), orphanChain.getChain().getAllBlockHeaderList(), orphanChain.getChain().getAllBlockList());
Chain chain = orphan.getChain();
List<Block> blockList = orphanChain.getChain().getAllBlockList();
for (Block block : blockList) {
chain.addBlock(block);
}
return true;
}
}
return false;
}
use of io.nuls.consensus.poc.model.Chain in project nuls by nuls-io.
the class ForkChainProcess method clearMasterChainData.
private void clearMasterChainData() {
Chain masterChain = chainManager.getMasterChain().getChain();
long bestHeight = masterChain.getEndBlockHeader().getHeight();
List<Agent> agentList = masterChain.getAgentList();
List<Deposit> depositList = masterChain.getDepositList();
Iterator<Agent> ait = agentList.iterator();
while (ait.hasNext()) {
Agent agent = ait.next();
if (agent.getDelHeight() > 0L && (bestHeight - 1000) > agent.getDelHeight()) {
ait.remove();
}
}
Iterator<Deposit> dit = depositList.iterator();
while (dit.hasNext()) {
Deposit deposit = dit.next();
if (deposit.getDelHeight() > 0L && (bestHeight - 1000) > deposit.getDelHeight()) {
dit.remove();
}
}
BlockExtendsData roundData = new BlockExtendsData(chainManager.getBestBlock().getHeader().getExtend());
List<PunishLogPo> yellowList = masterChain.getYellowPunishList();
Iterator<PunishLogPo> yit = yellowList.iterator();
while (yit.hasNext()) {
PunishLogPo punishLog = yit.next();
if (punishLog.getRoundIndex() < roundData.getPackingIndexOfRound() - PocConsensusConstant.INIT_HEADERS_OF_ROUND_COUNT) {
yit.remove();
}
}
}
use of io.nuls.consensus.poc.model.Chain in project nuls by nuls-io.
the class ChainContainerTest method testGetAfterTheForkChain.
@Test
public void testGetAfterTheForkChain() {
Block forkBlock = null;
for (int i = 0; i < 30; 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 == 20) {
forkBlock = bestBlock;
}
}
Chain chain = new Chain();
chain.addBlock(forkBlock);
Block newBlock = newBlock(forkBlock);
chain.addBlock(newBlock);
ChainContainer otherChainContainer = new ChainContainer(chain);
ChainContainer newForkChainContainer = chainContainer.getAfterTheForkChain(otherChainContainer);
assertEquals(newForkChainContainer.getBestBlock().getHeader().getHeight(), 30L);
}
use of io.nuls.consensus.poc.model.Chain in project nuls by nuls-io.
the class ChainManagerTest method testCheckIsBeforeOrphanChainAndAdd.
@Test
public void testCheckIsBeforeOrphanChainAndAdd() {
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(block1);
chainManager.getOrphanChains().add(orphanChain);
assertEquals(1, chainManager.getOrphanChains().size());
boolean success = chainManager.checkIsBeforeOrphanChainAndAdd(block);
assertTrue(success);
}
Aggregations