Skip to content

Commit

Permalink
add switch for index in SolidityNode
Browse files Browse the repository at this point in the history
  • Loading branch information
Yrp committed Oct 18, 2018
1 parent ecf806f commit 1d5469a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/main/java/org/tron/core/config/DefaultConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.tron.core.config;

import org.apache.commons.lang3.BooleanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowire;
Expand Down Expand Up @@ -32,10 +33,11 @@ public DefaultConfig() {

@Bean
public IndexHelper indexHelper() {
if (!Args.getInstance().isSolidityNode()) {
return null;
if (Args.getInstance().isSolidityNode()
&& BooleanUtils.toBoolean(Args.getInstance().getStorage().getIndexSwitch())) {
return new IndexHelper();
}
return new IndexHelper();
return null;
}

@Bean
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/tron/core/config/args/Args.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ public class Args {
@Parameter(names = {"--storage-index-directory"}, description = "Storage index directory")
private String storageIndexDirectory = "";

@Parameter(names = {"--storage-index-switch"}, description = "Storage index switch.(on or off)")
private String storageIndexSwitch = "";

@Getter
private Storage storage;

Expand Down Expand Up @@ -338,6 +341,7 @@ public static void clearParam() {
INSTANCE.privateKey = "";
INSTANCE.storageDbDirectory = "";
INSTANCE.storageIndexDirectory = "";
INSTANCE.storageIndexSwitch = "";

// FIXME: INSTANCE.storage maybe null ?
if (INSTANCE.storage != null) {
Expand Down Expand Up @@ -482,6 +486,10 @@ public static void setParam(final String[] args, final String confFileName) {
.filter(StringUtils::isNotEmpty)
.orElse(Storage.getIndexDirectoryFromConfig(config)));

INSTANCE.storage.setIndexSwitch(Optional.ofNullable(INSTANCE.storageIndexSwitch)
.filter(StringUtils::isNotEmpty)
.orElse(Storage.getIndexSwitchFromConfig(config)));

INSTANCE.storage.setPropertyMapFromConfig(config);

INSTANCE.seedNode = new SeedNode();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/tron/core/config/args/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.typesafe.config.Config;
import com.typesafe.config.ConfigObject;

import org.apache.commons.lang3.StringUtils;
import org.iq80.leveldb.CompressionType;
import org.iq80.leveldb.Options;

Expand Down Expand Up @@ -46,6 +47,7 @@ public class Storage {
private static final String DB_DIRECTORY_CONFIG_KEY = "storage.db.directory";
private static final String DB_VERSION_CONFIG_KEY = "storage.db.version";
private static final String INDEX_DIRECTORY_CONFIG_KEY = "storage.index.directory";
private static final String INDEX_SWITCH_CONFIG_KEY = "storage.index.switch";
private static final String PROPERTIES_CONFIG_KEY = "storage.properties";

private static final String NAME_CONFIG_KEY = "name";
Expand All @@ -65,6 +67,7 @@ public class Storage {
private static final int DEFAULT_DB_VERSION = 1;
private static final String DEFAULT_DB_DIRECTORY = "database";
private static final String DEFAULT_INDEX_DIRECTORY = "index";
private static final String DEFAULT_INDEX_SWTICH = "on";

/**
* Default values of db options:
Expand Down Expand Up @@ -99,6 +102,10 @@ public class Storage {
@Setter
private String indexDirectory;

@Getter
@Setter
private String indexSwitch;

/**
* Other custom database configurations
*/
Expand Down Expand Up @@ -130,6 +137,12 @@ public static String getIndexDirectoryFromConfig(final Config config) {
config.getString(INDEX_DIRECTORY_CONFIG_KEY) : DEFAULT_INDEX_DIRECTORY;
}

public static String getIndexSwitchFromConfig(final Config config) {
return config.hasPath(INDEX_SWITCH_CONFIG_KEY)
&& StringUtils.isNotEmpty(config.getString(INDEX_SWITCH_CONFIG_KEY)) ?
config.getString(INDEX_SWITCH_CONFIG_KEY) : DEFAULT_INDEX_SWTICH;
}

/**
* Set propertyMap of Storage object from Config
*
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/tron/core/db/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,13 @@ public void pushVerifiedBlock(BlockCapsule block) throws ContractValidateExcepti
VMIllegalException, TooBigTransactionResultException, UnLinkedBlockException,
NonCommonBlockException, BadNumberBlockException, BadBlockException {
block.generatedByMyself = true;
long start = System.currentTimeMillis();
pushBlock(block);
logger.info("push block cost:{}ms, blockNum:{}, blockHash:{}, trx count:{}",
System.currentTimeMillis() - start,
block.getNum(),
block.getBlockId(),
block.getTransactions().size());
}

private void applyBlock(BlockCapsule block) throws ContractValidateException,
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/tron/core/db/TransactionStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public TransactionCapsule get(byte[] key) throws BadItemException {
/**
* get total transaction.
*/
@Deprecated
public long getTotalTransactions() {
return Streams.stream(iterator()).count();
return 0; //Streams.stream(iterator()).count();
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/tron/core/db/api/IndexHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class IndexHelper {
@Resource
private Index.Iface<AssetIssueContract> assetIssueIndex;

@PostConstruct
//@PostConstruct
public void init() {
transactionIndex.fill();
//blockIndex.fill();
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/tron/program/SolidityNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -163,6 +164,18 @@ private void syncSolidityBlock() throws BadBlockException {
logger.info("Sync with trust node completed!!!");
}

private void resolveCompatibilityIssueIfUsingFullNodeDatabase() {
long lastSolidityBlockNum = dbManager.getDynamicPropertiesStore().getLatestSolidifiedBlockNum();
long headBlockNum = dbManager.getHeadBlockNum();
logger.info("headBlockNum:{}, solidityBlockNum:{}, diff:{}",
headBlockNum, lastSolidityBlockNum, headBlockNum - lastSolidityBlockNum);
if (lastSolidityBlockNum < headBlockNum) {
logger.info("use fullnode database, headBlockNum:{}, solidityBlockNum:{}, diff:{}",
headBlockNum, lastSolidityBlockNum, headBlockNum - lastSolidityBlockNum);
dbManager.getDynamicPropertiesStore().saveLatestSolidifiedBlockNum(headBlockNum);
}
}

private void start(Args cfgArgs) {
syncExecutor.scheduleWithFixedDelay(() -> {
try {
Expand All @@ -184,6 +197,8 @@ public static void main(String[] args) throws InterruptedException {
Args.setParam(args, Constant.TESTNET_CONF);
Args cfgArgs = Args.getInstance();

logger.info("index switch is {}",
BooleanUtils.toStringOnOff(BooleanUtils.toBoolean(cfgArgs.getStorage().getIndexSwitch())));
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger)LoggerFactory
.getLogger(Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.toLevel(cfgArgs.getLogLevel()));
Expand Down Expand Up @@ -224,6 +239,7 @@ public static void main(String[] args) throws InterruptedException {

SolidityNode node = new SolidityNode();
node.setDbManager(appT.getDbManager());
node.resolveCompatibilityIssueIfUsingFullNodeDatabase();
node.start(cfgArgs);

rpcApiService.blockUntilShutdown();
Expand Down

0 comments on commit 1d5469a

Please sign in to comment.