use of org.tron.protos.Contract.VoteWitnessContract 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;
}
use of org.tron.protos.Contract.VoteWitnessContract in project java-tron by tronprotocol.
the class VoteWitnessActuator method execute.
@Override
public boolean execute(TransactionResultCapsule ret) throws ContractExeException {
long fee = calcFee();
try {
VoteWitnessContract voteContract = contract.unpack(VoteWitnessContract.class);
countVoteAccount(voteContract);
ret.setStatus(fee, code.SUCESS);
} catch (InvalidProtocolBufferException e) {
logger.debug(e.getMessage(), e);
ret.setStatus(fee, code.FAILED);
throw new ContractExeException(e.getMessage());
}
return true;
}
Aggregations