Search in sources :

Example 11 with PunishLogPo

use of io.nuls.consensus.poc.storage.po.PunishLogPo 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();
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) Chain(io.nuls.consensus.poc.model.Chain) ChainContainer(io.nuls.consensus.poc.container.ChainContainer) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) Block(io.nuls.kernel.model.Block) PunishLogStorageService(io.nuls.consensus.poc.storage.service.PunishLogStorageService) BlockHeader(io.nuls.kernel.model.BlockHeader) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Example 12 with PunishLogPo

use of io.nuls.consensus.poc.storage.po.PunishLogPo in project nuls by nuls-io.

the class RedPunishTxProcessor method onCommit.

@Override
public Result onCommit(RedPunishTransaction tx, Object secondaryData) {
    RedPunishData punishData = tx.getTxData();
    BlockHeader header = (BlockHeader) secondaryData;
    BlockExtendsData roundData = new BlockExtendsData(header.getExtend());
    PunishLogPo punishLogPo = new PunishLogPo();
    punishLogPo.setAddress(punishData.getAddress());
    punishLogPo.setHeight(tx.getBlockHeight());
    punishLogPo.setRoundIndex(roundData.getRoundIndex());
    punishLogPo.setTime(tx.getTime());
    punishLogPo.setType(PunishType.RED.getCode());
    punishLogPo.setEvidence(punishData.getEvidence());
    punishLogPo.setReasonCode(punishData.getReasonCode());
    List<AgentPo> agentList = agentStorageService.getList();
    AgentPo agent = null;
    for (AgentPo agentPo : agentList) {
        if (agentPo.getDelHeight() > 0) {
            continue;
        }
        if (Arrays.equals(agentPo.getAgentAddress(), punishLogPo.getAddress())) {
            agent = agentPo;
            break;
        }
    }
    if (null == agent) {
        Log.error("There is no agent can be punished.");
        return Result.getSuccess();
    }
    List<DepositPo> depositPoList = depositStorageService.getList();
    List<DepositPo> updatedList = new ArrayList<>();
    for (DepositPo po : depositPoList) {
        if (po.getDelHeight() >= 0) {
            continue;
        }
        if (!po.getAgentHash().equals(agent.getHash())) {
            continue;
        }
        po.setDelHeight(tx.getBlockHeight());
        boolean b = depositStorageService.save(po);
        if (!b) {
            for (DepositPo po2 : updatedList) {
                po2.setDelHeight(-1);
                this.depositStorageService.save(po2);
            }
            return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.UPDATE_DEPOSIT_FAILED);
        }
        updatedList.add(po);
    }
    boolean success = storageService.save(punishLogPo);
    if (!success) {
        for (DepositPo po2 : updatedList) {
            po2.setDelHeight(-1);
            this.depositStorageService.save(po2);
        }
        throw new NulsRuntimeException(TransactionErrorCode.ROLLBACK_TRANSACTION_FAILED);
    }
    AgentPo agentPo = agent;
    agentPo.setDelHeight(tx.getBlockHeight());
    success = agentStorageService.save(agentPo);
    if (!success) {
        for (DepositPo po2 : updatedList) {
            po2.setDelHeight(-1);
            this.depositStorageService.save(po2);
        }
        this.storageService.delete(punishLogPo.getKey());
        return Result.getFailed(PocConsensusErrorCode.UPDATE_AGENT_FAILED);
    }
    return Result.getSuccess();
}
Also used : DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) RedPunishData(io.nuls.consensus.poc.protocol.entity.RedPunishData) BlockExtendsData(io.nuls.consensus.poc.model.BlockExtendsData) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) BlockHeader(io.nuls.kernel.model.BlockHeader) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 13 with PunishLogPo

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

Example 14 with PunishLogPo

use of io.nuls.consensus.poc.storage.po.PunishLogPo in project nuls by nuls-io.

the class BaseConsensusProtocolValidator method getRedPunishCount.

protected long getRedPunishCount(byte[] address) {
    List<PunishLogPo> list = PocConsensusContext.getChainManager().getMasterChain().getChain().getRedPunishList();
    if (null == list || list.isEmpty()) {
        return 0;
    }
    long count = 0;
    for (PunishLogPo po : list) {
        if (Arrays.equals(address, po.getAddress())) {
            count++;
        }
    }
    return count;
}
Also used : 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