use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class CreateAgentTxValidator method validate.
@Override
public ValidateResult validate(CreateAgentTransaction tx) {
Agent agent = tx.getTxData();
if (null == agent) {
return ValidateResult.getFailedResult(getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
}
if (!AddressTool.validNormalAddress(agent.getPackingAddress())) {
return ValidateResult.getFailedResult(getClass().getName(), AccountErrorCode.ADDRESS_ERROR);
}
if (Arrays.equals(agent.getAgentAddress(), agent.getPackingAddress())) {
return ValidateResult.getFailedResult(getClass().getName(), PocConsensusErrorCode.AGENTADDR_AND_PACKING_SAME);
}
if (Arrays.equals(agent.getRewardAddress(), agent.getPackingAddress())) {
return ValidateResult.getFailedResult(getClass().getName(), PocConsensusErrorCode.REWARDADDR_PACKING_SAME);
}
if (tx.getTime() <= 0) {
return ValidateResult.getFailedResult(getClass().getName(), KernelErrorCode.DATA_ERROR);
}
double commissionRate = agent.getCommissionRate();
if (commissionRate < PocConsensusProtocolConstant.MIN_COMMISSION_RATE || commissionRate > PocConsensusProtocolConstant.MAX_COMMISSION_RATE) {
return ValidateResult.getFailedResult(this.getClass().getSimpleName(), PocConsensusErrorCode.COMMISSION_RATE_OUT_OF_RANGE);
}
if (PocConsensusProtocolConstant.AGENT_DEPOSIT_LOWER_LIMIT.isGreaterThan(agent.getDeposit())) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_NOT_ENOUGH);
}
if (PocConsensusProtocolConstant.AGENT_DEPOSIT_UPPER_LIMIT.isLessThan(agent.getDeposit())) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_TOO_MUCH);
}
if (!isDepositOk(agent.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());
}
try {
if (!SignatureUtil.containsAddress(tx, agent.getAgentAddress())) {
ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
return result;
}
} catch (NulsException e) {
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.kernel.exception.NulsException in project nuls by nuls-io.
the class StopAgentTxValidator method validate.
@Override
public ValidateResult validate(StopAgentTransaction data) throws NulsException {
AgentPo agentPo = agentStorageService.get(data.getTxData().getCreateTxHash());
if (null == agentPo || agentPo.getDelHeight() > 0) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
}
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, agentPo.getAgentAddress())) {
ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
return result;
}
if (data.getCoinData().getTo() == null || data.getCoinData().getTo().isEmpty()) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}
List<DepositPo> allDepositList = depositStorageService.getList();
Map<NulsDigestData, DepositPo> depositMap = new HashMap<>();
Na totalNa = agentPo.getDeposit();
DepositPo ownDeposit = new DepositPo();
ownDeposit.setDeposit(agentPo.getDeposit());
ownDeposit.setAddress(agentPo.getAgentAddress());
depositMap.put(data.getTxData().getCreateTxHash(), ownDeposit);
for (DepositPo deposit : allDepositList) {
if (deposit.getDelHeight() > -1L && (data.getBlockHeight() == -1L || deposit.getDelHeight() < data.getBlockHeight())) {
continue;
}
if (!deposit.getAgentHash().equals(agentPo.getHash())) {
continue;
}
depositMap.put(deposit.getTxHash(), deposit);
totalNa = totalNa.add(deposit.getDeposit());
}
Na fromTotal = Na.ZERO;
Map<String, Na> verifyToMap = new HashMap<>();
for (Coin coin : data.getCoinData().getFrom()) {
if (coin.getLockTime() != -1L) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}
NulsDigestData txHash = new NulsDigestData();
txHash.parse(coin.getOwner(), 0);
DepositPo deposit = depositMap.remove(txHash);
if (deposit == null) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}
if (deposit.getAgentHash() == null && !coin.getNa().equals(agentPo.getDeposit())) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
} else if (!deposit.getDeposit().equals(coin.getNa())) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}
fromTotal = fromTotal.add(coin.getNa());
if (deposit.getAgentHash() == null) {
continue;
}
String address = AddressTool.getStringAddressByBytes(deposit.getAddress());
Na na = verifyToMap.get(address);
if (null == na) {
na = deposit.getDeposit();
} else {
na = na.add(deposit.getDeposit());
}
verifyToMap.put(address, na);
}
if (!depositMap.isEmpty()) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}
if (!totalNa.equals(fromTotal)) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}
Na ownToCoin = ownDeposit.getDeposit().subtract(data.getFee());
long ownLockTime = 0L;
for (Coin coin : data.getCoinData().getTo()) {
// String address = AddressTool.getStringAddressByBytes(coin.());
String address = AddressTool.getStringAddressByBytes(coin.getAddress());
Na na = verifyToMap.get(address);
if (null != na && na.equals(coin.getNa())) {
verifyToMap.remove(address);
continue;
}
if (ownToCoin != null && Arrays.equals(coin.getAddress(), ownDeposit.getAddress()) && coin.getNa().equals(ownToCoin)) {
ownToCoin = null;
ownLockTime = coin.getLockTime();
continue;
} else {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}
}
if (ownLockTime < (data.getTime() + PocConsensusConstant.STOP_AGENT_LOCK_TIME)) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.LOCK_TIME_NOT_REACHED);
} else if (data.getBlockHeight() <= 0 && ownLockTime < (TimeService.currentTimeMillis() + PocConsensusConstant.STOP_AGENT_LOCK_TIME - 300000L)) {
return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.LOCK_TIME_NOT_REACHED);
}
if (!verifyToMap.isEmpty()) {
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}
return ValidateResult.getSuccessResult();
}
use of io.nuls.kernel.exception.NulsException 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.kernel.exception.NulsException 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.kernel.exception.NulsException in project nuls by nuls-io.
the class NulsSignData method sign.
public NulsSignData sign(NulsDigestData nulsDigestData, short signAlgType, BigInteger privkey) {
if (signAlgType == NulsSignData.SIGN_ALG_ECC) {
ECKey ecKey = ECKey.fromPrivate(privkey);
byte[] signBytes = ecKey.sign(nulsDigestData.getDigestBytes(), privkey);
NulsSignData signData = new NulsSignData();
try {
signData.parse(signBytes, 0);
} catch (NulsException e) {
Log.error(e);
}
return signData;
}
return null;
}
Aggregations