Search in sources :

Example 6 with Chain

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();
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) Chain(io.nuls.consensus.poc.model.Chain) ChainContainer(io.nuls.consensus.poc.container.ChainContainer) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) Block(io.nuls.kernel.model.Block) PunishLogStorageService(io.nuls.consensus.poc.storage.service.PunishLogStorageService) BlockHeader(io.nuls.kernel.model.BlockHeader) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Example 7 with Chain

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;
}
Also used : Chain(io.nuls.consensus.poc.model.Chain) ChainContainer(io.nuls.consensus.poc.container.ChainContainer)

Example 8 with Chain

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();
        }
    }
}
Also used : Chain(io.nuls.consensus.poc.model.Chain) Agent(io.nuls.consensus.poc.protocol.entity.Agent) Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) BlockExtendsData(io.nuls.consensus.poc.model.BlockExtendsData) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Example 9 with Chain

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);
}
Also used : Chain(io.nuls.consensus.poc.model.Chain) BaseChainTest(io.nuls.consensus.poc.BaseChainTest) Test(org.junit.Test)

Example 10 with Chain

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);
}
Also used : Chain(io.nuls.consensus.poc.model.Chain) ChainContainer(io.nuls.consensus.poc.container.ChainContainer) Block(io.nuls.kernel.model.Block) Test(org.junit.Test) BaseTest(io.nuls.consensus.poc.BaseTest)

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