Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	framework/src/main/java/org/tron/program/Version.java
#	framework/src/test/java/org/tron/core/db/ManagerTest.java
  • Loading branch information
halibobo1205 committed Mar 15, 2024
2 parents e81a5aa + b1fc2f0 commit faa9d8d
Show file tree
Hide file tree
Showing 103 changed files with 2,545 additions and 1,633 deletions.
10 changes: 5 additions & 5 deletions actuator/src/main/java/org/tron/core/utils/ProposalUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -733,19 +733,19 @@ public static void validator(DynamicPropertiesStore dynamicPropertiesStore,
throw new ContractValidateException(
"Bad chain parameter id [ALLOW_OLD_REWARD_OPT]");
}
if (dynamicPropertiesStore.allowOldRewardOpt()) {
throw new ContractValidateException(
"[ALLOW_OLD_REWARD_OPT] has been valid, no need to propose again");
}
if (value != 1) {
throw new ContractValidateException(
"This value[ALLOW_OLD_REWARD_OPT] is only allowed to be 1");
}
if (!dynamicPropertiesStore.useNewRewardAlgorithm()) {
throw new ContractValidateException(
"[ALLOW_NEW_REWARD] proposal must be approved "
"[ALLOW_NEW_REWARD] or [ALLOW_TVM_VOTE] proposal must be approved "
+ "before [ALLOW_OLD_REWARD_OPT] can be proposed");
}
if (dynamicPropertiesStore.useNewRewardAlgorithmFromStart()) {
throw new ContractValidateException(
"no need old reward opt, ALLOW_NEW_REWARD from start cycle 1");
}
break;
}
default:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ subprojects {
testAnnotationProcessor 'org.projectlombok:lombok:1.18.12'

testImplementation group: 'junit', name: 'junit', version: '4.13.2'
testImplementation "org.mockito:mockito-core:2.1.0"
testImplementation "org.mockito:mockito-core:2.13.0"
}

