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);
}
}
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);
}
}
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);
}
}
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;
}
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;
}
Aggregations