Skip to content

Commit 1450156

Browse files
authored
Merge pull request #4986 from halibobo1205/feat/lite_node
feat(db): optimize the judgement of node type
2 parents c728bfb + ed38d78 commit 1450156

File tree

14 files changed

+88
-178
lines changed

14 files changed

+88
-178
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.google.protobuf.ByteString;
66
import java.util.List;
7+
import javax.annotation.PostConstruct;
78
import lombok.Getter;
89
import lombok.Setter;
910
import lombok.extern.slf4j.Slf4j;
@@ -236,6 +237,14 @@ public class ChainBaseManager {
236237
@Autowired
237238
private DbStatService dbStatService;
238239

240+
@Getter
241+
@Setter
242+
private NodeType nodeType;
243+
244+
@Getter
245+
@Setter
246+
private long lowestBlockNum = -1; // except num = 0.
247+
239248
public void closeOneStore(ITronChainBase database) {
240249
logger.info("******** Begin to close {}. ********", database.getName());
241250
try {
@@ -414,5 +423,28 @@ public static synchronized void init(ChainBaseManager manager) {
414423
AssetUtil.setAccountAssetStore(manager.getAccountAssetStore());
415424
AssetUtil.setDynamicPropertiesStore(manager.getDynamicPropertiesStore());
416425
}
426+
427+
@PostConstruct
428+
private void init() {
429+
this.lowestBlockNum = this.blockIndexStore.getLimitNumber(1, 1).stream()
430+
.map(BlockId::getNum).findFirst().orElse(0L);
431+
this.nodeType = getLowestBlockNum() > 1 ? NodeType.LITE : NodeType.FULL;
432+
}
433+
434+
public boolean isLiteNode() {
435+
return getNodeType() == NodeType.LITE;
436+
}
437+
438+
public enum NodeType {
439+
FULL(0),
440+
LITE(1);
441+
442+
@Getter
443+
private final int type;
444+
445+
NodeType(int type) {
446+
this.type = type;
447+
}
448+
}
417449
}
418450

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package org.tron.core.db;
22

3-
import java.util.Arrays;
3+
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
import java.util.Set;
7+
import lombok.extern.slf4j.Slf4j;
48
import org.apache.commons.lang3.ArrayUtils;
59
import org.springframework.beans.factory.annotation.Autowired;
610
import org.springframework.beans.factory.annotation.Value;
@@ -12,6 +16,7 @@
1216
import org.tron.core.exception.ItemNotFoundException;
1317

1418
@Component
19+
@Slf4j(topic = "DB")
1520
public class BlockIndexStore extends TronStoreWithRevoking<BytesCapsule> {
1621

1722

@@ -44,4 +49,17 @@ public BytesCapsule get(byte[] key)
4449
}
4550
return new BytesCapsule(value);
4651
}
47-
}
52+
53+
public List<BlockId> getLimitNumber(long startNumber, long limit) {
54+
return pack(revokingDB.getValuesNext(ByteArray.fromLong(startNumber), limit));
55+
}
56+
57+
private List<BlockId> pack(Set<byte[]> values) {
58+
List<BlockId> blocks = new ArrayList<>();
59+
for (byte[] bytes : values) {
60+
blocks.add(new BlockId(Sha256Hash.wrap(bytes)));
61+
}
62+
blocks.sort(Comparator.comparing(BlockId::getNum));
63+
return blocks;
64+
}
65+
}

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: 2 additions & 4 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;
@@ -526,10 +528,6 @@ public class CommonParameter {
526528
@Setter
527529
public boolean openHistoryQueryWhenLiteFN = false;
528530

529-
@Getter
530-
@Setter
531-
public boolean isLiteFullNode = false;
532-
533531
@Getter
534532
@Setter
535533
@Parameter(names = {"--history-balance-lookup"})

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,9 +1006,6 @@ public static void setParam(final String[] args, final String confFileName) {
10061006
.getBoolean(Constant.METRICS_PROMETHEUS_ENABLE);
10071007
PARAMETER.metricsPrometheusPort = config.hasPath(Constant.METRICS_PROMETHEUS_PORT) ? config
10081008
.getInt(Constant.METRICS_PROMETHEUS_PORT) : 9527;
1009-
1010-
// lite fullnode params
1011-
PARAMETER.setLiteFullNode(checkIsLiteFullNode());
10121009
PARAMETER.setOpenHistoryQueryWhenLiteFN(
10131010
config.hasPath(Constant.NODE_OPEN_HISTORY_QUERY_WHEN_LITEFN)
10141011
&& config.getBoolean(Constant.NODE_OPEN_HISTORY_QUERY_WHEN_LITEFN));
@@ -1506,19 +1503,6 @@ public static void setFullNodeAllowShieldedTransaction(boolean fullNodeAllowShie
15061503
PARAMETER.fullNodeAllowShieldedTransactionArgs = fullNodeAllowShieldedTransaction;
15071504
}
15081505

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-
15221506
private static void witnessAddressCheck(Config config) {
15231507
if (config.hasPath(Constant.LOCAL_WITNESS_ACCOUNT_ADDRESS)) {
15241508
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.isLiteNode();
515+
logger.info("Node type is: {}.", isLite ? "lite" : "full");
516+
if (isLite) {
517+
logger.info("Lite node lowestNum: {}", chainBaseManager.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: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
import org.tron.common.utils.ByteArray;
66
import org.tron.common.utils.StringUtil;
77
import org.tron.core.ChainBaseManager;
8-
import org.tron.core.Constant;
98
import org.tron.core.capsule.BlockCapsule;
109
import org.tron.core.config.args.Args;
11-
import org.tron.core.db.CommonStore;
1210
import org.tron.core.net.message.MessageTypes;
1311
import org.tron.core.net.message.TronMessage;
1412
import org.tron.p2p.discover.Node;
@@ -56,24 +54,16 @@ public HelloMessage(Node from, long timestamp, ChainBaseManager chainBaseManager
5654
.setHash(hid.getByteString())
5755
.setNumber(hid.getNum())
5856
.build();
59-
60-
CommonStore commonStore = chainBaseManager.getCommonStore();
61-
long lowestBlockNum = 0;
62-
int nodeType = commonStore.getNodeType();
63-
if (nodeType == Constant.NODE_TYPE_LIGHT_NODE) {
64-
lowestBlockNum = commonStore.getLowestBlockNum();
65-
}
66-
6757
Builder builder = Protocol.HelloMessage.newBuilder();
68-
6958
builder.setFrom(fromEndpoint);
7059
builder.setVersion(Args.getInstance().getNodeP2pVersion());
7160
builder.setTimestamp(timestamp);
7261
builder.setGenesisBlockId(gBlockId);
7362
builder.setSolidBlockId(sBlockId);
7463
builder.setHeadBlockId(hBlockId);
75-
builder.setNodeType(nodeType);
76-
builder.setLowestBlockNum(lowestBlockNum);
64+
builder.setNodeType(chainBaseManager.getNodeType().getType());
65+
builder.setLowestBlockNum(chainBaseManager.isLiteNode()
66+
? chainBaseManager.getLowestBlockNum() : 0);
7767

7868
this.helloMessage = builder.build();
7969
this.type = MessageTypes.P2P_HELLO.asByte();

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

Lines changed: 6 additions & 6 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.ChainBaseManager;
1316

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

1923
// for test
@@ -75,13 +79,9 @@ public static Set<String> getFilterMethods() {
7579
@Override
7680
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
7781
Metadata headers, ServerCallHandler<ReqT, RespT> next) {
78-
boolean shouldBeFiltered = false;
79-
if (CommonParameter.getInstance().isLiteFullNode
82+
if (chainBaseManager.isLiteNode()
8083
&& !CommonParameter.getInstance().openHistoryQueryWhenLiteFN
8184
&& filterMethods.contains(call.getMethodDescriptor().getFullMethodName())) {
82-
shouldBeFiltered = true;
83-
}
84-
if (shouldBeFiltered) {
8585
call.close(Status.UNAVAILABLE
8686
.withDescription("this API is closed because this node is a lite fullnode"), headers);
8787
return new ServerCall.Listener<ReqT>() {};

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

Lines changed: 6 additions & 5 deletions
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.ChainBaseManager;
1618

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

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

2328
// for test
@@ -106,13 +111,9 @@ public void init(FilterConfig filterConfig) throws ServletException {
106111
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
107112
FilterChain filterChain) throws IOException, ServletException {
108113
String requestPath = ((HttpServletRequest) servletRequest).getRequestURI();
109-
boolean shouldBeFiltered = false;
110-
if (CommonParameter.getInstance().isLiteFullNode
114+
if (chainBaseManager.isLiteNode()
111115
&& !CommonParameter.getInstance().openHistoryQueryWhenLiteFN
112116
&& filterPaths.contains(requestPath)) {
113-
shouldBeFiltered = true;
114-
}
115-
if (shouldBeFiltered) {
116117
servletResponse.setContentType("application/json; charset=utf-8");
117118
servletResponse.getWriter().write("this API is closed because this node is a lite fullnode");
118119
} else {

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 {

0 commit comments

Comments
 (0)