Skip to content

Commit d27c40b

Browse files
committed
feat(db): optimize the judgment of node type
1 parent 50ad05a commit d27c40b

File tree

12 files changed

+50
-150
lines changed

12 files changed

+50
-150
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import org.springframework.beans.factory.annotation.Autowired;
2424
import org.springframework.beans.factory.annotation.Value;
2525
import org.springframework.stereotype.Component;
26-
import org.tron.common.error.TronDBException;
2726
import org.tron.common.utils.Sha256Hash;
27+
import org.tron.core.Constant;
2828
import org.tron.core.capsule.BlockCapsule;
2929
import org.tron.core.capsule.BlockCapsule.BlockId;
3030
import org.tron.core.exception.BadItemException;
@@ -33,6 +33,8 @@
3333
@Component
3434
public class BlockStore extends TronStoreWithRevoking<BlockCapsule> {
3535

36+
private long lowestBlockNum = -1;
37+
3638
@Autowired
3739
private BlockStore(@Value("block") String dbName) {
3840
super(dbName);
@@ -60,4 +62,25 @@ private List<BlockCapsule> pack(Set<byte[]> values) {
6062
blocks.sort(Comparator.comparing(BlockCapsule::getNum));
6163
return blocks;
6264
}
65+
66+
public int getNodeType() {
67+
return getLowestBlockNum() > 1 ? Constant.NODE_TYPE_LIGHT_NODE : Constant.NODE_TYPE_FULL_NODE;
68+
}
69+
70+
public boolean isLiteNode() {
71+
return getNodeType() == Constant.NODE_TYPE_LIGHT_NODE;
72+
}
73+
74+
/**
75+
* get next block for genesis block 0, TODO 1. auto-pruner, 2. Thread-safe
76+
*
77+
* @return second block num
78+
*/
79+
public long getLowestBlockNum() {
80+
if (lowestBlockNum == -1) {
81+
lowestBlockNum = this.getLimitNumber(1, 1).stream()
82+
.map(BlockCapsule::getNum).findFirst().orElse(0L);
83+
}
84+
return lowestBlockNum;
85+
}
6386
}

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

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@
33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.context.ApplicationContext;
55
import org.springframework.stereotype.Component;
6-
import org.tron.common.utils.ByteArray;
7-
import org.tron.core.Constant;
86
import org.tron.core.capsule.BytesCapsule;
97

108
@Component
119
public class CommonStore extends TronDatabase<BytesCapsule> {
1210

13-
private static final byte[] DB_KEY_LOWEST_BLOCK_NUM = "lowest_block_num".getBytes();
14-
private static final byte[] DB_KEY_NODE_TYPE = "node_type".getBytes();
15-
1611
@Autowired
1712
public CommonStore(ApplicationContext ctx) {
1813
super("common");
@@ -37,31 +32,4 @@ public BytesCapsule get(byte[] key) {
3732
public boolean has(byte[] key) {
3833
return dbSource.getData(key) != null;
3934
}
40-
41-
public int getNodeType() {
42-
int nodeType = 0;
43-
byte[] bytes = get(DB_KEY_NODE_TYPE).getData();
44-
if (bytes != null) {
45-
nodeType = ByteArray.toInt(bytes);
46-
}
47-
return nodeType;
48-
}
49-
50-
public void setNodeType(int nodeType) {
51-
put(DB_KEY_NODE_TYPE, new BytesCapsule(ByteArray.fromInt(nodeType)));
52-
}
53-
54-
public long getLowestBlockNum() {
55-
long lowestBlockNum = 0;
56-
byte[] bytes = get(DB_KEY_LOWEST_BLOCK_NUM).getData();
57-
if (bytes != null) {
58-
lowestBlockNum = ByteArray.toLong(bytes);
59-
}
60-
return lowestBlockNum;
61-
}
62-
63-
public void setLowestBlockNum(long lowestBlockNum) {
64-
put(DB_KEY_LOWEST_BLOCK_NUM, new BytesCapsule(ByteArray.fromLong(lowestBlockNum)));
65-
}
66-
6735
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.util.ArrayList;
99
import java.util.List;
1010
import java.util.Set;
11+
12+
import com.google.common.annotations.VisibleForTesting;
1113
import lombok.Getter;
1214
import lombok.Setter;
1315
import org.quartz.CronExpression;
@@ -528,6 +530,7 @@ public class CommonParameter {
528530

529531
@Getter
530532
@Setter
533+
@VisibleForTesting
531534
public boolean isLiteFullNode = false;
532535

533536
@Getter

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ public static void clearParam() {
221221
PARAMETER.allowNewRewardAlgorithm = 0;
222222
PARAMETER.allowNewReward = 0;
223223
PARAMETER.memoFee = 0;
224+
PARAMETER.isLiteFullNode = false;
224225
}
225226

226227
/**
@@ -1006,9 +1007,6 @@ public static void setParam(final String[] args, final String confFileName) {
10061007
.getBoolean(Constant.METRICS_PROMETHEUS_ENABLE);
10071008
PARAMETER.metricsPrometheusPort = config.hasPath(Constant.METRICS_PROMETHEUS_PORT) ? config
10081009
.getInt(Constant.METRICS_PROMETHEUS_PORT) : 9527;
1009-
1010-
// lite fullnode params
1011-
PARAMETER.setLiteFullNode(checkIsLiteFullNode());
10121010
PARAMETER.setOpenHistoryQueryWhenLiteFN(
10131011
config.hasPath(Constant.NODE_OPEN_HISTORY_QUERY_WHEN_LITEFN)
10141012
&& config.getBoolean(Constant.NODE_OPEN_HISTORY_QUERY_WHEN_LITEFN));
@@ -1506,19 +1504,6 @@ public static void setFullNodeAllowShieldedTransaction(boolean fullNodeAllowShie
15061504
PARAMETER.fullNodeAllowShieldedTransactionArgs = fullNodeAllowShieldedTransaction;
15071505
}
15081506

1509-
/**
1510-
* set isLiteFullNode=true when this node is a lite fullnode.
1511-
*/
1512-
public static boolean checkIsLiteFullNode() {
1513-
String infoFile = Paths.get(PARAMETER.outputDirectory,
1514-
PARAMETER.storage.getDbDirectory(), Constant.INFO_FILE_NAME).toString();
1515-
if (FileUtil.isExists(infoFile)) {
1516-
String value = PropUtil.readProperty(infoFile, Constant.SPLIT_BLOCK_NUM);
1517-
return !"".equals(value) && Long.parseLong(value) > 1;
1518-
}
1519-
return false;
1520-
}
1521-
15221507
private static void witnessAddressCheck(Config config) {
15231508
if (config.hasPath(Constant.LOCAL_WITNESS_ACCOUNT_ADDRESS)) {
15241509
byte[] bytes = Commons

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,11 @@ public void init() {
511511

512512
long headNum = chainBaseManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber();
513513
logger.info("Current headNum is: {}.", headNum);
514-
int nodeType = chainBaseManager.getCommonStore().getNodeType();
515-
logger.info("Node type is: {}.", Constant.NODE_TYPE_LIGHT_NODE == nodeType ? "lite" : "full");
514+
boolean isLite = chainBaseManager.getBlockStore().isLiteNode();
515+
logger.info("Node type is: {}.", isLite ? "lite" : "full");
516+
if (isLite) {
517+
logger.info("Lite node lowestNum: {}", chainBaseManager.getBlockStore().getLowestBlockNum());
518+
}
516519
revokingStore.enable();
517520
validateSignService = Executors
518521
.newFixedThreadPool(Args.getInstance().getValidateSignThreadNum());

framework/src/main/java/org/tron/core/net/message/handshake/HelloMessage.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import org.tron.core.Constant;
99
import org.tron.core.capsule.BlockCapsule;
1010
import org.tron.core.config.args.Args;
11-
import org.tron.core.db.CommonStore;
11+
import org.tron.core.db.BlockStore;
1212
import org.tron.core.net.message.MessageTypes;
1313
import org.tron.core.net.message.TronMessage;
1414
import org.tron.p2p.discover.Node;
@@ -57,11 +57,11 @@ public HelloMessage(Node from, long timestamp, ChainBaseManager chainBaseManager
5757
.setNumber(hid.getNum())
5858
.build();
5959

60-
CommonStore commonStore = chainBaseManager.getCommonStore();
60+
BlockStore blockStore = chainBaseManager.getBlockStore();
6161
long lowestBlockNum = 0;
62-
int nodeType = commonStore.getNodeType();
62+
int nodeType = blockStore.getNodeType();
6363
if (nodeType == Constant.NODE_TYPE_LIGHT_NODE) {
64-
lowestBlockNum = commonStore.getLowestBlockNum();
64+
lowestBlockNum = blockStore.getLowestBlockNum();
6565
}
6666

6767
Builder builder = Protocol.HelloMessage.newBuilder();

framework/src/main/java/org/tron/core/services/filter/LiteFnQueryGrpcInterceptor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88
import io.grpc.ServerInterceptor;
99
import io.grpc.Status;
1010
import java.util.Set;
11+
12+
import org.springframework.beans.factory.annotation.Autowired;
1113
import org.springframework.stereotype.Component;
1214
import org.tron.common.parameter.CommonParameter;
15+
import org.tron.core.db.BlockStore;
1316

1417
@Component
1518
public class LiteFnQueryGrpcInterceptor implements ServerInterceptor {
16-
19+
@Autowired
20+
private BlockStore blockStore;
1721
private static final Set<String> filterMethods = Sets.newHashSet();
1822

1923
// for test
@@ -76,7 +80,7 @@ public static Set<String> getFilterMethods() {
7680
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
7781
Metadata headers, ServerCallHandler<ReqT, RespT> next) {
7882
boolean shouldBeFiltered = false;
79-
if (CommonParameter.getInstance().isLiteFullNode
83+
if ((CommonParameter.getInstance().isLiteFullNode || blockStore.isLiteNode())
8084
&& !CommonParameter.getInstance().openHistoryQueryWhenLiteFN
8185
&& filterMethods.contains(call.getMethodDescriptor().getFullMethodName())) {
8286
shouldBeFiltered = true;

framework/src/main/java/org/tron/core/services/filter/LiteFnQueryHttpFilter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111
import javax.servlet.ServletResponse;
1212
import javax.servlet.http.HttpServletRequest;
1313
import lombok.extern.slf4j.Slf4j;
14+
import org.springframework.beans.factory.annotation.Autowired;
1415
import org.springframework.stereotype.Component;
1516
import org.tron.common.parameter.CommonParameter;
17+
import org.tron.core.db.BlockStore;
1618

1719
@Component
1820
@Slf4j(topic = "API")
1921
public class LiteFnQueryHttpFilter implements Filter {
2022

23+
@Autowired
24+
private BlockStore blockStore;
25+
2126
private static Set<String> filterPaths = Sets.newHashSet();
2227

2328
// for test
@@ -107,7 +112,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
107112
FilterChain filterChain) throws IOException, ServletException {
108113
String requestPath = ((HttpServletRequest) servletRequest).getRequestURI();
109114
boolean shouldBeFiltered = false;
110-
if (CommonParameter.getInstance().isLiteFullNode
115+
if ((CommonParameter.getInstance().isLiteFullNode || blockStore.isLiteNode())
111116
&& !CommonParameter.getInstance().openHistoryQueryWhenLiteFN
112117
&& filterPaths.contains(requestPath)) {
113118
shouldBeFiltered = true;

framework/src/main/java/org/tron/tool/litefullnode/LiteFullNodeTool.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141
@Slf4j(topic = "tool")
4242
public class LiteFullNodeTool {
4343

44-
private static final byte[] DB_KEY_LOWEST_BLOCK_NUM = "lowest_block_num".getBytes();
45-
private static final byte[] DB_KEY_NODE_TYPE = "node_type".getBytes();
46-
4744
private static final long START_TIME = System.currentTimeMillis() / 1000;
4845

4946
private static long RECENT_BLKS = 65536;
@@ -57,7 +54,6 @@ public class LiteFullNodeTool {
5754
private static final String BLOCK_DB_NAME = "block";
5855
private static final String BLOCK_INDEX_DB_NAME = "block-index";
5956
private static final String TRANS_DB_NAME = "trans";
60-
private static final String COMMON_DB_NAME = "common";
6157
private static final String TRANSACTION_RET_DB_NAME = "transactionRetStore";
6258
private static final String TRANSACTION_HISTORY_DB_NAME = "transactionHistoryStore";
6359
private static final String PROPERTIES_DB_NAME = "properties";
@@ -328,10 +324,6 @@ private void fillSnapshotBlockAndTransDb(String sourceDir, String snapshotDir)
328324
throw new RuntimeException(e.getMessage());
329325
}
330326
}
331-
332-
DBInterface destCommonDb = DbTool.getDB(snapshotDir, COMMON_DB_NAME);
333-
destCommonDb.put(DB_KEY_NODE_TYPE, ByteArray.fromInt(Constant.NODE_TYPE_LIGHT_NODE));
334-
destCommonDb.put(DB_KEY_LOWEST_BLOCK_NUM, ByteArray.fromLong(startIndex));
335327
// copy engine.properties for block、block-index、trans from source if exist
336328
copyEngineIfExist(sourceDir, snapshotDir, BLOCK_DB_NAME, BLOCK_INDEX_DB_NAME, TRANS_DB_NAME);
337329
}
@@ -487,14 +479,6 @@ private static boolean isEmptyBytes(byte[] b) {
487479
private void deleteSnapshotFlag(String databaseDir) throws IOException, RocksDBException {
488480
logger.info("Delete the info file from {}.", databaseDir);
489481
Files.delete(Paths.get(databaseDir, INFO_FILE_NAME));
490-
if (!isLite(databaseDir)) {
491-
DBInterface destCommonDb = DbTool.getDB(databaseDir, COMMON_DB_NAME);
492-
destCommonDb.delete(DB_KEY_NODE_TYPE);
493-
destCommonDb.delete(DB_KEY_LOWEST_BLOCK_NUM);
494-
logger.info("Deleted {} and {} from {} to identify this node is a real fullnode.",
495-
"node_type", "lowest_block_num", COMMON_DB_NAME);
496-
}
497-
498482
}
499483

500484
private void hasEnoughBlock(String sourceDir) throws RocksDBException, IOException {

framework/src/test/java/org/tron/core/db/CommonStoreTest.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

framework/src/test/java/org/tron/core/services/filter/LiteFnQueryHttpFilterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class LiteFnQueryHttpFilterTest {
4040
private Application appTest;
4141
private CloseableHttpClient httpClient = HttpClients.createDefault();
4242

43-
private String dbPath = "output_grpc_filter_test";
43+
private String dbPath = "output_http_filter_test";
4444

4545
/**
4646
* init dependencies.

plugins/src/main/java/org/tron/plugins/DbLite.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141
"1:Internal error: exception occurred,please check toolkit.log"})
4242
public class DbLite implements Callable<Integer> {
4343

44-
private static final byte[] DB_KEY_LOWEST_BLOCK_NUM = "lowest_block_num".getBytes();
45-
private static final byte[] DB_KEY_NODE_TYPE = "node_type".getBytes();
46-
4744
private static final long START_TIME = System.currentTimeMillis() / 1000;
4845

4946
private static long RECENT_BLKS = 65536;
@@ -56,7 +53,6 @@ public class DbLite implements Callable<Integer> {
5653
private static final String BLOCK_DB_NAME = "block";
5754
private static final String BLOCK_INDEX_DB_NAME = "block-index";
5855
private static final String TRANS_DB_NAME = "trans";
59-
private static final String COMMON_DB_NAME = "common";
6056
private static final String TRANSACTION_RET_DB_NAME = "transactionRetStore";
6157
private static final String TRANSACTION_HISTORY_DB_NAME = "transactionHistoryStore";
6258
private static final String PROPERTIES_DB_NAME = "properties";
@@ -432,10 +428,6 @@ private void fillSnapshotBlockAndTransDb(String sourceDir, String snapshotDir)
432428
throw new RuntimeException(e.getMessage());
433429
}
434430
});
435-
436-
DBInterface destCommonDb = DbTool.getDB(snapshotDir, COMMON_DB_NAME);
437-
destCommonDb.put(DB_KEY_NODE_TYPE, ByteArray.fromInt(DBUtils.NODE_TYPE_LIGHT_NODE));
438-
destCommonDb.put(DB_KEY_LOWEST_BLOCK_NUM, ByteArray.fromLong(startIndex));
439431
// copy engine.properties for block、block-index、trans from source if exist
440432
copyEngineIfExist(sourceDir, snapshotDir, BLOCK_DB_NAME, BLOCK_INDEX_DB_NAME, TRANS_DB_NAME);
441433
}
@@ -636,17 +628,6 @@ private void deleteSnapshotFlag(String databaseDir) throws IOException, RocksDBE
636628
logger.info("Delete the info file from {}.", databaseDir);
637629
spec.commandLine().getOut().format("Delete the info file from %s.", databaseDir).println();
638630
Files.delete(Paths.get(databaseDir, INFO_FILE_NAME));
639-
if (!isLite(databaseDir)) {
640-
DBInterface destCommonDb = DbTool.getDB(databaseDir, COMMON_DB_NAME);
641-
destCommonDb.delete(DB_KEY_NODE_TYPE);
642-
destCommonDb.delete(DB_KEY_LOWEST_BLOCK_NUM);
643-
logger.info("Deleted {} and {} from {} to identify this node is a real fullnode.",
644-
"node_type", "lowest_block_num", COMMON_DB_NAME);
645-
spec.commandLine().getOut().format(
646-
"Deleted %s and %s from %s to identify this node is a real fullnode.",
647-
"node_type", "lowest_block_num", COMMON_DB_NAME).println();
648-
}
649-
650631
}
651632

652633
private void deleteBackupArchiveDbs(String liteDir) throws IOException, RocksDBException {

0 commit comments

Comments
 (0)