Skip to content

Commit e436e19

Browse files
committed
feat(db): add state-root check
1 parent d61e134 commit e436e19

57 files changed

Lines changed: 1655 additions & 422 deletions

File tree

Some content is hidden

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

actuator/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ test {
3333

3434
jacocoTestReport {
3535
reports {
36-
xml.enabled = true
37-
html.enabled = true
36+
xml.required.set(true)
37+
html.required.set(true)
3838
}
3939
getExecutionData().setFrom(fileTree('../framework/build/jacoco').include("**.exec"))
4040
afterEvaluate {
Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,49 @@
11
package org.tron.core.utils;
22

33
import java.util.Set;
4+
import java.util.concurrent.atomic.AtomicBoolean;
45
import lombok.extern.slf4j.Slf4j;
56
import org.reflections.Reflections;
67
import org.tron.core.actuator.AbstractActuator;
8+
import org.tron.core.exception.TronError;
79

810
@Slf4j(topic = "TransactionRegister")
911
public class TransactionRegister {
1012

13+
private static final AtomicBoolean REGISTERED = new AtomicBoolean(false);
14+
private static final String PACKAGE_NAME = "org.tron.core.actuator";
15+
1116
public static void registerActuator() {
12-
Reflections reflections = new Reflections("org.tron");
13-
Set<Class<? extends AbstractActuator>> subTypes = reflections
14-
.getSubTypesOf(AbstractActuator.class);
15-
for (Class _class : subTypes) {
16-
try {
17-
_class.newInstance();
18-
} catch (Exception e) {
19-
logger.error("{} contract actuator register fail!", _class, e);
17+
if (REGISTERED.get()) {
18+
logger.info("Actuator already registered.");
19+
return;
20+
}
21+
22+
synchronized (TransactionRegister.class) {
23+
if (REGISTERED.get()) {
24+
logger.info("Actuator already registered.");
25+
return;
26+
}
27+
28+
logger.info("Register actuator start.");
29+
Reflections reflections = new Reflections(PACKAGE_NAME);
30+
Set<Class<? extends AbstractActuator>> subTypes = reflections
31+
.getSubTypesOf(AbstractActuator.class);
32+
33+
for (Class<? extends AbstractActuator> clazz : subTypes) {
34+
try {
35+
logger.debug("Registering actuator: {} start", clazz.getName());
36+
clazz.getDeclaredConstructor().newInstance();
37+
logger.debug("Registering actuator: {} done", clazz.getName());
38+
} catch (Exception e) {
39+
throw new TronError(clazz.getName() + ": "
40+
+ (e.getCause() == null ? e.getMessage() : e.getCause().getMessage()),
41+
e, TronError.ErrCode.ACTUATOR_REGISTER);
42+
}
2043
}
44+
45+
REGISTERED.set(true);
46+
logger.info("Register actuator done, total {}.", subTypes.size());
2147
}
2248
}
23-
2449
}

build.gradle

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ ext.archInfo = [
4040
VMOptions: isArm64 ? "${rootDir}/gradle/jdk17/java-tron.vmoptions" : "${rootDir}/gradle/java-tron.vmoptions"
4141
]
4242

43-
if (!archInfo.java.is(archInfo.requires.JavaVersion)) {
44-
throw new GradleException("Java ${archInfo.requires.JavaVersion} is required for ${archInfo.name}. Detected version ${archInfo.java}")
45-
}
46-
4743
println "Building for architecture: ${archInfo.name}, Java version: ${archInfo.java}"
4844

4945

@@ -108,14 +104,15 @@ subprojects {
108104
testImplementation "org.mockito:mockito-inline:4.11.0"
109105
}
110106

111-
task sourcesJar(type: Jar, dependsOn: classes) {
112-
classifier = "sources"
107+
tasks.register('sourcesJar', Jar) {
108+
dependsOn classes
109+
archiveClassifier.set('sources')
113110
from sourceSets.main.allSource
114111
duplicatesStrategy = DuplicatesStrategy.INCLUDE // allow duplicates
115112
}
116113

117114

118-
tasks.withType(AbstractArchiveTask) {
115+
tasks.withType(AbstractArchiveTask).configureEach {
119116
preserveFileTimestamps = false
120117
reproducibleFileOrder = true
121118
duplicatesStrategy = DuplicatesStrategy.INCLUDE // allow duplicates
@@ -149,7 +146,7 @@ subprojects {
149146
}
150147
}
151148

152-
task copyToParent(type: Copy) {
149+
tasks.register('copyToParent', Copy) {
153150
into "$buildDir/libs"
154151
subprojects {
155152
from tasks.withType(Jar)

chainbase/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ test {
4242
jacocoTestReport {
4343
dependsOn(processResources) // explicit_dependency
4444
reports {
45-
xml.enabled = true
46-
html.enabled = true
45+
xml.required.set(true)
46+
html.required.set(true)
4747
}
4848
getExecutionData().setFrom(fileTree('../framework/build/jacoco').include("**.exec"))
4949
afterEvaluate {

chainbase/src/main/java/org/tron/core/ChainBaseManager.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import org.tron.core.store.NullifierStore;
6060
import org.tron.core.store.ProposalStore;
6161
import org.tron.core.store.SectionBloomStore;
62+
import org.tron.core.store.StateRootStore;
6263
import org.tron.core.store.StorageRowStore;
6364
import org.tron.core.store.TransactionHistoryStore;
6465
import org.tron.core.store.TransactionRetStore;
@@ -233,6 +234,10 @@ public class ChainBaseManager {
233234
@Getter
234235
private SectionBloomStore sectionBloomStore;
235236

237+
@Autowired
238+
@Getter
239+
private StateRootStore stateRootStore;
240+
236241
@Autowired
237242
private DbStatService dbStatService;
238243

@@ -315,7 +320,7 @@ public boolean containBlockInMainChain(BlockId blockId) {
315320
}
316321
}
317322

318-
public BlockCapsule getKhaosDbHead(){
323+
public BlockCapsule getKhaosDbHead() {
319324
return this.khaosDb.getHead();
320325
}
321326

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,16 @@ public String toString() {
335335
return toStringBuff.toString();
336336
}
337337

338+
public Sha256Hash getStateRoot() {
339+
ByteString stateRoot = this.block.getBlockHeader().getStateRoot();
340+
return stateRoot.isEmpty() ? Sha256Hash.ZERO_HASH : Sha256Hash.wrap(stateRoot);
341+
}
342+
343+
public void clearStateRoot() {
344+
BlockHeader blockHeader = this.block.getBlockHeader().toBuilder().clearStateRoot().build();
345+
this.block = this.block.toBuilder().setBlockHeader(blockHeader).build();
346+
}
347+
338348
public static class BlockId extends Sha256Hash {
339349

340350
private long num;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public DbSourceInter<byte[]> getDbSource() {
6060
}
6161

6262
public void updateByBatch(Map<byte[], byte[]> rows) {
63+
this.updateByBatch(rows, writeOptions);
64+
}
65+
66+
public void updateByBatch(Map<byte[], byte[]> rows, WriteOptionsWrapper writeOptions) {
6367
this.dbSource.updateByBatch(rows, writeOptions);
6468
}
6569

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import lombok.Getter;
2626
import lombok.Setter;
2727
import lombok.extern.slf4j.Slf4j;
28+
import org.springframework.beans.factory.ObjectFactory;
2829
import org.springframework.beans.factory.annotation.Autowired;
2930
import org.tron.common.error.TronDBException;
3031
import org.tron.common.es.ExecutorServiceManager;
@@ -49,7 +50,7 @@ public class SnapshotManager implements RevokingDatabase {
4950

5051
public static final int DEFAULT_MIN_FLUSH_COUNT = 1;
5152
private static final int DEFAULT_STACK_MAX_SIZE = 256;
52-
private static final long ONE_MINUTE_MILLS = 60*1000L;
53+
private static final long ONE_MINUTE_MILLS = 60 * 1000L;
5354
private static final String CHECKPOINT_V2_DIR = "checkpoint";
5455
@Getter
5556
private List<Chainbase> dbs = new ArrayList<>();
@@ -79,6 +80,9 @@ public class SnapshotManager implements RevokingDatabase {
7980
@Getter
8081
private CheckTmpStore checkTmpStore;
8182

83+
@Autowired
84+
private ObjectFactory<CheckPointV2Store> checkPointV2Store;
85+
8286
@Setter
8387
private volatile int maxFlushCount = DEFAULT_MIN_FLUSH_COUNT;
8488

@@ -385,8 +389,7 @@ public void createCheckpoint() {
385389
}
386390
}
387391
if (isV2Open()) {
388-
String dbName = String.valueOf(System.currentTimeMillis());
389-
checkPointStore = getCheckpointDB(dbName);
392+
checkPointStore = checkPointV2Store.getObject();
390393
} else {
391394
checkPointStore = checkTmpStore;
392395
}
@@ -405,7 +408,7 @@ public void createCheckpoint() {
405408
}
406409

407410
private TronDatabase<byte[]> getCheckpointDB(String dbName) {
408-
return new CheckPointV2Store(CHECKPOINT_V2_DIR+"/"+dbName);
411+
return new CheckPointV2Store(CHECKPOINT_V2_DIR, dbName);
409412
}
410413

411414
public List<String> getCheckpointList() {
@@ -431,7 +434,7 @@ private void deleteCheckpoint() {
431434
for (Map.Entry<byte[], byte[]> e : checkTmpStore.getDbSource()) {
432435
hmap.put(e.getKey(), null);
433436
}
434-
if (hmap.size() != 0) {
437+
if (!hmap.isEmpty()) {
435438
checkTmpStore.getDbSource().updateByBatch(hmap);
436439
}
437440
} catch (Exception e) {
@@ -450,10 +453,10 @@ private void pruneCheckpoint() {
450453
if (cpList.size() < 3) {
451454
return;
452455
}
453-
long latestTimestamp = Long.parseLong(cpList.get(cpList.size()-1));
454-
for (String cp: cpList.subList(0, cpList.size()-3)) {
456+
long latestTimestamp = Long.parseLong(cpList.get(cpList.size() - 1));
457+
for (String cp: cpList.subList(0, cpList.size() - 3)) {
455458
long timestamp = Long.parseLong(cp);
456-
if (latestTimestamp - timestamp <= ONE_MINUTE_MILLS*2) {
459+
if (latestTimestamp - timestamp <= ONE_MINUTE_MILLS * 2) {
457460
break;
458461
}
459462
String checkpointPath = Paths.get(StorageUtils.getOutputDirectoryByDbName(CHECKPOINT_V2_DIR),

0 commit comments

Comments
 (0)