@Test public void testRollback() { testAddAgent(); assertEquals(chainContainer.getCurrentRound().getMemberCount(), 2); for (int i = 3; i > 0; i--) { Block bestBlock = chainContainer.getBestBlock(); boolean success = chainContainer.rollback(bestBlock); assert (success); } assertEquals(chainContainer.getCurrentRound().getMemberCount(), 1); }
public boolean rollback(Block block) { int length = chain.getAllBlockList().size(); if (block == null || length == 0) { return false; } Block bestBlock = chain.getBestBlock(); if (!block.getHeader().getHash().equals(bestBlock.getHeader().getHash())) { Log.warn("rollbackTransaction block is not best block"); return false; } if (length <= 2) { addBlockInBlockList(chain); } BlockHeader rollbackBlockHeader = chain.rollbackBlock(); List<Agent> agentList = chain.getAgentList(); List<Deposit> depositList = chain.getDepositList(); List<PunishLogPo> yellowList = chain.getYellowPunishList(); List<PunishLogPo> redPunishList = chain.getRedPunishList(); long height = rollbackBlockHeader.getHeight(); for (int i = agentList.size() - 1; i >= 0; i--) { Agent agent = agentList.get(i); if (agent.getDelHeight() == height) { agent.setDelHeight(-1L); } if (agent.getBlockHeight() == height) { agentList.remove(i); } } for (int i = depositList.size() - 1; i >= 0; i--) { Deposit deposit = depositList.get(i); if (deposit.getDelHeight() == height) { deposit.setDelHeight(-1L); } if (deposit.getBlockHeight() == height) { depositList.remove(i); } } for (int i = yellowList.size() - 1; i >= 0; i--) { PunishLogPo tempYellow = yellowList.get(i); if (tempYellow.getHeight() < height) { break; } if (tempYellow.getHeight() == height) { yellowList.remove(i); } } for (int i = redPunishList.size() - 1; i >= 0; i--) { PunishLogPo redPunish = redPunishList.get(i); if (redPunish.getHeight() < height) { break; } if (redPunish.getHeight() == height) { redPunishList.remove(i); } } roundManager.checkIsNeedReset(); return true; }
ChainContainer implements Cloneable { public boolean rollback(Block block) { int length = chain.getAllBlockList().size(); if (block == null || length == 0) { return false; } Block bestBlock = chain.getBestBlock(); if (!block.getHeader().getHash().equals(bestBlock.getHeader().getHash())) { Log.warn("rollbackTransaction block is not best block"); return false; } if (length <= 2) { addBlockInBlockList(chain); } BlockHeader rollbackBlockHeader = chain.rollbackBlock(); List<Agent> agentList = chain.getAgentList(); List<Deposit> depositList = chain.getDepositList(); List<PunishLogPo> yellowList = chain.getYellowPunishList(); List<PunishLogPo> redPunishList = chain.getRedPunishList(); long height = rollbackBlockHeader.getHeight(); for (int i = agentList.size() - 1; i >= 0; i--) { Agent agent = agentList.get(i); if (agent.getDelHeight() == height) { agent.setDelHeight(-1L); } if (agent.getBlockHeight() == height) { agentList.remove(i); } } for (int i = depositList.size() - 1; i >= 0; i--) { Deposit deposit = depositList.get(i); if (deposit.getDelHeight() == height) { deposit.setDelHeight(-1L); } if (deposit.getBlockHeight() == height) { depositList.remove(i); } } for (int i = yellowList.size() - 1; i >= 0; i--) { PunishLogPo tempYellow = yellowList.get(i); if (tempYellow.getHeight() < height) { break; } if (tempYellow.getHeight() == height) { yellowList.remove(i); } } for (int i = redPunishList.size() - 1; i >= 0; i--) { PunishLogPo redPunish = redPunishList.get(i); if (redPunish.getHeight() < height) { break; } if (redPunish.getHeight() == height) { redPunishList.remove(i); } } roundManager.checkIsNeedReset(); return true; } }
ChainContainer implements Cloneable { public boolean rollback(Block block) { int length = chain.getAllBlockList().size(); if (block == null || length == 0) { return false; } Block bestBlock = chain.getBestBlock(); if (!block.getHeader().getHash().equals(bestBlock.getHeader().getHash())) { Log.warn("rollbackTransaction block is not best block"); return false; } if (length <= 2) { addBlockInBlockList(chain); } BlockHeader rollbackBlockHeader = chain.rollbackBlock(); List<Agent> agentList = chain.getAgentList(); List<Deposit> depositList = chain.getDepositList(); List<PunishLogPo> yellowList = chain.getYellowPunishList(); List<PunishLogPo> redPunishList = chain.getRedPunishList(); long height = rollbackBlockHeader.getHeight(); for (int i = agentList.size() - 1; i >= 0; i--) { Agent agent = agentList.get(i); if (agent.getDelHeight() == height) { agent.setDelHeight(-1L); } if (agent.getBlockHeight() == height) { agentList.remove(i); } } for (int i = depositList.size() - 1; i >= 0; i--) { Deposit deposit = depositList.get(i); if (deposit.getDelHeight() == height) { deposit.setDelHeight(-1L); } if (deposit.getBlockHeight() == height) { depositList.remove(i); } } for (int i = yellowList.size() - 1; i >= 0; i--) { PunishLogPo tempYellow = yellowList.get(i); if (tempYellow.getHeight() < height) { break; } if (tempYellow.getHeight() == height) { yellowList.remove(i); } } for (int i = redPunishList.size() - 1; i >= 0; i--) { PunishLogPo redPunish = redPunishList.get(i); if (redPunish.getHeight() < height) { break; } if (redPunish.getHeight() == height) { redPunishList.remove(i); } } roundManager.checkIsNeedReset(); return true; } ChainContainer(Chain chain); }
ChainContainer implements Cloneable { public boolean rollback(Block block) { int length = chain.getAllBlockList().size(); if (block == null || length == 0) { return false; } Block bestBlock = chain.getBestBlock(); if (!block.getHeader().getHash().equals(bestBlock.getHeader().getHash())) { Log.warn("rollbackTransaction block is not best block"); return false; } if (length <= 2) { addBlockInBlockList(chain); } BlockHeader rollbackBlockHeader = chain.rollbackBlock(); List<Agent> agentList = chain.getAgentList(); List<Deposit> depositList = chain.getDepositList(); List<PunishLogPo> yellowList = chain.getYellowPunishList(); List<PunishLogPo> redPunishList = chain.getRedPunishList(); long height = rollbackBlockHeader.getHeight(); for (int i = agentList.size() - 1; i >= 0; i--) { Agent agent = agentList.get(i); if (agent.getDelHeight() == height) { agent.setDelHeight(-1L); } if (agent.getBlockHeight() == height) { agentList.remove(i); } } for (int i = depositList.size() - 1; i >= 0; i--) { Deposit deposit = depositList.get(i); if (deposit.getDelHeight() == height) { deposit.setDelHeight(-1L); } if (deposit.getBlockHeight() == height) { depositList.remove(i); } } for (int i = yellowList.size() - 1; i >= 0; i--) { PunishLogPo tempYellow = yellowList.get(i); if (tempYellow.getHeight() < height) { break; } if (tempYellow.getHeight() == height) { yellowList.remove(i); } } for (int i = redPunishList.size() - 1; i >= 0; i--) { PunishLogPo redPunish = redPunishList.get(i); if (redPunish.getHeight() < height) { break; } if (redPunish.getHeight() == height) { redPunishList.remove(i); } } roundManager.checkIsNeedReset(); 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 rollback(Block block) { int length = chain.getAllBlockList().size(); if (block == null || length == 0) { return false; } Block bestBlock = chain.getBestBlock(); if (!block.getHeader().getHash().equals(bestBlock.getHeader().getHash())) { Log.warn("rollbackTransaction block is not best block"); return false; } if (length <= 2) { addBlockInBlockList(chain); } BlockHeader rollbackBlockHeader = chain.rollbackBlock(); List<Agent> agentList = chain.getAgentList(); List<Deposit> depositList = chain.getDepositList(); List<PunishLogPo> yellowList = chain.getYellowPunishList(); List<PunishLogPo> redPunishList = chain.getRedPunishList(); long height = rollbackBlockHeader.getHeight(); for (int i = agentList.size() - 1; i >= 0; i--) { Agent agent = agentList.get(i); if (agent.getDelHeight() == height) { agent.setDelHeight(-1L); } if (agent.getBlockHeight() == height) { agentList.remove(i); } } for (int i = depositList.size() - 1; i >= 0; i--) { Deposit deposit = depositList.get(i); if (deposit.getDelHeight() == height) { deposit.setDelHeight(-1L); } if (deposit.getBlockHeight() == height) { depositList.remove(i); } } for (int i = yellowList.size() - 1; i >= 0; i--) { PunishLogPo tempYellow = yellowList.get(i); if (tempYellow.getHeight() < height) { break; } if (tempYellow.getHeight() == height) { yellowList.remove(i); } } for (int i = redPunishList.size() - 1; i >= 0; i--) { PunishLogPo redPunish = redPunishList.get(i); if (redPunish.getHeight() < height) { break; } if (redPunish.getHeight() == height) { redPunishList.remove(i); } } roundManager.checkIsNeedReset(); 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();  }