use of org.tron.core.db.AccountStore in project java-tron by tronprotocol.
the class TransferAssetActuator method execute.
@Override
public boolean execute(TransactionResultCapsule ret) throws ContractExeException {
long fee = calcFee();
if (!this.contract.is(TransferAssetContract.class)) {
throw new ContractExeException();
}
if (this.dbManager == null) {
throw new ContractExeException();
}
try {
TransferAssetContract transferAssetContract = this.contract.unpack(TransferAssetContract.class);
AccountStore accountStore = this.dbManager.getAccountStore();
byte[] ownerKey = transferAssetContract.getOwnerAddress().toByteArray();
byte[] toKey = transferAssetContract.getToAddress().toByteArray();
ByteString assertName = transferAssetContract.getAssetName();
long amount = transferAssetContract.getAmount();
AccountCapsule ownerAccountCapsule = accountStore.get(ownerKey);
if (!ownerAccountCapsule.reduceAssetAmount(assertName, amount)) {
throw new ContractExeException("reduceAssetAmount failed !");
}
accountStore.put(ownerKey, ownerAccountCapsule);
AccountCapsule toAccountCapsule = accountStore.get(toKey);
toAccountCapsule.addAssetAmount(assertName, amount);
accountStore.put(toKey, toAccountCapsule);
ret.setStatus(fee, code.SUCESS);
} catch (InvalidProtocolBufferException e) {
ret.setStatus(fee, code.FAILED);
throw new ContractExeException();
}
return true;
}
use of org.tron.core.db.AccountStore in project java-tron by tronprotocol.
the class Wallet method getBalance.
public Account getBalance(Account account) {
AccountStore accountStore = dbManager.getAccountStore();
AccountCapsule accountCapsule = accountStore.get(account.getAddress().toByteArray());
return accountCapsule == null ? null : accountCapsule.getInstance();
}
use of org.tron.core.db.AccountStore 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;
}
Aggregations