2020import com .google .protobuf .InvalidProtocolBufferException ;
2121import org .slf4j .Logger ;
2222import org .slf4j .LoggerFactory ;
23- import org .tron .config .Configer ;
24- import org .tron .consensus .client .Client ;
23+ import org .tron .core .events .BlockchainListener ;
2524import org .tron .crypto .ECKey ;
26- import org .tron .example .Tron ;
2725import org .tron .overlay .Net ;
28- import org .tron .overlay .message .Message ;
29- import org .tron .overlay .message .Type ;
3026import org .tron .peer .Peer ;
31- import org .tron .peer .PeerType ;
3227import org .tron .protos .core .TronBlock .Block ;
3328import org .tron .protos .core .TronTXInput .TXInput ;
3429import org .tron .protos .core .TronTXOutput .TXOutput ;
3732import org .tron .storage .leveldb .LevelDbDataSourceImpl ;
3833import org .tron .utils .ByteArray ;
3934
40- import javax .inject .Inject ;
41- import javax .inject .Named ;
42- import java .io .File ;
4335import java .io .IOException ;
4436import java .io .InputStream ;
45- import java .nio .file .Paths ;
4637import java .util .*;
4738
48- import static org .tron .core .Constant .BLOCK_DB_NAME ;
4939import static org .tron .core .Constant .LAST_HASH ;
5040
5141public 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}
0 commit comments