task sourcesJar(type: Jar, dependsOn: classes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private boolean passOld(int version) {
private boolean passNew(int version) {
ForkBlockVersionEnum versionEnum = ForkBlockVersionEnum.getForkBlockVersionEnum(version);
if (versionEnum == null) {
logger.error("Not exist block version: {}.", version);
logger.warn("Not exist block version: {}.", version);
return false;
}
long latestBlockTime = manager.getDynamicPropertiesStore().getLatestBlockHeaderTimestamp();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public class TransactionCapsule implements ProtoCapsule<Transaction> {
@Setter
private TransactionTrace trxTrace;

private StringBuilder toStringBuff = new StringBuilder();
@Getter
@Setter
private long time;
Expand Down Expand Up @@ -738,8 +737,7 @@ public Transaction getInstance() {

@Override
public String toString() {

toStringBuff.setLength(0);
StringBuilder toStringBuff = new StringBuilder();
toStringBuff.append("TransactionCapsule \n[ ");

toStringBuff.append("hash=").append(getTransactionId()).append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.Getter;
import net.jcip.annotations.NotThreadSafe;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.utils.Sha256Hash;

@Getter
@NotThreadSafe
public class MerkleTree {

private static volatile MerkleTree instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ public interface DBIterator extends Iterator<Entry<byte[], byte[]>>, AutoCloseab

void seekToLast();

default UnmodifiableIterator<Entry<byte[], byte[]>> prefixQueryAfterThat
(byte[] key, byte[] afterThat) {
this.seek(afterThat == null ? key : afterThat);
return Iterators.filter(this, entry -> Bytes.indexOf(entry.getKey(), key) == 0);
}

/**
* An iterator is either positioned at a key/value pair, or
* not valid. This method returns true iff the iterator is valid.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,12 @@ public boolean valid() {

@Override
public byte[] getKey() {
checkState();
checkValid();
return dbIterator.key();
}

@Override
public byte[] getValue() {
checkState();
checkValid();
return dbIterator.value();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,12 @@ public boolean valid() {

@Override
public byte[] getKey() {
checkState();
checkValid();
return dbIterator.peekNext().getKey();
}

@Override
public byte[] getValue() {
checkState();
checkValid();
return dbIterator.peekNext().getValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.tron.core.store.DelegationStore.DECIMAL_OF_VI_REWARD;
import static org.tron.core.store.DelegationStore.REMARK;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Streams;
import com.google.common.primitives.Bytes;
import com.google.protobuf.ByteString;
Expand All @@ -17,9 +16,7 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -28,9 +25,9 @@
import org.tron.common.es.ExecutorServiceManager;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.MerkleRoot;
import org.tron.common.utils.Pair;
import org.tron.common.utils.Sha256Hash;
import org.tron.core.capsule.utils.MerkleTree;
import org.tron.core.db.common.iterator.DBIterator;
import org.tron.core.db2.common.DB;
import org.tron.core.store.DelegationStore;
Expand All @@ -56,10 +53,11 @@ public class RewardViCalService {

private volatile long lastBlockNumber = -1;

@VisibleForTesting
@Setter
private Sha256Hash rewardViRoot = Sha256Hash.wrap(
ByteString.fromHex("9debcb9924055500aaae98cdee10501c5c39d4daa75800a996f4bdda73dbccd8"));
private static final String MAIN_NET_ROOT_HEX =
"9debcb9924055500aaae98cdee10501c5c39d4daa75800a996f4bdda73dbccd8";

private final Sha256Hash rewardViRoot = CommonParameter.getInstance().getStorage().getDbRoot(
"reward-vi", Sha256Hash.wrap(ByteString.fromHex(MAIN_NET_ROOT_HEX)));

private final CountDownLatch lock = new CountDownLatch(1);

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

@PostConstruct
private void init() {
public void init() {
// after init, we can get the latest block header number from db
this.newRewardCalStartCycle = this.getNewRewardAlgorithmEffectiveCycle();
boolean ret = this.newRewardCalStartCycle != Long.MAX_VALUE;
if (ret) {
// checkpoint is flushed to db, we can start rewardViCalService immediately
lastBlockNumber = Long.MAX_VALUE;
}
es.scheduleWithFixedDelay(this::maybeRun, 0, 3, TimeUnit.SECONDS);
}

Expand All @@ -94,24 +98,30 @@ private boolean isDone() {
}

private void maybeRun() {
if (enableNewRewardAlgorithm()) {
if (this.newRewardCalStartCycle > 1) {
if (isDone()) {
this.clearUp(true);
logger.info("rewardViCalService is already done");
} else {
if (this.getLatestBlockHeaderNumber() > lastBlockNumber) {
// checkpoint is flushed to db, so we can start rewardViCalService
startRewardCal();
clearUp(true);
try {
if (enableNewRewardAlgorithm()) {
if (this.newRewardCalStartCycle > 1) {
if (isDone()) {
this.clearUp(true);
logger.info("rewardViCalService is already done");
} else {
logger.info("startRewardCal will run after checkpoint is flushed to db");
if (lastBlockNumber == Long.MAX_VALUE // start rewardViCalService immediately
|| this.getLatestBlockHeaderNumber() > lastBlockNumber) {
// checkpoint is flushed to db, so we can start rewardViCalService
startRewardCal();
clearUp(true);
} else {
logger.info("startRewardCal will run after checkpoint is flushed to db");
}
}
} else {
clearUp(false);
logger.info("rewardViCalService is no need to run");
}
} else {
clearUp(false);
logger.info("rewardViCalService is no need to run");
}
} catch (Exception e) {
logger.error(" Find fatal error, program will be exited soon.", e);
System.exit(1);
}
}

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

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

Sha256Hash rewardViRootLocal = MerkleTree.getInstance().createTree(ids).getRoot().getHash();
Sha256Hash rewardViRootLocal = MerkleRoot.root(ids);
if (!Objects.equals(rewardViRoot, rewardViRootLocal)) {
logger.error("merkle root mismatch, expect: {}, actual: {}",
rewardViRoot, rewardViRootLocal);
logger.warn("Merkle root mismatch, expect: {}, actual: {}."
+ " If you are quite sure that there is no miscalculation (not on the main network)"
+ ", please configure 'storage.merkleRoot.reward-vi = {}'"
+ "(for a specific network such as Nile, etc.) in config.conf to fix the hints",
rewardViRoot, rewardViRootLocal, rewardViRootLocal);
}
logger.info("calcMerkleRoot: {}", rewardViRootLocal);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2525,10 +2525,6 @@ public boolean useNewRewardAlgorithm() {
return getNewRewardAlgorithmEffectiveCycle() != Long.MAX_VALUE;
}

public boolean useNewRewardAlgorithmFromStart() {
return getNewRewardAlgorithmEffectiveCycle() == 1;
}

public void saveNewRewardAlgorithmEffectiveCycle() {
if (getNewRewardAlgorithmEffectiveCycle() == Long.MAX_VALUE) {
long currentCycle = getCurrentCycleNumber();
Expand Down Expand Up @@ -2839,19 +2835,8 @@ public boolean supportMaxDelegateLockPeriod() {
getUnfreezeDelayDays() > 0;
}

/**
* @require NEW_REWARD_ALGORITHM_EFFECTIVE_CYCLE != Long.MAX_VALUE
* @require NEW_REWARD_ALGORITHM_EFFECTIVE_CYCLE > 1
*/
public void saveAllowOldRewardOpt(long allowOldRewardOpt) {
if (useNewRewardAlgorithm()) {
if (useNewRewardAlgorithmFromStart()) {
throw new IllegalStateException("no need old reward opt, ALLOW_NEW_REWARD from start");
}
this.put(ALLOW_OLD_REWARD_OPT, new BytesCapsule(ByteArray.fromLong(allowOldRewardOpt)));
} else {
throw new IllegalStateException("not support old reward opt, ALLOW_NEW_REWARD not set");
}
this.put(ALLOW_OLD_REWARD_OPT, new BytesCapsule(ByteArray.fromLong(allowOldRewardOpt)));
}

public boolean allowOldRewardOpt() {
Expand Down
2 changes: 1 addition & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ dependencies {
compile 'org.aspectj:aspectjrt:1.8.13'
compile 'org.aspectj:aspectjweaver:1.8.13'
compile 'org.aspectj:aspectjtools:1.8.13'
compile group: 'com.github.tronprotocol', name: 'libp2p', version: 'test-v2.2.1',{
compile group: 'io.github.tronprotocol', name: 'libp2p', version: '2.2.1',{
exclude group: 'io.grpc', module: 'grpc-context'
exclude group: 'io.grpc', module: 'grpc-core'
exclude group: 'io.grpc', module: 'grpc-netty'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ public static class Histogram {
public static final String MESSAGE_PROCESS_LATENCY = "tron:message_process_latency_seconds";
public static final String BLOCK_FETCH_LATENCY = "tron:block_fetch_latency_seconds";
public static final String BLOCK_RECEIVE_DELAY = "tron:block_receive_delay_seconds";
public static final String DO_REWARD_CAL_DELAY = "tron:do_reward_cal_seconds";


private Histogram() {
throw new IllegalStateException("Histogram");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ public class MetricsHistogram {
init(MetricKeys.Histogram.BLOCK_FETCH_LATENCY, "fetch block latency.");
init(MetricKeys.Histogram.BLOCK_RECEIVE_DELAY,
"receive block delay time, receiveTime - blockTime.");
init(MetricKeys.Histogram.DO_REWARD_CAL_DELAY,
"do reward cal delay time.", "depth");
}

private MetricsHistogram() {
Expand Down
68 changes: 68 additions & 0 deletions common/src/main/java/org/tron/common/utils/MerkleRoot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.tron.common.utils;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.Getter;

public class MerkleRoot {

private MerkleRoot() {

}

public static Sha256Hash root(List<Sha256Hash> hashList) {
List<Leaf> leaves = createLeaves(hashList);
while (leaves.size() > 1) {
leaves = createParentLeaves(leaves);
}
return leaves.isEmpty() ? Sha256Hash.ZERO_HASH : leaves.get(0).hash;
}

private static List<Leaf> createParentLeaves(List<Leaf> leaves) {
int step = 2;
int len = leaves.size();
return IntStream.iterate(0, i -> i + step)
.limit(len)
.filter(i -> i < len)
.mapToObj(i -> {
Leaf right = i + 1 < len ? leaves.get(i + 1) : null;
return createLeaf(leaves.get(i), right);
}).collect(Collectors.toList());
}

private static List<Leaf> createLeaves(List<Sha256Hash> hashList) {
int step = 2;
int len = hashList.size();
return IntStream.iterate(0, i -> i + step)
.limit(len)
.filter(i -> i < len)
.mapToObj(i -> {
Leaf right = i + 1 < len ? createLeaf(hashList.get(i + 1)) : null;
return createLeaf(createLeaf(hashList.get(i)), right);
}).collect(Collectors.toList());
}

private static Leaf createLeaf(Leaf left, Leaf right) {
Leaf leaf = new Leaf();
leaf.hash = right == null ? left.hash : computeHash(left.hash, right.hash);
return leaf;
}

private static Leaf createLeaf(Sha256Hash hash) {
Leaf leaf = new Leaf();
leaf.hash = hash;
return leaf;
}

private static Sha256Hash computeHash(Sha256Hash leftHash, Sha256Hash rightHash) {
return Sha256Hash.of(true,
leftHash.getByteString().concat(rightHash.getByteString()).toByteArray());
}

@Getter
private static class Leaf {

private Sha256Hash hash;
}
}
Loading

0 comments on commit faa9d8d

Please sign in to comment.