Skip to content

Commit 942981f

Browse files
authored
Merge pull request #5556 from tronprotocol/master
merge master into develop
2 parents ba20e25 + 440d062 commit 942981f

File tree

82 files changed

+2872
-255
lines changed

Some content is hidden

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

82 files changed

+2872
-255
lines changed

actuator/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jacocoTestReport {
5252
xml.enabled = true
5353
html.enabled = true
5454
}
55-
executionData.from = '../framework/build/jacoco/jacocoTest.exec'
55+
getExecutionData().setFrom(fileTree('../framework/build/jacoco').include("**.exec"))
5656
afterEvaluate {
5757
classDirectories.from = classDirectories.files.collect {
5858
fileTree(dir: it,)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public boolean validate() throws ContractValidateException {
189189

190190
byte[] receiverAddress = delegateResourceContract.getReceiverAddress().toByteArray();
191191

192-
if (ArrayUtils.isEmpty(receiverAddress) || !DecodeUtil.addressValid(receiverAddress)) {
192+
if (!DecodeUtil.addressValid(receiverAddress)) {
193193
throw new ContractValidateException("Invalid receiverAddress");
194194
}
195195

actuator/src/main/java/org/tron/core/vm/nativecontract/DelegateResourceProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void validate(DelegateResourceParam param, Repository repo) throws Contra
9393

9494
byte[] receiverAddress = param.getReceiverAddress();
9595

96-
if (ArrayUtils.isEmpty(receiverAddress) || !DecodeUtil.addressValid(receiverAddress)) {
96+
if (!DecodeUtil.addressValid(receiverAddress)) {
9797
throw new ContractValidateException("Invalid receiverAddress");
9898
}
9999
if (Arrays.equals(receiverAddress, ownerAddress)) {

chainbase/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ jacocoTestReport {
8080
xml.enabled = true
8181
html.enabled = true
8282
}
83-
executionData.from = '../framework/build/jacoco/jacocoTest.exec'
83+
getExecutionData().setFrom(fileTree('../framework/build/jacoco').include("**.exec"))
8484
afterEvaluate {
8585
classDirectories.from = classDirectories.files.collect {
8686
fileTree(dir: it,)

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
import lombok.extern.slf4j.Slf4j;
44
import org.springframework.beans.factory.annotation.Autowired;
55
import org.springframework.beans.factory.annotation.Value;
6+
import org.springframework.stereotype.Component;
67
import org.tron.core.capsule.BytesCapsule;
78
import org.tron.core.db2.common.TxCacheDB;
9+
import org.tron.core.store.DynamicPropertiesStore;
810

911
@Slf4j
12+
@Component
1013
public class TransactionCache extends TronStoreWithRevoking<BytesCapsule> {
1114

1215
@Autowired
1316
public TransactionCache(@Value("trans-cache") String dbName,
14-
RecentTransactionStore recentTransactionStore) {
15-
super(new TxCacheDB(dbName, recentTransactionStore));
17+
@Autowired RecentTransactionStore recentTransactionStore,
18+
@Autowired DynamicPropertiesStore dynamicPropertiesStore) {
19+
super(new TxCacheDB(dbName, recentTransactionStore, dynamicPropertiesStore));
1620
}
1721

1822
public void initCache() {

chainbase/src/main/java/org/tron/core/db2/common/TxCacheDB.java

Lines changed: 94 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import com.google.common.hash.BloomFilter;
44
import com.google.common.hash.Funnels;
5+
import com.google.common.hash.HashCode;
6+
import com.google.common.hash.Hashing;
7+
import com.google.common.io.ByteSource;
58
import com.google.common.primitives.Longs;
69
import java.io.BufferedInputStream;
710
import java.io.BufferedOutputStream;
11+
import java.io.IOException;
812
import java.io.InputStream;
913
import java.io.InputStreamReader;
1014
import java.io.OutputStream;
@@ -18,6 +22,7 @@
1822
import java.util.Iterator;
1923
import java.util.Map;
2024
import java.util.Map.Entry;
25+
import java.util.Objects;
2126
import java.util.Properties;
2227
import java.util.concurrent.CompletableFuture;
2328
import java.util.concurrent.atomic.AtomicBoolean;
@@ -40,6 +45,7 @@
4045
import org.tron.core.db.RecentTransactionItem;
4146
import org.tron.core.db.RecentTransactionStore;
4247
import org.tron.core.db.common.iterator.DBIterator;
48+
import org.tron.core.store.DynamicPropertiesStore;
4349

4450
@Slf4j(topic = "DB")
4551
public class TxCacheDB implements DB<byte[], byte[]>, Flusher {
@@ -59,7 +65,6 @@ public class TxCacheDB implements DB<byte[], byte[]>, Flusher {
5965
private BloomFilter<byte[]>[] bloomFilters = new BloomFilter[2];
6066
// filterStartBlock record the start block of the active filter
6167
private volatile long filterStartBlock = INVALID_BLOCK;
62-
private volatile long currentBlockNum = INVALID_BLOCK;
6368
// currentFilterIndex records the index of the active filter
6469
private volatile int currentFilterIndex = 0;
6570

@@ -75,21 +80,28 @@ public class TxCacheDB implements DB<byte[], byte[]>, Flusher {
7580
// replace persistentStore and optimizes startup performance
7681
private RecentTransactionStore recentTransactionStore;
7782

83+
private DynamicPropertiesStore dynamicPropertiesStore;
84+
7885
private final Path cacheFile0;
7986
private final Path cacheFile1;
87+
private String crc32c0;
88+
private String crc32c1;
8089
private final Path cacheProperties;
8190
private final Path cacheDir;
8291
private AtomicBoolean isValid = new AtomicBoolean(false);
92+
private boolean txCacheInitOptimization;
8393

8494
@Getter
8595
@Setter
8696
private volatile boolean alive;
8797

88-
public TxCacheDB(String name, RecentTransactionStore recentTransactionStore) {
98+
public TxCacheDB(String name, RecentTransactionStore recentTransactionStore,
99+
DynamicPropertiesStore dynamicPropertiesStore) {
89100
this.name = name;
90101
this.TRANSACTION_COUNT =
91102
CommonParameter.getInstance().getStorage().getEstimatedBlockTransactions();
92103
this.recentTransactionStore = recentTransactionStore;
104+
this.dynamicPropertiesStore = dynamicPropertiesStore;
93105
String dbEngine = CommonParameter.getInstance().getStorage().getDbEngine();
94106
if ("LEVELDB".equals(dbEngine.toUpperCase())) {
95107
this.persistentStore = new LevelDB(
@@ -117,6 +129,8 @@ public TxCacheDB(String name, RecentTransactionStore recentTransactionStore) {
117129
this.cacheFile0 = Paths.get(cacheDir.toString(), "bloomFilters_0");
118130
this.cacheFile1 = Paths.get(cacheDir.toString(), "bloomFilters_1");
119131
this.cacheProperties = Paths.get(cacheDir.toString(), "txCache.properties");
132+
this.txCacheInitOptimization = CommonParameter.getInstance()
133+
.getStorage().isTxCacheInitOptimization();
120134

121135
}
122136

@@ -211,7 +225,6 @@ public void put(byte[] key, byte[] value) {
211225
MAX_BLOCK_SIZE * TRANSACTION_COUNT);
212226
}
213227
bloomFilters[currentFilterIndex].put(key);
214-
currentBlockNum = blockNum;
215228
if (lastMetricBlock != blockNum) {
216229
lastMetricBlock = blockNum;
217230
Metrics.gaugeSet(MetricKeys.Gauge.TX_CACHE,
@@ -270,6 +283,12 @@ public void reset() {
270283
}
271284

272285
private boolean recovery() {
286+
if (!txCacheInitOptimization) {
287+
logger.info("txCache init optimization is disabled, skip fast recovery mode.");
288+
logger.info("If you want fast recovery mode,"
289+
+ " please set `storage.txCache.initOptimization = true` in config.conf.");
290+
return false;
291+
}
273292
FileUtil.createDirIfNotExists(this.cacheDir.toString());
274293
logger.info("recovery bloomFilters start.");
275294
CompletableFuture<Boolean> loadProperties = CompletableFuture.supplyAsync(this::loadProperties);
@@ -278,13 +297,18 @@ private boolean recovery() {
278297
CompletableFuture<Boolean> tk1 = loadProperties.thenApplyAsync(
279298
v -> recovery(1, this.cacheFile1));
280299

281-
return CompletableFuture.allOf(tk0, tk1).thenApply(v -> {
282-
logger.info("recovery bloomFilters success.");
283-
return true;
284-
}).exceptionally(this::handleException).join();
300+
try {
301+
return CompletableFuture.allOf(tk0, tk1).thenApply(v -> {
302+
logger.info("recovery bloomFilters success.");
303+
return true;
304+
}).exceptionally(this::handleException).join();
305+
} finally {
306+
clearCrc32c();
307+
}
285308
}
286309

287310
private boolean recovery(int index, Path file) {
311+
checkCrc32c(index, file);
288312
try (InputStream in = new BufferedInputStream(Files.newInputStream(file,
289313
StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE))) {
290314
logger.info("recovery bloomFilter[{}] from file.", index);
@@ -326,24 +350,38 @@ private void dump() {
326350
() -> dump(0, this.cacheFile0));
327351
CompletableFuture<Void> task1 = CompletableFuture.runAsync(
328352
() -> dump(1, this.cacheFile1));
329-
CompletableFuture.allOf(task0, task1).thenRun(() -> {
330-
writeProperties();
331-
logger.info("dump bloomFilters done.");
332-
333-
}).exceptionally(e -> {
334-
logger.info("dump bloomFilters to file failed. {}", e.getMessage());
335-
return null;
336-
}).join();
353+
try {
354+
CompletableFuture.allOf(task0, task1).thenRun(() -> {
355+
writeProperties();
356+
logger.info("dump bloomFilters done.");
357+
}).exceptionally(e -> {
358+
logger.info("dump bloomFilters to file failed. {}", e.getMessage());
359+
return null;
360+
}).join();
361+
} finally {
362+
clearCrc32c();
363+
}
337364
}
338365

339366
private void dump(int index, Path file) {
367+
logger.info("dump bloomFilters[{}] to file.", index);
368+
long start = System.currentTimeMillis();
340369
try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(file))) {
341-
logger.info("dump bloomFilters[{}] to file.", index);
342-
long start = System.currentTimeMillis();
343370
bloomFilters[index].writeTo(out);
344-
logger.info("dump bloomFilters[{}] to file done,filter: {}, filter-fpp: {}, cost {} ms.",
371+
} catch (Exception e) {
372+
throw new RuntimeException(e);
373+
}
374+
try {
375+
String crc32c = getCrc32c(file);
376+
if (index == 0) {
377+
this.crc32c0 = crc32c;
378+
} else {
379+
this.crc32c1 = crc32c;
380+
}
381+
logger.info("dump bloomFilters[{}] to file done,filter: {}, filter-fpp: {}, "
382+
+ "crc32c: {}, cost {} ms.",
345383
index, bloomFilters[index].approximateElementCount(), bloomFilters[index].expectedFpp(),
346-
System.currentTimeMillis() - start);
384+
crc32c, System.currentTimeMillis() - start);
347385
} catch (Exception e) {
348386
throw new RuntimeException(e);
349387
}
@@ -356,8 +394,16 @@ private boolean loadProperties() {
356394
Properties properties = new Properties();
357395
properties.load(r);
358396
filterStartBlock = Long.parseLong(properties.getProperty("filterStartBlock"));
359-
currentBlockNum = Long.parseLong(properties.getProperty("currentBlockNum"));
397+
long currentBlockNum = Long.parseLong(properties.getProperty("currentBlockNum"));
398+
long currentBlockNumFromDB = dynamicPropertiesStore.getLatestBlockHeaderNumberFromDB();
360399
currentFilterIndex = Integer.parseInt(properties.getProperty("currentFilterIndex"));
400+
if (currentBlockNum != currentBlockNumFromDB) {
401+
throw new IllegalStateException(
402+
String.format("currentBlockNum not match. filter: %d, db: %d",
403+
currentBlockNum, currentBlockNumFromDB));
404+
}
405+
this.crc32c0 = properties.getProperty("crc32c0");
406+
this.crc32c1 = properties.getProperty("crc32c1");
361407
logger.info("filterStartBlock: {}, currentBlockNum: {}, currentFilterIndex: {}, load done.",
362408
filterStartBlock, currentBlockNum, currentFilterIndex);
363409
return true;
@@ -369,9 +415,12 @@ private boolean loadProperties() {
369415
private void writeProperties() {
370416
try (Writer w = Files.newBufferedWriter(this.cacheProperties, StandardCharsets.UTF_8)) {
371417
Properties properties = new Properties();
418+
long currentBlockNum = dynamicPropertiesStore.getLatestBlockHeaderNumberFromDB();
372419
properties.setProperty("filterStartBlock", String.valueOf(filterStartBlock));
373420
properties.setProperty("currentBlockNum", String.valueOf(currentBlockNum));
374421
properties.setProperty("currentFilterIndex", String.valueOf(currentFilterIndex));
422+
properties.setProperty("crc32c0", this.crc32c0);
423+
properties.setProperty("crc32c1", this.crc32c1);
375424
properties.store(w, "Generated by the application. PLEASE DO NOT EDIT! ");
376425
logger.info("filterStartBlock: {}, currentBlockNum: {}, currentFilterIndex: {}, write done.",
377426
filterStartBlock, currentBlockNum, currentFilterIndex);
@@ -380,9 +429,33 @@ private void writeProperties() {
380429
}
381430
}
382431

432+
private String getCrc32c(Path file) throws IOException {
433+
ByteSource byteSource = com.google.common.io.Files.asByteSource(file.toFile());
434+
HashCode hc = byteSource.hash(Hashing.crc32c());
435+
return hc.toString();
436+
}
437+
438+
private void checkCrc32c(int index, Path file) {
439+
try {
440+
String actual = getCrc32c(file);
441+
String expect = index == 0 ? this.crc32c0 : this.crc32c1;
442+
if (!Objects.equals(actual, expect)) {
443+
throw new IllegalStateException("crc32c not match. index: " + index + ", expect: " + expect
444+
+ ", actual: " + actual);
445+
}
446+
} catch (Exception e) {
447+
throw new RuntimeException(e);
448+
}
449+
}
450+
451+
private void clearCrc32c() {
452+
this.crc32c0 = null;
453+
this.crc32c1 = null;
454+
}
455+
383456
@Override
384457
public TxCacheDB newInstance() {
385-
return new TxCacheDB(name, recentTransactionStore);
458+
return new TxCacheDB(name, recentTransactionStore, dynamicPropertiesStore);
386459
}
387460

388461
@Override

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

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

33
import com.google.protobuf.InvalidProtocolBufferException;
4+
import lombok.extern.slf4j.Slf4j;
45
import org.springframework.beans.factory.annotation.Autowired;
56
import org.tron.core.db.TronDatabase;
67
import org.tron.core.exception.BadItemException;
@@ -9,6 +10,7 @@
910
import java.util.Spliterator;
1011
import java.util.function.Consumer;
1112

13+
@Slf4j(topic = "DB")
1214
public class CheckPointV2Store extends TronDatabase<byte[]> {
1315

1416
@Autowired
@@ -50,4 +52,19 @@ public Spliterator spliterator() {
5052
protected void init() {
5153
}
5254

55+
/**
56+
* close the database.
57+
*/
58+
@Override
59+
public void close() {
60+
logger.debug("******** Begin to close {}. ********", getName());
61+
try {
62+
dbSource.closeDB();
63+
} catch (Exception e) {
64+
logger.warn("Failed to close {}.", getName(), e);
65+
} finally {
66+
logger.debug("******** End to close {}. ********", getName());
67+
}
68+
}
69+
5370
}

common/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ dependencies {
5353
compile 'org.aspectj:aspectjrt:1.8.13'
5454
compile 'org.aspectj:aspectjweaver:1.8.13'
5555
compile 'org.aspectj:aspectjtools:1.8.13'
56-
compile group: 'com.github.tronprotocol', name: 'libp2p', version: 'test-v2.0.2',{
56+
compile group: 'io.github.tronprotocol', name: 'libp2p', version: '2.1.0',{
5757
exclude group: 'io.grpc', module: 'grpc-context'
5858
exclude group: 'io.grpc', module: 'grpc-core'
5959
exclude group: 'io.grpc', module: 'grpc-netty'
@@ -68,7 +68,7 @@ jacocoTestReport {
6868
xml.enabled = true
6969
html.enabled = true
7070
}
71-
executionData.from = '../framework/build/jacoco/jacocoTest.exec'
71+
getExecutionData().setFrom(fileTree('../framework/build/jacoco').include("**.exec"))
7272
afterEvaluate {
7373
classDirectories.from = classDirectories.files.collect {
7474
fileTree(dir: it,)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ public class CommonParameter {
415415
@Setter
416416
public int rateLimiterGlobalIpQps;
417417
@Getter
418+
public int rateLimiterGlobalApiQps;
419+
@Getter
418420
public DbBackupConfig dbBackupConfig;
419421
@Getter
420422
public RocksDbSettings rocksDBCustomSettings;
@@ -516,6 +518,9 @@ public class CommonParameter {
516518
public int pBFTHttpPort;
517519
@Getter
518520
@Setter
521+
public long pBFTExpireNum;
522+
@Getter
523+
@Setter
519524
public long oldSolidityBlockNum = -1;
520525

521526
@Getter/**/

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ public class Constant {
256256

257257
public static final String RATE_LIMITER_GLOBAL_IP_QPS = "rate.limiter.global.ip.qps";
258258

259+
public static final String RATE_LIMITER_GLOBAL_API_QPS = "rate.limiter.global.api.qps";
260+
259261
public static final String COMMITTEE_CHANGED_DELEGATION = "committee.changedDelegation";
260262

261263
public static final String CRYPTO_ENGINE = "crypto.engine";
@@ -301,6 +303,7 @@ public class Constant {
301303
public static final String SEED_NODE_IP_LIST = "seed.node.ip.list";
302304
public static final String NODE_METRICS_ENABLE = "node.metricsEnable";
303305
public static final String COMMITTEE_ALLOW_PBFT = "committee.allowPBFT";
306+
public static final String COMMITTEE_PBFT_EXPIRE_NUM = "committee.pBFTExpireNum";
304307
public static final String NODE_AGREE_NODE_COUNT = "node.agreeNodeCount";
305308

306309
public static final String COMMITTEE_ALLOW_TRANSACTION_FEE_POOL = "committee.allowTransactionFeePool";

0 commit comments

Comments
 (0)