Search in sources :

Example 6 with ContractValidateException

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

the class Wallet method broadcastTransaction.

/**
 * Broadcast a transaction.
 */
public boolean broadcastTransaction(Transaction signaturedTransaction) {
    TransactionCapsule trx = new TransactionCapsule(signaturedTransaction);
    try {
        if (trx.validateSignature()) {
            Message message = new TransactionMessage(signaturedTransaction);
            dbManager.pushTransactions(trx);
            p2pnode.broadcast(message);
            return true;
        }
    } catch (ValidateSignatureException e) {
        logger.debug(e.getMessage(), e);
    } catch (ContractValidateException e) {
        logger.debug(e.getMessage(), e);
    } catch (ContractExeException e) {
        logger.debug(e.getMessage(), e);
    } catch (Exception e) {
        logger.debug(e.getMessage(), e);
    }
    return false;
}
Also used : TransactionCapsule(org.tron.core.capsule.TransactionCapsule) TransactionMessage(org.tron.core.net.message.TransactionMessage) NumberMessage(org.tron.api.GrpcAPI.NumberMessage) TransactionMessage(org.tron.core.net.message.TransactionMessage) Message(org.tron.common.overlay.message.Message) ValidateSignatureException(org.tron.core.exception.ValidateSignatureException) ContractValidateException(org.tron.core.exception.ContractValidateException) ContractValidateException(org.tron.core.exception.ContractValidateException) ContractExeException(org.tron.core.exception.ContractExeException) ValidateSignatureException(org.tron.core.exception.ValidateSignatureException) ContractExeException(org.tron.core.exception.ContractExeException)

Example 7 with ContractValidateException

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

the class ParticipateAssetIssueActuator method validate.

@Override
public boolean validate() throws ContractValidateException {
    if (!this.contract.is(Contract.ParticipateAssetIssueContract.class)) {
        throw new ContractValidateException();
    }
    try {
        final Contract.ParticipateAssetIssueContract participateAssetIssueContract = this.contract.unpack(Contract.ParticipateAssetIssueContract.class);
        Preconditions.checkNotNull(participateAssetIssueContract.getOwnerAddress(), "OwnerAddress is null");
        Preconditions.checkNotNull(participateAssetIssueContract.getToAddress(), "ToAddress is null");
        Preconditions.checkNotNull(participateAssetIssueContract.getAssetName(), "trx name is null");
        if (participateAssetIssueContract.getAmount() <= 0) {
            throw new ContractValidateException("Trx Num must be positive!");
        }
        if (participateAssetIssueContract.getOwnerAddress().equals(participateAssetIssueContract.getToAddress())) {
            throw new ContractValidateException("Cannot participate asset Issue yourself !");
        }
        byte[] addressBytes = participateAssetIssueContract.getOwnerAddress().toByteArray();
        // Whether the account exist
        if (!this.dbManager.getAccountStore().has(addressBytes)) {
            throw new ContractValidateException("Account does not exist!");
        }
        AccountCapsule ac = this.dbManager.getAccountStore().get(addressBytes);
        long fee = calcFee();
        // Whether the balance is enough
        if (ac.getBalance() < participateAssetIssueContract.getAmount() + fee) {
            throw new ContractValidateException("No enough balance !");
        }
        // Whether have the mapping
        if (!this.dbManager.getAssetIssueStore().has(participateAssetIssueContract.getAssetName().toByteArray())) {
            throw new ContractValidateException("No asset named " + ByteArray.toStr(participateAssetIssueContract.getAssetName().toByteArray()));
        }
        AssetIssueCapsule assetIssueCapsule = this.dbManager.getAssetIssueStore().get(participateAssetIssueContract.getAssetName().toByteArray());
        if (!participateAssetIssueContract.getToAddress().equals(assetIssueCapsule.getOwnerAddress())) {
            throw new ContractValidateException("The asset is not issued by " + ByteArray.toHexString(participateAssetIssueContract.getToAddress().toByteArray()));
        }
        // Whether the exchange can be processed: to see if the exchange can be the exact int
        long cost = participateAssetIssueContract.getAmount();
        DateTime now = DateTime.now();
        if (now.getMillis() >= assetIssueCapsule.getEndTime() || now.getMillis() < assetIssueCapsule.getStartTime()) {
            throw new ContractValidateException("No longer valid period!");
        }
        int trxNum = assetIssueCapsule.getTrxNum();
        int num = assetIssueCapsule.getNum();
        long exchangeAmount = cost * num / trxNum;
        if (exchangeAmount == 0) {
            throw new ContractValidateException("Can not process the exchange!");
        }
        AccountCapsule toAccount = this.dbManager.getAccountStore().get(participateAssetIssueContract.getToAddress().toByteArray());
        if (!toAccount.assetBalanceEnough(assetIssueCapsule.getName(), exchangeAmount)) {
            throw new ContractValidateException("Asset balance is not enough !");
        }
    } catch (InvalidProtocolBufferException e) {
        throw new ContractValidateException();
    }
    return true;
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) ContractValidateException(org.tron.core.exception.ContractValidateException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) AssetIssueCapsule(org.tron.core.capsule.AssetIssueCapsule) Contract(org.tron.protos.Contract) DateTime(org.joda.time.DateTime)

