Skip to content

Commit

Permalink
contract TX validation
Browse files Browse the repository at this point in the history
  • Loading branch information
MIMIEYES committed Apr 4, 2019
1 parent 8921d2f commit 573490c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.nuls.contract.entity.txdata.CallContractData;
import io.nuls.contract.ledger.util.ContractLedgerUtil;
import io.nuls.core.tools.array.ArraysTool;
import io.nuls.core.tools.calc.LongUtils;
import io.nuls.core.tools.log.Log;
import io.nuls.kernel.constant.TransactionErrorCode;
import io.nuls.kernel.exception.NulsException;
Expand All @@ -38,6 +39,7 @@
import io.nuls.kernel.model.Na;
import io.nuls.kernel.script.SignatureUtil;
import io.nuls.kernel.utils.AddressTool;
import io.nuls.kernel.utils.TransactionFeeCalculator;
import io.nuls.kernel.validate.NulsDataValidator;
import io.nuls.kernel.validate.ValidateResult;
import io.nuls.protocol.constant.ProtocolConstant;
Expand All @@ -60,12 +62,12 @@ public ValidateResult validate(CallContractTransaction tx) throws NulsException
Set<String> addressSet = SignatureUtil.getAddressFromTX(tx);

if(!ContractLedgerUtil.isExistContractAddress(contractAddress)) {
Log.error("contract data error: The contract does not exist.");
Log.error("contract call error: The contract does not exist.");
return ValidateResult.getFailedResult(this.getClass().getSimpleName(), ContractErrorCode.CONTRACT_ADDRESS_NOT_EXIST);
}

if (!addressSet.contains(AddressTool.getStringAddressByBytes(sender))) {
Log.error("contract data error: The contract caller is not the transaction creator.");
Log.error("contract call error: The contract caller is not the transaction creator.");
return ValidateResult.getFailedResult(this.getClass().getSimpleName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}

Expand All @@ -82,26 +84,34 @@ public ValidateResult validate(CallContractTransaction tx) throws NulsException
}

if (coin.getLockTime() != 0) {
Log.error("contract data error: The amount of the transfer cannot be locked(UTXO status error).");
Log.error("contract call error: The amount of the transfer cannot be locked(UTXO status error).");
return ValidateResult.getFailedResult(this.getClass().getSimpleName(), TransactionErrorCode.UTXO_STATUS_CHANGE);
}

if (!ArraysTool.arrayEquals(owner, contractAddress)) {
Log.error("contract data error: The receiver is not the contract address.");
Log.error("contract call error: The receiver is not the contract address.");
return ValidateResult.getFailedResult(this.getClass().getSimpleName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
} else {
contractReceivedNa = contractReceivedNa.add(coin.getNa());
}

if (coin.getNa().isLessThan(ProtocolConstant.MININUM_TRANSFER_AMOUNT)) {
Log.error("contract data error: The amount of the transfer is too small.");
Log.error("contract call error: The amount of the transfer is too small.");
return ValidateResult.getFailedResult(this.getClass().getSimpleName(), TransactionErrorCode.TOO_SMALL_AMOUNT);
}
}
if (contractReceivedNa.isLessThan(transferNa)) {
Log.error("contract data error: Insufficient amount to transfer to the contract address.");
Log.error("contract call error: Insufficient amount to transfer to the contract address.");
return ValidateResult.getFailedResult(this.getClass().getSimpleName(), TransactionErrorCode.INVALID_AMOUNT);
}
return ValidateResult.getSuccessResult();

Na realFee = tx.getCoinData().getFee();
Na fee = TransactionFeeCalculator.getTransferFee(tx.size()).add(Na.valueOf(LongUtils.mul(txData.getGasLimit(), txData.getPrice())));
if (realFee.isGreaterOrEquals(fee)) {
return ValidateResult.getSuccessResult();
} else {
Log.error("contract call error: The contract transaction fee is not right.");
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.FEE_NOT_RIGHT);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
import io.nuls.contract.entity.tx.CreateContractTransaction;
import io.nuls.contract.entity.txdata.CreateContractData;
import io.nuls.contract.util.ContractUtil;
import io.nuls.core.tools.calc.LongUtils;
import io.nuls.core.tools.log.Log;
import io.nuls.kernel.constant.TransactionErrorCode;
import io.nuls.kernel.exception.NulsException;
import io.nuls.kernel.lite.annotation.Component;
import io.nuls.kernel.model.Na;
import io.nuls.kernel.script.SignatureUtil;
import io.nuls.kernel.utils.AddressTool;
import io.nuls.kernel.utils.TransactionFeeCalculator;
import io.nuls.kernel.validate.NulsDataValidator;
import io.nuls.kernel.validate.ValidateResult;

