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