Skip to content

Commit 2dfe1e7

Browse files
committed
feat: Combined layout for transactions
1 parent efab75b commit 2dfe1e7

File tree

6 files changed

+103
-86
lines changed

6 files changed

+103
-86
lines changed

blockchain/blockchain.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,7 @@ 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.TransactionsByBlockNumberAndIndexBucket.Get(
188-
b.database,
189-
db.BlockNumIndexKey{
190-
Number: blockNumber,
191-
Index: index,
192-
},
193-
)
187+
return core.GetTxByBlockNumberAndIndex(b.database, blockNumber, index)
194188
}
195189

196190
// TransactionByHash gets the transaction for a given hash.
@@ -208,7 +202,7 @@ func (b *Blockchain) Receipt(hash *felt.Felt) (*core.TransactionReceipt, *felt.F
208202
return nil, nil, 0, err
209203
}
210204

211-
receipt, err := core.GetReceiptByHash(b.database, txHash)
205+
receipt, err := core.GetReceiptByBlockNumberAndIndex(b.database, bnIndex.Number, bnIndex.Index)
212206
if err != nil {
213207
return nil, nil, 0, err
214208
}
@@ -255,11 +249,13 @@ func (b *Blockchain) Store(
255249
return err
256250
}
257251

258-
for i, tx := range block.Transactions {
259-
if err := core.WriteTxAndReceipt(txn, block.Number, uint64(i), tx,
260-
block.Receipts[i]); err != nil {
261-
return err
262-
}
252+
if err := core.WriteTransactionsAndReceipts(
253+
txn,
254+
block.Number,
255+
block.Transactions,
256+
block.Receipts,
257+
); err != nil {
258+
return err
263259
}
264260

265261
if err := core.WriteStateUpdateByBlockNum(txn, block.Number, stateUpdate); err != nil {
@@ -724,11 +720,13 @@ func (b *Blockchain) storeBlockData(
724720
return err
725721
}
726722

727-
// Store transactions and receipts
728-
for i, tx := range block.Transactions {
729-
if err := core.WriteTxAndReceipt(txn, block.Number, uint64(i), tx, block.Receipts[i]); err != nil {
730-
return err
731-
}
723+
if err := core.WriteTransactionsAndReceipts(
724+
txn,
725+
block.Number,
726+
block.Transactions,
727+
block.Receipts,
728+
); err != nil {
729+
return err
732730
}
733731

734732
// Store state update

core/accessors.go

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -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

265266
func 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
392385
func 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

421407
func 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

471471
func 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

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

core/block_transaction.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package core
2+
3+
type BlockTransactions struct {
4+
Transactions []Transaction `cbor:"1,keyasint,omitempty"`
5+
Receipts []*TransactionReceipt `cbor:"2,keyasint,omitempty"`
6+
}

core/typed_buckets.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,10 @@ var ClassHashToCasmHashV2Bucket = typed.NewBucket(
158158
key.SierraClassHash,
159159
value.CasmClassHash,
160160
)
161+
162+
// Bucket 40: Block number (uint64) -> Block transactions (BlockTransactions)
163+
var BlockTransactionsBucket = typed.NewBucket(
164+
db.BlockTransactions,
165+
key.Cbor[uint64](),
166+
value.Cbor[BlockTransactions](),
167+
)

db/buckets.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const (
4949
AggregatedBloomFilters // maps block range to AggregatedBloomFilter
5050
RunningEventFilter // aggregated filter not full yet
5151
ClassHashToCasmHashV2 // blake2s casm class hashes of the all sierra classes
52+
BlockTransactions // maps block number to transactions and receipts
5253
)
5354

5455
// Key flattens a prefix and series of byte arrays into a single []byte.

rpc/v6/init_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package rpcv6_test
2+
3+
import (
4+
_ "github.com/NethermindEth/juno/encoder/registry"
5+
)

0 commit comments

Comments
 (0)