Search in sources :

Example 46 with ByteString

use of com.google.protobuf.ByteString in project java-tron by tronprotocol.

the class TransferActuator method validate.

@Override
public boolean validate() throws ContractValidateException {
    try {
        if (!contract.is(TransferContract.class)) {
            throw new ContractValidateException("contract type error,expected type [TransferContract],real type[" + contract.getClass() + "]");
        }
        TransferContract transferContract = this.contract.unpack(TransferContract.class);
        Preconditions.checkNotNull(transferContract.getOwnerAddress(), "OwnerAddress is null");
        Preconditions.checkNotNull(transferContract.getToAddress(), "ToAddress is null");
        Preconditions.checkNotNull(transferContract.getAmount(), "Amount is null");
        if (transferContract.getOwnerAddress().equals(transferContract.getToAddress())) {
            throw new ContractValidateException("Cannot transfer trx to yourself.");
        }
        if (!dbManager.getAccountStore().has(transferContract.getOwnerAddress().toByteArray())) {
            throw new ContractValidateException("Validate TransferContract error, no OwnerAccount.");
        }
        AccountCapsule ownerAccount = dbManager.getAccountStore().get(transferContract.getOwnerAddress().toByteArray());
        long balance = ownerAccount.getBalance();
        long laststOperationTime = ownerAccount.getLatestOperationTime();
        long now = System.currentTimeMillis();
        // TODO:
        // if (now - laststOperationTime < balance) {
        // throw new ContractValidateException();
        // }
        // if account with to_address is not existed,  create it.
        ByteString toAddress = transferContract.getToAddress();
        if (!dbManager.getAccountStore().has(toAddress.toByteArray())) {
            AccountCapsule account = new AccountCapsule(toAddress, AccountType.Normal);
            dbManager.getAccountStore().put(toAddress.toByteArray(), account);
        }
        if (ownerAccount.getBalance() < calcFee()) {
            throw new ContractValidateException("Validate TransferContract error, insufficient fee.");
        }
        long amount = transferContract.getAmount();
        if (amount < 0) {
            throw new ContractValidateException("Amount is less than 0.");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new ContractValidateException(ex.getMessage());
    }
    return true;
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) ByteString(com.google.protobuf.ByteString) ContractValidateException(org.tron.core.exception.ContractValidateException) TransferContract(org.tron.protos.Contract.TransferContract) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ContractValidateException(org.tron.core.exception.ContractValidateException) BalanceInsufficientException(org.tron.core.exception.BalanceInsufficientException) ContractExeException(org.tron.core.exception.ContractExeException)

Example 47 with ByteString

use of com.google.protobuf.ByteString in project java-tron by tronprotocol.

the class TransferAssetActuator method validate.

@Override
public boolean validate() throws ContractValidateException {
    try {
        TransferAssetContract transferAssetContract = this.contract.unpack(TransferAssetContract.class);
        Preconditions.checkNotNull(transferAssetContract.getOwnerAddress(), "OwnerAddress is null");
        Preconditions.checkNotNull(transferAssetContract.getToAddress(), "ToAddress is null");
        Preconditions.checkNotNull(transferAssetContract.getAssetName(), "AssetName is null");
        Preconditions.checkNotNull(transferAssetContract.getAmount(), "Amount is null");
        if (transferAssetContract.getOwnerAddress().equals(transferAssetContract.getToAddress())) {
            throw new ContractValidateException("Cannot transfer asset to yourself.");
        }
        byte[] ownerKey = transferAssetContract.getOwnerAddress().toByteArray();
        if (!this.dbManager.getAccountStore().has(ownerKey)) {
            throw new ContractValidateException();
        }
        // if account with to_address is not existed,  create it.
        ByteString toAddress = transferAssetContract.getToAddress();
        if (!dbManager.getAccountStore().has(toAddress.toByteArray())) {
            AccountCapsule account = new AccountCapsule(toAddress, AccountType.Normal);
            dbManager.getAccountStore().put(toAddress.toByteArray(), account);
        }
        byte[] nameKey = transferAssetContract.getAssetName().toByteArray();
        if (!this.dbManager.getAssetIssueStore().has(nameKey)) {
            throw new ContractValidateException();
        }
        long amount = transferAssetContract.getAmount();
        AccountCapsule ownerAccount = this.dbManager.getAccountStore().get(ownerKey);
        Map<String, Long> asset = ownerAccount.getAssetMap();
        if (asset.isEmpty()) {
            throw new ContractValidateException();
        }
        Long assetAmount = asset.get(ByteArray.toStr(nameKey));
        if (amount <= 0 || null == assetAmount || amount > assetAmount || assetAmount <= 0) {
            throw new ContractValidateException();
        }
    } catch (InvalidProtocolBufferException e) {
        throw new ContractValidateException();
    }
    return true;
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) ByteString(com.google.protobuf.ByteString) ContractValidateException(org.tron.core.exception.ContractValidateException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteString(com.google.protobuf.ByteString) TransferAssetContract(org.tron.protos.Contract.TransferAssetContract)

Example 48 with ByteString

use of com.google.protobuf.ByteString 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 49 with ByteString

use of com.google.protobuf.ByteString in project java-tron by tronprotocol.

the class AccountCapsule method reduceAssetAmount.

/**
 * reduce asset amount.
 */
public boolean reduceAssetAmount(ByteString name, long amount) {
    Map<String, Long> assetMap = this.account.getAssetMap();
    String nameKey = ByteArray.toStr(name.toByteArray());
    Long currentAmount = assetMap.get(nameKey);
    if (amount > 0 && null != currentAmount && amount <= currentAmount) {
        this.account = this.account.toBuilder().putAsset(nameKey, currentAmount - amount).build();
        return true;
    }
    return false;
}
Also used : ByteString(com.google.protobuf.ByteString)

Example 50 with ByteString

use of com.google.protobuf.ByteString in project java-tron by tronprotocol.

the class AccountCapsuleTest method addVotesTest.

@Test
public void addVotesTest() {
    // test addVote and getVotesList function
    ByteString voteAddress = ByteString.copyFrom(AccountCapsuleTest.randomBytes(32));
    long voteAdd = 10L;
    accountCapsuleTest.addVotes(voteAddress, voteAdd);
    List<Vote> votesList = accountCapsuleTest.getVotesList();
    for (Vote vote : votesList) {
        Assert.assertEquals(voteAddress, vote.getVoteAddress());
        Assert.assertEquals(voteAdd, vote.getVoteCount());
    }
}
Also used : Vote(org.tron.protos.Protocol.Account.Vote) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Aggregations

ByteString (com.google.protobuf.ByteString)406 Test (org.junit.Test)143 ArrayList (java.util.ArrayList)65 ByteString (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)63 HashMap (java.util.HashMap)41 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)40 IOException (java.io.IOException)37 List (java.util.List)33 Map (java.util.Map)33 ServerRequest (com.pokegoapi.main.ServerRequest)17 ExecutionException (java.util.concurrent.ExecutionException)16 AnnotateImageRequest (com.google.cloud.vision.v1.AnnotateImageRequest)14 AnnotateImageResponse (com.google.cloud.vision.v1.AnnotateImageResponse)14 BatchAnnotateImagesResponse (com.google.cloud.vision.v1.BatchAnnotateImagesResponse)14 Feature (com.google.cloud.vision.v1.Feature)14 Image (com.google.cloud.vision.v1.Image)14 ImageAnnotatorClient (com.google.cloud.vision.v1.ImageAnnotatorClient)14 FileInputStream (java.io.FileInputStream)13 ByteBuffer (java.nio.ByteBuffer)13 WebImage (com.google.cloud.vision.v1.WebDetection.WebImage)12