Skip to content

Commit 5a29292

Browse files
authored
Merge pull request tronprotocol#3118 from tronprotocol/HotFix/getRewardForSR
statistics SR Reward
2 parents 3cef481 + be90869 commit 5a29292

File tree

27 files changed

+1642
-460
lines changed

27 files changed

+1642
-460
lines changed

actuator/src/main/java/org/tron/core/actuator/WithdrawBalanceActuator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public boolean execute(Object result) throws ContractExeException {
5454
delegationService.withdrawReward(withdrawBalanceContract.getOwnerAddress()
5555
.toByteArray());
5656

57+
delegationService.getDelegationStore().setLastWithdrawCycle(
58+
dynamicStore.getCurrentCycleNumber(), withdrawBalanceContract.getOwnerAddress()
59+
.toByteArray());
60+
5761
AccountCapsule accountCapsule = accountStore.
5862
get(withdrawBalanceContract.getOwnerAddress().toByteArray());
5963
long oldBalance = accountCapsule.getBalance();

chainbase/src/main/java/org/tron/core/db/DelegationService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import java.util.ArrayList;
55
import java.util.Comparator;
66
import java.util.List;
7+
import lombok.Getter;
78
import lombok.Setter;
89
import lombok.extern.slf4j.Slf4j;
910
import org.apache.commons.collections4.CollectionUtils;
1011
import org.spongycastle.util.encoders.Hex;
1112
import org.springframework.stereotype.Component;
13+
import org.tron.common.parameter.CommonParameter;
1214
import org.tron.common.utils.StringUtil;
1315
import org.tron.core.capsule.AccountCapsule;
1416
import org.tron.core.capsule.WitnessCapsule;
@@ -28,6 +30,7 @@ public class DelegationService {
2830
private WitnessStore witnessStore;
2931

3032
@Setter
33+
@Getter
3134
private DelegationStore delegationStore;
3235

3336
@Setter
@@ -64,6 +67,8 @@ public void payStandbyWitness() {
6467
double eachVotePay = (double) totalPay / voteSum;
6568
long pay = (long) (getWitnesseByAddress(b).getVoteCount() * eachVotePay);
6669
logger.debug("pay {} stand reward {}", Hex.toHexString(b.toByteArray()), pay);
70+
delegationStore.addVodeReward(dynamicPropertiesStore
71+
.getCurrentCycleNumber(), b.toByteArray(), pay);
6772
payReward(b.toByteArray(), pay);
6873
}
6974
}
@@ -72,6 +77,8 @@ public void payStandbyWitness() {
7277

7378
public void payBlockReward(byte[] witnessAddress, long value) {
7479
logger.debug("pay {} block reward {}", Hex.toHexString(witnessAddress), value);
80+
long cycle = dynamicPropertiesStore.getCurrentCycleNumber();
81+
delegationStore.addBlockReward(cycle, witnessAddress, value);
7582
payReward(witnessAddress, value);
7683
}
7784

@@ -117,6 +124,7 @@ public void withdrawReward(byte[] address) {
117124
//
118125
endCycle = currentCycle;
119126
if (CollectionUtils.isEmpty(accountCapsule.getVotesList())) {
127+
delegationStore.setRemark(endCycle, address);
120128
delegationStore.setBeginCycle(address, endCycle + 1);
121129
return;
122130
}
@@ -225,5 +233,4 @@ private void sortWitness(List<ByteString> list) {
225233
list.sort(Comparator.comparingLong((ByteString b) -> getWitnesseByAddress(b).getVoteCount())
226234
.reversed().thenComparing(Comparator.comparingInt(ByteString::hashCode).reversed()));
227235
}
228-
229236
}

chainbase/src/main/java/org/tron/core/db2/core/RevokingDBWithCachingOldValue.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.Objects;
66
import java.util.Set;
77
import lombok.Getter;
8+
import lombok.extern.slf4j.Slf4j;
9+
import lombok.extern.slf4j.XSlf4j;
810
import org.apache.commons.lang3.ArrayUtils;
911
import org.iq80.leveldb.WriteOptions;
1012
import org.tron.common.parameter.CommonParameter;
@@ -14,7 +16,7 @@
1416
import org.tron.core.db.RevokingStore;
1517
import org.tron.core.db2.common.IRevokingDB;
1618
import org.tron.core.exception.ItemNotFoundException;
17-
19+
@Slf4j
1820
public class RevokingDBWithCachingOldValue implements IRevokingDB {
1921

2022
private AbstractRevokingStore revokingDatabase;

chainbase/src/main/java/org/tron/core/store/DelegationStore.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.tron.core.store;
22

3+
import lombok.extern.slf4j.Slf4j;
34
import org.apache.commons.lang3.ArrayUtils;
45
import org.spongycastle.util.encoders.Hex;
56
import org.springframework.beans.factory.annotation.Autowired;
@@ -10,6 +11,7 @@
1011
import org.tron.core.capsule.BytesCapsule;
1112
import org.tron.core.db.TronStoreWithRevoking;
1213

14+
@Slf4j
1315
@Component
1416
public class DelegationStore extends TronStoreWithRevoking<BytesCapsule> {
1517

@@ -27,6 +29,48 @@ public BytesCapsule get(byte[] key) {
2729
return ArrayUtils.isEmpty(value) ? null : new BytesCapsule(value);
2830
}
2931

32+
public void addBlockReward(long cycle, byte[] address, long value) {
33+
byte[] key = buildRewardBlockKey(cycle, address);
34+
BytesCapsule bytesCapsule = get(key);
35+
36+
if (bytesCapsule == null) {
37+
put(key, new BytesCapsule(ByteArray.fromLong(value)));
38+
} else {
39+
put(key, new BytesCapsule(ByteArray
40+
.fromLong(ByteArray.toLong(bytesCapsule.getData()) + value)));
41+
}
42+
}
43+
44+
public long getBlockReward(long cycle, byte[] address) {
45+
BytesCapsule bytesCapsule = get(buildRewardBlockKey(cycle, address));
46+
if (bytesCapsule == null) {
47+
return 0L;
48+
} else {
49+
return ByteArray.toLong(bytesCapsule.getData());
50+
}
51+
}
52+
53+
public void addVodeReward(long cycle, byte[] address, long value) {
54+
byte[] key = buildRewardVoteKey(cycle, address);
55+
BytesCapsule bytesCapsule = get(key);
56+
57+
if (bytesCapsule == null) {
58+
put(key, new BytesCapsule(ByteArray.fromLong(value)));
59+
} else {
60+
put(key, new BytesCapsule(ByteArray
61+
.fromLong(ByteArray.toLong(bytesCapsule.getData()) + value)));
62+
}
63+
}
64+
65+
public long getVoteReward(long cycle, byte[] address) {
66+
BytesCapsule bytesCapsule = get(buildRewardVoteKey(cycle, address));
67+
if (bytesCapsule == null) {
68+
return 0L;
69+
} else {
70+
return ByteArray.toLong(bytesCapsule.getData());
71+
}
72+
}
73+
3074
public void addReward(long cycle, byte[] address, long value) {
3175
byte[] key = buildRewardKey(cycle, address);
3276
BytesCapsule bytesCapsule = get(key);
@@ -51,6 +95,23 @@ public void setBeginCycle(byte[] address, long number) {
5195
put(address, new BytesCapsule(ByteArray.fromLong(number)));
5296
}
5397

98+
public BytesCapsule getRemark(long cycle, byte[] address) {
99+
return get(buildRemarkKey(cycle, address));
100+
}
101+
102+
public void setLastWithdrawCycle(long cycle, byte[] address) {
103+
put(buildLastWithdrawCycleKey(address), new BytesCapsule(ByteArray.fromLong(cycle)));
104+
}
105+
106+
public long getLastWithdrawCycle(byte[] address) {
107+
BytesCapsule bytesCapsule = get(buildLastWithdrawCycleKey(address));
108+
return bytesCapsule == null ? REMARK : ByteArray.toLong(bytesCapsule.getData());
109+
}
110+
111+
public void setRemark(long cycle, byte[] address) {
112+
put(buildRemarkKey(cycle, address), new BytesCapsule(ByteArray.fromLong(REMARK)));
113+
}
114+
54115
public long getBeginCycle(byte[] address) {
55116
BytesCapsule bytesCapsule = get(address);
56117
return bytesCapsule == null ? 0 : ByteArray.toLong(bytesCapsule.getData());
@@ -120,6 +181,22 @@ private byte[] buildRewardKey(long cycle, byte[] address) {
120181
return (cycle + "-" + Hex.toHexString(address) + "-reward").getBytes();
121182
}
122183

184+
private byte[] buildRewardBlockKey(long cycle, byte[] address) {
185+
return (cycle + "-" + Hex.toHexString(address) + "-block").getBytes();
186+
}
187+
188+
private byte[] buildRewardVoteKey(long cycle, byte[] address) {
189+
return (cycle + "-" + Hex.toHexString(address) + "-reward-vote").getBytes();
190+
}
191+
192+
private byte[] buildRemarkKey(long cycle, byte[] address) {
193+
return (cycle + "-" + Hex.toHexString(address) + "-remark").getBytes();
194+
}
195+
196+
private byte[] buildLastWithdrawCycleKey(byte[] address) {
197+
return ("lastWithdraw-" + Hex.toHexString(address)).getBytes();
198+
}
199+
123200
private byte[] buildAccountVoteKey(long cycle, byte[] address) {
124201
return (cycle + "-" + Hex.toHexString(address) + "-account-vote").getBytes();
125202
}

chainbase/src/main/java/org/tron/core/store/DynamicPropertiesStore.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public class DynamicPropertiesStore extends TronStoreWithRevoking<BytesCapsule>
133133
private static final byte[] CURRENT_CYCLE_NUMBER = "CURRENT_CYCLE_NUMBER".getBytes();
134134
private static final byte[] CHANGE_DELEGATION = "CHANGE_DELEGATION".getBytes();
135135
private static final byte[] ALLOW_PBFT = "ALLOW_PBFT".getBytes();
136+
private static final byte[] CURRENT_CYCLE_TIMESTAMP = "CURRENT_CYCLE_TIMESTAMP".getBytes();
136137

137138
@Autowired
138139
private DynamicPropertiesStore(@Value("properties") String dbName) {
@@ -1811,6 +1812,18 @@ public long getAllowAccountStateRoot() {
18111812
() -> new IllegalArgumentException("not found ALLOW_ACCOUNT_STATE_ROOT"));
18121813
}
18131814

1815+
public void saveCurrentCycleTiimeStamp(long timeStamp) {
1816+
this.put(CURRENT_CYCLE_TIMESTAMP, new BytesCapsule(ByteArray.fromLong(timeStamp)));
1817+
1818+
}
1819+
1820+
public long getCurrentCycleTimeStamp() {
1821+
return Optional.ofNullable(getUnchecked(CURRENT_CYCLE_TIMESTAMP))
1822+
.map(BytesCapsule::getData)
1823+
.map(ByteArray::toLong)
1824+
.orElse(0L);
1825+
}
1826+
18141827
public boolean allowAccountStateRoot() {
18151828
return getAllowAccountStateRoot() == 1;
18161829
}

common/src/main/java/org/tron/common/parameter/CommonParameter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,9 @@ public class CommonParameter {
371371
public boolean solidityNodeHttpEnable = true;
372372
@Getter
373373
@Setter
374+
public boolean nodeHttpStatisticsSRRewardEnable = false;
375+
@Getter
376+
@Setter
374377
public int maxTransactionPendingSize;
375378

376379
@Getter

common/src/main/java/org/tron/core/Constant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public class Constant {
8888
public static final String NODE_HTTP_SOLIDITY_PORT = "node.http.solidityPort";
8989
public static final String NODE_HTTP_FULLNODE_ENABLE = "node.http.fullNodeEnable";
9090
public static final String NODE_HTTP_SOLIDITY_ENABLE = "node.http.solidityEnable";
91+
public static final String NODE_HTTP_STATISTICS_SR_REWARD_SWITCH = "node.http.statisticsSRRewardSwitch";
9192
public static final String NODE_HTTP_PBFT_PORT = "node.http.PBFTPort";
9293

9394
public static final String NODE_RPC_THREAD = "node.rpc.thread";
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.tron.core.exception;
2+
3+
public class AddressNotFound extends StoreException {
4+
5+
public AddressNotFound() {
6+
super();
7+
}
8+
9+
public AddressNotFound(String message) {
10+
super(message);
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.tron.core.exception;
2+
3+
public class InvalidAddress extends StoreException {
4+
5+
public InvalidAddress() {
6+
super();
7+
}
8+
9+
public InvalidAddress(String message) {
10+
super(message);
11+
}
12+
}

consensus/src/main/java/org/tron/consensus/dpos/MaintenanceManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ public void doMaintenance() {
138138
if (dynamicPropertiesStore.allowChangeDelegation()) {
139139
long nextCycle = dynamicPropertiesStore.getCurrentCycleNumber() + 1;
140140
dynamicPropertiesStore.saveCurrentCycleNumber(nextCycle);
141+
dynamicPropertiesStore.saveCurrentCycleTiimeStamp(dynamicPropertiesStore
142+
.getLatestBlockHeaderTimestamp());
141143
consensusDelegate.getAllWitnesses().forEach(witness -> {
142144
delegationStore.setBrokerage(nextCycle, witness.createDbKey(),
143145
delegationStore.getBrokerage(witness.createDbKey()));

0 commit comments

Comments
 (0)