Skip to content

Commit faa9d8d

Browse files
committed
Merge remote-tracking branch 'upstream/master' into develop
# Conflicts: # framework/src/main/java/org/tron/program/Version.java # framework/src/test/java/org/tron/core/db/ManagerTest.java
2 parents e81a5aa + b1fc2f0 commit faa9d8d

File tree

103 files changed

+2545
-1633
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+2545
-1633
lines changed

actuator/src/main/java/org/tron/core/utils/ProposalUtil.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -733,19 +733,19 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
733733
throw new ContractValidateException(
734734
"Bad chain parameter id [ALLOW_OLD_REWARD_OPT]");
735735
}
736+
if (dynamicPropertiesStore.allowOldRewardOpt()) {
737+
throw new ContractValidateException(
738+
"[ALLOW_OLD_REWARD_OPT] has been valid, no need to propose again");
739+
}
736740
if (value != 1) {
737741
throw new ContractValidateException(
738742
"This value[ALLOW_OLD_REWARD_OPT] is only allowed to be 1");
739743
}
740744
if (!dynamicPropertiesStore.useNewRewardAlgorithm()) {
741745
throw new ContractValidateException(
742-
"[ALLOW_NEW_REWARD] proposal must be approved "
746+
"[ALLOW_NEW_REWARD] or [ALLOW_TVM_VOTE] proposal must be approved "
743747
+ "before [ALLOW_OLD_REWARD_OPT] can be proposed");
744748
}
745-
if (dynamicPropertiesStore.useNewRewardAlgorithmFromStart()) {
746-
throw new ContractValidateException(
747-
"no need old reward opt, ALLOW_NEW_REWARD from start cycle 1");
748-
}
749749
break;
750750
}
751751
default:

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ subprojects {
5555
testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'
5656

5757
testImplementation group: 'junit', name: 'junit', version: '4.13.2'
58-
testImplementation "org.mockito:mockito-core:2.1.0"
58+
testImplementation "org.mockito:mockito-core:2.13.0"
5959
}
6060

