2424import org .springframework .stereotype .Component ;
2525import org .tron .common .crypto .ECKey ;
2626import org .tron .common .overlay .discover .Node ;
27- import org .tron .common .storage .leveldb .LevelDbDataSourceImpl ;
2827import org .tron .common .utils .ByteArray ;
2928import org .tron .common .utils .DialogOptional ;
3029import org .tron .common .utils .Sha256Hash ;
3534import org .tron .core .capsule .AccountCapsule ;
3635import org .tron .core .capsule .BlockCapsule ;
3736import org .tron .core .capsule .BlockCapsule .BlockId ;
37+ import org .tron .core .capsule .BytesCapsule ;
3838import org .tron .core .capsule .TransactionCapsule ;
3939import org .tron .core .capsule .TransactionResultCapsule ;
4040import org .tron .core .capsule .WitnessCapsule ;
@@ -65,20 +65,22 @@ public class Manager {
6565 private static final long TRXS_SIZE = 2_000_000 ; // < 2MiB
6666 public static final long LOOP_INTERVAL = 5000L ; // ms,produce block period, must be divisible by 60. millisecond
6767
68+
69+ // db store
6870 private AccountStore accountStore ;
6971 private TransactionStore transactionStore ;
7072 private BlockStore blockStore ;
7173 private UtxoStore utxoStore ;
7274 private WitnessStore witnessStore ;
7375 private AssetIssueStore assetIssueStore ;
7476 private DynamicPropertiesStore dynamicPropertiesStore ;
77+ private BlockIndexStore blockIndexStore ;
7578
7679 @ Autowired
7780 private PeersStore peersStore ;
7881 private BlockCapsule genesisBlock ;
7982
8083
81- private LevelDbDataSourceImpl numHashCache ;
8284 private KhaosDatabase khaosDb ;
8385 private RevokingDatabase revokingStore ;
8486 @ Getter
@@ -220,19 +222,16 @@ public void init() {
220222 this .setAssetIssueStore (AssetIssueStore .create ("asset-issue" ));
221223 this .setDynamicPropertiesStore (DynamicPropertiesStore .create ("properties" ));
222224 this .setWitnessController (WitnessController .createInstance (this ));
223-
225+ this . setBlockIndexStore ( BlockIndexStore . create ( "block-index" ));
224226 revokingStore = RevokingStore .getInstance ();
225227 revokingStore .enable ();
226- this .numHashCache = new LevelDbDataSourceImpl (
227- Args .getInstance ().getOutputDirectory (), "block" + "_NUM_HASH" );
228- this .numHashCache .initDB ();
229228 this .khaosDb = new KhaosDatabase ("block" + "_KDB" );
230229 this .pendingTransactions = new ArrayList <>();
231230 this .initGenesis ();
232231 this .witnessController .initWits ();
233232 this .khaosDb .start (genesisBlock );
234233 }
235-
234+
236235 public BlockId getGenesisBlockId () {
237236 return this .genesisBlock .getBlockId ();
238237 }
@@ -259,8 +258,8 @@ public void initGenesis() {
259258 Args .getInstance ().setChainId (this .genesisBlock .getBlockId ().toString ());
260259 //this.pushBlock(this.genesisBlock);
261260 blockStore .put (this .genesisBlock .getBlockId ().getBytes (), this .genesisBlock );
262- this .numHashCache . putData (ByteArray .fromLong (this .genesisBlock .getNum ()),
263- this .genesisBlock .getBlockId ().getBytes ());
261+ this .blockIndexStore . put (ByteArray .fromLong (this .genesisBlock .getNum ()),
262+ new BytesCapsule ( this .genesisBlock .getBlockId ().getBytes () ));
264263
265264 logger .info ("save block: " + this .genesisBlock );
266265 // init DynamicPropertiesStore
@@ -437,9 +436,10 @@ private void switchFork(BlockCapsule newHead) {
437436 // todo process the exception carefully later
438437 try (Dialog tmpDialog = revokingStore .buildDialog ()) {
439438 processBlock (item );
440- blockStore .put (item .getBlockId ().getBytes (), item );
441- this .numHashCache
442- .putData (ByteArray .fromLong (item .getNum ()), item .getBlockId ().getBytes ());
439+ this .blockStore .put (item .getBlockId ().getBytes (), item );
440+ this .blockIndexStore
441+ .put (ByteArray .fromLong (item .getNum ()),
442+ new BytesCapsule (item .getBlockId ().getBytes ()));
443443 tmpDialog .commit ();
444444 } catch (ValidateSignatureException e ) {
445445 logger .debug (e .getMessage (), e );
@@ -513,13 +513,14 @@ public void pushBlock(final BlockCapsule block)
513513 try (Dialog tmpDialog = revokingStore .buildDialog ()) {
514514 this .processBlock (newBlock );
515515 tmpDialog .commit ();
516+ blockStore .put (block .getBlockId ().getBytes (), block );
517+ this .blockIndexStore
518+ .put (ByteArray .fromLong (block .getNum ()),
519+ new BytesCapsule (block .getBlockId ().getBytes ()));
516520 } catch (RevokingStoreIllegalStateException e ) {
517521 logger .debug (e .getMessage (), e );
518522 }
519523 }
520- blockStore .put (block .getBlockId ().getBytes (), block );
521- this .numHashCache .putData (ByteArray .fromLong (block .getNum ()), block .getBlockId ().getBytes ());
522- //refreshHead(newBlock);
523524 logger .info ("save block: " + newBlock );
524525 }
525526 }
@@ -546,7 +547,6 @@ public void updateDynamicProperties(BlockCapsule block) {
546547 logger .warn ("missedBlocks [" + slot + "] is illegal" );
547548 }
548549
549- //this.head = block;
550550 logger .info ("update head, num = {}" , block .getNum ());
551551 this .dynamicPropertiesStore
552552 .saveLatestBlockHeaderHash (block .getBlockId ().getByteString ());
@@ -619,7 +619,6 @@ public void deleteBlock(final Sha256Hash blockHash)
619619 final BlockCapsule block = this .getBlockById (blockHash );
620620 this .khaosDb .removeBlk (blockHash );
621621 blockStore .delete (blockHash .getBytes ());
622- this .numHashCache .deleteData (ByteArray .fromLong (block .getNum ()));
623622 }
624623
625624 /**
@@ -655,8 +654,9 @@ public boolean processTransaction(final TransactionCapsule trxCap)
655654 /**
656655 * Get the block id from the number.
657656 */
658- public BlockId getBlockIdByNum (final long num ) {
659- final byte [] hash = this .numHashCache .getData (ByteArray .fromLong (num ));
657+ public BlockId getBlockIdByNum (final long num )
658+ throws BadItemException , ItemNotFoundException {
659+ final byte [] hash = this .blockIndexStore .get (ByteArray .fromLong (num )).getData ();
660660 return ArrayUtils .isEmpty (hash )
661661 ? this .genesisBlock .getBlockId ()
662662 : new BlockId (Sha256Hash .wrap (hash ), num );
@@ -891,4 +891,7 @@ public void setAssetIssueStore(AssetIssueStore assetIssueStore) {
891891 this .assetIssueStore = assetIssueStore ;
892892 }
893893
894+ public void setBlockIndexStore (BlockIndexStore indexStore ) {
895+ this .blockIndexStore = indexStore ;
896+ }
894897}
0 commit comments