use of io.nuls.consensus.poc.storage.po.AgentPo in project nuls by nuls-io.
the class StopAgentTxProcessor method conflictDetect.
@Override
public ValidateResult conflictDetect(List<Transaction> txList) {
if (txList == null || txList.isEmpty()) {
return ValidateResult.getSuccessResult();
}
Set<NulsDigestData> hashSet = new HashSet<>();
Set<String> addressSet = new HashSet<>();
for (Transaction tx : txList) {
if (tx.getType() == ConsensusConstant.TX_TYPE_RED_PUNISH) {
RedPunishTransaction transaction = (RedPunishTransaction) tx;
addressSet.add(AddressTool.getStringAddressByBytes(transaction.getTxData().getAddress()));
}
}
for (Transaction tx : txList) {
if (tx.getType() == ConsensusConstant.TX_TYPE_STOP_AGENT) {
StopAgentTransaction transaction = (StopAgentTransaction) tx;
if (!hashSet.add(transaction.getTxData().getCreateTxHash())) {
ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TRANSACTION_REPEATED);
result.setData(transaction);
return result;
}
if (transaction.getTxData().getAddress() == null) {
CreateAgentTransaction agentTransaction = (CreateAgentTransaction) ledgerService.getTx(transaction.getTxData().getCreateTxHash());
if (null == agentTransaction) {
ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
result.setData(transaction);
return result;
}
transaction.getTxData().setAddress(agentTransaction.getTxData().getAgentAddress());
}
AgentPo po = agentStorageService.get(transaction.getTxData().getCreateTxHash());
if (null == po || po.getDelHeight() > 0) {
ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_STOPPED);
result.setData(transaction);
return result;
}
if (addressSet.contains(AddressTool.getStringAddressByBytes(transaction.getTxData().getAddress()))) {
ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_STOPPED);
result.setData(transaction);
return result;
}
}
}
return ValidateResult.getSuccessResult();
}
use of io.nuls.consensus.poc.storage.po.AgentPo 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.AgentPo in project nuls by nuls-io.
the class AgentCountValidator method validate.
@Override
public ValidateResult validate(CreateAgentTransaction tx) {
ValidateResult result = ValidateResult.getSuccessResult();
Agent agent = tx.getTxData();
List<AgentPo> caList = agentStorageService.getList();
if (caList != null) {
Set<String> set = new HashSet<>();
for (AgentPo ca : caList) {
if (ca.getHash().equals(tx.getHash())) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TRANSACTION_REPEATED);
}
if (ca.getDelHeight() > 0L) {
continue;
}
set.add(Hex.encode(ca.getAgentAddress()));
set.add(Hex.encode(ca.getPackingAddress()));
}
boolean b = set.add(Hex.encode(agent.getAgentAddress()));
if (!b) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_EXIST);
}
b = set.add(Hex.encode(agent.getPackingAddress()));
if (!b) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_PACKING_EXIST);
}
}
return result;
}
use of io.nuls.consensus.poc.storage.po.AgentPo 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.AgentPo in project nuls by nuls-io.
the class AgentStorageServiceImpl method getList.
@Override
public List<AgentPo> getList() {
List<Entry<byte[], byte[]>> list = dbService.entryList(ConsensusStorageConstant.DB_NAME_CONSENSUS_AGENT);
List<AgentPo> resultList = new ArrayList<>();
if (list == null) {
return resultList;
}
for (Entry<byte[], byte[]> entry : list) {
AgentPo agentPo = new AgentPo();
try {
agentPo.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);
}
agentPo.setHash(hash);
resultList.add(agentPo);
}
return resultList;
}
Aggregations