Skip to content

Commit a58558a

Browse files
committed
Merge branch 'develop' of https://github.com/tronprotocol/java-tron into develop
2 parents 0ee3c70 + f144079 commit a58558a

104 files changed

Lines changed: 8047 additions & 119 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ database
2323

2424
# doc
2525
doc
26+
27+
# gossip
28+
pernodedata.*
29+
ringstate.*
30+
shareddata.*

build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ dependencies {
8080
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.44'
8181

8282
compile group: 'com.google.inject', name: 'guice', version: '4.1.0'
83+
84+
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.5'
85+
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.5'
86+
compile group: 'org.apache.commons', name: 'commons-math', version: '2.2'
87+
88+
compile group: 'io.dropwizard.metrics', name: 'metrics-core', version: '3.1.2'
8389
}
8490

8591
tasks.matching { it instanceof Test }.all {

src/main/java/org/tron/command/ConsensusCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void putClient(String[] args) {
5151
}
5252

5353
public void getClient(Peer peer) {
54-
if (Tron.getPeer().getType().equals(PeerType.PEER_SERVER)) {
54+
if (peer.getType().equals(PeerType.PEER_SERVER)) {
5555
client.getMessage(peer, MessageType.TRANSACTION);
5656
client.getMessage(peer, MessageType.BLOCK);
5757
} else {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.tron.consensus.client;
2+
3+
import org.tron.core.events.BlockchainListener;
4+
import org.tron.overlay.Net;
5+
import org.tron.overlay.message.Message;
6+
import org.tron.overlay.message.Type;
7+
import org.tron.peer.Peer;
8+
import org.tron.peer.PeerType;
9+
import org.tron.protos.core.TronBlock;
10+
import org.tron.utils.ByteArray;
11+
12+
public class BlockchainClientListener implements BlockchainListener {
13+
14+
private Client client;
15+
private Peer peer;
16+
17+
public BlockchainClientListener(Client client, Peer peer) {
18+
this.client = client;
19+
this.peer = peer;
20+
}
21+
22+
@Override
23+
public void addBlock(TronBlock.Block block) {
24+
String value = ByteArray.toHexString(block.toByteArray());
25+
26+
if (peer.getType().equals(PeerType.PEER_SERVER)) {
27+
Message message = new Message(value, Type.BLOCK);
28+
//net.broadcast(message);
29+
client.putMessage1(message); // consensus: put message
30+
}
31+
}
32+
33+
@Override
34+
public void addBlockNet(TronBlock.Block block, Net net) {
35+
if (peer.getType().equals(PeerType.PEER_SERVER)) {
36+
String value = ByteArray.toHexString(block.toByteArray());
37+
Message message = new Message(value, Type.BLOCK);
38+
net.broadcast(message);
39+
}
40+
}
41+
42+
@Override
43+
public void addGenesisBlock(TronBlock.Block block) {
44+
if (peer.getType().equals(PeerType.PEER_SERVER)) {
45+
String value = ByteArray.toHexString(block.toByteArray());
46+
Message message = new Message(value, Type.BLOCK);
47+
client.putMessage1(message); // consensus: put message GenesisBlock
48+
//Merely for the placeholders, no real meaning
49+
Message time = new Message(value, Type.TRANSACTION);
50+
client.putMessage1(time);
51+
}
52+
}
53+
}

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

Lines changed: 25 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,10 @@
2020
import com.google.protobuf.InvalidProtocolBufferException;
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
23-
import org.tron.config.Configer;
24-
import org.tron.consensus.client.Client;
23+
import org.tron.core.events.BlockchainListener;
2524
import org.tron.crypto.ECKey;
26-
import org.tron.example.Tron;
2725
import org.tron.overlay.Net;
28-
import org.tron.overlay.message.Message;
29-
import org.tron.overlay.message.Type;
3026
import org.tron.peer.Peer;
31-
import org.tron.peer.PeerType;
3227
import org.tron.protos.core.TronBlock.Block;
3328
import org.tron.protos.core.TronTXInput.TXInput;
3429
import org.tron.protos.core.TronTXOutput.TXOutput;
@@ -37,49 +32,38 @@
3732
import org.tron.storage.leveldb.LevelDbDataSourceImpl;
3833
import org.tron.utils.ByteArray;
3934

40-
import javax.inject.Inject;
41-
import javax.inject.Named;
42-
import java.io.File;
4335
import java.io.IOException;
4436
import java.io.InputStream;
45-
import java.nio.file.Paths;
4637
import java.util.*;
4738

48-
import static org.tron.core.Constant.BLOCK_DB_NAME;
4939
import static org.tron.core.Constant.LAST_HASH;
5040

5141
public class Blockchain {
5242

5343
public static final String GENESIS_COINBASE_DATA = "0x10";
54-
public static String parentName=Constant.NORMAL;
44+
public static String parentName = Constant.NORMAL;
5545

5646
public static final Logger logger = LoggerFactory.getLogger("BlockChain");
57-
private LevelDbDataSourceImpl blockDB = null;
47+
private LevelDbDataSourceImpl blockDB;
5848
private PendingState pendingState = new PendingStateImpl();
5949

6050
private byte[] lastHash;
6151
private byte[] currentHash;
6252

63-
private Client client;
53+
private List<BlockchainListener> listeners = new ArrayList<>();
6454

6555
/**
6656
* create new blockchain
6757
*
58+
* @param blockDB block database
6859
* @param address wallet address
60+
* @param type peer type
6961
*/
70-
public Blockchain(String address, String type) {
71-
if (dbExists()) {
72-
blockDB = new LevelDbDataSourceImpl(parentName,BLOCK_DB_NAME);
73-
blockDB.initDB();
74-
75-
this.lastHash = blockDB.getData(LAST_HASH);
76-
this.currentHash = this.lastHash;
77-
78-
logger.info("load blockchain");
79-
} else {
80-
blockDB = new LevelDbDataSourceImpl(Constant.NORMAL,BLOCK_DB_NAME);
81-
blockDB.initDB();
62+
public Blockchain(LevelDbDataSourceImpl blockDB, String address, String type) {
63+
this.blockDB = blockDB;
64+
this.lastHash = blockDB.getData(LAST_HASH);
8265

66+
if (this.lastHash == null) {
8367
InputStream is = getClass().getClassLoader().getResourceAsStream("genesis.json");
8468
String json = null;
8569
try {
@@ -115,38 +99,17 @@ public Blockchain(String address, String type) {
11599
.toByteArray();
116100
blockDB.putData(LAST_HASH, lastHash);
117101

118-
// put message to consensus
119-
if (type.equals(PeerType.PEER_SERVER) && client != null) {
120-
String value = ByteArray.toHexString(genesisBlock.toByteArray());
121-
Message message = new Message(value, Type.BLOCK);
122-
client.putMessage1(message); // consensus: put message GenesisBlock
123-
//Merely for the placeholders, no real meaning
124-
Message time = new Message(value, Type.TRANSACTION);
125-
client.putMessage1(time);
126-
102+
for (BlockchainListener listener : listeners) {
103+
listener.addGenesisBlock(genesisBlock);
127104
}
105+
128106
logger.info("new blockchain");
107+
} else {
108+
this.currentHash = this.lastHash;
109+
logger.info("load blockchain");
129110
}
130111
}
131112

132-
/**
133-
* create blockchain by db source
134-
*/
135-
@Inject
136-
public Blockchain(@Named("block") LevelDbDataSourceImpl blockDb) {
137-
if (!dbExists()) {
138-
logger.info("no existing blockchain found. please create one first");
139-
throw new IllegalStateException("No existing blockchain found. please create one first");
140-
}
141-
142-
blockDB = blockDb;
143-
144-
this.lastHash = blockDB.getData(LAST_HASH);
145-
this.currentHash = this.lastHash;
146-
147-
logger.info("load blockchain");
148-
}
149-
150113
/**
151114
* find transaction by id
152115
*
@@ -234,23 +197,6 @@ public HashMap<String, TXOutputs> findUTXO() {
234197
return utxo;
235198
}
236199

237-
/**
238-
* Checks if the database file exists
239-
*
240-
* @return boolean
241-
*/
242-
public static boolean dbExists() {
243-
if (Constant.NORMAL==parentName){
244-
parentName= Configer.getConf(Constant.NORMAL_CONF).getString(Constant.DATABASE_DIR);
245-
}else {
246-
parentName=Configer.getConf(Constant.TEST_CONF).getString(Constant.DATABASE_DIR);
247-
248-
}
249-
File file = new File(Paths.get(parentName, BLOCK_DB_NAME).toString());
250-
return file.exists();
251-
}
252-
253-
254200
/**
255201
* add a block into database
256202
*
@@ -309,11 +255,8 @@ public void addBlock(List<Transaction> transactions, Net net) {
309255
Block block = BlockUtils.newBlock(transactions, parentHash, difficulty,
310256
number);
311257

312-
String value = ByteArray.toHexString(block.toByteArray());
313-
314-
if (Tron.getPeer().getType().equals(PeerType.PEER_SERVER)) {
315-
Message message = new Message(value, Type.BLOCK);
316-
net.broadcast(message);
258+
for (BlockchainListener listener : listeners) {
259+
listener.addBlockNet(block, net);
317260
}
318261
}
319262

@@ -325,17 +268,10 @@ public void addBlock(List<Transaction> transactions) {
325268
long number = BlockUtils.getIncreaseNumber(this);
326269
// get difficulty
327270
ByteString difficulty = ByteString.copyFromUtf8(Constant.DIFFICULTY);
328-
Block block = BlockUtils.newBlock(transactions, parentHash, difficulty,
329-
number);
330-
331-
String value = ByteArray.toHexString(block.toByteArray());
332-
// View the type of peer
333-
//System.out.println(Tron.getPeer().getType());
271+
Block block = BlockUtils.newBlock(transactions, parentHash, difficulty, number);
334272

335-
if (Tron.getPeer().getType().equals(PeerType.PEER_SERVER) && client != null) {
336-
Message message = new Message(value, Type.BLOCK);
337-
//net.broadcast(message);
338-
client.putMessage1(message); // consensus: put message
273+
for (BlockchainListener listener : listeners) {
274+
listener.addBlock(block);
339275
}
340276
}
341277

@@ -373,6 +309,10 @@ public void receiveBlock(Block block, UTXOSet utxoSet, Peer peer) {
373309
utxoSet.reindex();
374310
}
375311

312+
public void addListener(BlockchainListener listener) {
313+
this.listeners.add(listener);
314+
}
315+
376316
public LevelDbDataSourceImpl getBlockDB() {
377317
return blockDB;
378318
}
@@ -404,8 +344,4 @@ public byte[] getCurrentHash() {
404344
public void setCurrentHash(byte[] currentHash) {
405345
this.currentHash = currentHash;
406346
}
407-
408-
public void setClient(Client client) {
409-
this.client = client;
410-
}
411347
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.tron.core.events;
2+
3+
import org.tron.overlay.Net;
4+
import org.tron.protos.core.TronBlock;
5+
6+
public interface BlockchainListener {
7+
8+
/**
9+
* New block added to blockchain
10+
*/
11+
void addBlock(TronBlock.Block block);
12+
13+
/**
14+
* New block added to blockchain
15+
* includes net reference
16+
*/
17+
void addBlockNet(TronBlock.Block block, Net net);
18+
19+
/**
20+
* Genesis block added to blockchain
21+
*/
22+
void addGenesisBlock(TronBlock.Block block);
23+
}

src/main/java/org/tron/example/Tron.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ public class Tron {
3131
@Parameter(names = {"--type", "-t"}, validateWith = PeerType.class)
3232
private String type = "normal";
3333

34-
private static Peer peer;
35-
36-
3734
public static void main(String[] args) {
3835
Tron tron = new Tron();
3936
JCommander.newBuilder()
@@ -51,7 +48,7 @@ public void run() {
5148
app.addService(new Server());
5249
app.run();
5350

54-
peer = app.getInjector().getInstance(PeerBuilder.class)
51+
Peer peer = app.getInjector().getInstance(PeerBuilder.class)
5552
.setKey(Configer.getMyKey())
5653
.setType(type)
5754
.build();
@@ -61,8 +58,4 @@ public void run() {
6158
Cli cli = new Cli();
6259
cli.run(app);
6360
}
64-
65-
public static Peer getPeer() {
66-
return peer;
67-
}
6861
}

0 commit comments

Comments
 (0)