use of org.tron.core.exception.ContractExeException 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;
}
use of org.tron.core.exception.ContractExeException in project java-tron by tronprotocol.
the class AssetIssueActuator method execute.
@Override
public boolean execute(TransactionResultCapsule ret) throws ContractExeException {
long fee = calcFee();
try {
if (!this.contract.is(AssetIssueContract.class)) {
throw new ContractExeException();
}
if (dbManager == null) {
throw new ContractExeException();
}
AssetIssueContract assetIssueContract = contract.unpack(AssetIssueContract.class);
AssetIssueCapsule assetIssueCapsule = new AssetIssueCapsule(assetIssueContract);
dbManager.getAssetIssueStore().put(assetIssueCapsule.getName().toByteArray(), assetIssueCapsule);
dbManager.adjustBalance(assetIssueContract.getOwnerAddress().toByteArray(), -calcFee());
ret.setStatus(fee, code.SUCESS);
AccountCapsule accountCapsule = dbManager.getAccountStore().get(assetIssueContract.getOwnerAddress().toByteArray());
accountCapsule.addAsset(ByteArray.toStr(assetIssueContract.getName().toByteArray()), assetIssueContract.getTotalSupply());
dbManager.getAccountStore().put(assetIssueContract.getOwnerAddress().toByteArray(), accountCapsule);
} catch (InvalidProtocolBufferException e) {
ret.setStatus(fee, code.FAILED);
throw new ContractExeException();
} catch (BalanceInsufficientException e) {
ret.setStatus(fee, code.FAILED);
throw new ContractExeException();
}
return true;
}
use of org.tron.core.exception.ContractExeException in project java-tron by tronprotocol.
the class ParticipateAssetIssueActuator method execute.
@Override
public boolean execute(TransactionResultCapsule ret) throws ContractExeException {
long fee = calcFee();
try {
Contract.ParticipateAssetIssueContract participateAssetIssueContract = contract.unpack(Contract.ParticipateAssetIssueContract.class);
long cost = participateAssetIssueContract.getAmount();
// subtract from owner address
byte[] ownerAddressBytes = participateAssetIssueContract.getOwnerAddress().toByteArray();
AccountCapsule ownerAccount = this.dbManager.getAccountStore().get(ownerAddressBytes);
ownerAccount.setBalance(ownerAccount.getBalance() - cost - fee);
// calculate the exchange amount
AssetIssueCapsule assetIssueCapsule = this.dbManager.getAssetIssueStore().get(participateAssetIssueContract.getAssetName().toByteArray());
long exchangeAmount = cost * assetIssueCapsule.getNum() / assetIssueCapsule.getTrxNum();
ownerAccount.addAssetAmount(assetIssueCapsule.getName(), exchangeAmount);
// add to to_address
byte[] toAddressBytes = participateAssetIssueContract.getToAddress().toByteArray();
AccountCapsule toAccount = this.dbManager.getAccountStore().get(toAddressBytes);
toAccount.setBalance(toAccount.getBalance() + cost);
if (!toAccount.reduceAssetAmount(assetIssueCapsule.getName(), exchangeAmount)) {
throw new ContractExeException("reduceAssetAmount failed !");
}
// write to db
dbManager.getAccountStore().put(ownerAddressBytes, ownerAccount);
dbManager.getAccountStore().put(toAddressBytes, toAccount);
ret.setStatus(fee, Protocol.Transaction.Result.code.SUCESS);
return true;
} catch (InvalidProtocolBufferException e) {
ret.setStatus(fee, Protocol.Transaction.Result.code.FAILED);
logger.debug(e.getMessage(), e);
throw new ContractExeException(e.getMessage());
}
}
use of org.tron.core.exception.ContractExeException 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));
}
}
use of org.tron.core.exception.ContractExeException 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));
}
}
Aggregations