Search in sources :

Example 11 with DepositPo

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

the class DepositStorageServiceImpl method getList.

@Override
public List<DepositPo> getList() {
    List<Entry<byte[], byte[]>> list = dbService.entryList(ConsensusStorageConstant.DB_NAME_CONSENSUS_DEPOSIT);
    List<DepositPo> resultList = new ArrayList<>();
    if (list == null) {
        return resultList;
    }
    for (Entry<byte[], byte[]> entry : list) {
        DepositPo depositPo = new DepositPo();
        try {
            depositPo.parse(entry.getValue(), 0);
        } catch (NulsException e) {
            Log.error(e);
            throw new NulsRuntimeException(e);
        }
        NulsDigestData hash = new NulsDigestData();
        try {
            hash.parse(entry.getKey(), 0);
        } catch (NulsException e) {
            Log.error(e);
        }
        depositPo.setTxHash(hash);
        resultList.add(depositPo);
    }
    return resultList;
}
Also used : Entry(io.nuls.db.model.Entry) DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) NulsException(io.nuls.kernel.exception.NulsException) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) NulsDigestData(io.nuls.kernel.model.NulsDigestData)

Example 12 with DepositPo

use of io.nuls.consensus.poc.storage.po.DepositPo 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 DepositPo

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

the class DepositTxValidator method getDepositListByAgent.

private List<DepositPo> getDepositListByAgent(NulsDigestData agentHash) {
    List<DepositPo> depositList = depositStorageService.getList();
    long startBlockHeight = NulsContext.getInstance().getBestHeight();
    List<DepositPo> resultList = new ArrayList<>();
    for (DepositPo deposit : depositList) {
        if (deposit.getDelHeight() != -1L && deposit.getDelHeight() <= startBlockHeight) {
            continue;
        }
        if (deposit.getBlockHeight() > startBlockHeight || deposit.getBlockHeight() < 0L) {
            continue;
        }
        if (!deposit.getAgentHash().equals(agentHash)) {
            continue;
        }
        if (deposit.getAgentHash().equals(agentHash)) {
            resultList.add(deposit);
        }
    }
    return resultList;
}
Also used : DepositPo(io.nuls.consensus.poc.storage.po.DepositPo)

Example 14 with DepositPo

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

the class CancelDepositTxProcessor method onRollback.

@Override
public Result onRollback(CancelDepositTransaction tx, Object secondaryData) {
    DepositTransaction transaction = (DepositTransaction) ledgerService.getTx(tx.getTxData().getJoinTxHash());
    if (null == transaction) {
        return Result.getFailed(TransactionErrorCode.TX_NOT_EXIST);
    }
    DepositPo po = depositStorageService.get(tx.getTxData().getJoinTxHash());
    if (null == po) {
        return Result.getFailed(KernelErrorCode.DATA_NOT_FOUND);
    }
    if (po.getDelHeight() != tx.getBlockHeight()) {
        return Result.getFailed(PocConsensusErrorCode.DEPOSIT_NEVER_CANCELED);
    }
    po.setDelHeight(-1L);
    boolean b = depositStorageService.save(po);
    if (b) {
        return Result.getSuccess();
    }
    return Result.getFailed(TransactionErrorCode.ROLLBACK_TRANSACTION_FAILED);
}
Also used : DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction) DepositPo(io.nuls.consensus.poc.storage.po.DepositPo)

Example 15 with DepositPo

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

the class CancelDepositTxProcessor method onCommit.

@Override
public Result onCommit(CancelDepositTransaction tx, Object secondaryData) {
    DepositTransaction transaction = (DepositTransaction) ledgerService.getTx(tx.getTxData().getJoinTxHash());
    if (null == transaction) {
        return Result.getFailed(TransactionErrorCode.TX_NOT_EXIST);
    }
    DepositPo po = depositStorageService.get(tx.getTxData().getJoinTxHash());
    if (null == po) {
        return Result.getFailed(KernelErrorCode.DATA_NOT_FOUND);
    }
    tx.getTxData().setAddress(po.getAddress());
    if (po.getDelHeight() > 0L) {
        return Result.getFailed(PocConsensusErrorCode.DEPOSIT_WAS_CANCELED);
    }
    po.setDelHeight(tx.getBlockHeight());
    boolean b = depositStorageService.save(po);
    if (b) {
        return Result.getSuccess();
    }
    return Result.getFailed(TransactionErrorCode.SAVE_TX_ERROR);
}
Also used : DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction) DepositPo(io.nuls.consensus.poc.storage.po.DepositPo)

Aggregations

DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)17 AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)7 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)6 NulsException (io.nuls.kernel.exception.NulsException)5 ValidateResult (io.nuls.kernel.validate.ValidateResult)4 ArrayList (java.util.ArrayList)4 Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)3 CancelDepositTransaction (io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction)3 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)3 NulsDigestData (io.nuls.kernel.model.NulsDigestData)3 TransactionSignature (io.nuls.kernel.script.TransactionSignature)3 RedPunishData (io.nuls.consensus.poc.protocol.entity.RedPunishData)2 BlockHeader (io.nuls.kernel.model.BlockHeader)2 Coin (io.nuls.kernel.model.Coin)2 Na (io.nuls.kernel.model.Na)2 BlockExtendsData (io.nuls.consensus.poc.model.BlockExtendsData)1 RedPunishTransaction (io.nuls.consensus.poc.protocol.tx.RedPunishTransaction)1 StopAgentTransaction (io.nuls.consensus.poc.protocol.tx.StopAgentTransaction)1 DepositComparator (io.nuls.consensus.poc.protocol.util.DepositComparator)1 PunishLogPo (io.nuls.consensus.poc.storage.po.PunishLogPo)1