Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions chainbase/src/main/java/org/tron/core/ChainBaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.google.protobuf.ByteString;
import java.util.List;
import javax.annotation.PostConstruct;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -236,6 +237,14 @@ public class ChainBaseManager {
@Autowired
private DbStatService dbStatService;

@Getter
@Setter
private NodeType nodeType;

@Getter
@Setter
private long lowestBlockNum = -1; // except num = 0.

public void closeOneStore(ITronChainBase database) {
logger.info("******** Begin to close {}. ********", database.getName());
try {
Expand Down Expand Up @@ -414,5 +423,28 @@ public static synchronized void init(ChainBaseManager manager) {
AssetUtil.setAccountAssetStore(manager.getAccountAssetStore());
AssetUtil.setDynamicPropertiesStore(manager.getDynamicPropertiesStore());
}

@PostConstruct
private void init() {
this.lowestBlockNum = this.blockIndexStore.getLimitNumber(1, 1).stream()
.map(BlockId::getNum).findFirst().orElse(0L);
this.nodeType = getLowestBlockNum() > 1 ? NodeType.LITE : NodeType.FULL;
}

public boolean isLiteNode() {
return getNodeType() == NodeType.LITE;
}

public enum NodeType {
FULL(0),
LITE(1);

@Getter
private final int type;

NodeType(int type) {
this.type = type;
}
}
}

22 changes: 20 additions & 2 deletions chainbase/src/main/java/org/tron/core/db/BlockIndexStore.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.tron.core.db;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -12,6 +16,7 @@
import org.tron.core.exception.ItemNotFoundException;

@Component
@Slf4j(topic = "DB")
public class BlockIndexStore extends TronStoreWithRevoking<BytesCapsule> {


Expand Down Expand Up @@ -44,4 +49,17 @@ public BytesCapsule get(byte[] key)
}
return new BytesCapsule(value);
}
}

public List<BlockId> getLimitNumber(long startNumber, long limit) {
return pack(revokingDB.getValuesNext(ByteArray.fromLong(startNumber), limit));
}

private List<BlockId> pack(Set<byte[]> values) {
List<BlockId> blocks = new ArrayList<>();
for (byte[] bytes : values) {
blocks.add(new BlockId(Sha256Hash.wrap(bytes)));
}
blocks.sort(Comparator.comparing(BlockId::getNum));
return blocks;
}
}
32 changes: 0 additions & 32 deletions chainbase/src/main/java/org/tron/core/db/CommonStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.tron.common.utils.ByteArray;
import org.tron.core.Constant;
import org.tron.core.capsule.BytesCapsule;

