Search in sources :

Example 1 with Chain

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

Example 2 with Chain

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

Example 3 with Chain

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());
}
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)

Example 4 with Chain

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);
}
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)

Example 5 with Chain

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

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