6161
task sourcesJar(type: Jar, dependsOn: classes) {

chainbase/src/main/java/org/tron/common/utils/ForkController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private boolean passOld(int version) {
7878
private boolean passNew(int version) {
7979
ForkBlockVersionEnum versionEnum = ForkBlockVersionEnum.getForkBlockVersionEnum(version);
8080
if (versionEnum == null) {
81-
logger.error("Not exist block version: {}.", version);
81+
logger.warn("Not exist block version: {}.", version);
8282
return false;
8383
}
8484
long latestBlockTime = manager.getDynamicPropertiesStore().getLatestBlockHeaderTimestamp();

chainbase/src/main/java/org/tron/core/capsule/TransactionCapsule.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ public class TransactionCapsule implements ProtoCapsule<Transaction> {
102102
@Setter
103103
private TransactionTrace trxTrace;
104104

105-
private StringBuilder toStringBuff = new StringBuilder();
106105
@Getter
107106
@Setter
108107
private long time;
@@ -738,8 +737,7 @@ public Transaction getInstance() {
738737

739738
@Override
740739
public String toString() {
741-
742-
toStringBuff.setLength(0);
740+
StringBuilder toStringBuff = new StringBuilder();
743741
toStringBuff.append("TransactionCapsule \n[ ");
744742

745743
toStringBuff.append("hash=").append(getTransactionId()).append("\n");

chainbase/src/main/java/org/tron/core/capsule/utils/MerkleTree.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import java.util.stream.Collectors;
66
import java.util.stream.IntStream;
77
import lombok.Getter;
8+
import net.jcip.annotations.NotThreadSafe;
89
import org.tron.common.parameter.CommonParameter;
910
import org.tron.common.utils.Sha256Hash;
1011

1112
@Getter
13+
@NotThreadSafe
1214
public class MerkleTree {
1315

1416
private static volatile MerkleTree instance;

chainbase/src/main/java/org/tron/core/db/common/iterator/DBIterator.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ public interface DBIterator extends Iterator<Entry<byte[], byte[]>>, AutoCloseab
1616

1717
void seekToLast();
1818

19-
default UnmodifiableIterator<Entry<byte[], byte[]>> prefixQueryAfterThat
20-
(byte[] key, byte[] afterThat) {
21-
this.seek(afterThat == null ? key : afterThat);
22-
return Iterators.filter(this, entry -> Bytes.indexOf(entry.getKey(), key) == 0);
23-
}
24-
2519
/**
2620
* An iterator is either positioned at a key/value pair, or
2721
* not valid. This method returns true iff the iterator is valid.

chainbase/src/main/java/org/tron/core/db/common/iterator/RockStoreIterator.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,12 @@ public boolean valid() {
108108

109109
@Override
110110
public byte[] getKey() {
111-
checkState();
112111
checkValid();
113112
return dbIterator.key();
114113
}
115114

116115
@Override
117116
public byte[] getValue() {
118-
checkState();
119117
checkValid();
120118
return dbIterator.value();
121119
}

chainbase/src/main/java/org/tron/core/db/common/iterator/StoreIterator.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,12 @@ public boolean valid() {
9393

9494
@Override
9595
public byte[] getKey() {
96-
checkState();
9796
checkValid();
9897
return dbIterator.peekNext().getKey();
9998
}
10099

101100
@Override
102101
public byte[] getValue() {
103-
checkState();
104102
checkValid();
105103
return dbIterator.peekNext().getValue();
106104
}

chainbase/src/main/java/org/tron/core/service/RewardViCalService.java

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static org.tron.core.store.DelegationStore.DECIMAL_OF_VI_REWARD;
44
import static org.tron.core.store.DelegationStore.REMARK;
55

6-
import com.google.common.annotations.VisibleForTesting;
76
import com.google.common.collect.Streams;
87
import com.google.common.primitives.Bytes;
98
import com.google.protobuf.ByteString;
@@ -17,9 +16,7 @@
1716
import java.util.concurrent.TimeUnit;
1817
import java.util.stream.Collectors;
1918
import java.util.stream.LongStream;
20-
import javax.annotation.PostConstruct;
2119
import javax.annotation.PreDestroy;
22-
import lombok.Setter;
2320
import lombok.extern.slf4j.Slf4j;
2421
import org.bouncycastle.util.encoders.Hex;
2522
import org.springframework.beans.factory.annotation.Autowired;
@@ -28,9 +25,9 @@
2825
import org.tron.common.es.ExecutorServiceManager;
2926
import org.tron.common.parameter.CommonParameter;
3027
import org.tron.common.utils.ByteArray;
28+
import org.tron.common.utils.MerkleRoot;
3129
import org.tron.common.utils.Pair;
3230
import org.tron.common.utils.Sha256Hash;
33-
import org.tron.core.capsule.utils.MerkleTree;
3431
import org.tron.core.db.common.iterator.DBIterator;
3532
import org.tron.core.db2.common.DB;
3633
import org.tron.core.store.DelegationStore;
@@ -56,10 +53,11 @@ public class RewardViCalService {
5653

5754
private volatile long lastBlockNumber = -1;
5855

59-
@VisibleForTesting
60-
@Setter
61-
private Sha256Hash rewardViRoot = Sha256Hash.wrap(
62-
ByteString.fromHex("9debcb9924055500aaae98cdee10501c5c39d4daa75800a996f4bdda73dbccd8"));
56+
private static final String MAIN_NET_ROOT_HEX =
57+
"9debcb9924055500aaae98cdee10501c5c39d4daa75800a996f4bdda73dbccd8";
58+
59+
private final Sha256Hash rewardViRoot = CommonParameter.getInstance().getStorage().getDbRoot(
60+
"reward-vi", Sha256Hash.wrap(ByteString.fromHex(MAIN_NET_ROOT_HEX)));
6361

6462
private final CountDownLatch lock = new CountDownLatch(1);
6563

@@ -75,8 +73,14 @@ public RewardViCalService(@Autowired DynamicPropertiesStore propertiesStore,
7573
this.witnessStore = witnessStore.getDb();
7674
}
7775

78-
@PostConstruct
79-
private void init() {
76+
public void init() {
77+
// after init, we can get the latest block header number from db
78+
this.newRewardCalStartCycle = this.getNewRewardAlgorithmEffectiveCycle();
79+
boolean ret = this.newRewardCalStartCycle != Long.MAX_VALUE;
80+
if (ret) {
81+
// checkpoint is flushed to db, we can start rewardViCalService immediately
82+
lastBlockNumber = Long.MAX_VALUE;
83+
}
8084
es.scheduleWithFixedDelay(this::maybeRun, 0, 3, TimeUnit.SECONDS);
8185
}
8286

@@ -94,24 +98,30 @@ private boolean isDone() {
9498
}
9599

96100
private void maybeRun() {
97-
if (enableNewRewardAlgorithm()) {
98-
if (this.newRewardCalStartCycle > 1) {
99-
if (isDone()) {
100-
this.clearUp(true);
101-
logger.info("rewardViCalService is already done");
102-
} else {
103-
if (this.getLatestBlockHeaderNumber() > lastBlockNumber) {
104-
// checkpoint is flushed to db, so we can start rewardViCalService
105-
startRewardCal();
106-
clearUp(true);
101+
try {
102+
if (enableNewRewardAlgorithm()) {
103+
if (this.newRewardCalStartCycle > 1) {
104+
if (isDone()) {
105+
this.clearUp(true);
106+
logger.info("rewardViCalService is already done");
107107
} else {
108-
logger.info("startRewardCal will run after checkpoint is flushed to db");
108+
if (lastBlockNumber == Long.MAX_VALUE // start rewardViCalService immediately
109+
|| this.getLatestBlockHeaderNumber() > lastBlockNumber) {
110+
// checkpoint is flushed to db, so we can start rewardViCalService
111+
startRewardCal();
112+
clearUp(true);
113+
} else {
114+
logger.info("startRewardCal will run after checkpoint is flushed to db");
115+
}
109116
}
117+
} else {
118+
clearUp(false);
119+
logger.info("rewardViCalService is no need to run");
110120
}
111-
} else {
112-
clearUp(false);
113-
logger.info("rewardViCalService is no need to run");
114121
}
122+
} catch (Exception e) {
123+
logger.error(" Find fatal error, program will be exited soon.", e);
124+
System.exit(1);
115125
}
116126
}
117127

@@ -131,7 +141,7 @@ private void destroy() {
131141

132142
public long getNewRewardAlgorithmReward(long beginCycle, long endCycle,
133143
List<Pair<byte[], Long>> votes) {
134-
if (!rewardViStore.has(IS_DONE_KEY)) {
144+
if (!isDone()) {
135145
logger.warn("rewardViCalService is not done, wait for it");
136146
try {
137147
lock.await();
@@ -168,10 +178,13 @@ private void calcMerkleRoot() {
168178
.map(this::getHash)
169179
.collect(Collectors.toCollection(ArrayList::new));
170180

171-
Sha256Hash rewardViRootLocal = MerkleTree.getInstance().createTree(ids).getRoot().getHash();
181+
Sha256Hash rewardViRootLocal = MerkleRoot.root(ids);
172182
if (!Objects.equals(rewardViRoot, rewardViRootLocal)) {
173-
logger.error("merkle root mismatch, expect: {}, actual: {}",
174-
rewardViRoot, rewardViRootLocal);
183+
logger.warn("Merkle root mismatch, expect: {}, actual: {}."
184+
+ " If you are quite sure that there is no miscalculation (not on the main network)"
185+
+ ", please configure 'storage.merkleRoot.reward-vi = {}'"
186+
+ "(for a specific network such as Nile, etc.) in config.conf to fix the hints",
187+
rewardViRoot, rewardViRootLocal, rewardViRootLocal);
175188
}
176189
logger.info("calcMerkleRoot: {}", rewardViRootLocal);
177190
}

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2525,10 +2525,6 @@ public boolean useNewRewardAlgorithm() {
25252525
return getNewRewardAlgorithmEffectiveCycle() != Long.MAX_VALUE;
25262526
}
25272527

2528-
public boolean useNewRewardAlgorithmFromStart() {
2529-
return getNewRewardAlgorithmEffectiveCycle() == 1;
2530-
}
2531-
25322528
public void saveNewRewardAlgorithmEffectiveCycle() {
25332529
if (getNewRewardAlgorithmEffectiveCycle() == Long.MAX_VALUE) {
25342530
long currentCycle = getCurrentCycleNumber();
@@ -2839,19 +2835,8 @@ public boolean supportMaxDelegateLockPeriod() {
28392835
getUnfreezeDelayDays() > 0;
28402836
}
28412837

2842-
/**
2843-
* @require NEW_REWARD_ALGORITHM_EFFECTIVE_CYCLE != Long.MAX_VALUE
2844-
* @require NEW_REWARD_ALGORITHM_EFFECTIVE_CYCLE > 1
2845-
*/
28462838
public void saveAllowOldRewardOpt(long allowOldRewardOpt) {
2847-
if (useNewRewardAlgorithm()) {
2848-
if (useNewRewardAlgorithmFromStart()) {
2849-
throw new IllegalStateException("no need old reward opt, ALLOW_NEW_REWARD from start");
2850-
}
2851-
this.put(ALLOW_OLD_REWARD_OPT, new BytesCapsule(ByteArray.fromLong(allowOldRewardOpt)));
2852-
} else {
2853-
throw new IllegalStateException("not support old reward opt, ALLOW_NEW_REWARD not set");
2854-
}
2839+
this.put(ALLOW_OLD_REWARD_OPT, new BytesCapsule(ByteArray.fromLong(allowOldRewardOpt)));
28552840
}
28562841

28572842
public boolean allowOldRewardOpt() {

0 commit comments

Comments
 (0)