Search in sources :

Example 1 with AccountStore

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;
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) AccountStore(org.tron.core.db.AccountStore) ByteString(com.google.protobuf.ByteString) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) TransferAssetContract(org.tron.protos.Contract.TransferAssetContract) ContractExeException(org.tron.core.exception.ContractExeException)

Example 2 with AccountStore

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();
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) AccountStore(org.tron.core.db.AccountStore)

Example 3 with AccountStore

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

Aggregations

AccountCapsule (org.tron.core.capsule.AccountCapsule)3 AccountStore (org.tron.core.db.AccountStore)3 ByteString (com.google.protobuf.ByteString)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 ContractExeException (org.tron.core.exception.ContractExeException)2 Preconditions (com.google.common.base.Preconditions)1 Any (com.google.protobuf.Any)1 Iterator (java.util.Iterator)1 Slf4j (lombok.extern.slf4j.Slf4j)1 ByteArray (org.tron.common.utils.ByteArray)1 TransactionResultCapsule (org.tron.core.capsule.TransactionResultCapsule)1 Manager (org.tron.core.db.Manager)1 ContractValidateException (org.tron.core.exception.ContractValidateException)1 TransferAssetContract (org.tron.protos.Contract.TransferAssetContract)1 VoteWitnessContract (org.tron.protos.Contract.VoteWitnessContract)1 Vote (org.tron.protos.Contract.VoteWitnessContract.Vote)1 Result.code (org.tron.protos.Protocol.Transaction.Result.code)1