@@ -246,47 +246,40 @@ func DeleteBlockHeaderByNumber(w db.KeyValueWriter, number uint64) error {
246246 return w .Delete (db .BlockHeaderByNumberKey (number ))
247247}
248248
249- func GetReceiptByHash (
249+ func GetReceiptByBlockNumberAndIndex (
250250 r db.KeyValueReader ,
251- hash * felt.TransactionHash ,
251+ blockNumber uint64 ,
252+ index uint64 ,
252253) (* TransactionReceipt , error ) {
253- val , err := TransactionBlockNumbersAndIndicesByHashBucket . RawValue (). Get (r , hash )
254+ blockTransactions , err := BlockTransactionsBucket . Get (r , blockNumber )
254255 if err != nil {
255256 return nil , err
256257 }
257258
258- receipt , err := ReceiptsByBlockNumberAndIndexBucket .RawKey ().Get (r , val )
259- if err != nil {
260- return nil , err
259+ if index >= uint64 (len (blockTransactions .Receipts )) {
260+ return nil , db .ErrKeyNotFound
261261 }
262- return & receipt , nil
262+
263+ return blockTransactions .Receipts [index ], nil
263264}
264265
265266func DeleteTxsAndReceipts (batch db.IndexedBatch , blockNum , numTxs uint64 ) error {
266- // remove txs and receipts
267- for i := range numTxs {
268- key := db.BlockNumIndexKey {
269- Number : blockNum ,
270- Index : i ,
271- }
272- txn , err := TransactionsByBlockNumberAndIndexBucket .Get (batch , key )
273- if err != nil {
274- return err
275- }
267+ blockTransactions , err := BlockTransactionsBucket .Get (batch , blockNum )
268+ if err != nil {
269+ return err
270+ }
276271
277- if err := TransactionsByBlockNumberAndIndexBucket .Delete (batch , key ); err != nil {
278- return err
279- }
280- if err := ReceiptsByBlockNumberAndIndexBucket .Delete (batch , key ); err != nil {
281- return err
282- }
272+ if err := BlockTransactionsBucket .Delete (batch , blockNum ); err != nil {
273+ return err
274+ }
283275
284- txHash := (* felt .TransactionHash )(txn .Hash ())
276+ for _ , tx := range blockTransactions .Transactions {
277+ txHash := (* felt .TransactionHash )(tx .Hash ())
285278 if err := TransactionBlockNumbersAndIndicesByHashBucket .Delete (batch , txHash ); err != nil {
286279 return err
287280 }
288281
289- if l1handler , ok := txn .(* L1HandlerTransaction ); ok {
282+ if l1handler , ok := tx .(* L1HandlerTransaction ); ok {
290283 if err := DeleteL1HandlerTxnHashByMsgHash (batch , l1handler .MessageHash ()); err != nil {
291284 return err
292285 }
@@ -390,32 +383,25 @@ func WriteBlockHeader(w db.KeyValueWriter, header *Header) error {
390383
391384// Returns all transactions in a given block
392385func GetTxsByBlockNum (r db.KeyValueReader , blockNum uint64 ) ([]Transaction , error ) {
393- txs := []Transaction {}
394- txSeq := TransactionsByBlockNumberAndIndexBucket .Prefix ().Add (blockNum ).Scan (r )
395-
396- for tx , err := range txSeq {
397- if err != nil {
398- return nil , err
399- }
400- txs = append (txs , tx .Value )
386+ blockTransactions , err := BlockTransactionsBucket .Get (r , blockNum )
387+ if err != nil {
388+ return nil , err
401389 }
402390
403- return txs , nil
391+ return blockTransactions . Transactions , nil
404392}
405393
406394// Returns all receipts in a given block
407- func GetReceiptsByBlockNum (r db.KeyValueReader , blockNum uint64 ) ([]* TransactionReceipt , error ) {
408- receipts := []* TransactionReceipt {}
409- receiptSeq := ReceiptsByBlockNumberAndIndexBucket .Prefix ().Add (blockNum ).Scan (r )
410-
411- for receipt , err := range receiptSeq {
412- if err != nil {
413- return nil , err
414- }
415- receipts = append (receipts , & receipt .Value )
395+ func GetReceiptsByBlockNum (
396+ r db.KeyValueReader ,
397+ blockNum uint64 ,
398+ ) ([]* TransactionReceipt , error ) {
399+ blockTransactions , err := BlockTransactionsBucket .Get (r , blockNum )
400+ if err != nil {
401+ return nil , err
416402 }
417403
418- return receipts , nil
404+ return blockTransactions . Receipts , nil
419405}
420406
421407func GetBlockByNumber (r db.KeyValueReader , blockNum uint64 ) (* Block , error ) {
@@ -424,57 +410,71 @@ func GetBlockByNumber(r db.KeyValueReader, blockNum uint64) (*Block, error) {
424410 return nil , err
425411 }
426412
427- txs , err := GetTxsByBlockNum (r , blockNum )
428- if err != nil {
429- return nil , err
430- }
431-
432- receipts , err := GetReceiptsByBlockNum (r , blockNum )
413+ blockTransactions , err := BlockTransactionsBucket .Get (r , blockNum )
433414 if err != nil {
434415 return nil , err
435416 }
436417
437418 return & Block {
438419 Header : header ,
439- Transactions : txs ,
440- Receipts : receipts ,
420+ Transactions : blockTransactions . Transactions ,
421+ Receipts : blockTransactions . Receipts ,
441422 }, nil
442423}
443424
444- func WriteTxAndReceipt (
425+ func WriteTransactionsAndReceipts (
445426 w db.KeyValueWriter ,
446- num , index uint64 ,
447- tx Transaction ,
448- receipt * TransactionReceipt ,
427+ blockNumber uint64 ,
428+ transactions [] Transaction ,
429+ receipts [] * TransactionReceipt ,
449430) error {
450- txHash := (* felt .TransactionHash )(tx .Hash ())
451- key := db.BlockNumIndexKey {
452- Number : num ,
453- Index : index ,
431+ for index , tx := range transactions {
432+ txHash := (* felt .TransactionHash )(tx .Hash ())
433+ key := db.BlockNumIndexKey {
434+ Number : blockNumber ,
435+ Index : uint64 (index ),
436+ }
437+
438+ if err := TransactionBlockNumbersAndIndicesByHashBucket .Put (w , txHash , & key ); err != nil {
439+ return err
440+ }
454441 }
455442
456- if err := TransactionBlockNumbersAndIndicesByHashBucket .Put (w , txHash , & key ); err != nil {
443+ blockTransactions := BlockTransactions {
444+ Transactions : transactions ,
445+ Receipts : receipts ,
446+ }
447+ if err := BlockTransactionsBucket .Put (w , blockNumber , & blockTransactions ); err != nil {
457448 return err
458449 }
459450
460- if err := TransactionsByBlockNumberAndIndexBucket .Put (w , key , & tx ); err != nil {
461- return err
451+ return nil
452+ }
453+
454+ func GetTxByBlockNumberAndIndex (
455+ r db.KeyValueReader ,
456+ blockNumber uint64 ,
457+ index uint64 ,
458+ ) (Transaction , error ) {
459+ blockTransactions , err := BlockTransactionsBucket .Get (r , blockNumber )
460+ if err != nil {
461+ return nil , err
462462 }
463463
464- if err := ReceiptsByBlockNumberAndIndexBucket . Put ( w , key , receipt ); err != nil {
465- return err
464+ if index >= uint64 ( len ( blockTransactions . Transactions )) {
465+ return nil , db . ErrKeyNotFound
466466 }
467467
468- return nil
468+ return blockTransactions . Transactions [ index ], nil
469469}
470470
471471func GetTxByHash (r db.KeyValueReader , hash * felt.TransactionHash ) (Transaction , error ) {
472- val , err := TransactionBlockNumbersAndIndicesByHashBucket . RawValue () .Get (r , hash )
472+ blockNumIndex , err := TransactionBlockNumbersAndIndicesByHashBucket .Get (r , hash )
473473 if err != nil {
474474 return nil , err
475475 }
476476
477- return TransactionsByBlockNumberAndIndexBucket . RawKey (). Get ( r , val )
477+ return GetTxByBlockNumberAndIndex ( r , blockNumIndex . Number , blockNumIndex . Index )
478478}
479479
480480func GetAggregatedBloomFilter (r db.KeyValueReader , fromBlock , toBLock uint64 ) (AggregatedBloomFilter , error ) {
0 commit comments