Search in sources :

Example 26 with InvalidProtocolBufferException

use of org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.InvalidProtocolBufferException 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 27 with InvalidProtocolBufferException

use of org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.InvalidProtocolBufferException in project java-tron by tronprotocol.

the class WitnessCreateActuator method execute.

@Override
public boolean execute(TransactionResultCapsule ret) throws ContractExeException {
    long fee = calcFee();
    try {
        final WitnessCreateContract witnessCreateContract = this.contract.unpack(WitnessCreateContract.class);
        this.createWitness(witnessCreateContract);
        ret.setStatus(fee, code.SUCESS);
    } catch (final InvalidProtocolBufferException e) {
        logger.debug(e.getMessage(), e);
        ret.setStatus(fee, code.FAILED);
        throw new ContractExeException(e.getMessage());
    }
    return true;
}
Also used : WitnessCreateContract(org.tron.protos.Contract.WitnessCreateContract) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ContractExeException(org.tron.core.exception.ContractExeException)

Example 28 with InvalidProtocolBufferException

use of org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.InvalidProtocolBufferException 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;
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) BalanceInsufficientException(org.tron.core.exception.BalanceInsufficientException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) AssetIssueCapsule(org.tron.core.capsule.AssetIssueCapsule) AssetIssueContract(org.tron.protos.Contract.AssetIssueContract) ContractExeException(org.tron.core.exception.ContractExeException)

Example 29 with InvalidProtocolBufferException

use of org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.InvalidProtocolBufferException 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());
    }
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) AssetIssueCapsule(org.tron.core.capsule.AssetIssueCapsule) Contract(org.tron.protos.Contract) ContractExeException(org.tron.core.exception.ContractExeException)

Example 30 with InvalidProtocolBufferException

use of org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.InvalidProtocolBufferException in project java-tron by tronprotocol.

the class ParticipateAssetIssueActuator method validate.

@Override
public boolean validate() throws ContractValidateException {
    if (!this.contract.is(Contract.ParticipateAssetIssueContract.class)) {
        throw new ContractValidateException();
    }
    try {
        final Contract.ParticipateAssetIssueContract participateAssetIssueContract = this.contract.unpack(Contract.ParticipateAssetIssueContract.class);
        Preconditions.checkNotNull(participateAssetIssueContract.getOwnerAddress(), "OwnerAddress is null");
        Preconditions.checkNotNull(participateAssetIssueContract.getToAddress(), "ToAddress is null");
        Preconditions.checkNotNull(participateAssetIssueContract.getAssetName(), "trx name is null");
        if (participateAssetIssueContract.getAmount() <= 0) {
            throw new ContractValidateException("Trx Num must be positive!");
        }
        if (participateAssetIssueContract.getOwnerAddress().equals(participateAssetIssueContract.getToAddress())) {
            throw new ContractValidateException("Cannot participate asset Issue yourself !");
        }
        byte[] addressBytes = participateAssetIssueContract.getOwnerAddress().toByteArray();
        // Whether the account exist
        if (!this.dbManager.getAccountStore().has(addressBytes)) {
            throw new ContractValidateException("Account does not exist!");
        }
        AccountCapsule ac = this.dbManager.getAccountStore().get(addressBytes);
        long fee = calcFee();
        // Whether the balance is enough
        if (ac.getBalance() < participateAssetIssueContract.getAmount() + fee) {
            throw new ContractValidateException("No enough balance !");
        }
        // Whether have the mapping
        if (!this.dbManager.getAssetIssueStore().has(participateAssetIssueContract.getAssetName().toByteArray())) {
            throw new ContractValidateException("No asset named " + ByteArray.toStr(participateAssetIssueContract.getAssetName().toByteArray()));
        }
        AssetIssueCapsule assetIssueCapsule = this.dbManager.getAssetIssueStore().get(participateAssetIssueContract.getAssetName().toByteArray());
        if (!participateAssetIssueContract.getToAddress().equals(assetIssueCapsule.getOwnerAddress())) {
            throw new ContractValidateException("The asset is not issued by " + ByteArray.toHexString(participateAssetIssueContract.getToAddress().toByteArray()));
        }
        // Whether the exchange can be processed: to see if the exchange can be the exact int
        long cost = participateAssetIssueContract.getAmount();
        DateTime now = DateTime.now();
        if (now.getMillis() >= assetIssueCapsule.getEndTime() || now.getMillis() < assetIssueCapsule.getStartTime()) {
            throw new ContractValidateException("No longer valid period!");
        }
        int trxNum = assetIssueCapsule.getTrxNum();
        int num = assetIssueCapsule.getNum();
        long exchangeAmount = cost * num / trxNum;
        if (exchangeAmount == 0) {
            throw new ContractValidateException("Can not process the exchange!");
        }
        AccountCapsule toAccount = this.dbManager.getAccountStore().get(participateAssetIssueContract.getToAddress().toByteArray());
        if (!toAccount.assetBalanceEnough(assetIssueCapsule.getName(), exchangeAmount)) {
            throw new ContractValidateException("Asset balance is not enough !");
        }
    } catch (InvalidProtocolBufferException e) {
        throw new ContractValidateException();
    }
    return true;
}
Also used : AccountCapsule(org.tron.core.capsule.AccountCapsule) ContractValidateException(org.tron.core.exception.ContractValidateException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) AssetIssueCapsule(org.tron.core.capsule.AssetIssueCapsule) Contract(org.tron.protos.Contract) DateTime(org.joda.time.DateTime)

Aggregations

InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)334 IOException (java.io.IOException)69 ByteString (com.google.protobuf.ByteString)46 ServerRequest (com.pokegoapi.main.ServerRequest)46 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)39 GeneralSecurityException (java.security.GeneralSecurityException)32 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)25 KeysetHandle (com.google.crypto.tink.KeysetHandle)25 HashMap (java.util.HashMap)25 ArrayList (java.util.ArrayList)22 InvalidProtocolBufferException (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.InvalidProtocolBufferException)22 List (java.util.List)19 Any (com.google.protobuf.Any)18 Map (java.util.Map)18 Key (org.apache.accumulo.core.data.Key)17 Value (org.apache.accumulo.core.data.Value)17 Status (org.apache.accumulo.server.replication.proto.Replication.Status)17 Text (org.apache.hadoop.io.Text)17 HashSet (java.util.HashSet)12 RunnerApi (org.apache.beam.model.pipeline.v1.RunnerApi)11