use of io.nuls.consensus.poc.storage.po.DepositPo in project nuls by nuls-io.
the class RedPunishTxProcessor method onRollback.
@Override
public Result onRollback(RedPunishTransaction tx, Object secondaryData) {
RedPunishData punishData = tx.getTxData();
List<AgentPo> agentList = agentStorageService.getList();
AgentPo agent = null;
for (AgentPo agentPo : agentList) {
if (agentPo.getDelHeight() <= 0) {
continue;
}
if (Arrays.equals(agentPo.getAgentAddress(), punishData.getAddress())) {
agent = agentPo;
break;
}
}
if (null == agent) {
return Result.getFailed(PocConsensusErrorCode.AGENT_NOT_EXIST);
}
List<DepositPo> depositPoList = depositStorageService.getList();
List<DepositPo> updatedList = new ArrayList<>();
for (DepositPo po : depositPoList) {
po.setDelHeight(-1);
boolean success = this.depositStorageService.save(po);
if (!success) {
for (DepositPo po2 : depositPoList) {
po2.setDelHeight(tx.getBlockHeight());
this.depositStorageService.save(po2);
}
return Result.getFailed(PocConsensusErrorCode.UPDATE_DEPOSIT_FAILED);
}
updatedList.add(po);
}
AgentPo agentPo = agent;
agentPo.setDelHeight(-1L);
boolean success = agentStorageService.save(agentPo);
if (!success) {
for (DepositPo po2 : depositPoList) {
po2.setDelHeight(tx.getBlockHeight());
this.depositStorageService.save(po2);
}
return Result.getFailed(PocConsensusErrorCode.UPDATE_AGENT_FAILED);
}
byte[] key = ArraysTool.concatenate(punishData.getAddress(), new byte[] { PunishType.RED.getCode() }, SerializeUtils.uint64ToByteArray(tx.getBlockHeight()), new byte[] { 0 });
success = storageService.delete(key);
if (!success) {
for (DepositPo po2 : depositPoList) {
po2.setDelHeight(tx.getBlockHeight());
this.depositStorageService.save(po2);
}
agentPo.setDelHeight(tx.getBlockHeight());
agentStorageService.save(agentPo);
throw new NulsRuntimeException(TransactionErrorCode.ROLLBACK_TRANSACTION_FAILED);
}
return Result.getSuccess();
}
use of io.nuls.consensus.poc.storage.po.DepositPo in project nuls by nuls-io.
the class CancelDepositTxValidator method validate.
@Override
public ValidateResult validate(CancelDepositTransaction data) throws NulsException {
DepositPo depositPo = depositStorageService.get(data.getTxData().getJoinTxHash());
if (null == depositPo || depositPo.getDelHeight() > 0) {
return ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.DATA_NOT_FOUND);
}
TransactionSignature sig = new TransactionSignature();
try {
sig.parse(data.getTransactionSignature(), 0);
} catch (NulsException e) {
Log.error(e);
return ValidateResult.getFailedResult(this.getClass().getName(), e.getErrorCode());
}
if (!SignatureUtil.containsAddress(data, depositPo.getAddress())) {
ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
return result;
}
return ValidateResult.getSuccessResult();
}
use of io.nuls.consensus.poc.storage.po.DepositPo in project nuls by nuls-io.
the class DepositTxValidator method validate.
@Override
public ValidateResult validate(DepositTransaction tx) throws NulsException {
if (null == tx || null == tx.getTxData() || null == tx.getTxData().getAgentHash() || null == tx.getTxData().getDeposit() || null == tx.getTxData().getAddress()) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}
Deposit deposit = tx.getTxData();
AgentPo agentPo = agentStorageService.get(deposit.getAgentHash());
if (null == agentPo || agentPo.getDelHeight() > 0) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
}
List<DepositPo> poList = this.getDepositListByAgent(deposit.getAgentHash());
if (null != poList && poList.size() >= PocConsensusProtocolConstant.MAX_ACCEPT_NUM_OF_DEPOSIT) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_OVER_COUNT);
}
Na limit = PocConsensusProtocolConstant.ENTRUSTER_DEPOSIT_LOWER_LIMIT;
Na max = PocConsensusProtocolConstant.SUM_OF_DEPOSIT_OF_AGENT_UPPER_LIMIT;
Na total = Na.ZERO;
for (DepositPo cd : poList) {
total = total.add(cd.getDeposit());
}
if (limit.isGreaterThan(deposit.getDeposit())) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_NOT_ENOUGH);
}
if (max.isLessThan(total.add(deposit.getDeposit()))) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_TOO_MUCH);
}
if (!isDepositOk(deposit.getDeposit(), tx.getCoinData())) {
return ValidateResult.getFailedResult(this.getClass().getName(), SeverityLevelEnum.FLAGRANT_FOUL, PocConsensusErrorCode.DEPOSIT_ERROR);
}
TransactionSignature sig = new TransactionSignature();
try {
sig.parse(tx.getTransactionSignature(), 0);
} catch (NulsException e) {
Log.error(e);
return ValidateResult.getFailedResult(this.getClass().getName(), e.getErrorCode());
}
if (!SignatureUtil.containsAddress(tx, deposit.getAddress())) {
ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
return result;
}
CoinData coinData = tx.getCoinData();
Set<String> addressSet = new HashSet<>();
int lockCount = 0;
for (Coin coin : coinData.getTo()) {
if (coin.getLockTime() == PocConsensusConstant.CONSENSUS_LOCK_TIME) {
lockCount++;
}
// addressSet.add(AddressTool.getStringAddressByBytes(coin.()));
addressSet.add(AddressTool.getStringAddressByBytes(coin.getAddress()));
}
if (lockCount > 1) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}
if (addressSet.size() > 1) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}
return ValidateResult.getSuccessResult();
}
use of io.nuls.consensus.poc.storage.po.DepositPo in project nuls by nuls-io.
the class PoConvertUtil method depositToPo.
public static DepositPo depositToPo(Deposit deposit) {
DepositPo po = new DepositPo();
po.setTxHash(deposit.getTxHash());
po.setAddress(deposit.getAddress());
po.setAgentHash(deposit.getAgentHash());
po.setBlockHeight(deposit.getBlockHeight());
po.setDelHeight(deposit.getDelHeight());
po.setDeposit(deposit.getDeposit());
po.setTime(deposit.getTime());
return po;
}
use of io.nuls.consensus.poc.storage.po.DepositPo in project nuls by nuls-io.
the class DepositStorageServiceImpl method get.
@Override
public DepositPo get(NulsDigestData hash) {
if (hash == null) {
return null;
}
byte[] body = null;
try {
body = dbService.get(ConsensusStorageConstant.DB_NAME_CONSENSUS_DEPOSIT, hash.serialize());
} catch (IOException e) {
Log.error(e);
}
if (body == null) {
return null;
}
DepositPo depositPo = new DepositPo();
try {
depositPo.parse(body, 0);
} catch (NulsException e) {
Log.error(e);
throw new NulsRuntimeException(e);
}
depositPo.setTxHash(hash);
return depositPo;
}
Aggregations