use of org.tron.core.capsule.WitnessCapsule 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.WitnessCapsule in project java-tron by tronprotocol.
the class WitnessCreateActuator method createWitness.
private void createWitness(final WitnessCreateContract witnessCreateContract) {
// Create Witness by witnessCreateContract
final WitnessCapsule witnessCapsule = new WitnessCapsule(witnessCreateContract.getOwnerAddress(), 0, witnessCreateContract.getUrl().toStringUtf8());
logger.debug("createWitness,address[{}]", witnessCapsule.createReadableString());
this.dbManager.getWitnessStore().put(witnessCapsule.createDbKey(), witnessCapsule);
}
use of org.tron.core.capsule.WitnessCapsule in project java-tron by tronprotocol.
the class WitnessUpdateActuator method updateWitness.
private void updateWitness(final WitnessUpdateContract witnessUpdateContract) {
final WitnessCapsule witnessCapsule = new WitnessCapsule(witnessUpdateContract.getOwnerAddress(), 0, witnessUpdateContract.getUpdateUrl().toString());
this.dbManager.getWitnessStore().put(witnessCapsule.createDbKey(), witnessCapsule);
}
Aggregations