Skip to content

Commit c8ea8bd

Browse files
committed
refactor: init genesis block logic, checkstyle, default output directory, determine if there is block
1 parent c269288 commit c8ea8bd

10 files changed

Lines changed: 68 additions & 18 deletions

File tree

src/main/java/org/tron/common/application/Module.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.google.inject.Singleton;
2424
import javax.inject.Named;
2525
import org.tron.common.storage.leveldb.LevelDbDataSourceImpl;
26-
import org.tron.core.Constant;
26+
import org.tron.core.config.args.Args;
2727

2828
public class Module extends AbstractModule {
2929

@@ -39,7 +39,7 @@ protected void configure() {
3939
@Singleton
4040
@Named("transaction")
4141
public LevelDbDataSourceImpl buildTransactionDb() {
42-
LevelDbDataSourceImpl db = new LevelDbDataSourceImpl(Constant.OUTPUT_DIR,
42+
LevelDbDataSourceImpl db = new LevelDbDataSourceImpl(Args.getInstance().getOutputDirectory(),
4343
TRANSACTION_DB_NAME);
4444
db.initDB();
4545
return db;
@@ -52,7 +52,7 @@ public LevelDbDataSourceImpl buildTransactionDb() {
5252
@Singleton
5353
@Named("block")
5454
public LevelDbDataSourceImpl buildBlockDb() {
55-
LevelDbDataSourceImpl db = new LevelDbDataSourceImpl(Constant.OUTPUT_DIR,
55+
LevelDbDataSourceImpl db = new LevelDbDataSourceImpl(Args.getInstance().getOutputDirectory(),
5656
BLOCK_DB_NAME);
5757
db.initDB();
5858
return db;

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
package org.tron.core;
1717

18-
import java.io.File;
1918
import org.tron.common.utils.ByteArray;
2019

2120
public class Constant {
@@ -34,6 +33,4 @@ public class Constant {
3433
public static final String NORMAL_CONF = "config.conf";
3534
public static final String TEST_CONF = "config-test.conf";
3635
public static final String DATABASE_DIR = "storage.directory";
37-
public static final String OUTPUT_DIR = "output-directory" + File.separator;
38-
3936
}

src/main/java/org/tron/core/capsule/utils/BlockUtil.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public static BlockCapsule newGenesisBlockCapsule() {
4646

4747
BlockCapsule blockCapsule = new BlockCapsule(timestamp, parentHash, number, transactionList);
4848

49+
blockCapsule.setMerklerRoot();
50+
blockCapsule.sign(Args.getInstance().getPrivateKey().getBytes());
51+
blockCapsule.generatedByMyself = true;
52+
4953
return blockCapsule;
5054
}
5155

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class Args {
1919
private static final Args INSTANCE = new Args();
2020

2121
@Parameter(names = {"-d", "--output-directory"}, description = "Directory")
22-
private String outputDirectory = new String("");
22+
private String outputDirectory = new String("output-directory");
2323

2424
@Parameter(names = {"-h", "--help"}, help = true, description = "Directory")
2525
private boolean help = false;

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import org.slf4j.LoggerFactory;
2424
import org.tron.common.storage.leveldb.LevelDbDataSourceImpl;
2525
import org.tron.common.utils.ByteArray;
26-
import org.tron.core.Constant;
2726
import org.tron.core.Sha256Hash;
2827
import org.tron.core.capsule.BlockCapsule;
2928
import org.tron.core.capsule.TransactionCapsule;
29+
import org.tron.core.config.args.Args;
3030

3131
public class BlockStore extends TronDatabase {
3232

@@ -43,7 +43,7 @@ public class BlockStore extends TronDatabase {
4343
private BlockStore(String dbName) {
4444
super(dbName);
4545
numHashCache = new LevelDbDataSourceImpl(
46-
Constant.OUTPUT_DIR, dbName + "_NUM_HASH");
46+
Args.getInstance().getOutputDirectory(), dbName + "_NUM_HASH");
4747
numHashCache.initDB();
4848
khaosDb = new KhaosDatabase(dbName + "_KDB");
4949
}
@@ -156,6 +156,24 @@ public boolean containBlock(Sha256Hash blockHash) {
156156
return false;
157157
}
158158

159+
/**
160+
* judge has blocks.
161+
*/
162+
public boolean hasBlocks() {
163+
if (dbSource.allKeys().isEmpty()) {
164+
return false;
165+
}
166+
167+
if (khaosDb.hasData()) {
168+
return true;
169+
}
170+
171+
return true;
172+
}
173+
174+
/**
175+
* push transaction into db.
176+
*/
159177
public boolean pushTransactions(TransactionCapsule trx) {
160178
logger.info("push transaction");
161179
if (!trx.validateSignature()) {
@@ -185,7 +203,7 @@ public void pushBlock(BlockCapsule block) {
185203
return;
186204
}
187205

188-
block.getTransactions().forEach(trx->{
206+
block.getTransactions().forEach(trx -> {
189207
if (!pushTransactions(trx)) {
190208
return;
191209
}

src/main/java/org/tron/core/db/KhaosDatabase.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,7 @@ public Pair<ArrayList<BlockCapsule>, ArrayList<BlockCapsule>> getBranch(Sha256Ha
232232
return ret;
233233
}
234234

235+
public boolean hasData() {
236+
return !this.miniStore.hashKblkMap.isEmpty();
237+
}
235238
}

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.tron.core.capsule.BlockCapsule;
1212
import org.tron.core.capsule.TransactionCapsule;
1313
import org.tron.core.capsule.WitnessCapsule;
14+
import org.tron.core.capsule.utils.BlockUtil;
15+
import org.tron.core.config.args.Args;
1416
import org.tron.protos.Protocal.Transaction;
1517

1618
public class Manager {
@@ -110,6 +112,27 @@ public void init() {
110112

111113
pendingTrxs = new ArrayList<>();
112114

115+
initGenesis();
116+
}
117+
118+
/**
119+
* init genesis block.
120+
*/
121+
public void initGenesis() {
122+
BlockCapsule genesisBlockCapsule = BlockUtil.newGenesisBlockCapsule();
123+
if (this.getBlockStore().containBlock(genesisBlockCapsule.getBlockId())) {
124+
Args.getInstance().setChainId(genesisBlockCapsule.getBlockId().toString());
125+
} else {
126+
if (this.getBlockStore().hasBlocks()) {
127+
logger.error("genesis block modify, please delete database directory({}) and restart",
128+
Args.getInstance().getOutputDirectory());
129+
System.exit(1);
130+
} else {
131+
logger.info("create genesis block");
132+
Args.getInstance().setChainId(genesisBlockCapsule.getBlockId().toString());
133+
this.getBlockStore().pushBlock(genesisBlockCapsule);
134+
}
135+
}
113136
}
114137

115138
public AccountStore getAccountStore() {

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

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

33
import org.tron.common.storage.leveldb.LevelDbDataSourceImpl;
4-
import org.tron.core.Constant;
4+
import org.tron.core.config.args.Args;
55

66
public abstract class TronDatabase {
77

88
protected LevelDbDataSourceImpl dbSource;
99

1010
protected TronDatabase(String dbName) {
11-
dbSource = new LevelDbDataSourceImpl(Constant.OUTPUT_DIR, dbName);
11+
dbSource = new LevelDbDataSourceImpl(Args.getInstance().getOutputDirectory(), dbName);
1212
dbSource.initDB();
1313
}
1414

src/test/java/org/tron/common/storage/leveldb/LevelDbDataSourceImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class LevelDbDataSourceImplTest {
3333
@Before
3434
public void setup() {
3535
Args.setParam(new String[]{}, Configuration.getByPath(Constant.TEST_CONF));
36-
dataSource = new LevelDbDataSourceImpl(Constant.OUTPUT_DIR, "test");
36+
dataSource = new LevelDbDataSourceImpl(Args.getInstance().getOutputDirectory(), "test");
3737
}
3838

3939
@Test

src/test/java/org/tron/storage/leveldb/LevelDbDataSourceImplTest.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public void init() {
4040

4141
@Test
4242
public void testGet() {
43-
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(Constant.OUTPUT_DIR,"test");
43+
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(
44+
Args.getInstance().getOutputDirectory(), "test");
4445
dataSource.initDB();
4546
String key1 = "000134yyyhy";
4647
byte[] key = key1.getBytes();
@@ -52,7 +53,8 @@ public void testGet() {
5253

5354
@Test
5455
public void testGetBlock() {
55-
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(Constant.OUTPUT_DIR,
56+
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(
57+
Args.getInstance().getOutputDirectory(),
5658
"properties");
5759
dataSource.initDB();
5860
String key1 = "latest_block_header_number";
@@ -65,7 +67,8 @@ public void testGetBlock() {
6567

6668
@Test
6769
public void testPutBloc() {
68-
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(Constant.OUTPUT_DIR,
70+
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(
71+
Args.getInstance().getOutputDirectory(),
6972
"block");
7073
dataSource.initDB();
7174
String key1 = "latest_block_header_number";
@@ -84,7 +87,8 @@ public void testPutBloc() {
8487

8588
@Test
8689
public void testPut() {
87-
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(Constant.OUTPUT_DIR,
90+
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(
91+
Args.getInstance().getOutputDirectory(),
8892
"test");
8993
dataSource.initDB();
9094
String key1 = "000134yyyhy";
@@ -104,7 +108,8 @@ public void testPut() {
104108
@Test
105109
public void testRest() {
106110

107-
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(Constant.OUTPUT_DIR, "test");
111+
LevelDbDataSourceImpl dataSource = new LevelDbDataSourceImpl(
112+
Args.getInstance().getOutputDirectory(), "test");
108113
dataSource.resetDb();
109114
dataSource.closeDB();
110115
}

0 commit comments

Comments
 (0)