Skip to content

Commit e7b6372

Browse files
author
wubin1
committed
Merge branch 'develop' of https://github.com/tronprotocol/java-tron into deferred_transaction
2 parents 75ca433 + 8a121d3 commit e7b6372

24 files changed

Lines changed: 161 additions & 118 deletions

src/main/java/org/tron/common/storage/DBSettings.java renamed to src/main/java/org/tron/common/storage/RocksDbSettings.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import lombok.extern.slf4j.Slf4j;
55

66
@Slf4j
7-
public class DBSettings {
7+
public class RocksDbSettings {
88

9-
private static DBSettings settings;
9+
private static RocksDbSettings settings;
1010

1111
@Getter
1212
private int levelNumber;
@@ -29,29 +29,31 @@ public class DBSettings {
2929
@Getter
3030
private boolean enableStatistics;
3131

32-
private DBSettings() {
32+
private RocksDbSettings() {
3333

3434
}
3535

36-
public static DBSettings getDefaultSettings() {
37-
DBSettings defaultSettings = new DBSettings();
36+
public static RocksDbSettings getDefaultSettings() {
37+
RocksDbSettings defaultSettings = new RocksDbSettings();
3838
return defaultSettings.withLevelNumber(7).withBlockSize(64).withCompactThreads(32)
39-
.withTargetFileSizeBase(256).withMaxBytesForLevelMultiplier(10).withTargetFileSizeMultiplier(1)
39+
.withTargetFileSizeBase(256).withMaxBytesForLevelMultiplier(10)
40+
.withTargetFileSizeMultiplier(1)
4041
.withMaxBytesForLevelBase(256).withMaxOpenFiles(-1).withEnableStatistics(false);
4142
}
4243

43-
public static DBSettings getSettings() {
44+
public static RocksDbSettings getSettings() {
4445
if (settings == null) {
4546
return getDefaultSettings();
4647
}
4748
return settings;
4849
}
4950

50-
public static DBSettings initCustomSettings(int levelNumber, int compactThreads, int blocksize,
51-
long maxBytesForLevelBase,
52-
double maxBytesForLevelMultiplier, int level0FileNumCompactionTrigger, long targetFileSizeBase,
51+
public static RocksDbSettings initCustomSettings(int levelNumber, int compactThreads,
52+
int blocksize, long maxBytesForLevelBase,
53+
double maxBytesForLevelMultiplier, int level0FileNumCompactionTrigger,
54+
long targetFileSizeBase,
5355
int targetFileSizeMultiplier) {
54-
settings = new DBSettings()
56+
settings = new RocksDbSettings()
5557
.withMaxOpenFiles(-1)
5658
.withEnableStatistics(false)
5759
.withLevelNumber(levelNumber)
@@ -66,60 +68,62 @@ public static DBSettings initCustomSettings(int levelNumber, int compactThreads,
6668
}
6769

6870

69-
public DBSettings withMaxOpenFiles(int maxOpenFiles) {
71+
public RocksDbSettings withMaxOpenFiles(int maxOpenFiles) {
7072
this.maxOpenFiles = maxOpenFiles;
7173
return this;
7274
}
7375

74-
public DBSettings withCompactThreads(int compactThreads) {
76+
public RocksDbSettings withCompactThreads(int compactThreads) {
7577
this.compactThreads = compactThreads;
7678
return this;
7779
}
7880

79-
public DBSettings withBlockSize(long blockSize) {
81+
public RocksDbSettings withBlockSize(long blockSize) {
8082
this.blockSize = blockSize * 1024;
8183
return this;
8284
}
8385

84-
public DBSettings withMaxBytesForLevelBase(long maxBytesForLevelBase) {
86+
public RocksDbSettings withMaxBytesForLevelBase(long maxBytesForLevelBase) {
8587
this.maxBytesForLevelBase = maxBytesForLevelBase * 1024 * 1024;
8688
return this;
8789
}
8890

89-
public DBSettings withMaxBytesForLevelMultiplier(double maxBytesForLevelMultiplier) {
91+
public RocksDbSettings withMaxBytesForLevelMultiplier(double maxBytesForLevelMultiplier) {
9092
this.maxBytesForLevelMultiplier = maxBytesForLevelMultiplier;
9193
return this;
9294
}
9395

94-
public DBSettings withLevel0FileNumCompactionTrigger(int level0FileNumCompactionTrigger) {
96+
public RocksDbSettings withLevel0FileNumCompactionTrigger(int level0FileNumCompactionTrigger) {
9597
this.level0FileNumCompactionTrigger = level0FileNumCompactionTrigger;
9698
return this;
9799
}
98100

99-
public DBSettings withEnableStatistics(boolean enable) {
101+
public RocksDbSettings withEnableStatistics(boolean enable) {
100102
this.enableStatistics = enable;
101103
return this;
102104
}
103105

104-
public DBSettings withLevelNumber(int levelNumber) {
106+
public RocksDbSettings withLevelNumber(int levelNumber) {
105107
this.levelNumber = levelNumber;
106108
return this;
107109
}
108110

109111

110-
public DBSettings withTargetFileSizeBase(long targetFileSizeBase) {
112+
public RocksDbSettings withTargetFileSizeBase(long targetFileSizeBase) {
111113
this.targetFileSizeBase = targetFileSizeBase * 1024 * 1024;
112114
return this;
113115
}
114116

115-
public DBSettings withTargetFileSizeMultiplier(int targetFileSizeMultiplier) {
117+
public RocksDbSettings withTargetFileSizeMultiplier(int targetFileSizeMultiplier) {
116118
this.targetFileSizeMultiplier = targetFileSizeMultiplier;
117119
return this;
118120
}
119121

120122
public static void loggingSettings() {
121123
logger.info(String.format(
122-
"level number: %d, CompactThreads: %d, Blocksize: %d, maxBytesForLevelBase: %d, withMaxBytesForLevelMultiplier: %f, level0FileNumCompactionTrigger: %d, withTargetFileSizeBase: %d, withTargetFileSizeMultiplier: %d",
124+
"level number: %d, CompactThreads: %d, Blocksize: %d, maxBytesForLevelBase: %d,"
125+
+ " withMaxBytesForLevelMultiplier: %f, level0FileNumCompactionTrigger: %d, "
126+
+ "withTargetFileSizeBase: %d, withTargetFileSizeMultiplier: %d",
123127
settings.getLevelNumber(),
124128
settings.getCompactThreads(), settings.getBlockSize(), settings.getMaxBytesForLevelBase(),
125129
settings.getMaxBytesForLevelMultiplier(), settings.getLevel0FileNumCompactionTrigger(),

‎src/main/java/org/tron/common/storage/leveldb/LevelDbDataSourceImpl.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public boolean checkOrInitEngine() {
9191
}
9292
}
9393
engine = PropUtil.readProperty(enginePath, "ENGINE");
94-
if (engine.equals("LEVELDB")) {
94+
if ("LEVELDB".equals(engine)) {
9595
return true;
9696
} else {
9797
return false;

‎src/main/java/org/tron/common/storage/leveldb/RocksDbDataSourceImpl.java‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import org.rocksdb.Statistics;
2727
import org.rocksdb.WriteBatch;
2828
import org.rocksdb.WriteOptions;
29-
import org.tron.common.storage.DBSettings;
3029
import org.tron.common.storage.DbSourceInter;
30+
import org.tron.common.storage.RocksDbSettings;
3131
import org.tron.common.storage.WriteOptionsWrapper;
3232
import org.tron.common.utils.FileUtil;
3333
import org.tron.common.utils.PropUtil;
@@ -116,7 +116,19 @@ public Set<byte[]> allValues() throws RuntimeException {
116116

117117
@Override
118118
public long getTotal() throws RuntimeException {
119-
return 0;
119+
if (quitIfNotAlive()) {
120+
return 0;
121+
}
122+
resetDbLock.readLock().lock();
123+
try (RocksIterator iterator = database.newIterator()) {
124+
long total = 0;
125+
for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) {
126+
total++;
127+
}
128+
return total;
129+
} finally {
130+
resetDbLock.readLock().unlock();
131+
}
120132
}
121133

122134
@Override
@@ -148,7 +160,7 @@ public boolean checkOrInitEngine() {
148160
}
149161
}
150162
engine = PropUtil.readProperty(enginePath, "ENGINE");
151-
if (engine.equals("ROCKSDB")) {
163+
if ("ROCKSDB".equals(engine)) {
152164
return true;
153165
} else {
154166
return false;
@@ -160,10 +172,10 @@ public void initDB() {
160172
logger.error("database engine do not match");
161173
throw new RuntimeException("Failed to initialize database");
162174
}
163-
initDB(DBSettings.getSettings());
175+
initDB(RocksDbSettings.getSettings());
164176
}
165177

166-
public void initDB(DBSettings settings) {
178+
public void initDB(RocksDbSettings settings) {
167179
resetDbLock.writeLock().lock();
168180
try {
169181
if (isAlive()) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class Constant {
4545
public static final long TRANSACTION_MAX_BYTE_SIZE = 500 * 1_024L;
4646
public static final long MAXIMUM_TIME_UNTIL_EXPIRATION = 24 * 60 * 60 * 1_000L; //one day
4747
public static final long TRANSACTION_DEFAULT_EXPIRATION_TIME = 60 * 1_000L; //60 seconds
48-
public static final long MAX_DEFERRED_TRANSACTION_DELAY_SECONDS = 45 * 24 * 60 * 60; //45 days
48+
public static final long MAX_DEFERRED_TRANSACTION_DELAY_SECONDS = 45 * 24 * 3_600L; //45 days
4949
public static final long MAX_DEFERRED_TRANSACTION_OCCUPY_SPACE = 500 * 1_024 * 1_024L;
5050
public static final String DELAY_SECONDS = "delaySeconds";
5151

‎src/main/java/org/tron/core/Wallet.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,13 @@ public DeferredTransaction getDeferredTransactionById(ByteString transactionId)
12231223
Objects.isNull(dbManager.getDeferredTransactionIdIndexCache())){
12241224
return null;
12251225
}
1226-
DeferredTransactionCapsule deferredTransactionCapsule = dbManager.getDeferredTransactionCache().getByTransactionId(transactionId);
1226+
1227+
DeferredTransactionCapsule deferredTransactionCapsule;
1228+
if (Objects.nonNull(dbManager.getDeferredTransactionCache()) && Objects.nonNull(dbManager.getDeferredTransactionIdIndexCache())) {
1229+
deferredTransactionCapsule = dbManager.getDeferredTransactionCache().getByTransactionId(transactionId);
1230+
} else {
1231+
deferredTransactionCapsule = dbManager.getDeferredTransactionStore().getByTransactionId(transactionId);
1232+
}
12271233

12281234
if (deferredTransactionCapsule != null) {
12291235
return deferredTransactionCapsule.getDeferredTransaction();

‎src/main/java/org/tron/core/actuator/CancelDeferredTransactionContractActuator.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ public boolean validate() throws ContractValidateException {
5757
}
5858

5959
ByteString trxId = cancelDeferredTransactionContract.getTransactionId();
60-
DeferredTransactionCapsule capsule = null;
60+
DeferredTransactionCapsule capsule;
6161
if (Objects.nonNull(dbManager.getDeferredTransactionCache())) {
6262
capsule = dbManager.getDeferredTransactionCache().getByTransactionId(trxId);
63+
} else {
64+
capsule = dbManager.getDeferredTransactionStore().getByTransactionId(trxId);
6365
}
6466

6567
if (Objects.isNull(capsule)) {

‎src/main/java/org/tron/core/config/args/Args.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import org.tron.common.logsfilter.FilterQuery;
4040
import org.tron.common.logsfilter.TriggerConfig;
4141
import org.tron.common.overlay.discover.node.Node;
42-
import org.tron.common.storage.DBSettings;
42+
import org.tron.common.storage.RocksDbSettings;
4343
import org.tron.common.utils.ByteArray;
4444
import org.tron.core.Constant;
4545
import org.tron.core.Wallet;
@@ -433,7 +433,7 @@ public class Args {
433433
private DbBackupConfig dbBackupConfig;
434434

435435
@Getter
436-
private DBSettings rocksDBCustomSettings;
436+
private RocksDbSettings rocksDBCustomSettings;
437437

438438
public static void clearParam() {
439439
INSTANCE.outputDirectory = "output-directory";
@@ -625,7 +625,7 @@ public static void setParam(final String[] args, final String confFileName) {
625625
.filter(StringUtils::isNotEmpty)
626626
.orElse(Storage.getDbEngineFromConfig(config)));
627627

628-
if (INSTANCE.storage.getDbEngine().toUpperCase().equals("ROCKSDB")
628+
if ("ROCKSDB".equals(INSTANCE.storage.getDbEngine().toUpperCase())
629629
&& INSTANCE.storage.getDbVersion() == 1) {
630630
throw new RuntimeException("db.version = 1 is not supported by ROCKSDB engine.");
631631
}
@@ -879,7 +879,7 @@ public static void setParam(final String[] args, final String confFileName) {
879879
config.hasPath("event.subscribe.filter") ? getEventFilter(config) : null;
880880

881881
initBackupProperty(config);
882-
if (Args.getInstance().getStorage().getDbEngine().toUpperCase().equals("ROCKSDB")) {
882+
if ("ROCKSDB".equals(Args.getInstance().getStorage().getDbEngine().toUpperCase())) {
883883
initRocksDbBackupProperty(config);
884884
initRocksDbSettings(config);
885885
}
@@ -1184,11 +1184,11 @@ private static void initRocksDbSettings(Config config) {
11841184
int targetFileSizeMultiplier = config.hasPath(prefix + "targetFileSizeMultiplier") ? config
11851185
.getInt(prefix + "targetFileSizeMultiplier") : 1;
11861186

1187-
INSTANCE.rocksDBCustomSettings = DBSettings
1187+
INSTANCE.rocksDBCustomSettings = RocksDbSettings
11881188
.initCustomSettings(levelNumber, compactThreads, blocksize, maxBytesForLevelBase,
11891189
maxBytesForLevelMultiplier, level0FileNumCompactionTrigger,
11901190
targetFileSizeBase, targetFileSizeMultiplier);
1191-
DBSettings.loggingSettings();
1191+
RocksDbSettings.loggingSettings();
11921192
}
11931193

11941194
private static void initRocksDbBackupProperty(Config config) {

‎src/main/java/org/tron/core/db/Manager.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,7 @@ public void closeAllStore() {
18221822
closeOneStore(transactionHistoryStore);
18231823
closeOneStore(votesStore);
18241824
closeOneStore(delegatedResourceStore);
1825+
closeOneStore(delegatedResourceAccountIndexStore);
18251826
closeOneStore(assetIssueV2Store);
18261827
closeOneStore(exchangeV2Store);
18271828
closeOneStore(deferredTransactionStore);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public abstract class TronDatabase<T> implements ITronChainBase<T> {
2929
protected TronDatabase(String dbName) {
3030
this.dbName = dbName;
3131

32-
if (Args.getInstance().getStorage().getDbEngine().toUpperCase().equals("LEVELDB")) {
32+
if ("LEVELDB".equals(Args.getInstance().getStorage().getDbEngine().toUpperCase())) {
3333
dbSource =
3434
new LevelDbDataSourceImpl(Args.getInstance().getOutputDirectoryByDbName(dbName), dbName);
35-
} else if (Args.getInstance().getStorage().getDbEngine().toUpperCase().equals("ROCKSDB")) {
35+
} else if ("ROCKSDB".equals(Args.getInstance().getStorage().getDbEngine().toUpperCase())) {
3636
String parentName = Paths.get(Args.getInstance().getOutputDirectoryByDbName(dbName), Args.getInstance().getStorage().getDbDirectory()).toString();
3737
dbSource =
3838
new RocksDbDataSourceImpl(parentName, dbName);

‎src/main/java/org/tron/core/db/TronStoreWithRevoking.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ protected TronStoreWithRevoking(String dbName) {
4848
if (dbVersion == 1) {
4949
this.revokingDB = new RevokingDBWithCachingOldValue(dbName);
5050
} else if (dbVersion == 2) {
51-
if (dbEngine.toUpperCase().equals("LEVELDB")) {
51+
if ("LEVELDB".equals(dbEngine.toUpperCase())) {
5252
this.revokingDB = new RevokingDBWithCachingNewValue(dbName, LevelDB.class);
53-
} else if (dbEngine.toUpperCase().equals("ROCKSDB")) {
53+
} else if ("ROCKSDB".equals(dbEngine.toUpperCase())) {
5454
this.revokingDB = new RevokingDBWithCachingNewValue(dbName, RocksDB.class);
5555
}
5656
} else {

0 commit comments

Comments
 (0)