use of io.nuls.consensus.poc.storage.po.PunishLogPo in project nuls by nuls-io.
the class CacheLoader method loadYellowPunishList.
public List<PunishLogPo> loadYellowPunishList(List<PunishLogPo> allPunishList, int roundSize) {
List<PunishLogPo> list = new ArrayList<>();
BlockHeader blockHeader = blockService.getBestBlockHeader().getData();
if (null == blockHeader) {
return list;
}
BlockExtendsData roundData = new BlockExtendsData(blockHeader.getExtend());
long breakRoundIndex = roundData.getRoundIndex() - roundSize;
for (PunishLogPo po : allPunishList) {
if (po.getType() == PunishType.RED.getCode()) {
continue;
}
if (po.getRoundIndex() <= breakRoundIndex) {
continue;
}
list.add(po);
}
Collections.sort(list, new PunishLogComparator());
return list;
}
use of io.nuls.consensus.poc.storage.po.PunishLogPo in project nuls by nuls-io.
the class ChainContainer method addBlock.
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) {
// Registered agent transaction
// 注册代理交易
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;
}
use of io.nuls.consensus.poc.storage.po.PunishLogPo in project nuls by nuls-io.
the class ChainContainer method rollback.
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();
// update txs
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;
}
use of io.nuls.consensus.poc.storage.po.PunishLogPo in project nuls by nuls-io.
the class RoundManager method getPunishCountMap.
private Map<String, Integer> getPunishCountMap(long roundStart, long roundEnd, int code) {
List<PunishLogPo> punishList = new ArrayList<>(chain.getYellowPunishList());
if (code == PunishType.RED.getCode()) {
punishList = chain.getRedPunishList();
}
Map<String, Integer> map = new HashMap<>();
for (int i = punishList.size() - 1; i >= 0; i--) {
PunishLogPo punish = punishList.get(i);
if (punish.getRoundIndex() > roundEnd) {
continue;
}
if (punish.getRoundIndex() < roundStart) {
break;
}
Integer count = map.get(punish.getAddressStr());
if (null == count) {
count = 0;
}
map.put(punish.getAddressStr(), count + 1);
}
return map;
}
use of io.nuls.consensus.poc.storage.po.PunishLogPo in project nuls by nuls-io.
the class YellowPunishTxProcessor method onCommit.
@Override
public Result onCommit(YellowPunishTransaction tx, Object secondaryData) {
YellowPunishData punishData = tx.getTxData();
BlockHeader header = (BlockHeader) secondaryData;
BlockExtendsData roundData = new BlockExtendsData(header.getExtend());
List<PunishLogPo> savedList = new ArrayList<>();
int index = 1;
for (byte[] address : punishData.getAddressList()) {
PunishLogPo po = new PunishLogPo();
po.setAddress(address);
po.setHeight(tx.getBlockHeight());
po.setRoundIndex(roundData.getRoundIndex());
po.setTime(tx.getTime());
po.setIndex(index++);
po.setType(PunishType.YELLOW.getCode());
boolean result = punishLogStorageService.save(po);
if (!result) {
for (PunishLogPo punishLogPo : savedList) {
punishLogStorageService.delete(getPoKey(punishLogPo.getAddress(), PunishType.YELLOW.getCode(), punishLogPo.getHeight(), punishLogPo.getIndex()));
}
throw new NulsRuntimeException(TransactionErrorCode.ROLLBACK_TRANSACTION_FAILED);
} else {
savedList.add(po);
}
}
return Result.getSuccess();
}
Aggregations