Search in sources :

Example 1 with PunishLogPo

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;
}
Also used : BlockExtendsData(io.nuls.consensus.poc.model.BlockExtendsData) PunishLogComparator(io.nuls.consensus.poc.storage.utils.PunishLogComparator) ArrayList(java.util.ArrayList) BlockHeader(io.nuls.kernel.model.BlockHeader) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Example 2 with PunishLogPo

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;
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) CoinBaseTransaction(io.nuls.protocol.model.tx.CoinBaseTransaction) RedPunishData(io.nuls.consensus.poc.protocol.entity.RedPunishData) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Example 3 with PunishLogPo

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;
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) SmallBlock(io.nuls.protocol.model.SmallBlock) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Example 4 with PunishLogPo

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;
}
Also used : PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Example 5 with PunishLogPo

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();
}
Also used : BlockExtendsData(io.nuls.consensus.poc.model.BlockExtendsData) YellowPunishData(io.nuls.consensus.poc.protocol.entity.YellowPunishData) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) BlockHeader(io.nuls.kernel.model.BlockHeader) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Aggregations

PunishLogPo (io.nuls.consensus.poc.storage.po.PunishLogPo)14 ArrayList (java.util.ArrayList)6 BlockExtendsData (io.nuls.consensus.poc.model.BlockExtendsData)5 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)5 BlockHeader (io.nuls.kernel.model.BlockHeader)5 Agent (io.nuls.consensus.poc.protocol.entity.Agent)4 Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)4 Chain (io.nuls.consensus.poc.model.Chain)2 RedPunishData (io.nuls.consensus.poc.protocol.entity.RedPunishData)2 YellowPunishData (io.nuls.consensus.poc.protocol.entity.YellowPunishData)2 PunishLogComparator (io.nuls.consensus.poc.storage.utils.PunishLogComparator)2 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)1 ChainContainer (io.nuls.consensus.poc.container.ChainContainer)1 AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)1 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)1 PunishLogStorageService (io.nuls.consensus.poc.storage.service.PunishLogStorageService)1 Entry (io.nuls.db.model.Entry)1 NulsException (io.nuls.kernel.exception.NulsException)1 Block (io.nuls.kernel.model.Block)1 SmallBlock (io.nuls.protocol.model.SmallBlock)1