Skip to content

Commit 4656507

Browse files
committed
refactor: Integrate typed buckets for transactions and receipts
1 parent d025c87 commit 4656507

File tree

4 files changed

+215
-168
lines changed

4 files changed

+215
-168
lines changed

blockchain/blockchain.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,24 +184,31 @@ func (b *Blockchain) L1HandlerTxnHash(msgHash *common.Hash) (felt.Felt, error) {
184184
// TransactionByBlockNumberAndIndex gets the transaction for a given block number and index.
185185
func (b *Blockchain) TransactionByBlockNumberAndIndex(blockNumber, index uint64) (core.Transaction, error) {
186186
b.listener.OnRead("TransactionByBlockNumberAndIndex")
187-
return core.GetTxByBlockNumIndex(b.database, blockNumber, index)
187+
return core.TransactionsByBlockNumberAndIndexBucket.Get(
188+
b.database,
189+
db.BlockNumIndexKey{
190+
Number: blockNumber,
191+
Index: index,
192+
},
193+
)
188194
}
189195

190196
// TransactionByHash gets the transaction for a given hash.
191197
func (b *Blockchain) TransactionByHash(hash *felt.Felt) (core.Transaction, error) {
192198
b.listener.OnRead("TransactionByHash")
193-
return core.GetTxByHash(b.database, hash)
199+
return core.GetTxByHash(b.database, (*felt.TransactionHash)(hash))
194200
}
195201

196202
// Receipt gets the transaction receipt for a given transaction hash.
197203
func (b *Blockchain) Receipt(hash *felt.Felt) (*core.TransactionReceipt, *felt.Felt, uint64, error) {
198204
b.listener.OnRead("Receipt")
199-
bnIndex, err := core.GetTxBlockNumIndexByHash(b.database, hash)
205+
txHash := (*felt.TransactionHash)(hash)
206+
bnIndex, err := core.TransactionBlockNumbersAndIndicesByHashBucket.Get(b.database, txHash)
200207
if err != nil {
201208
return nil, nil, 0, err
202209
}
203210

204-
receipt, err := core.GetReceiptByHash(b.database, hash)
211+
receipt, err := core.GetReceiptByHash(b.database, txHash)
205212
if err != nil {
206213
return nil, nil, 0, err
207214
}

core/accessors.go

Lines changed: 44 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/NethermindEth/juno/core/felt"
77
"github.com/NethermindEth/juno/db"
88
"github.com/NethermindEth/juno/encoder"
9-
"github.com/NethermindEth/juno/utils"
109
)
1110

1211
/**
@@ -247,66 +246,46 @@ func DeleteBlockHeaderByNumber(w db.KeyValueWriter, number uint64) error {
247246
return w.Delete(db.BlockHeaderByNumberKey(number))
248247
}
249248

250-
func GetReceiptByBlockNumIndexBytes(r db.KeyValueReader, bnIndex []byte) (*TransactionReceipt, error) {
251-
var (
252-
receipt *TransactionReceipt
253-
val []byte
254-
)
255-
err := r.Get(db.ReceiptByBlockNumIndexKeyBytes(bnIndex), func(data []byte) error {
256-
val = data
257-
return nil
258-
})
249+
func GetReceiptByHash(
250+
r db.KeyValueReader,
251+
hash *felt.TransactionHash,
252+
) (*TransactionReceipt, error) {
253+
val, err := TransactionBlockNumbersAndIndicesByHashBucket.RawValue().Get(r, hash)
259254
if err != nil {
260255
return nil, err
261256
}
262-
if err = encoder.Unmarshal(val, &receipt); err != nil {
263-
return nil, err
264-
}
265-
return receipt, nil
266-
}
267257

268-
func WriteReceiptByBlockNumIndex(w db.KeyValueWriter, num, index uint64, receipt *TransactionReceipt) error {
269-
data, err := encoder.Marshal(receipt)
270-
if err != nil {
271-
return err
272-
}
273-
return w.Put(db.ReceiptByBlockNumIndexKey(num, index), data)
274-
}
275-
276-
func DeleteReceiptByBlockNumIndex(w db.KeyValueWriter, num, index uint64) error {
277-
return w.Delete(db.ReceiptByBlockNumIndexKey(num, index))
278-
}
279-
280-
func GetReceiptByHash(r db.KeyValueReader, hash *felt.Felt) (*TransactionReceipt, error) {
281-
var val []byte
282-
err := r.Get(db.TxBlockNumIndexByHashKey(hash), func(data []byte) error {
283-
val = data
284-
return nil
285-
})
258+
receipt, err := ReceiptsByBlockNumberAndIndexBucket.RawKey().Get(r, val)
286259
if err != nil {
287260
return nil, err
288261
}
289-
290-
return GetReceiptByBlockNumIndexBytes(r, val)
262+
return &receipt, nil
291263
}
292264

293265
func DeleteTxsAndReceipts(batch db.IndexedBatch, blockNum, numTxs uint64) error {
294266
// remove txs and receipts
295267
for i := range numTxs {
296-
txn, err := GetTxByBlockNumIndex(batch, blockNum, i)
268+
key := db.BlockNumIndexKey{
269+
Number: blockNum,
270+
Index: i,
271+
}
272+
txn, err := TransactionsByBlockNumberAndIndexBucket.Get(batch, key)
297273
if err != nil {
298274
return err
299275
}
300276

301-
if err := DeleteTxByBlockNumIndex(batch, blockNum, i); err != nil {
277+
if err := TransactionsByBlockNumberAndIndexBucket.Delete(batch, key); err != nil {
302278
return err
303279
}
304-
if err := DeleteReceiptByBlockNumIndex(batch, blockNum, i); err != nil {
280+
if err := ReceiptsByBlockNumberAndIndexBucket.Delete(batch, key); err != nil {
305281
return err
306282
}
307-
if err := DeleteTxBlockNumIndexByHash(batch, txn.Hash()); err != nil {
283+
284+
txHash := (*felt.TransactionHash)(txn.Hash())
285+
if err := TransactionBlockNumbersAndIndicesByHashBucket.Delete(batch, txHash); err != nil {
308286
return err
309287
}
288+
310289
if l1handler, ok := txn.(*L1HandlerTransaction); ok {
311290
if err := DeleteL1HandlerTxnHashByMsgHash(batch, l1handler.MessageHash()); err != nil {
312291
return err
@@ -411,61 +390,29 @@ func WriteBlockHeader(w db.KeyValueWriter, header *Header) error {
411390

412391
// Returns all transactions in a given block
413392
func GetTxsByBlockNum(r db.KeyValueReader, blockNum uint64) ([]Transaction, error) {
414-
prefix := db.TransactionsByBlockNumberAndIndex.Key(MarshalBlockNumber(blockNum))
415-
416-
it, err := r.NewIterator(prefix, true)
417-
if err != nil {
418-
return nil, err
419-
}
420-
421-
var txs []Transaction
422-
for it.First(); it.Valid(); it.Next() {
423-
val, vErr := it.Value()
424-
if vErr != nil {
425-
return nil, utils.RunAndWrapOnError(it.Close, vErr)
426-
}
393+
txs := []Transaction{}
394+
txSeq := TransactionsByBlockNumberAndIndexBucket.Prefix().Add(blockNum).Scan(r)
427395

428-
var tx Transaction
429-
if err = encoder.Unmarshal(val, &tx); err != nil {
430-
return nil, utils.RunAndWrapOnError(it.Close, err)
396+
for tx, err := range txSeq {
397+
if err != nil {
398+
return nil, err
431399
}
432-
433-
txs = append(txs, tx)
434-
}
435-
436-
if err = it.Close(); err != nil {
437-
return nil, err
400+
txs = append(txs, tx.Value)
438401
}
439402

440403
return txs, nil
441404
}
442405

443406
// Returns all receipts in a given block
444407
func GetReceiptsByBlockNum(r db.KeyValueReader, blockNum uint64) ([]*TransactionReceipt, error) {
445-
prefix := db.ReceiptsByBlockNumberAndIndex.Key(MarshalBlockNumber(blockNum))
446-
447-
it, err := r.NewIterator(prefix, true)
448-
if err != nil {
449-
return nil, err
450-
}
408+
receipts := []*TransactionReceipt{}
409+
receiptSeq := ReceiptsByBlockNumberAndIndexBucket.Prefix().Add(blockNum).Scan(r)
451410

452-
var receipts []*TransactionReceipt
453-
for it.First(); it.Valid(); it.Next() {
454-
val, vErr := it.Value()
455-
if vErr != nil {
456-
return nil, utils.RunAndWrapOnError(it.Close, vErr)
457-
}
458-
459-
var receipt *TransactionReceipt
460-
if err = encoder.Unmarshal(val, &receipt); err != nil {
461-
return nil, utils.RunAndWrapOnError(it.Close, err)
411+
for receipt, err := range receiptSeq {
412+
if err != nil {
413+
return nil, err
462414
}
463-
464-
receipts = append(receipts, receipt)
465-
}
466-
467-
if err = it.Close(); err != nil {
468-
return nil, err
415+
receipts = append(receipts, &receipt.Value)
469416
}
470417

471418
return receipts, nil
@@ -494,85 +441,40 @@ func GetBlockByNumber(r db.KeyValueReader, blockNum uint64) (*Block, error) {
494441
}, nil
495442
}
496443

497-
func GetTxBlockNumIndexByHash(r db.KeyValueReader, hash *felt.Felt) (db.BlockNumIndexKey, error) {
498-
bnIndex := db.BlockNumIndexKey{}
499-
err := r.Get(db.TxBlockNumIndexByHashKey(hash), bnIndex.UnmarshalBinary)
500-
return bnIndex, err
501-
}
502-
503-
func WriteTxBlockNumIndexByHash(w db.KeyValueWriter, num, index uint64, hash *felt.Felt) error {
504-
val := db.BlockNumIndexKey{
444+
func WriteTxAndReceipt(
445+
w db.KeyValueWriter,
446+
num, index uint64,
447+
tx Transaction,
448+
receipt *TransactionReceipt,
449+
) error {
450+
txHash := (*felt.TransactionHash)(tx.Hash())
451+
key := db.BlockNumIndexKey{
505452
Number: num,
506453
Index: index,
507454
}
508-
return w.Put(db.TxBlockNumIndexByHashKey(hash), val.Marshal())
509-
}
510455

511-
func DeleteTxBlockNumIndexByHash(w db.KeyValueWriter, hash *felt.Felt) error {
512-
return w.Delete(db.TxBlockNumIndexByHashKey(hash))
513-
}
514-
515-
func GetTxByBlockNumIndex(r db.KeyValueReader, blockNum, index uint64) (Transaction, error) {
516-
var tx Transaction
517-
err := r.Get(db.TxByBlockNumIndexKey(blockNum, index), func(data []byte) error {
518-
return encoder.Unmarshal(data, &tx)
519-
})
520-
if err != nil {
521-
return nil, err
522-
}
523-
return tx, nil
524-
}
525-
526-
func GetTxByBlockNumIndexBytes(r db.KeyValueReader, val []byte) (Transaction, error) {
527-
var tx Transaction
528-
err := r.Get(db.TxByBlockNumIndexKeyBytes(val), func(data []byte) error {
529-
return encoder.Unmarshal(data, &tx)
530-
})
531-
if err != nil {
532-
return nil, err
533-
}
534-
return tx, nil
535-
}
536-
537-
func WriteTxByBlockNumIndex(w db.KeyValueWriter, num, index uint64, tx Transaction) error {
538-
enc, err := encoder.Marshal(tx)
539-
if err != nil {
540-
return err
541-
}
542-
return w.Put(db.TxByBlockNumIndexKey(num, index), enc)
543-
}
544-
545-
func DeleteTxByBlockNumIndex(w db.KeyValueWriter, num, index uint64) error {
546-
return w.Delete(db.TxByBlockNumIndexKey(num, index))
547-
}
548-
549-
func WriteTxAndReceipt(w db.KeyValueWriter, num, index uint64, tx Transaction, receipt *TransactionReceipt) error {
550-
if err := WriteTxBlockNumIndexByHash(w, num, index, receipt.TransactionHash); err != nil {
456+
if err := TransactionBlockNumbersAndIndicesByHashBucket.Put(w, txHash, &key); err != nil {
551457
return err
552458
}
553459

554-
if err := WriteTxByBlockNumIndex(w, num, index, tx); err != nil {
460+
if err := TransactionsByBlockNumberAndIndexBucket.Put(w, key, &tx); err != nil {
555461
return err
556462
}
557463

558-
if err := WriteReceiptByBlockNumIndex(w, num, index, receipt); err != nil {
464+
if err := ReceiptsByBlockNumberAndIndexBucket.Put(w, key, receipt); err != nil {
559465
return err
560466
}
561467

562468
return nil
563469
}
564470

565-
func GetTxByHash(r db.KeyValueReader, hash *felt.Felt) (Transaction, error) {
566-
var val []byte
567-
err := r.Get(db.TxBlockNumIndexByHashKey(hash), func(data []byte) error {
568-
val = data
569-
return nil
570-
})
471+
func GetTxByHash(r db.KeyValueReader, hash *felt.TransactionHash) (Transaction, error) {
472+
val, err := TransactionBlockNumbersAndIndicesByHashBucket.RawValue().Get(r, hash)
571473
if err != nil {
572474
return nil, err
573475
}
574476

575-
return GetTxByBlockNumIndexBytes(r, val)
477+
return TransactionsByBlockNumberAndIndexBucket.RawKey().Get(r, val)
576478
}
577479

578480
func GetAggregatedBloomFilter(r db.KeyValueReader, fromBlock, toBLock uint64) (AggregatedBloomFilter, error) {

0 commit comments

Comments
 (0)