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());
}
}
}
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;
}
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;
}
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);
}
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();
}
Aggregations