use of org.tron.core.capsule.AccountCapsule in project java-tron by tronprotocol.
the class Manager method initAccount.
/**
* save account into database.
*/
public void initAccount() {
final Args args = Args.getInstance();
final GenesisBlock genesisBlockArg = args.getGenesisBlock();
genesisBlockArg.getAssets().forEach(account -> {
// to be set in conf
account.setAccountType("Normal");
final AccountCapsule accountCapsule = new AccountCapsule(account.getAccountName(), ByteString.copyFrom(account.getAddressBytes()), account.getAccountType(), account.getBalance());
this.accountStore.put(account.getAddressBytes(), accountCapsule);
});
}
use of org.tron.core.capsule.AccountCapsule in project java-tron by tronprotocol.
the class Manager method updateWitness.
/**
* update witness.
*/
public void updateWitness() {
List<WitnessCapsule> currentWits = getWitnesses();
final Map<ByteString, Long> countWitness = Maps.newHashMap();
final List<AccountCapsule> accountList = this.accountStore.getAllAccounts();
// logger.info("there is account List size is {}", accountList.size());
accountList.forEach(account -> {
// logger.info("there is account ,account address is {}",
// account.createReadableString());
Optional<Long> sum = account.getVotesList().stream().map(vote -> vote.getVoteCount()).reduce((a, b) -> a + b);
if (sum.isPresent()) {
if (sum.get() <= account.getShare()) {
account.getVotesList().forEach(vote -> {
// TODO validate witness //active_witness
ByteString voteAddress = vote.getVoteAddress();
long voteCount = vote.getVoteCount();
if (countWitness.containsKey(voteAddress)) {
countWitness.put(voteAddress, countWitness.get(voteAddress) + voteCount);
} else {
countWitness.put(voteAddress, voteCount);
}
});
} else {
logger.info("account" + account.createReadableString() + ",share[" + account.getShare() + "] > voteSum[" + sum.get() + "]");
}
}
});
witnessStore.getAllWitnesses().forEach(witnessCapsule -> {
witnessCapsule.setVoteCount(0);
witnessCapsule.setIsJobs(false);
this.witnessStore.put(witnessCapsule.createDbKey(), witnessCapsule);
});
final List<WitnessCapsule> witnessCapsuleList = Lists.newArrayList();
logger.info("countWitnessMap size is {}", countWitness.keySet().size());
// Only possible during the initialization phase
if (countWitness.size() == 0) {
witnessCapsuleList.addAll(this.witnessStore.getAllWitnesses());
}
countWitness.forEach((address, voteCount) -> {
final WitnessCapsule witnessCapsule = this.witnessStore.get(createDbKey(address));
if (null == witnessCapsule) {
logger.warn("witnessCapsule is null.address is {}", createReadableString(address));
return;
}
ByteString witnessAddress = witnessCapsule.getInstance().getAddress();
AccountCapsule witnessAccountCapsule = accountStore.get(createDbKey(witnessAddress));
if (witnessAccountCapsule == null) {
logger.warn("witnessAccount[" + createReadableString(witnessAddress) + "] not exists");
} else {
if (witnessAccountCapsule.getBalance() < WitnessCapsule.MIN_BALANCE) {
logger.warn("witnessAccount[" + createReadableString(witnessAddress) + "] has balance[" + witnessAccountCapsule.getBalance() + "] < MIN_BALANCE[" + WitnessCapsule.MIN_BALANCE + "]");
} else {
witnessCapsule.setVoteCount(witnessCapsule.getVoteCount() + voteCount);
witnessCapsule.setIsJobs(false);
witnessCapsuleList.add(witnessCapsule);
this.witnessStore.put(witnessCapsule.createDbKey(), witnessCapsule);
logger.info("address is {} ,countVote is {}", witnessCapsule.createReadableString(), witnessCapsule.getVoteCount());
}
}
});
sortWitness(witnessCapsuleList);
if (witnessCapsuleList.size() > MAX_ACTIVE_WITNESS_NUM) {
setWitnesses(witnessCapsuleList.subList(0, MAX_ACTIVE_WITNESS_NUM));
} else {
setWitnesses(witnessCapsuleList);
}
getWitnesses().forEach(witnessCapsule -> {
witnessCapsule.setIsJobs(true);
this.witnessStore.put(witnessCapsule.createDbKey(), witnessCapsule);
});
logger.info("updateWitness,before:{} ", getWitnessStringList(currentWits) + ",\nafter:{} " + getWitnessStringList(getWitnesses()));
}
use of org.tron.core.capsule.AccountCapsule in project java-tron by tronprotocol.
the class Manager method adjustBalance.
/**
* judge balance.
*/
public void adjustBalance(byte[] accountAddress, long amount) throws BalanceInsufficientException {
AccountCapsule account = getAccountStore().get(accountAddress);
long balance = account.getBalance();
if (amount == 0) {
return;
}
if (amount < 0 && balance < -amount) {
throw new BalanceInsufficientException(accountAddress + " Insufficient");
}
account.setBalance(balance + amount);
this.getAccountStore().put(account.getAddress().toByteArray(), account);
}
use of org.tron.core.capsule.AccountCapsule in project java-tron by tronprotocol.
the class WitnessCreateActuator method validate.
@Override
public boolean validate() throws ContractValidateException {
try {
if (!this.contract.is(WitnessCreateContract.class)) {
throw new ContractValidateException("contract type error,expected type [AccountCreateContract],real type[" + this.contract.getClass() + "]");
}
final WitnessCreateContract contract = this.contract.unpack(WitnessCreateContract.class);
Preconditions.checkNotNull(contract.getOwnerAddress(), "OwnerAddress is null");
Preconditions.checkArgument(this.dbManager.getAccountStore().has(contract.getOwnerAddress().toByteArray()), "account not exists");
AccountCapsule accountCapsule = this.dbManager.getAccountStore().get(contract.getOwnerAddress().toByteArray());
Preconditions.checkArgument(accountCapsule.getShare() >= WitnessCapsule.MIN_BALANCE, "witnessAccount has balance[" + accountCapsule.getShare() + "] < MIN_BALANCE[" + WitnessCapsule.MIN_BALANCE + "]");
Preconditions.checkArgument(!this.dbManager.getWitnessStore().has(contract.getOwnerAddress().toByteArray()), "Witness has existed");
} catch (final Exception ex) {
ex.printStackTrace();
throw new ContractValidateException(ex.getMessage());
}
return true;
}
use of org.tron.core.capsule.AccountCapsule in project java-tron by tronprotocol.
the class CreateAccountActuator method execute.
@Override
public boolean execute(TransactionResultCapsule ret) throws ContractExeException {
long fee = calcFee();
try {
AccountCreateContract accountCreateContract = contract.unpack(AccountCreateContract.class);
AccountCapsule accountCapsule = new AccountCapsule(accountCreateContract);
dbManager.getAccountStore().put(accountCreateContract.getOwnerAddress().toByteArray(), accountCapsule);
ret.setStatus(fee, code.SUCESS);
return true;
} catch (InvalidProtocolBufferException e) {
ret.setStatus(fee, code.FAILED);
logger.debug(e.getMessage(), e);
throw new ContractExeException(e.getMessage());
}
}
Aggregations