use of io.nuls.consensus.poc.protocol.entity.Deposit in project nuls by nuls-io.
the class ChainContainer method getBeforeTheForkChain.
/**
* Get the state of the complete chain after the combination of a chain and the current chain bifurcation point, that is, first obtain the bifurcation point between the bifurcation chain and the current chain.
* Then create a brand new chain, copy all the states before the bifurcation point of the main chain to the brand new chain
* <p>
* 获取一条链与当前链分叉点组合之后的完整链的状态,也就是,先获取到分叉链与当前链的分叉点,
* 然后创建一条全新的链,把主链分叉点之前的所有状态复制到全新的链
*
* @return ChainContainer
*/
public ChainContainer getBeforeTheForkChain(ChainContainer chainContainer) {
Chain newChain = new Chain();
newChain.setId(chainContainer.getChain().getId());
newChain.initData(chain.getStartBlockHeader(), new CopyOnWriteArrayList<>(chain.getAllBlockHeaderList()), new CopyOnWriteArrayList<>(chain.getAllBlockList()));
if (chain.getAgentList() != null) {
List<Agent> agentList = new ArrayList<>();
for (Agent agent : chain.getAgentList()) {
try {
agentList.add(agent.clone());
} catch (CloneNotSupportedException e) {
Log.error(e);
}
}
newChain.setAgentList(agentList);
}
if (chain.getDepositList() != null) {
List<Deposit> depositList = new ArrayList<>();
for (Deposit deposit : chain.getDepositList()) {
try {
depositList.add(deposit.clone());
} catch (CloneNotSupportedException e) {
Log.error(e);
}
}
newChain.setDepositList(depositList);
}
if (chain.getYellowPunishList() != null) {
newChain.setYellowPunishList(new ArrayList<>(chain.getYellowPunishList()));
}
if (chain.getRedPunishList() != null) {
newChain.setRedPunishList(new ArrayList<>(chain.getRedPunishList()));
}
ChainContainer newChainContainer = new ChainContainer(newChain);
// Bifurcation
// 分叉点
BlockHeader pointBlockHeader = chainContainer.getChain().getStartBlockHeader();
List<Block> blockList = newChain.getAllBlockList();
for (int i = blockList.size() - 1; i >= 0; i--) {
Block block = blockList.get(i);
if (pointBlockHeader.getPreHash().equals(block.getHeader().getHash())) {
break;
}
newChainContainer.rollback(block);
}
newChainContainer.initRound();
return newChainContainer;
}
use of io.nuls.consensus.poc.protocol.entity.Deposit 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.protocol.entity.Deposit in project nuls by nuls-io.
the class RoundManager method getDepositListMap.
private Map<NulsDigestData, List<Deposit>> getDepositListMap(long startBlockHeight) {
List<Deposit> depositList = chain.getDepositList();
Map<NulsDigestData, List<Deposit>> map = new HashMap<>();
for (int i = depositList.size() - 1; i >= 0; i--) {
Deposit deposit = depositList.get(i);
if (deposit.getDelHeight() != -1L && deposit.getDelHeight() <= startBlockHeight) {
continue;
}
if (deposit.getBlockHeight() > startBlockHeight || deposit.getBlockHeight() < 0L) {
continue;
}
List<Deposit> list = map.get(deposit.getAgentHash());
if (null == list) {
list = new ArrayList<>();
map.put(deposit.getAgentHash(), list);
}
list.add(deposit);
}
return map;
}
use of io.nuls.consensus.poc.protocol.entity.Deposit 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);
}
use of io.nuls.consensus.poc.protocol.entity.Deposit 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();
}
}
}
Aggregations