Search in sources :

Example 1 with Vote

use of org.tron.protos.Contract.VoteWitnessContract.Vote 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

Preconditions (com.google.common.base.Preconditions)1 Any (com.google.protobuf.Any)1 ByteString (com.google.protobuf.ByteString)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Iterator (java.util.Iterator)1 Slf4j (lombok.extern.slf4j.Slf4j)1 ByteArray (org.tron.common.utils.ByteArray)1 AccountCapsule (org.tron.core.capsule.AccountCapsule)1 TransactionResultCapsule (org.tron.core.capsule.TransactionResultCapsule)1 AccountStore (org.tron.core.db.AccountStore)1 Manager (org.tron.core.db.Manager)1 ContractExeException (org.tron.core.exception.ContractExeException)1 ContractValidateException (org.tron.core.exception.ContractValidateException)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