/
DelegationService.java
206 lines (191 loc) · 7.8 KB
/
DelegationService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package org.tron.core.services;
import com.google.protobuf.ByteString;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.spongycastle.util.encoders.Hex;
import org.springframework.stereotype.Component;
import org.tron.common.storage.Deposit;
import org.tron.core.capsule.AccountCapsule;
import org.tron.core.capsule.WitnessCapsule;
import org.tron.core.config.Parameter.ChainConstant;
import org.tron.core.db.AccountStore;
import org.tron.core.db.DelegationStore;
import org.tron.core.db.DynamicPropertiesStore;
import org.tron.core.db.Manager;
import org.tron.core.exception.BalanceInsufficientException;
import org.tron.protos.Protocol.Vote;
@Slf4j(topic = "delegation")
@Component
public class DelegationService {
@Setter
private Manager manager;
public void payStandbyWitness() {
List<ByteString> witnessAddressList = new ArrayList<>();
for (WitnessCapsule witnessCapsule : manager.getWitnessStore().getAllWitnesses()) {
witnessAddressList.add(witnessCapsule.getAddress());
}
sortWitness(witnessAddressList);
if (witnessAddressList.size() > ChainConstant.WITNESS_STANDBY_LENGTH) {
witnessAddressList = witnessAddressList.subList(0, ChainConstant.WITNESS_STANDBY_LENGTH);
}
long voteSum = 0;
long totalPay = manager.getDynamicPropertiesStore().getWitness127PayPerBlock();
for (ByteString b : witnessAddressList) {
voteSum += getWitnesseByAddress(b).getVoteCount();
}
if (voteSum > 0) {
for (ByteString b : witnessAddressList) {
double eachVotePay = (double) totalPay / voteSum;
long pay = (long) (getWitnesseByAddress(b).getVoteCount() * eachVotePay);
logger.debug("pay {} stand reward {}", Hex.toHexString(b.toByteArray()), pay);
payReward(b.toByteArray(), pay);
}
}
}
public void payBlockReward(byte[] witnessAddress, long value) {
logger.debug("pay {} block reward {}", Hex.toHexString(witnessAddress), value);
payReward(witnessAddress, value);
}
private void payReward(byte[] witnessAddress, long value) {
long cycle = manager.getDynamicPropertiesStore().getCurrentCycleNumber();
int brokerage = manager.getDelegationStore().getBrokerage(cycle, witnessAddress);
double brokerageRate = (double) brokerage / 100;
long brokerageAmount = (long) (brokerageRate * value);
value -= brokerageAmount;
manager.getDelegationStore().addReward(cycle, witnessAddress, value);
adjustAllowance(witnessAddress, brokerageAmount);
}
public void withdrawReward(byte[] address, Deposit deposit) {
if (!manager.getDynamicPropertiesStore().allowChangeDelegation()) {
return;
}
AccountStore accountStore = manager.getAccountStore();
DelegationStore delegationStore = manager.getDelegationStore();
DynamicPropertiesStore dynamicPropertiesStore = manager.getDynamicPropertiesStore();
AccountCapsule accountCapsule;
if (deposit == null) {
accountCapsule = accountStore.get(address);
} else {
accountCapsule = deposit.getAccount(address);
}
long beginCycle = delegationStore.getBeginCycle(address);
long endCycle = delegationStore.getEndCycle(address);
long currentCycle = dynamicPropertiesStore.getCurrentCycleNumber();
long reward = 0;
if (beginCycle > currentCycle || accountCapsule == null) {
return;
}
if (beginCycle == currentCycle) {
AccountCapsule account = delegationStore.getAccountVote(beginCycle, address);
if (account != null) {
return;
}
}
//withdraw the latest cycle reward
if (beginCycle + 1 == endCycle && beginCycle < currentCycle) {
AccountCapsule account = delegationStore.getAccountVote(beginCycle, address);
if (account != null) {
reward = computeReward(beginCycle, account);
adjustAllowance(address, reward);
reward = 0;
logger.info("latest cycle reward {},{}", beginCycle, account.getVotesList());
}
beginCycle += 1;
}
//
endCycle = currentCycle;
if (CollectionUtils.isEmpty(accountCapsule.getVotesList())) {
manager.getDelegationStore().setBeginCycle(address, endCycle + 1);
return;
}
if (beginCycle < endCycle) {
for (long cycle = beginCycle; cycle < endCycle; cycle++) {
reward += computeReward(cycle, accountCapsule);
}
adjustAllowance(address, reward);
}
delegationStore.setBeginCycle(address, endCycle);
delegationStore.setEndCycle(address, endCycle + 1);
delegationStore.setAccountVote(endCycle, address, accountCapsule);
logger.info("adjust {} allowance {}, now currentCycle {}, beginCycle {}, endCycle {}, "
+ "account vote {},", Hex.toHexString(address), reward, currentCycle,
beginCycle, endCycle, accountCapsule.getVotesList());
}
public long queryReward(byte[] address) {
if (!manager.getDynamicPropertiesStore().allowChangeDelegation()) {
return 0;
}
AccountStore accountStore = manager.getAccountStore();
DelegationStore delegationStore = manager.getDelegationStore();
DynamicPropertiesStore dynamicPropertiesStore = manager.getDynamicPropertiesStore();
AccountCapsule accountCapsule = accountStore.get(address);
long beginCycle = delegationStore.getBeginCycle(address);
long endCycle = delegationStore.getEndCycle(address);
long currentCycle = dynamicPropertiesStore.getCurrentCycleNumber();
long reward = 0;
if (accountCapsule == null) {
return 0;
}
if (beginCycle > currentCycle) {
return accountCapsule.getAllowance();
}
//withdraw the latest cycle reward
if (beginCycle + 1 == endCycle && beginCycle < currentCycle) {
AccountCapsule account = delegationStore.getAccountVote(beginCycle, address);
if (account != null) {
reward = computeReward(beginCycle, account);
}
beginCycle += 1;
}
//
endCycle = currentCycle;
if (CollectionUtils.isEmpty(accountCapsule.getVotesList())) {
return reward + accountCapsule.getAllowance();
}
if (beginCycle < endCycle) {
for (long cycle = beginCycle; cycle < endCycle; cycle++) {
reward += computeReward(cycle, accountCapsule);
}
}
return reward + accountCapsule.getAllowance();
}
private long computeReward(long cycle, AccountCapsule accountCapsule) {
long reward = 0;
for (Vote vote : accountCapsule.getVotesList()) {
byte[] srAddress = vote.getVoteAddress().toByteArray();
long totalReward = manager.getDelegationStore().getReward(cycle, srAddress);
long totalVote = manager.getDelegationStore().getWitnessVote(cycle, srAddress);
if (totalVote == DelegationStore.REMARK || totalVote == 0) {
continue;
}
long userVote = vote.getVoteCount();
double voteRate = (double) userVote / totalVote;
reward += voteRate * totalReward;
logger.debug("computeReward {} {} {} {},{},{},{}", cycle,
Hex.toHexString(accountCapsule.getAddress().toByteArray()), Hex.toHexString(srAddress),
userVote, totalVote, totalReward, reward);
}
return reward;
}
public WitnessCapsule getWitnesseByAddress(ByteString address) {
return this.manager.getWitnessStore().get(address.toByteArray());
}
private void adjustAllowance(byte[] address, long amount) {
try {
if (amount <= 0) {
return;
}
manager.adjustAllowance(address, amount);
} catch (BalanceInsufficientException e) {
logger.error("withdrawReward error: {},{}", Hex.toHexString(address), address, e);
}
}
private void sortWitness(List<ByteString> list) {
list.sort(Comparator.comparingLong((ByteString b) -> getWitnesseByAddress(b).getVoteCount())
.reversed().thenComparing(Comparator.comparingInt(ByteString::hashCode).reversed()));
}
}