@@ -43,6 +43,7 @@ const chainInfo = {
43
43
const stateDB = new Level ( __dirname + "/../../log/stateStore" , { valueEncoding : "buffer" } ) ;
44
44
const blockDB = new Level ( __dirname + "/../../log/blockStore" , { valueEncoding : "buffer" } ) ;
45
45
const bhashDB = new Level ( __dirname + "/../../log/bhashStore" , { valueEncoding : "buffer" } ) ;
46
+ const txhashDB = new Level ( __dirname + "/../../log/txhashStore" ) ;
46
47
const codeDB = new Level ( __dirname + "/../../log/codeStore" ) ;
47
48
48
49
async function startServer ( options ) {
@@ -122,6 +123,14 @@ async function startServer(options) {
122
123
await blockDB . put ( newBlock . blockNumber . toString ( ) , Buffer . from ( _message . data ) ) ; // Add block to chain
123
124
await bhashDB . put ( newBlock . hash , numToBuffer ( newBlock . blockNumber ) ) ; // Assign block number to the matching block hash
124
125
126
+ // Apply to all txns of the block: Assign transaction index and block number to transaction hash
127
+ for ( let txIndex = 0 ; txIndex < newBlock . transactions . length ; txIndex ++ ) {
128
+ const tx = Transaction . deserialize ( newBlock . transactions [ txIndex ] ) ;
129
+ const txHash = Transaction . getHash ( tx ) ;
130
+
131
+ await txhashDB . put ( txHash , newBlock . blockNumber . toString ( ) + " " + txIndex . toString ( ) ) ;
132
+ }
133
+
125
134
chainInfo . latestBlock = newBlock ; // Update latest block cache
126
135
127
136
// Update the new transaction pool (remove all the transactions that are no longer valid).
@@ -237,6 +246,14 @@ async function startServer(options) {
237
246
await blockDB . put ( block . blockNumber . toString ( ) , Buffer . from ( _message . data ) ) ; // Add block to chain
238
247
await bhashDB . put ( block . hash , numToBuffer ( block . blockNumber ) ) ; // Assign block number to the matching block hash
239
248
249
+ // Assign transaction index and block number to transaction hash
250
+ for ( let txIndex = 0 ; txIndex < block . transactions . length ; txIndex ++ ) {
251
+ const tx = Transaction . deserialize ( block . transactions [ txIndex ] ) ;
252
+ const txHash = Transaction . getHash ( tx ) ;
253
+
254
+ await txhashDB . put ( txHash , block . blockNumber . toString ( ) + " " + txIndex . toString ( ) ) ;
255
+ }
256
+
240
257
if ( ! chainInfo . latestSyncBlock ) {
241
258
chainInfo . latestSyncBlock = block ; // Update latest synced block.
242
259
@@ -349,7 +366,7 @@ async function startServer(options) {
349
366
}
350
367
351
368
if ( ENABLE_MINING ) loopMine ( publicKey , ENABLE_CHAIN_REQUEST , ENABLE_LOGGING ) ;
352
- if ( ENABLE_RPC ) rpc ( RPC_PORT , { publicKey, mining : ENABLE_MINING , chainInfo } , sendTransaction , keyPair , stateDB , blockDB , bhashDB , codeDB ) ;
369
+ if ( ENABLE_RPC ) rpc ( RPC_PORT , { publicKey, mining : ENABLE_MINING , chainInfo } , sendTransaction , keyPair , stateDB , blockDB , bhashDB , codeDB , txhashDB ) ;
353
370
}
354
371
355
372
// Function to connect to a node.
@@ -519,6 +536,14 @@ async function mine(publicKey, ENABLE_LOGGING) {
519
536
await blockDB . put ( result . blockNumber . toString ( ) , Buffer . from ( Block . serialize ( result ) ) ) ; // Add block to chain
520
537
await bhashDB . put ( result . hash , numToBuffer ( result . blockNumber ) ) ; // Assign block number to the matching block hash
521
538
539
+ // Assign transaction index and block number to transaction hash
540
+ for ( let txIndex = 0 ; txIndex < result . transactions . length ; txIndex ++ ) {
541
+ const tx = Transaction . deserialize ( result . transactions [ txIndex ] ) ;
542
+ const txHash = Transaction . getHash ( tx ) ;
543
+
544
+ await txhashDB . put ( txHash , result . blockNumber . toString ( ) + " " + txIndex . toString ( ) ) ;
545
+ }
546
+
522
547
chainInfo . latestBlock = result ; // Update latest block cache
523
548
524
549
// Reward
0 commit comments