Search in sources :

Example 11 with AgentPo

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

the class AgentStorageServiceTest method init.

@Before
public void init() {
    agentStorageService = SpringLiteContext.getBean(AgentStorageService.class);
    List<AgentPo> list = agentStorageService.getList();
    if (list != null) {
        for (AgentPo agentPo : list) {
            agentStorageService.delete(agentPo.getHash());
        }
    }
}
Also used : AgentPo(io.nuls.consensus.poc.storage.po.AgentPo) Before(org.junit.Before)

Example 12 with AgentPo

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

the class PoConvertUtil method agentToPo.

public static AgentPo agentToPo(Agent agent) {
    if (agent == null) {
        return null;
    }
    AgentPo agentPo = new AgentPo();
    agentPo.setAgentAddress(agent.getAgentAddress());
    agentPo.setBlockHeight(agent.getBlockHeight());
    agentPo.setCommissionRate(agent.getCommissionRate());
    agentPo.setDeposit(agent.getDeposit());
    agentPo.setPackingAddress(agent.getPackingAddress());
    agentPo.setRewardAddress(agent.getRewardAddress());
    agentPo.setHash(agent.getTxHash());
    agentPo.setTime(agent.getTime());
    return agentPo;
}
Also used : AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 13 with AgentPo

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

the class AgentStorageServiceImpl method get.

@Override
public AgentPo get(NulsDigestData hash) {
    if (hash == null) {
        return null;
    }
    byte[] body = null;
    try {
        body = dbService.get(ConsensusStorageConstant.DB_NAME_CONSENSUS_AGENT, hash.serialize());
    } catch (IOException e) {
        Log.error(e);
    }
    if (body == null) {
        return null;
    }
    AgentPo agentPo = new AgentPo();
    try {
        agentPo.parse(body, 0);
    } catch (NulsException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    agentPo.setHash(hash);
    return agentPo;
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 14 with AgentPo

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

the class CreateAgentTxProcessor method onCommit.

@Override
public Result onCommit(CreateAgentTransaction tx, Object secondaryData) {
    Agent agent = tx.getTxData();
    BlockHeader header = (BlockHeader) secondaryData;
    agent.setTxHash(tx.getHash());
    agent.setBlockHeight(header.getHeight());
    agent.setTime(tx.getTime());
    AgentPo agentPo = PoConvertUtil.agentToPo(agent);
    boolean success = agentStorageService.save(agentPo);
    return new Result(success, null);
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) BlockHeader(io.nuls.kernel.model.BlockHeader) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo) ValidateResult(io.nuls.kernel.validate.ValidateResult) Result(io.nuls.kernel.model.Result)

Example 15 with AgentPo

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

Aggregations

AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)21 NulsDigestData (io.nuls.kernel.model.NulsDigestData)8 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)7 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)6 ValidateResult (io.nuls.kernel.validate.ValidateResult)6 BaseTest (io.nuls.consensus.poc.storage.BaseTest)4 NulsException (io.nuls.kernel.exception.NulsException)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 Agent (io.nuls.consensus.poc.protocol.entity.Agent)3 RedPunishData (io.nuls.consensus.poc.protocol.entity.RedPunishData)3 RedPunishTransaction (io.nuls.consensus.poc.protocol.tx.RedPunishTransaction)3 StopAgentTransaction (io.nuls.consensus.poc.protocol.tx.StopAgentTransaction)3 BlockHeader (io.nuls.kernel.model.BlockHeader)3 HashSet (java.util.HashSet)3 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)2 Coin (io.nuls.kernel.model.Coin)2 Na (io.nuls.kernel.model.Na)2 Transaction (io.nuls.kernel.model.Transaction)2 TransactionSignature (io.nuls.kernel.script.TransactionSignature)2