1
1
import { RLP } from '@ethereumjs/rlp'
2
2
import {
3
- BIGINT_0 ,
4
3
EthereumJSErrorWithoutCode ,
5
4
bigIntToBytes ,
6
5
bytesToBigInt ,
@@ -17,6 +16,7 @@ import type { Block } from '@ethereumjs/block'
17
16
import type { Log } from '@ethereumjs/evm'
18
17
import type { TransactionType , TypedTransaction } from '@ethereumjs/tx'
19
18
import type { PostByzantiumTxReceipt , PreByzantiumTxReceipt , TxReceipt } from '@ethereumjs/vm'
19
+ import type { TxHashIndex } from './txIndex.ts'
20
20
21
21
/**
22
22
* TxReceiptWithType extends TxReceipt to provide:
@@ -49,30 +49,11 @@ type GetLogsReturn = {
49
49
logIndex : number
50
50
} [ ]
51
51
52
- /**
53
- * Indexes
54
- */
55
- type TxHashIndex = [ blockHash : Uint8Array , txIndex : number ]
56
-
57
- export type IndexType = ( typeof IndexType ) [ keyof typeof IndexType ]
58
-
59
- export const IndexType = {
60
- TxHash : 'txhash' ,
61
- } as const
62
-
63
- export type IndexOperation = ( typeof IndexOperation ) [ keyof typeof IndexOperation ]
64
-
65
- export const IndexOperation = {
66
- Save : 'save' ,
67
- Delete : 'delete' ,
68
- } as const
69
-
70
52
/**
71
53
* Storage encodings
72
54
*/
73
55
type rlpLog = Log
74
56
type rlpReceipt = [ postStateOrStatus : Uint8Array , cumulativeGasUsed : Uint8Array , logs : rlpLog [ ] ]
75
- type rlpTxHash = [ blockHash : Uint8Array , txIndex : Uint8Array ]
76
57
77
58
export type RlpConvert = ( typeof RlpConvert ) [ keyof typeof RlpConvert ]
78
59
@@ -85,9 +66,8 @@ export type RlpType = (typeof RlpType)[keyof typeof RlpType]
85
66
export const RlpType = {
86
67
Receipts : 'receipts' ,
87
68
Logs : 'logs' ,
88
- TxHash : 'txhash' ,
89
69
} as const
90
- type rlpOut = Log [ ] | TxReceipt [ ] | TxHashIndex
70
+ type rlpOut = Log [ ] | TxReceipt [ ]
91
71
92
72
export class ReceiptsManager extends MetaDBManager {
93
73
/**
@@ -114,12 +94,10 @@ export class ReceiptsManager extends MetaDBManager {
114
94
async saveReceipts ( block : Block , receipts : TxReceipt [ ] ) {
115
95
const encoded = this . rlp ( RlpConvert . Encode , RlpType . Receipts , receipts )
116
96
await this . put ( DBKey . Receipts , block . hash ( ) , encoded )
117
- void this . updateIndex ( IndexOperation . Save , IndexType . TxHash , block )
118
97
}
119
98
120
99
async deleteReceipts ( block : Block ) {
121
100
await this . delete ( DBKey . Receipts , block . hash ( ) )
122
- void this . updateIndex ( IndexOperation . Delete , IndexType . TxHash , block )
123
101
}
124
102
125
103
/**
@@ -166,9 +144,9 @@ export class ReceiptsManager extends MetaDBManager {
166
144
* Returns receipt by tx hash with additional metadata for the JSON RPC response, or null if not found
167
145
* @param txHash the tx hash
168
146
*/
169
- async getReceiptByTxHash ( txHash : Uint8Array ) : Promise < GetReceiptByTxHashReturn | null > {
170
- const txHashIndex = await this . getIndex ( IndexType . TxHash , txHash )
171
- if ( ! txHashIndex ) return null
147
+ async getReceiptByTxHashIndex (
148
+ txHashIndex : TxHashIndex ,
149
+ ) : Promise < GetReceiptByTxHashReturn | null > {
172
150
const [ blockHash , txIndex ] = txHashIndex
173
151
const receipts = await this . getReceipts ( blockHash )
174
152
if ( receipts . length === 0 ) return null
@@ -247,72 +225,6 @@ export class ReceiptsManager extends MetaDBManager {
247
225
return returnedLogs
248
226
}
249
227
250
- /**
251
- * Saves or deletes an index from the metaDB
252
- * @param operation the {@link IndexOperation}
253
- * @param type the {@link IndexType}
254
- * @param value for {@link IndexType.TxHash}, the block to save or delete the tx hash indexes for
255
- */
256
- private async updateIndex (
257
- operation : IndexOperation ,
258
- type : typeof IndexType . TxHash ,
259
- value : Block ,
260
- ) : Promise < void >
261
- private async updateIndex ( operation : IndexOperation , type : IndexType , value : any ) : Promise < void > {
262
- switch ( type ) {
263
- case IndexType . TxHash : {
264
- const block = value
265
- if ( operation === IndexOperation . Save ) {
266
- const withinTxLookupLimit =
267
- this . config . txLookupLimit === 0 ||
268
- this . chain . headers . height - BigInt ( this . config . txLookupLimit ) < block . header . number
269
- if ( withinTxLookupLimit ) {
270
- for ( const [ i , tx ] of block . transactions . entries ( ) ) {
271
- const index : TxHashIndex = [ block . hash ( ) , i ]
272
- const encoded = this . rlp ( RlpConvert . Encode , RlpType . TxHash , index )
273
- await this . put ( DBKey . TxHash , tx . hash ( ) , encoded )
274
- }
275
- }
276
- if ( this . config . txLookupLimit > 0 ) {
277
- // Remove tx hashes for one block past txLookupLimit
278
- const limit = this . chain . headers . height - BigInt ( this . config . txLookupLimit )
279
- if ( limit < BIGINT_0 ) return
280
- const blockDelIndexes = await this . chain . getBlock ( limit )
281
- void this . updateIndex ( IndexOperation . Delete , IndexType . TxHash , blockDelIndexes )
282
- }
283
- } else if ( operation === IndexOperation . Delete ) {
284
- for ( const tx of block . transactions ) {
285
- await this . delete ( DBKey . TxHash , tx . hash ( ) )
286
- }
287
- }
288
- break
289
- }
290
- default :
291
- throw EthereumJSErrorWithoutCode ( 'Unsupported index type' )
292
- }
293
- }
294
-
295
- /**
296
- * Returns the value for an index or null if not found
297
- * @param type the {@link IndexType}
298
- * @param value for {@link IndexType.TxHash}, the txHash to get
299
- */
300
- private async getIndex (
301
- type : typeof IndexType . TxHash ,
302
- value : Uint8Array ,
303
- ) : Promise < TxHashIndex | null >
304
- private async getIndex ( type : IndexType , value : Uint8Array ) : Promise < any | null > {
305
- switch ( type ) {
306
- case IndexType . TxHash : {
307
- const encoded = await this . get ( DBKey . TxHash , value )
308
- if ( ! encoded ) return null
309
- return this . rlp ( RlpConvert . Decode , RlpType . TxHash , encoded )
310
- }
311
- default :
312
- throw EthereumJSErrorWithoutCode ( 'Unsupported index type' )
313
- }
314
- }
315
-
316
228
/**
317
229
* RLP encodes or decodes the specified data type for storage or retrieval from the metaDB
318
230
* @param conversion {@link RlpConvert.Encode } or {@link RlpConvert.Decode }
@@ -330,11 +242,6 @@ export class ReceiptsManager extends MetaDBManager {
330
242
type : typeof RlpType . Logs ,
331
243
value : rlpLog [ ] ,
332
244
) : Log [ ]
333
- private rlp (
334
- conversion : typeof RlpConvert . Decode ,
335
- type : typeof RlpType . TxHash ,
336
- value : Uint8Array ,
337
- ) : TxHashIndex
338
245
private rlp (
339
246
conversion : RlpConvert ,
340
247
type : RlpType ,
@@ -380,14 +287,6 @@ export class ReceiptsManager extends MetaDBManager {
380
287
} else {
381
288
return RLP . decode ( value as Uint8Array ) as Log [ ]
382
289
}
383
- case RlpType . TxHash :
384
- if ( conversion === RlpConvert . Encode ) {
385
- const [ blockHash , txIndex ] = value as TxHashIndex
386
- return RLP . encode ( [ blockHash , intToBytes ( txIndex ) ] )
387
- } else {
388
- const [ blockHash , txIndex ] = RLP . decode ( value as Uint8Array ) as unknown as rlpTxHash
389
- return [ blockHash , bytesToInt ( txIndex ) ] as TxHashIndex
390
- }
391
290
default :
392
291
throw EthereumJSErrorWithoutCode ( 'Unknown rlp conversion' )
393
292
}
0 commit comments