Search in sources :

Example 6 with ChainContainer

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

use of io.nuls.consensus.poc.container.ChainContainer 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 8 with ChainContainer

use of io.nuls.consensus.poc.container.ChainContainer 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 9 with ChainContainer

use of io.nuls.consensus.poc.container.ChainContainer 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 10 with ChainContainer

use of io.nuls.consensus.poc.container.ChainContainer in project nuls by nuls-io.

the class BaseChainTest method initChain.

protected void initChain() {
    chain = new Chain();
    // new a block header
    BlockHeader blockHeader = new BlockHeader();
    blockHeader.setHeight(0);
    blockHeader.setPreHash(NulsDigestData.calcDigestData("00000000000".getBytes()));
    blockHeader.setTime(1L);
    blockHeader.setTxCount(0);
    // add a round data
    BlockRoundData roundData = new BlockRoundData();
    roundData.setConsensusMemberCount(1);
    roundData.setPackingIndexOfRound(1);
    roundData.setRoundIndex(1);
    roundData.setRoundStartTime(1L);
    try {
        blockHeader.setExtend(roundData.serialize());
    } catch (IOException e) {
        throw new NulsRuntimeException(e);
    }
    // new a block of height 0
    Block block = new Block();
    block.setHeader(blockHeader);
    // chain.setEndBlockHeader(blockHeader);
    // // add the block into chain
    // chain.getBlockList().add(block);
    // chain.setStartBlockHeader(blockHeader);
    // chain.setEndBlockHeader(blockHeader);
    chain.addBlock(block);
    // init some agent
    List<Agent> agentList = new ArrayList<>();
    Transaction<Agent> agentTx = new CreateAgentTransaction();
    Agent agent = new Agent();
    agent.setPackingAddress(AddressTool.getAddress(ecKey.getPubKey()));
    agent.setAgentAddress(AddressTool.getAddress(new ECKey().getPubKey()));
    agent.setRewardAddress(AddressTool.getAddress(ecKey.getPubKey()));
    agent.setTime(System.currentTimeMillis());
    agent.setDeposit(Na.NA.multiply(20000));
    agent.setCommissionRate(0.3d);
    agent.setBlockHeight(blockHeader.getHeight());
    agentTx.setTxData(agent);
    agentTx.setTime(agent.getTime());
    agentTx.setBlockHeight(blockHeader.getHeight());
    NulsSignData signData = signDigest(agentTx.getHash().getDigestBytes(), ecKey);
    agentTx.setTransactionSignature(signData.getSignBytes());
    agentTx.getTxData().setTxHash(agentTx.getHash());
    // add the agent tx into agent list
    agentList.add(agentTx.getTxData());
    // set the agent list into chain
    chain.setAgentList(agentList);
    // new a deposit
    Deposit deposit = new Deposit();
    deposit.setAddress(AddressTool.getAddress(ecKey.getPubKey()));
    deposit.setAgentHash(agentTx.getHash());
    deposit.setTime(System.currentTimeMillis());
    deposit.setDeposit(Na.NA.multiply(200000));
    deposit.setBlockHeight(blockHeader.getHeight());
    DepositTransaction depositTx = new DepositTransaction();
    depositTx.setTime(deposit.getTime());
    depositTx.setTxData(deposit);
    depositTx.setBlockHeight(blockHeader.getHeight());
    List<Deposit> depositList = new ArrayList<>();
    depositList.add(depositTx.getTxData());
    chain.setDepositList(depositList);
    chain.setYellowPunishList(new ArrayList<>());
    chain.setRedPunishList(new ArrayList<>());
    chainContainer = new ChainContainer(chain);
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) ChainContainer(io.nuls.consensus.poc.container.ChainContainer) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) ECKey(io.nuls.core.tools.crypto.ECKey) IOException(java.io.IOException) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)

Aggregations

ChainContainer (io.nuls.consensus.poc.container.ChainContainer)17 Chain (io.nuls.consensus.poc.model.Chain)9 Block (io.nuls.kernel.model.Block)4 IOException (java.io.IOException)4 BaseTest (io.nuls.consensus.poc.BaseTest)3 NulsException (io.nuls.kernel.exception.NulsException)3 BlockHeader (io.nuls.kernel.model.BlockHeader)3 Test (org.junit.Test)3 Agent (io.nuls.consensus.poc.protocol.entity.Agent)2 Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)2 ContractResult (io.nuls.contract.dto.ContractResult)2 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)2 ValidateResult (io.nuls.kernel.validate.ValidateResult)2 CreateAgentTransaction (io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)1 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)1 PunishLogPo (io.nuls.consensus.poc.storage.po.PunishLogPo)1 PunishLogStorageService (io.nuls.consensus.poc.storage.service.PunishLogStorageService)1 ECKey (io.nuls.core.tools.crypto.ECKey)1 ArrayList (java.util.ArrayList)1