Search in sources :

Example 21 with ContractValidateException

use of org.tron.core.exception.ContractValidateException in project java-tron by tronprotocol.

the class TransferActuatorTest method rightAssetIssue.

@Test
public void rightAssetIssue() {
    TransferActuator actuator = new TransferActuator(getContract(AMOUNT), dbManager);
    TransactionResultCapsule ret = new TransactionResultCapsule();
    try {
        actuator.validate();
        actuator.execute(ret);
        Assert.assertEquals(ret.getInstance().getRet(), code.SUCESS);
        AccountCapsule owner = dbManager.getAccountStore().get(ByteArray.fromHexString(OWNER_ADDRESS));
        AccountCapsule toAccount = dbManager.getAccountStore().get(ByteArray.fromHexString(TO_ADDRESS));
        Assert.assertEquals(owner.getBalance(), 10000 - AMOUNT - ChainConstant.TRANSFER_FEE);
        Assert.assertEquals(toAccount.getBalance(), 100 + AMOUNT);
    } catch (ContractValidateException e) {
        Assert.assertFalse(e instanceof ContractValidateException);
    } catch (ContractExeException e) {
        Assert.assertFalse(e instanceof ContractExeException);
    }
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) TransactionResultCapsule(org.tron.core.capsule.TransactionResultCapsule) ContractValidateException(org.tron.core.exception.ContractValidateException) ContractExeException(org.tron.core.exception.ContractExeException) Test(org.junit.Test)

Example 22 with ContractValidateException

use of org.tron.core.exception.ContractValidateException in project java-tron by tronprotocol.

the class TransferAssetActuatorTest method rightTransfer.

/**
 * Unit test.
 */
@Test
public void rightTransfer() {
    TransferAssetActuator actuator = new TransferAssetActuator(getContract(100L), dbManager);
    TransactionResultCapsule ret = new TransactionResultCapsule();
    try {
        actuator.validate();
        actuator.execute(ret);
        Assert.assertEquals(ret.getInstance().getRet(), code.SUCESS);
        AccountCapsule owner = dbManager.getAccountStore().get(ByteArray.fromHexString(OWNER_ADDRESS));
        AccountCapsule toAccount = dbManager.getAccountStore().get(ByteArray.fromHexString(TO_ADDRESS));
        Assert.assertEquals(owner.getInstance().getAssetMap().get(ASSET_NAME).longValue(), 9900L);
        Assert.assertEquals(toAccount.getInstance().getAssetMap().get(ASSET_NAME).longValue(), 100L);
    } catch (ContractValidateException e) {
        Assert.assertFalse(e instanceof ContractValidateException);
    } catch (ContractExeException e) {
        Assert.assertFalse(e instanceof ContractExeException);
    }
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) TransactionResultCapsule(org.tron.core.capsule.TransactionResultCapsule) ContractValidateException(org.tron.core.exception.ContractValidateException) ContractExeException(org.tron.core.exception.ContractExeException) Test(org.junit.Test)

Example 23 with ContractValidateException

use of org.tron.core.exception.ContractValidateException in project java-tron by tronprotocol.

the class TransferAssetActuatorTest method wrongTransfer.

/**
 * Unit test.
 */
@Test
public void wrongTransfer() {
    TransferAssetActuator actuator = new TransferAssetActuator(getContract(10001L), dbManager);
    TransactionResultCapsule ret = new TransactionResultCapsule();
    try {
        actuator.validate();
        actuator.execute(ret);
        Assert.assertTrue(false);
    } catch (ContractValidateException e) {
        Assert.assertTrue(e instanceof ContractValidateException);
        Assert.assertEquals(ret.getInstance().getRet(), code.SUCESS);
        AccountCapsule owner = dbManager.getAccountStore().get(ByteArray.fromHexString(OWNER_ADDRESS));
        AccountCapsule toAccount = dbManager.getAccountStore().get(ByteArray.fromHexString(TO_ADDRESS));
        Assert.assertEquals(owner.getAssetMap().get(ASSET_NAME).longValue(), 10000L);
        Assert.assertTrue(isNullOrZero(toAccount.getAssetMap().get(ASSET_NAME)));
    } catch (ContractExeException e) {
        Assert.assertFalse(e instanceof ContractExeException);
    }
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) TransactionResultCapsule(org.tron.core.capsule.TransactionResultCapsule) ContractValidateException(org.tron.core.exception.ContractValidateException) ContractExeException(org.tron.core.exception.ContractExeException) Test(org.junit.Test)

Example 24 with ContractValidateException

use of org.tron.core.exception.ContractValidateException in project java-tron by tronprotocol.

the class VoteWitnessActuator method validate.