Expand All @@ -53,16 +56,23 @@ public ValidateResult validate(CreateContractTransaction tx) throws NulsExceptio
byte[] sender = txData.getSender();
byte[] contractAddress = txData.getContractAddress();
if(!ContractUtil.isLegalContractAddress(contractAddress)) {
Log.error("contract data error: Illegal contract address.");
Log.error("contract create error: Illegal contract address.");
return ValidateResult.getFailedResult(this.getClass().getSimpleName(), ContractErrorCode.ILLEGAL_CONTRACT_ADDRESS);
}
Set<String> addressSet = SignatureUtil.getAddressFromTX(tx);

if (!addressSet.contains(AddressTool.getStringAddressByBytes(sender))) {
Log.error("contract data error: The contract creater is not the transaction creator.");
Log.error("contract create error: The contract creater is not the transaction creator.");
return ValidateResult.getFailedResult(this.getClass().getSimpleName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}

return ValidateResult.getSuccessResult();
Na realFee = tx.getCoinData().getFee();
Na fee = TransactionFeeCalculator.getTransferFee(tx.size()).add(Na.valueOf(LongUtils.mul(txData.getGasLimit(), txData.getPrice())));
if (realFee.isGreaterOrEquals(fee)) {
return ValidateResult.getSuccessResult();
} else {
Log.error("contract create error: The contract transaction fee is not right.");
return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.FEE_NOT_RIGHT);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public ValidateResult validate(DeleteContractTransaction tx) throws NulsExceptio
Set<String> addressSet = SignatureUtil.getAddressFromTX(tx);

if (!addressSet.contains(AddressTool.getStringAddressByBytes(sender))) {
Log.error("contract data error: The contract deleter is not the transaction creator.");
Log.error("contract delete error: The contract deleter is not the transaction creator.");
return ValidateResult.getFailedResult(this.getClass().getSimpleName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}

Expand All @@ -79,24 +79,24 @@ public ValidateResult validate(DeleteContractTransaction tx) throws NulsExceptio
}
ContractAddressInfoPo contractAddressInfoPo = contractAddressInfoPoResult.getData();
if(contractAddressInfoPo == null) {
Log.error("contract data error: The contract does not exist.");
Log.error("contract delete error: The contract does not exist.");
return ValidateResult.getFailedResult(this.getClass().getSimpleName(), ContractErrorCode.CONTRACT_ADDRESS_NOT_EXIST);
}
if(!ArraysTool.arrayEquals(sender, contractAddressInfoPo.getSender())) {
Log.error("contract data error: The contract deleter is not the contract creator.");
Log.error("contract delete error: The contract deleter is not the contract creator.");
return ValidateResult.getFailedResult(this.getClass().getSimpleName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}

Result<ContractBalance> result = contractUtxoService.getBalance(contractAddressBytes);
ContractBalance balance = (ContractBalance) result.getData();
if(balance == null) {
Log.error("contract data error: That balance of the contract is abnormal.");
Log.error("contract delete error: That balance of the contract is abnormal.");
return ValidateResult.getFailedResult(this.getClass().getSimpleName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
}

Na totalBalance = balance.getBalance();
if(totalBalance.compareTo(Na.ZERO) != 0) {
Log.error("contract data error: The balance of the contract is not 0.");
Log.error("contract delete error: The balance of the contract is not 0.");
return ValidateResult.getFailedResult(this.getClass().getSimpleName(), ContractErrorCode.CONTRACT_DELETE_BALANCE);
}

Expand Down

0 comments on commit 573490c

Please sign in to comment.