use of io.nuls.consensus.entity.meeting.ConsensusReward in project nuls by nuls-io.
the class ConsensusMeetingRunner method calcReward.
private List<ConsensusReward> calcReward(List<Transaction> txList, PocMeetingMember self) {
List<ConsensusReward> rewardList = new ArrayList<>();
Consensus<Agent> ca = self.getAgentConsensus();
if (ca.getExtend().getSeed()) {
long totalFee = 0;
for (Transaction tx : txList) {
totalFee += tx.getFee().getValue();
}
if (totalFee == 0L) {
return rewardList;
}
double caReward = totalFee;
ConsensusReward agentReword = new ConsensusReward();
agentReword.setAddress(ca.getAddress());
agentReword.setReward(Na.valueOf((long) caReward));
rewardList.add(agentReword);
return rewardList;
}
long totalFee = 0;
for (Transaction tx : txList) {
totalFee += tx.getFee().getValue();
}
double total = totalFee + DoubleUtils.mul(this.consensusManager.getCurrentRound().getMemberCount(), PocConsensusConstant.BLOCK_REWARD.getValue());
ConsensusReward agentReword = new ConsensusReward();
agentReword.setAddress(ca.getAddress());
double caReward = DoubleUtils.mul(total, DoubleUtils.div(ca.getExtend().getDeposit().getValue(), this.consensusManager.getCurrentRound().getTotalDeposit().getValue()));
caReward = caReward + DoubleUtils.mul(total, DoubleUtils.mul(DoubleUtils.div((this.consensusManager.getCurrentRound().getTotalDeposit().getValue() - ca.getExtend().getDeposit().getValue()), this.consensusManager.getCurrentRound().getTotalDeposit().getValue()), DoubleUtils.round(ca.getExtend().getCommissionRate() / 100, 2)));
agentReword.setReward(Na.valueOf((long) caReward));
Map<String, ConsensusReward> rewardMap = new HashMap<>();
rewardMap.put(ca.getAddress(), agentReword);
double delegateCommissionRate = DoubleUtils.div((100 - ca.getExtend().getCommissionRate()), 100, 2);
for (Consensus<Deposit> cd : self.getDelegateList()) {
double reward = DoubleUtils.mul(DoubleUtils.mul(total, delegateCommissionRate), DoubleUtils.div(cd.getExtend().getDeposit().getValue(), this.consensusManager.getCurrentRound().getTotalDeposit().getValue()));
ConsensusReward delegateReword = rewardMap.get(cd.getAddress());
if (null == delegateReword) {
delegateReword = new ConsensusReward();
delegateReword.setReward(Na.ZERO);
}
delegateReword.setAddress(cd.getAddress());
delegateReword.setReward(delegateReword.getReward().add(Na.valueOf((long) reward)));
rewardMap.put(cd.getAddress(), delegateReword);
}
rewardList.addAll(rewardMap.values());
return rewardList;
}
use of io.nuls.consensus.entity.meeting.ConsensusReward in project nuls by nuls-io.
the class ConsensusMeetingRunner method coinBaseTx.
private void coinBaseTx(List<Transaction> txList, PocMeetingMember self) throws NulsException, IOException {
CoinTransferData data = new CoinTransferData(OperationType.COIN_BASE, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_COIN_BASE));
data.setFee(Na.ZERO);
List<ConsensusReward> rewardList = calcReward(txList, self);
Na total = Na.ZERO;
for (ConsensusReward reward : rewardList) {
Coin coin = new Coin();
coin.setNa(reward.getReward());
data.addTo(reward.getAddress(), coin);
total = total.add(reward.getReward());
}
data.setTotalNa(total);
CoinBaseTransaction tx;
try {
tx = new CoinBaseTransaction(data, null);
} catch (NulsException e) {
Log.error(e);
throw new NulsRuntimeException(e);
}
tx.setFee(Na.ZERO);
tx.setHash(NulsDigestData.calcDigestData(tx));
tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), consensusManager.getConsensusStatusInfo().getAccount(), NulsContext.CACHED_PASSWORD_OF_WALLET).serialize());
ValidateResult validateResult = tx.verify();
confirmingTxCacheManager.putTx(tx);
if (null == validateResult || validateResult.isFailed()) {
throw new NulsRuntimeException(ErrorCode.CONSENSUS_EXCEPTION);
}
try {
ledgerService.approvalTx(tx);
} catch (NulsException e) {
throw new NulsRuntimeException(e);
}
txList.add(0, tx);
}
Aggregations