@Test public void testAddBlock() { Block bestBlock = chainContainer.getBestBlock(); assertNotNull(bestBlock); Block newBlock = newBlock(bestBlock); Result success = chainContainer.verifyBlock(newBlock); assertTrue(success.isSuccess()); bestBlock = chainContainer.getBestBlock(); assertEquals(bestBlock.getHeader().getHeight(), 0L); for (int i = 0; i < 100; i++) { newBlock = newBlock(bestBlock); success = chainContainer.verifyAndAddBlock(newBlock, false, true); assertTrue(success.isSuccess()); bestBlock = chainContainer.getBestBlock(); assertEquals(bestBlock.getHeader().getHeight(), 1L + i); } System.out.println(); }
public boolean addBlock(Block block) { if (!chain.getEndBlockHeader().getHash().equals(block.getHeader().getPreHash()) || chain.getEndBlockHeader().getHeight() + 1 != block.getHeader().getHeight()) { return false; } List<Agent> agentList = chain.getAgentList(); List<Deposit> depositList = chain.getDepositList(); List<PunishLogPo> yellowList = chain.getYellowPunishList(); List<PunishLogPo> redList = chain.getRedPunishList(); long height = block.getHeader().getHeight(); BlockExtendsData extendsData = new BlockExtendsData(block.getHeader().getExtend()); List<Transaction> txs = block.getTxs(); for (Transaction tx : txs) { int txType = tx.getType(); if (txType == ConsensusConstant.TX_TYPE_REGISTER_AGENT) { CreateAgentTransaction registerAgentTx = (CreateAgentTransaction) tx; CreateAgentTransaction agentTx = registerAgentTx.clone(); Agent agent = agentTx.getTxData(); agent.setDelHeight(-1L); agent.setBlockHeight(height); agent.setTxHash(agentTx.getHash()); agent.setTime(agentTx.getTime()); agentList.add(agent); } else if (txType == ConsensusConstant.TX_TYPE_JOIN_CONSENSUS) { DepositTransaction joinConsensusTx = (DepositTransaction) tx; DepositTransaction depositTx = joinConsensusTx.clone(); Deposit deposit = depositTx.getTxData(); deposit.setDelHeight(-1L); deposit.setBlockHeight(height); deposit.setTxHash(depositTx.getHash()); deposit.setTime(depositTx.getTime()); depositList.add(deposit); } else if (txType == ConsensusConstant.TX_TYPE_CANCEL_DEPOSIT) { CancelDepositTransaction cancelDepositTx = (CancelDepositTransaction) tx; NulsDigestData joinHash = cancelDepositTx.getTxData().getJoinTxHash(); Iterator<Deposit> it = depositList.iterator(); while (it.hasNext()) { Deposit deposit = it.next(); cancelDepositTx.getTxData().setAddress(deposit.getAddress()); if (deposit.getTxHash().equals(joinHash)) { if (deposit.getDelHeight() == -1L) { deposit.setDelHeight(height); } break; } } } else if (txType == ConsensusConstant.TX_TYPE_STOP_AGENT) { StopAgentTransaction stopAgentTx = (StopAgentTransaction) tx; NulsDigestData agentHash = stopAgentTx.getTxData().getCreateTxHash(); Iterator<Deposit> it = depositList.iterator(); while (it.hasNext()) { Deposit deposit = it.next(); if (deposit.getAgentHash().equals(agentHash) && deposit.getDelHeight() == -1L) { deposit.setDelHeight(height); } } Iterator<Agent> ita = agentList.iterator(); while (ita.hasNext()) { Agent agent = ita.next(); stopAgentTx.getTxData().setAddress(agent.getAgentAddress()); if (agent.getTxHash().equals(agentHash)) { if (agent.getDelHeight() == -1L) { agent.setDelHeight(height); } break; } } } else if (txType == ConsensusConstant.TX_TYPE_RED_PUNISH) { RedPunishTransaction transaction = (RedPunishTransaction) tx; RedPunishData redPunishData = transaction.getTxData(); PunishLogPo po = new PunishLogPo(); po.setAddress(redPunishData.getAddress()); po.setHeight(height); po.setRoundIndex(extendsData.getRoundIndex()); po.setTime(tx.getTime()); po.setType(PunishType.RED.getCode()); redList.add(po); for (Agent agent : agentList) { if (!Arrays.equals(agent.getAgentAddress(), po.getAddress())) { continue; } if (agent.getDelHeight() > 0) { continue; } agent.setDelHeight(height); for (Deposit deposit : depositList) { if (!deposit.getAgentHash().equals(agent.getTxHash())) { continue; } if (deposit.getDelHeight() > 0) { continue; } deposit.setDelHeight(height); } } } else if (txType == ConsensusConstant.TX_TYPE_YELLOW_PUNISH) { YellowPunishTransaction transaction = (YellowPunishTransaction) tx; for (byte[] bytes : transaction.getTxData().getAddressList()) { PunishLogPo po = new PunishLogPo(); po.setAddress(bytes); po.setHeight(height); po.setRoundIndex(extendsData.getRoundIndex()); po.setTime(tx.getTime()); po.setType(PunishType.YELLOW.getCode()); yellowList.add(po); } } } chain.addBlock(block); return true; }
ChainContainer implements Cloneable { public boolean addBlock(Block block) { if (!chain.getEndBlockHeader().getHash().equals(block.getHeader().getPreHash()) || chain.getEndBlockHeader().getHeight() + 1 != block.getHeader().getHeight()) { return false; } List<Agent> agentList = chain.getAgentList(); List<Deposit> depositList = chain.getDepositList(); List<PunishLogPo> yellowList = chain.getYellowPunishList(); List<PunishLogPo> redList = chain.getRedPunishList(); long height = block.getHeader().getHeight(); BlockExtendsData extendsData = new BlockExtendsData(block.getHeader().getExtend()); List<Transaction> txs = block.getTxs(); for (Transaction tx : txs) { int txType = tx.getType(); if (txType == ConsensusConstant.TX_TYPE_REGISTER_AGENT) { CreateAgentTransaction registerAgentTx = (CreateAgentTransaction) tx; CreateAgentTransaction agentTx = registerAgentTx.clone(); Agent agent = agentTx.getTxData(); agent.setDelHeight(-1L); agent.setBlockHeight(height); agent.setTxHash(agentTx.getHash()); agent.setTime(agentTx.getTime()); agentList.add(agent); } else if (txType == ConsensusConstant.TX_TYPE_JOIN_CONSENSUS) { DepositTransaction joinConsensusTx = (DepositTransaction) tx; DepositTransaction depositTx = joinConsensusTx.clone(); Deposit deposit = depositTx.getTxData(); deposit.setDelHeight(-1L); deposit.setBlockHeight(height); deposit.setTxHash(depositTx.getHash()); deposit.setTime(depositTx.getTime()); depositList.add(deposit); } else if (txType == ConsensusConstant.TX_TYPE_CANCEL_DEPOSIT) { CancelDepositTransaction cancelDepositTx = (CancelDepositTransaction) tx; NulsDigestData joinHash = cancelDepositTx.getTxData().getJoinTxHash(); Iterator<Deposit> it = depositList.iterator(); while (it.hasNext()) { Deposit deposit = it.next(); cancelDepositTx.getTxData().setAddress(deposit.getAddress()); if (deposit.getTxHash().equals(joinHash)) { if (deposit.getDelHeight() == -1L) { deposit.setDelHeight(height); } break; } } } else if (txType == ConsensusConstant.TX_TYPE_STOP_AGENT) { StopAgentTransaction stopAgentTx = (StopAgentTransaction) tx; NulsDigestData agentHash = stopAgentTx.getTxData().getCreateTxHash(); Iterator<Deposit> it = depositList.iterator(); while (it.hasNext()) { Deposit deposit = it.next(); if (deposit.getAgentHash().equals(agentHash) && deposit.getDelHeight() == -1L) { deposit.setDelHeight(height); } } Iterator<Agent> ita = agentList.iterator(); while (ita.hasNext()) { Agent agent = ita.next(); stopAgentTx.getTxData().setAddress(agent.getAgentAddress()); if (agent.getTxHash().equals(agentHash)) { if (agent.getDelHeight() == -1L) { agent.setDelHeight(height); } break; } } } else if (txType == ConsensusConstant.TX_TYPE_RED_PUNISH) { RedPunishTransaction transaction = (RedPunishTransaction) tx; RedPunishData redPunishData = transaction.getTxData(); PunishLogPo po = new PunishLogPo(); po.setAddress(redPunishData.getAddress()); po.setHeight(height); po.setRoundIndex(extendsData.getRoundIndex()); po.setTime(tx.getTime()); po.setType(PunishType.RED.getCode()); redList.add(po); for (Agent agent : agentList) { if (!Arrays.equals(agent.getAgentAddress(), po.getAddress())) { continue; } if (agent.getDelHeight() > 0) { continue; } agent.setDelHeight(height); for (Deposit deposit : depositList) { if (!deposit.getAgentHash().equals(agent.getTxHash())) { continue; } if (deposit.getDelHeight() > 0) { continue; } deposit.setDelHeight(height); } } } else if (txType == ConsensusConstant.TX_TYPE_YELLOW_PUNISH) { YellowPunishTransaction transaction = (YellowPunishTransaction) tx; for (byte[] bytes : transaction.getTxData().getAddressList()) { PunishLogPo po = new PunishLogPo(); po.setAddress(bytes); po.setHeight(height); po.setRoundIndex(extendsData.getRoundIndex()); po.setTime(tx.getTime()); po.setType(PunishType.YELLOW.getCode()); yellowList.add(po); } } } chain.addBlock(block); return true; } }
ChainContainer implements Cloneable { public boolean addBlock(Block block) { if (!chain.getEndBlockHeader().getHash().equals(block.getHeader().getPreHash()) || chain.getEndBlockHeader().getHeight() + 1 != block.getHeader().getHeight()) { return false; } List<Agent> agentList = chain.getAgentList(); List<Deposit> depositList = chain.getDepositList(); List<PunishLogPo> yellowList = chain.getYellowPunishList(); List<PunishLogPo> redList = chain.getRedPunishList(); long height = block.getHeader().getHeight(); BlockExtendsData extendsData = new BlockExtendsData(block.getHeader().getExtend()); List<Transaction> txs = block.getTxs(); for (Transaction tx : txs) { int txType = tx.getType(); if (txType == ConsensusConstant.TX_TYPE_REGISTER_AGENT) { CreateAgentTransaction registerAgentTx = (CreateAgentTransaction) tx; CreateAgentTransaction agentTx = registerAgentTx.clone(); Agent agent = agentTx.getTxData(); agent.setDelHeight(-1L); agent.setBlockHeight(height); agent.setTxHash(agentTx.getHash()); agent.setTime(agentTx.getTime()); agentList.add(agent); } else if (txType == ConsensusConstant.TX_TYPE_JOIN_CONSENSUS) { DepositTransaction joinConsensusTx = (DepositTransaction) tx; DepositTransaction depositTx = joinConsensusTx.clone(); Deposit deposit = depositTx.getTxData(); deposit.setDelHeight(-1L); deposit.setBlockHeight(height); deposit.setTxHash(depositTx.getHash()); deposit.setTime(depositTx.getTime()); depositList.add(deposit); } else if (txType == ConsensusConstant.TX_TYPE_CANCEL_DEPOSIT) { CancelDepositTransaction cancelDepositTx = (CancelDepositTransaction) tx; NulsDigestData joinHash = cancelDepositTx.getTxData().getJoinTxHash(); Iterator<Deposit> it = depositList.iterator(); while (it.hasNext()) { Deposit deposit = it.next(); cancelDepositTx.getTxData().setAddress(deposit.getAddress()); if (deposit.getTxHash().equals(joinHash)) { if (deposit.getDelHeight() == -1L) { deposit.setDelHeight(height); } break; } } } else if (txType == ConsensusConstant.TX_TYPE_STOP_AGENT) { StopAgentTransaction stopAgentTx = (StopAgentTransaction) tx; NulsDigestData agentHash = stopAgentTx.getTxData().getCreateTxHash(); Iterator<Deposit> it = depositList.iterator(); while (it.hasNext()) { Deposit deposit = it.next(); if (deposit.getAgentHash().equals(agentHash) && deposit.getDelHeight() == -1L) { deposit.setDelHeight(height); } } Iterator<Agent> ita = agentList.iterator(); while (ita.hasNext()) { Agent agent = ita.next(); stopAgentTx.getTxData().setAddress(agent.getAgentAddress()); if (agent.getTxHash().equals(agentHash)) { if (agent.getDelHeight() == -1L) { agent.setDelHeight(height); } break; } } } else if (txType == ConsensusConstant.TX_TYPE_RED_PUNISH) { RedPunishTransaction transaction = (RedPunishTransaction) tx; RedPunishData redPunishData = transaction.getTxData(); PunishLogPo po = new PunishLogPo(); po.setAddress(redPunishData.getAddress()); po.setHeight(height); po.setRoundIndex(extendsData.getRoundIndex()); po.setTime(tx.getTime()); po.setType(PunishType.RED.getCode()); redList.add(po); for (Agent agent : agentList) { if (!Arrays.equals(agent.getAgentAddress(), po.getAddress())) { continue; } if (agent.getDelHeight() > 0) { continue; } agent.setDelHeight(height); for (Deposit deposit : depositList) { if (!deposit.getAgentHash().equals(agent.getTxHash())) { continue; } if (deposit.getDelHeight() > 0) { continue; } deposit.setDelHeight(height); } } } else if (txType == ConsensusConstant.TX_TYPE_YELLOW_PUNISH) { YellowPunishTransaction transaction = (YellowPunishTransaction) tx; for (byte[] bytes : transaction.getTxData().getAddressList()) { PunishLogPo po = new PunishLogPo(); po.setAddress(bytes); po.setHeight(height); po.setRoundIndex(extendsData.getRoundIndex()); po.setTime(tx.getTime()); po.setType(PunishType.YELLOW.getCode()); yellowList.add(po); } } } chain.addBlock(block); return true; } ChainContainer(Chain chain); }
ChainContainer implements Cloneable { public boolean addBlock(Block block) { if (!chain.getEndBlockHeader().getHash().equals(block.getHeader().getPreHash()) || chain.getEndBlockHeader().getHeight() + 1 != block.getHeader().getHeight()) { return false; } List<Agent> agentList = chain.getAgentList(); List<Deposit> depositList = chain.getDepositList(); List<PunishLogPo> yellowList = chain.getYellowPunishList(); List<PunishLogPo> redList = chain.getRedPunishList(); long height = block.getHeader().getHeight(); BlockExtendsData extendsData = new BlockExtendsData(block.getHeader().getExtend()); List<Transaction> txs = block.getTxs(); for (Transaction tx : txs) { int txType = tx.getType(); if (txType == ConsensusConstant.TX_TYPE_REGISTER_AGENT) { CreateAgentTransaction registerAgentTx = (CreateAgentTransaction) tx; CreateAgentTransaction agentTx = registerAgentTx.clone(); Agent agent = agentTx.getTxData(); agent.setDelHeight(-1L); agent.setBlockHeight(height); agent.setTxHash(agentTx.getHash()); agent.setTime(agentTx.getTime()); agentList.add(agent); } else if (txType == ConsensusConstant.TX_TYPE_JOIN_CONSENSUS) { DepositTransaction joinConsensusTx = (DepositTransaction) tx; DepositTransaction depositTx = joinConsensusTx.clone(); Deposit deposit = depositTx.getTxData(); deposit.setDelHeight(-1L); deposit.setBlockHeight(height); deposit.setTxHash(depositTx.getHash()); deposit.setTime(depositTx.getTime()); depositList.add(deposit); } else if (txType == ConsensusConstant.TX_TYPE_CANCEL_DEPOSIT) { CancelDepositTransaction cancelDepositTx = (CancelDepositTransaction) tx; NulsDigestData joinHash = cancelDepositTx.getTxData().getJoinTxHash(); Iterator<Deposit> it = depositList.iterator(); while (it.hasNext()) { Deposit deposit = it.next(); cancelDepositTx.getTxData().setAddress(deposit.getAddress()); if (deposit.getTxHash().equals(joinHash)) { if (deposit.getDelHeight() == -1L) { deposit.setDelHeight(height); } break; } } } else if (txType == ConsensusConstant.TX_TYPE_STOP_AGENT) { StopAgentTransaction stopAgentTx = (StopAgentTransaction) tx; NulsDigestData agentHash = stopAgentTx.getTxData().getCreateTxHash(); Iterator<Deposit> it = depositList.iterator(); while (it.hasNext()) { Deposit deposit = it.next(); if (deposit.getAgentHash().equals(agentHash) && deposit.getDelHeight() == -1L) { deposit.setDelHeight(height); } } Iterator<Agent> ita = agentList.iterator(); while (ita.hasNext()) { Agent agent = ita.next(); stopAgentTx.getTxData().setAddress(agent.getAgentAddress()); if (agent.getTxHash().equals(agentHash)) { if (agent.getDelHeight() == -1L) { agent.setDelHeight(height); } break; } } } else if (txType == ConsensusConstant.TX_TYPE_RED_PUNISH) { RedPunishTransaction transaction = (RedPunishTransaction) tx; RedPunishData redPunishData = transaction.getTxData(); PunishLogPo po = new PunishLogPo(); po.setAddress(redPunishData.getAddress()); po.setHeight(height); po.setRoundIndex(extendsData.getRoundIndex()); po.setTime(tx.getTime()); po.setType(PunishType.RED.getCode()); redList.add(po); for (Agent agent : agentList) { if (!Arrays.equals(agent.getAgentAddress(), po.getAddress())) { continue; } if (agent.getDelHeight() > 0) { continue; } agent.setDelHeight(height); for (Deposit deposit : depositList) { if (!deposit.getAgentHash().equals(agent.getTxHash())) { continue; } if (deposit.getDelHeight() > 0) { continue; } deposit.setDelHeight(height); } } } else if (txType == ConsensusConstant.TX_TYPE_YELLOW_PUNISH) { YellowPunishTransaction transaction = (YellowPunishTransaction) tx; for (byte[] bytes : transaction.getTxData().getAddressList()) { PunishLogPo po = new PunishLogPo(); po.setAddress(bytes); po.setHeight(height); po.setRoundIndex(extendsData.getRoundIndex()); po.setTime(tx.getTime()); po.setType(PunishType.YELLOW.getCode()); yellowList.add(po); } } } chain.addBlock(block); return true; } ChainContainer(Chain chain); boolean addBlock(Block block); Result verifyBlock(Block block); Result verifyBlock(Block block, boolean isDownload, boolean isNeedCheckCoinBaseTx); boolean verifyCoinBaseTx(Block block, MeetingRound currentRound, MeetingMember member); boolean verifyPunishTx(Block block, MeetingRound currentRound, MeetingMember member); Result verifyAndAddBlock(Block block, boolean isDownload, boolean isNeedCheckCoinBaseTx); boolean rollback(Block block); ChainContainer getBeforeTheForkChain(ChainContainer chainContainer); ChainContainer getAfterTheForkChain(ChainContainer chainContainer); MeetingRound getCurrentRound(); MeetingRound getOrResetCurrentRound(); MeetingRound getOrResetCurrentRound(boolean isRealTime); MeetingRound initRound(); void clearRound(int count); Block getBestBlock(); Chain getChain(); void setChain(Chain chain); @Override boolean equals(Object obj); RoundManager getRoundManager(); }
ChainContainer implements Cloneable { public boolean addBlock(Block block) { if (!chain.getEndBlockHeader().getHash().equals(block.getHeader().getPreHash()) || chain.getEndBlockHeader().getHeight() + 1 != block.getHeader().getHeight()) { return false; } List<Agent> agentList = chain.getAgentList(); List<Deposit> depositList = chain.getDepositList(); List<PunishLogPo> yellowList = chain.getYellowPunishList(); List<PunishLogPo> redList = chain.getRedPunishList(); long height = block.getHeader().getHeight(); BlockExtendsData extendsData = new BlockExtendsData(block.getHeader().getExtend()); List<Transaction> txs = block.getTxs(); for (Transaction tx : txs) { int txType = tx.getType(); if (txType == ConsensusConstant.TX_TYPE_REGISTER_AGENT) { CreateAgentTransaction registerAgentTx = (CreateAgentTransaction) tx; CreateAgentTransaction agentTx = registerAgentTx.clone(); Agent agent = agentTx.getTxData(); agent.setDelHeight(-1L); agent.setBlockHeight(height); agent.setTxHash(agentTx.getHash()); agent.setTime(agentTx.getTime()); agentList.add(agent); } else if (txType == ConsensusConstant.TX_TYPE_JOIN_CONSENSUS) { DepositTransaction joinConsensusTx = (DepositTransaction) tx; DepositTransaction depositTx = joinConsensusTx.clone(); Deposit deposit = depositTx.getTxData(); deposit.setDelHeight(-1L); deposit.setBlockHeight(height); deposit.setTxHash(depositTx.getHash()); deposit.setTime(depositTx.getTime()); depositList.add(deposit); } else if (txType == ConsensusConstant.TX_TYPE_CANCEL_DEPOSIT) { CancelDepositTransaction cancelDepositTx = (CancelDepositTransaction) tx; NulsDigestData joinHash = cancelDepositTx.getTxData().getJoinTxHash(); Iterator<Deposit> it = depositList.iterator(); while (it.hasNext()) { Deposit deposit = it.next(); cancelDepositTx.getTxData().setAddress(deposit.getAddress()); if (deposit.getTxHash().equals(joinHash)) { if (deposit.getDelHeight() == -1L) { deposit.setDelHeight(height); } break; } } } else if (txType == ConsensusConstant.TX_TYPE_STOP_AGENT) { StopAgentTransaction stopAgentTx = (StopAgentTransaction) tx; NulsDigestData agentHash = stopAgentTx.getTxData().getCreateTxHash(); Iterator<Deposit> it = depositList.iterator(); while (it.hasNext()) { Deposit deposit = it.next(); if (deposit.getAgentHash().equals(agentHash) && deposit.getDelHeight() == -1L) { deposit.setDelHeight(height); } } Iterator<Agent> ita = agentList.iterator(); while (ita.hasNext()) { Agent agent = ita.next(); stopAgentTx.getTxData().setAddress(agent.getAgentAddress()); if (agent.getTxHash().equals(agentHash)) { if (agent.getDelHeight() == -1L) { agent.setDelHeight(height); } break; } } } else if (txType == ConsensusConstant.TX_TYPE_RED_PUNISH) { RedPunishTransaction transaction = (RedPunishTransaction) tx; RedPunishData redPunishData = transaction.getTxData(); PunishLogPo po = new PunishLogPo(); po.setAddress(redPunishData.getAddress()); po.setHeight(height); po.setRoundIndex(extendsData.getRoundIndex()); po.setTime(tx.getTime()); po.setType(PunishType.RED.getCode()); redList.add(po); for (Agent agent : agentList) { if (!Arrays.equals(agent.getAgentAddress(), po.getAddress())) { continue; } if (agent.getDelHeight() > 0) { continue; } agent.setDelHeight(height); for (Deposit deposit : depositList) { if (!deposit.getAgentHash().equals(agent.getTxHash())) { continue; } if (deposit.getDelHeight() > 0) { continue; } deposit.setDelHeight(height); } } } else if (txType == ConsensusConstant.TX_TYPE_YELLOW_PUNISH) { YellowPunishTransaction transaction = (YellowPunishTransaction) tx; for (byte[] bytes : transaction.getTxData().getAddressList()) { PunishLogPo po = new PunishLogPo(); po.setAddress(bytes); po.setHeight(height); po.setRoundIndex(extendsData.getRoundIndex()); po.setTime(tx.getTime()); po.setType(PunishType.YELLOW.getCode()); yellowList.add(po); } } } chain.addBlock(block); return true; } ChainContainer(Chain chain); boolean addBlock(Block block); Result verifyBlock(Block block); Result verifyBlock(Block block, boolean isDownload, boolean isNeedCheckCoinBaseTx); boolean verifyCoinBaseTx(Block block, MeetingRound currentRound, MeetingMember member); boolean verifyPunishTx(Block block, MeetingRound currentRound, MeetingMember member); Result verifyAndAddBlock(Block block, boolean isDownload, boolean isNeedCheckCoinBaseTx); boolean rollback(Block block); ChainContainer getBeforeTheForkChain(ChainContainer chainContainer); ChainContainer getAfterTheForkChain(ChainContainer chainContainer); MeetingRound getCurrentRound(); MeetingRound getOrResetCurrentRound(); MeetingRound getOrResetCurrentRound(boolean isRealTime); MeetingRound initRound(); void clearRound(int count); Block getBestBlock(); Chain getChain(); void setChain(Chain chain); @Override boolean equals(Object obj); RoundManager getRoundManager();  }