Example 8 with ContractValidateException

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

the class AssetIssueActuatorTest method rightAssetIssue.

@Test
public void rightAssetIssue() {
    AssetIssueActuator actuator = new AssetIssueActuator(getContract(), 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));
        AssetIssueCapsule assetIssueCapsule = dbManager.getAssetIssueStore().get(ByteString.copyFromUtf8(NAME).toByteArray());
        Assert.assertNotNull(assetIssueCapsule);
        Assert.assertEquals(owner.getBalance(), 0L);
        Assert.assertEquals(owner.getAssetMap().get(NAME).longValue(), 10000L);
    } catch (ContractValidateException e) {
        Assert.assertFalse(e instanceof ContractValidateException);
    } catch (ContractExeException e) {
        Assert.assertFalse(e instanceof ContractExeException);
    } finally {
        dbManager.getAssetIssueStore().delete(ByteArray.fromString(NAME));
    }
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) TransactionResultCapsule(org.tron.core.capsule.TransactionResultCapsule) ContractValidateException(org.tron.core.exception.ContractValidateException) AssetIssueCapsule(org.tron.core.capsule.AssetIssueCapsule) ContractExeException(org.tron.core.exception.ContractExeException) Test(org.junit.Test)

Example 9 with ContractValidateException

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

the class AssetIssueActuatorTest method repeatAssetIssue.

@Test
public void repeatAssetIssue() {
    AssetIssueActuator actuator = new AssetIssueActuator(getContract(), dbManager);
    TransactionResultCapsule ret = new TransactionResultCapsule();
    try {
        dbManager.getAssetIssueStore().put(ByteArray.fromString(NAME), new AssetIssueCapsule(getContract().unpack(Contract.AssetIssueContract.class)));
        actuator.validate();
        actuator.execute(ret);
        Assert.assertTrue(false);
    } catch (ContractValidateException e) {
        Assert.assertTrue(e instanceof ContractValidateException);
        AccountCapsule owner = dbManager.getAccountStore().get(ByteArray.fromHexString(OWNER_ADDRESS));
        AssetIssueCapsule assetIssueCapsule = dbManager.getAssetIssueStore().get(ByteArray.fromString(NAME));
        Assert.assertNotNull(assetIssueCapsule);
        Assert.assertNull(owner.getInstance().getAssetMap().get(NAME));
    } catch (ContractExeException e) {
        Assert.assertFalse(e instanceof ContractExeException);
    } catch (InvalidProtocolBufferException e) {
        Assert.assertFalse(e instanceof InvalidProtocolBufferException);
    } finally {
        dbManager.getAssetIssueStore().delete(ByteArray.fromString(NAME));
    }
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) TransactionResultCapsule(org.tron.core.capsule.TransactionResultCapsule) ContractValidateException(org.tron.core.exception.ContractValidateException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) AssetIssueCapsule(org.tron.core.capsule.AssetIssueCapsule) Contract(org.tron.protos.Contract) ContractExeException(org.tron.core.exception.ContractExeException) Test(org.junit.Test)

Example 10 with ContractValidateException

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

the class CreateAccountActuatorTest method firstCreateAccount.

/**
 * Unit test.
 */
@Test
public void firstCreateAccount() {
    CreateAccountActuator actuator = new CreateAccountActuator(getContract(ACCOUNT_NAME_FRIST, OWNER_ADDRESS_FRIST), dbManager);
    TransactionResultCapsule ret = new TransactionResultCapsule();
    try {
        actuator.validate();
        actuator.execute(ret);
        Assert.assertEquals(ret.getInstance().getRet(), code.SUCESS);
        AccountCapsule accountCapsule = dbManager.getAccountStore().get(ByteArray.fromHexString(OWNER_ADDRESS_FRIST));
        Assert.assertNotNull(accountCapsule);
        Assert.assertEquals(accountCapsule.getInstance().getAccountName(), ByteString.copyFromUtf8(ACCOUNT_NAME_FRIST));
    } 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)

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