@Override
public boolean validate() throws ContractValidateException {
    try {
        if (!contract.is(VoteWitnessContract.class)) {
            throw new ContractValidateException("contract type error,expected type [VoteWitnessContract],real type[" + contract.getClass() + "]");
        }
        VoteWitnessContract contract = this.contract.unpack(VoteWitnessContract.class);
        ByteString ownerAddress = contract.getOwnerAddress();
        Preconditions.checkNotNull(ownerAddress, "OwnerAddress is null");
        AccountStore accountStore = dbManager.getAccountStore();
        byte[] ownerAddressBytes = ownerAddress.toByteArray();
        Iterator<Vote> iterator = contract.getVotesList().iterator();
        while (iterator.hasNext()) {
            Vote vote = iterator.next();
            byte[] bytes = vote.getVoteAddress().toByteArray();
            if (!dbManager.getAccountStore().has(bytes)) {
                throw new ContractValidateException("Account[" + contract.getOwnerAddress() + "] not exists");
            }
            if (!dbManager.getWitnessStore().has(bytes)) {
                throw new ContractValidateException("Witness[" + contract.getOwnerAddress() + "] not exists");
            }
        }
        if (!dbManager.getAccountStore().has(contract.getOwnerAddress().toByteArray())) {
            throw new ContractValidateException("Account[" + contract.getOwnerAddress() + "] not exists");
        }
        long share = dbManager.getAccountStore().get(contract.getOwnerAddress().toByteArray()).getShare();
        long sum = contract.getVotesList().stream().mapToLong(vote -> vote.getVoteCount()).sum();
        if (sum > share) {
            throw new ContractValidateException("The total number of votes[" + sum + "] is greater than the share[" + share + "]");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new ContractValidateException(ex.getMessage());
    }
    return true;
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ContractValidateException(org.tron.core.exception.ContractValidateException) Iterator(java.util.Iterator) VoteWitnessContract(org.tron.protos.Contract.VoteWitnessContract) ContractExeException(org.tron.core.exception.ContractExeException) ByteString(com.google.protobuf.ByteString) Slf4j(lombok.extern.slf4j.Slf4j) ByteArray(org.tron.common.utils.ByteArray) AccountStore(org.tron.core.db.AccountStore) Vote(org.tron.protos.Contract.VoteWitnessContract.Vote) Result.code(org.tron.protos.Protocol.Transaction.Result.code) Manager(org.tron.core.db.Manager) Preconditions(com.google.common.base.Preconditions) Any(com.google.protobuf.Any) TransactionResultCapsule(org.tron.core.capsule.TransactionResultCapsule) AccountCapsule(org.tron.core.capsule.AccountCapsule) Vote(org.tron.protos.Contract.VoteWitnessContract.Vote) VoteWitnessContract(org.tron.protos.Contract.VoteWitnessContract) AccountStore(org.tron.core.db.AccountStore) ByteString(com.google.protobuf.ByteString) ContractValidateException(org.tron.core.exception.ContractValidateException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ContractValidateException(org.tron.core.exception.ContractValidateException) ContractExeException(org.tron.core.exception.ContractExeException)

Example 25 with ContractValidateException

use of org.tron.core.exception.ContractValidateException in project java-tron by tronprotocol.

the class WitnessCreateActuator method validate.

@Override
public boolean validate() throws ContractValidateException {
    try {
        if (!this.contract.is(WitnessCreateContract.class)) {
            throw new ContractValidateException("contract type error,expected type [AccountCreateContract],real type[" + this.contract.getClass() + "]");
        }
        final WitnessCreateContract contract = this.contract.unpack(WitnessCreateContract.class);
        Preconditions.checkNotNull(contract.getOwnerAddress(), "OwnerAddress is null");
        Preconditions.checkArgument(this.dbManager.getAccountStore().has(contract.getOwnerAddress().toByteArray()), "account not exists");
        AccountCapsule accountCapsule = this.dbManager.getAccountStore().get(contract.getOwnerAddress().toByteArray());
        Preconditions.checkArgument(accountCapsule.getShare() >= WitnessCapsule.MIN_BALANCE, "witnessAccount  has balance[" + accountCapsule.getShare() + "] < MIN_BALANCE[" + WitnessCapsule.MIN_BALANCE + "]");
        Preconditions.checkArgument(!this.dbManager.getWitnessStore().has(contract.getOwnerAddress().toByteArray()), "Witness has existed");
    } catch (final Exception ex) {
        ex.printStackTrace();
        throw new ContractValidateException(ex.getMessage());
    }
    return true;
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) WitnessCreateContract(org.tron.protos.Contract.WitnessCreateContract) ContractValidateException(org.tron.core.exception.ContractValidateException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ContractValidateException(org.tron.core.exception.ContractValidateException) ContractExeException(org.tron.core.exception.ContractExeException)

Aggregations

ContractValidateException (org.tron.core.exception.ContractValidateException)28 ContractExeException (org.tron.core.exception.ContractExeException)25 AccountCapsule (org.tron.core.capsule.AccountCapsule)20 Test (org.junit.Test)16 TransactionResultCapsule (org.tron.core.capsule.TransactionResultCapsule)16 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)9 DateTime (org.joda.time.DateTime)6 ByteString (com.google.protobuf.ByteString)5 BlockCapsule (org.tron.core.capsule.BlockCapsule)4 ValidateSignatureException (org.tron.core.exception.ValidateSignatureException)4 AssetIssueCapsule (org.tron.core.capsule.AssetIssueCapsule)3 Dialog (org.tron.core.db.AbstractRevokingStore.Dialog)3 RevokingStoreIllegalStateException (org.tron.core.exception.RevokingStoreIllegalStateException)3 Iterator (java.util.Iterator)2 TransactionCapsule (org.tron.core.capsule.TransactionCapsule)2 BalanceInsufficientException (org.tron.core.exception.BalanceInsufficientException)2 UnLinkedBlockException (org.tron.core.exception.UnLinkedBlockException)2 Contract (org.tron.protos.Contract)2 Preconditions (com.google.common.base.Preconditions)1 Any (com.google.protobuf.Any)1