@Component
public class CommonStore extends TronDatabase<BytesCapsule> {

private static final byte[] DB_KEY_LOWEST_BLOCK_NUM = "lowest_block_num".getBytes();
private static final byte[] DB_KEY_NODE_TYPE = "node_type".getBytes();

@Autowired
public CommonStore(ApplicationContext ctx) {
super("common");
Expand All @@ -37,31 +32,4 @@ public BytesCapsule get(byte[] key) {
public boolean has(byte[] key) {
return dbSource.getData(key) != null;
}

public int getNodeType() {
int nodeType = 0;
byte[] bytes = get(DB_KEY_NODE_TYPE).getData();
if (bytes != null) {
nodeType = ByteArray.toInt(bytes);
}
return nodeType;
}

public void setNodeType(int nodeType) {
put(DB_KEY_NODE_TYPE, new BytesCapsule(ByteArray.fromInt(nodeType)));
}

public long getLowestBlockNum() {
long lowestBlockNum = 0;
byte[] bytes = get(DB_KEY_LOWEST_BLOCK_NUM).getData();
if (bytes != null) {
lowestBlockNum = ByteArray.toLong(bytes);
}
return lowestBlockNum;
}

public void setLowestBlockNum(long lowestBlockNum) {
put(DB_KEY_LOWEST_BLOCK_NUM, new BytesCapsule(ByteArray.fromLong(lowestBlockNum)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.google.common.annotations.VisibleForTesting;
import lombok.Getter;
import lombok.Setter;
import org.quartz.CronExpression;
Expand Down Expand Up @@ -526,10 +528,6 @@ public class CommonParameter {
@Setter
public boolean openHistoryQueryWhenLiteFN = false;

@Getter
@Setter
public boolean isLiteFullNode = false;

@Getter
@Setter
@Parameter(names = {"--history-balance-lookup"})
Expand Down
16 changes: 0 additions & 16 deletions framework/src/main/java/org/tron/core/config/args/Args.java
Original file line number Diff line number Diff line change
Expand Up @@ -1006,9 +1006,6 @@ public static void setParam(final String[] args, final String confFileName) {
.getBoolean(Constant.METRICS_PROMETHEUS_ENABLE);
PARAMETER.metricsPrometheusPort = config.hasPath(Constant.METRICS_PROMETHEUS_PORT) ? config
.getInt(Constant.METRICS_PROMETHEUS_PORT) : 9527;

// lite fullnode params
PARAMETER.setLiteFullNode(checkIsLiteFullNode());
PARAMETER.setOpenHistoryQueryWhenLiteFN(
config.hasPath(Constant.NODE_OPEN_HISTORY_QUERY_WHEN_LITEFN)
&& config.getBoolean(Constant.NODE_OPEN_HISTORY_QUERY_WHEN_LITEFN));
Expand Down Expand Up @@ -1506,19 +1503,6 @@ public static void setFullNodeAllowShieldedTransaction(boolean fullNodeAllowShie
PARAMETER.fullNodeAllowShieldedTransactionArgs = fullNodeAllowShieldedTransaction;
}

/**
* set isLiteFullNode=true when this node is a lite fullnode.
*/
public static boolean checkIsLiteFullNode() {
String infoFile = Paths.get(PARAMETER.outputDirectory,
PARAMETER.storage.getDbDirectory(), Constant.INFO_FILE_NAME).toString();
if (FileUtil.isExists(infoFile)) {
String value = PropUtil.readProperty(infoFile, Constant.SPLIT_BLOCK_NUM);
return !"".equals(value) && Long.parseLong(value) > 1;
}
return false;
}

private static void witnessAddressCheck(Config config) {
if (config.hasPath(Constant.LOCAL_WITNESS_ACCOUNT_ADDRESS)) {
byte[] bytes = Commons
Expand Down
7 changes: 5 additions & 2 deletions framework/src/main/java/org/tron/core/db/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,11 @@ public void init() {

long headNum = chainBaseManager.getDynamicPropertiesStore().getLatestBlockHeaderNumber();
logger.info("Current headNum is: {}.", headNum);
int nodeType = chainBaseManager.getCommonStore().getNodeType();
logger.info("Node type is: {}.", Constant.NODE_TYPE_LIGHT_NODE == nodeType ? "lite" : "full");
boolean isLite = chainBaseManager.isLiteNode();
logger.info("Node type is: {}.", isLite ? "lite" : "full");
if (isLite) {
logger.info("Lite node lowestNum: {}", chainBaseManager.getLowestBlockNum());
}
revokingStore.enable();
validateSignService = Executors
.newFixedThreadPool(Args.getInstance().getValidateSignThreadNum());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.StringUtil;
import org.tron.core.ChainBaseManager;
import org.tron.core.Constant;
import org.tron.core.capsule.BlockCapsule;
import org.tron.core.config.args.Args;
import org.tron.core.db.CommonStore;
import org.tron.core.net.message.MessageTypes;
import org.tron.core.net.message.TronMessage;
import org.tron.p2p.discover.Node;
Expand Down Expand Up @@ -56,24 +54,16 @@ public HelloMessage(Node from, long timestamp, ChainBaseManager chainBaseManager
.setHash(hid.getByteString())
.setNumber(hid.getNum())
.build();

CommonStore commonStore = chainBaseManager.getCommonStore();
long lowestBlockNum = 0;
int nodeType = commonStore.getNodeType();
if (nodeType == Constant.NODE_TYPE_LIGHT_NODE) {
lowestBlockNum = commonStore.getLowestBlockNum();
}

Builder builder = Protocol.HelloMessage.newBuilder();

builder.setFrom(fromEndpoint);
builder.setVersion(Args.getInstance().getNodeP2pVersion());
builder.setTimestamp(timestamp);
builder.setGenesisBlockId(gBlockId);
builder.setSolidBlockId(sBlockId);
builder.setHeadBlockId(hBlockId);
builder.setNodeType(nodeType);
builder.setLowestBlockNum(lowestBlockNum);
builder.setNodeType(chainBaseManager.getNodeType().getType());
builder.setLowestBlockNum(chainBaseManager.isLiteNode()
? chainBaseManager.getLowestBlockNum() : 0);

this.helloMessage = builder.build();
this.type = MessageTypes.P2P_HELLO.asByte();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
import io.grpc.ServerInterceptor;
import io.grpc.Status;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tron.common.parameter.CommonParameter;
import org.tron.core.ChainBaseManager;

@Component
public class LiteFnQueryGrpcInterceptor implements ServerInterceptor {

@Autowired
private ChainBaseManager chainBaseManager;
private static final Set<String> filterMethods = Sets.newHashSet();

// for test
Expand Down Expand Up @@ -75,13 +79,9 @@ public static Set<String> getFilterMethods() {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call,
Metadata headers, ServerCallHandler<ReqT, RespT> next) {
boolean shouldBeFiltered = false;
if (CommonParameter.getInstance().isLiteFullNode
if (chainBaseManager.isLiteNode()
&& !CommonParameter.getInstance().openHistoryQueryWhenLiteFN
&& filterMethods.contains(call.getMethodDescriptor().getFullMethodName())) {
shouldBeFiltered = true;
}
if (shouldBeFiltered) {
call.close(Status.UNAVAILABLE
.withDescription("this API is closed because this node is a lite fullnode"), headers);
return new ServerCall.Listener<ReqT>() {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tron.common.parameter.CommonParameter;
import org.tron.core.ChainBaseManager;

@Component
@Slf4j(topic = "API")
public class LiteFnQueryHttpFilter implements Filter {

@Autowired
private ChainBaseManager chainBaseManager;

private static Set<String> filterPaths = Sets.newHashSet();

// for test
Expand Down Expand Up @@ -106,13 +111,9 @@ public void init(FilterConfig filterConfig) throws ServletException {
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
String requestPath = ((HttpServletRequest) servletRequest).getRequestURI();
boolean shouldBeFiltered = false;
if (CommonParameter.getInstance().isLiteFullNode
if (chainBaseManager.isLiteNode()
&& !CommonParameter.getInstance().openHistoryQueryWhenLiteFN
&& filterPaths.contains(requestPath)) {
shouldBeFiltered = true;
}
if (shouldBeFiltered) {
servletResponse.setContentType("application/json; charset=utf-8");
servletResponse.getWriter().write("this API is closed because this node is a lite fullnode");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
@Slf4j(topic = "tool")
public class LiteFullNodeTool {

private static final byte[] DB_KEY_LOWEST_BLOCK_NUM = "lowest_block_num".getBytes();
private static final byte[] DB_KEY_NODE_TYPE = "node_type".getBytes();

private static final long START_TIME = System.currentTimeMillis() / 1000;

private static long RECENT_BLKS = 65536;
Expand All @@ -57,7 +54,6 @@ public class LiteFullNodeTool {
private static final String BLOCK_DB_NAME = "block";
private static final String BLOCK_INDEX_DB_NAME = "block-index";
private static final String TRANS_DB_NAME = "trans";
private static final String COMMON_DB_NAME = "common";
private static final String TRANSACTION_RET_DB_NAME = "transactionRetStore";
private static final String TRANSACTION_HISTORY_DB_NAME = "transactionHistoryStore";
private static final String PROPERTIES_DB_NAME = "properties";
Expand Down Expand Up @@ -328,10 +324,6 @@ private void fillSnapshotBlockAndTransDb(String sourceDir, String snapshotDir)
throw new RuntimeException(e.getMessage());
}
}

DBInterface destCommonDb = DbTool.getDB(snapshotDir, COMMON_DB_NAME);
destCommonDb.put(DB_KEY_NODE_TYPE, ByteArray.fromInt(Constant.NODE_TYPE_LIGHT_NODE));
destCommonDb.put(DB_KEY_LOWEST_BLOCK_NUM, ByteArray.fromLong(startIndex));
// copy engine.properties for block、block-index、trans from source if exist
copyEngineIfExist(sourceDir, snapshotDir, BLOCK_DB_NAME, BLOCK_INDEX_DB_NAME, TRANS_DB_NAME);
}
Expand Down Expand Up @@ -487,14 +479,6 @@ private static boolean isEmptyBytes(byte[] b) {
private void deleteSnapshotFlag(String databaseDir) throws IOException, RocksDBException {
logger.info("Delete the info file from {}.", databaseDir);
Files.delete(Paths.get(databaseDir, INFO_FILE_NAME));
if (!isLite(databaseDir)) {
DBInterface destCommonDb = DbTool.getDB(databaseDir, COMMON_DB_NAME);
destCommonDb.delete(DB_KEY_NODE_TYPE);
destCommonDb.delete(DB_KEY_LOWEST_BLOCK_NUM);
logger.info("Deleted {} and {} from {} to identify this node is a real fullnode.",
"node_type", "lowest_block_num", COMMON_DB_NAME);
}

}

private void hasEnoughBlock(String sourceDir) throws RocksDBException, IOException {
Expand Down
Loading