Search in sources :

Example 1 with TransferContract

use of org.tron.protos.Contract.TransferContract in project java-tron by tronprotocol.

the class TransferActuator method validate.

@Override
public boolean validate() throws ContractValidateException {
    try {
        if (!contract.is(TransferContract.class)) {
            throw new ContractValidateException("contract type error,expected type [TransferContract],real type[" + contract.getClass() + "]");
        }
        TransferContract transferContract = this.contract.unpack(TransferContract.class);
        Preconditions.checkNotNull(transferContract.getOwnerAddress(), "OwnerAddress is null");
        Preconditions.checkNotNull(transferContract.getToAddress(), "ToAddress is null");
        Preconditions.checkNotNull(transferContract.getAmount(), "Amount is null");
        if (transferContract.getOwnerAddress().equals(transferContract.getToAddress())) {
            throw new ContractValidateException("Cannot transfer trx to yourself.");
        }
        if (!dbManager.getAccountStore().has(transferContract.getOwnerAddress().toByteArray())) {
            throw new ContractValidateException("Validate TransferContract error, no OwnerAccount.");
        }
        AccountCapsule ownerAccount = dbManager.getAccountStore().get(transferContract.getOwnerAddress().toByteArray());
        long balance = ownerAccount.getBalance();
        long laststOperationTime = ownerAccount.getLatestOperationTime();
        long now = System.currentTimeMillis();
        // TODO:
        // if (now - laststOperationTime < balance) {
        // throw new ContractValidateException();
        // }
        // if account with to_address is not existed,  create it.
        ByteString toAddress = transferContract.getToAddress();
        if (!dbManager.getAccountStore().has(toAddress.toByteArray())) {
            AccountCapsule account = new AccountCapsule(toAddress, AccountType.Normal);
            dbManager.getAccountStore().put(toAddress.toByteArray(), account);
        }
        if (ownerAccount.getBalance() < calcFee()) {
            throw new ContractValidateException("Validate TransferContract error, insufficient fee.");
        }
        long amount = transferContract.getAmount();
        if (amount < 0) {
            throw new ContractValidateException("Amount is less than 0.");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new ContractValidateException(ex.getMessage());
    }
    return true;
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) ByteString(com.google.protobuf.ByteString) ContractValidateException(org.tron.core.exception.ContractValidateException) TransferContract(org.tron.protos.Contract.TransferContract) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ContractValidateException(org.tron.core.exception.ContractValidateException) BalanceInsufficientException(org.tron.core.exception.BalanceInsufficientException) ContractExeException(org.tron.core.exception.ContractExeException)

Example 2 with TransferContract

use of org.tron.protos.Contract.TransferContract in project java-tron by tronprotocol.

the class TransferActuator method execute.

@Override
public boolean execute(TransactionResultCapsule ret) throws ContractExeException {
    long fee = calcFee();
    try {
        TransferContract transferContract = null;
        transferContract = contract.unpack(TransferContract.class);
        dbManager.adjustBalance(transferContract.getOwnerAddress().toByteArray(), -calcFee());
        ret.setStatus(fee, code.SUCESS);
        dbManager.adjustBalance(transferContract.getOwnerAddress().toByteArray(), -transferContract.getAmount());
        dbManager.adjustBalance(transferContract.getToAddress().toByteArray(), transferContract.getAmount());
    } catch (InvalidProtocolBufferException e) {
        logger.debug(e.getMessage(), e);
        ret.setStatus(fee, code.FAILED);
        throw new ContractExeException(e.getMessage());
    } catch (BalanceInsufficientException e) {
        logger.debug(e.getMessage(), e);
        ret.setStatus(fee, code.FAILED);
        throw new ContractExeException(e.getMessage());
    }
    return true;
}
Also used : BalanceInsufficientException(org.tron.core.exception.BalanceInsufficientException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) TransferContract(org.tron.protos.Contract.TransferContract) ContractExeException(org.tron.core.exception.ContractExeException)

Aggregations

InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 BalanceInsufficientException (org.tron.core.exception.BalanceInsufficientException)2 ContractExeException (org.tron.core.exception.ContractExeException)2 TransferContract (org.tron.protos.Contract.TransferContract)2 ByteString (com.google.protobuf.ByteString)1 AccountCapsule (org.tron.core.capsule.AccountCapsule)1 ContractValidateException (org.tron.core.exception.ContractValidateException)1