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