Skip to content

Commit

Permalink
Make blockchain.TransactionsByBlockNumber() private
Browse files Browse the repository at this point in the history
Functions in the blockchain package with a db.Transaction argument are
intended to be private function since blockchain package is one of the
few packages allowed to modify the DB.
  • Loading branch information
IronGauntlets committed Dec 12, 2024
1 parent 954f445 commit 5f2809d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
4 changes: 2 additions & 2 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ func blockByNumber(txn db.Transaction, number uint64) (*core.Block, error) {

block := new(core.Block)
block.Header = header
block.Transactions, err = TransactionsByBlockNumber(txn, number)
block.Transactions, err = transactionsByBlockNumber(txn, number)
if err != nil {
return nil, err
}
Expand All @@ -488,7 +488,7 @@ func blockByNumber(txn db.Transaction, number uint64) (*core.Block, error) {
return block, nil
}

func TransactionsByBlockNumber(txn db.Transaction, number uint64) ([]core.Transaction, error) {
func transactionsByBlockNumber(txn db.Transaction, number uint64) ([]core.Transaction, error) {
iterator, err := txn.NewIterator()
if err != nil {
return nil, err
Expand Down
42 changes: 40 additions & 2 deletions migration/migration.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package migration

import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"runtime"
"sync"

"github.com/NethermindEth/juno/blockchain"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/encoder"
"github.com/NethermindEth/juno/utils"
"github.com/fxamacker/cbor/v2"
"github.com/sourcegraph/conc/pool"
Expand Down Expand Up @@ -203,7 +204,7 @@ func processBlocks(txn db.Transaction, processBlock func(uint64, *sync.Mutex) er
func calculateL1MsgHashes(txn db.Transaction, n *utils.Network) error {
processBlockFunc := func(blockNumber uint64, txnLock *sync.Mutex) error {
txnLock.Lock()
txns, err := blockchain.TransactionsByBlockNumber(txn, blockNumber)
txns, err := transactionsByBlockNumber(txn, blockNumber)
txnLock.Unlock()
if err != nil {
return err
Expand Down Expand Up @@ -238,3 +239,40 @@ func storeL1HandlerMsgHashes(dbTxn db.Transaction, blockTxns []core.Transaction)
}
return nil
}

// transactionsByBlockNumber has been copied from the blockchain package since functions which take db.Transaction as an
// argument are intended to be private and as migrations are temporary, the duplication of code is acceptable.
func transactionsByBlockNumber(txn db.Transaction, number uint64) ([]core.Transaction, error) {
iterator, err := txn.NewIterator()
if err != nil {
return nil, err

Check warning on line 248 in migration/migration.go

View check run for this annotation

Codecov / codecov/patch

migration/migration.go#L248

Added line #L248 was not covered by tests
}

var txs []core.Transaction
numBytes := core.MarshalBlockNumber(number)

prefix := db.TransactionsByBlockNumberAndIndex.Key(numBytes)
for iterator.Seek(prefix); iterator.Valid(); iterator.Next() {
if !bytes.HasPrefix(iterator.Key(), prefix) {
break
}

val, vErr := iterator.Value()
if vErr != nil {
return nil, utils.RunAndWrapOnError(iterator.Close, vErr)

Check warning on line 262 in migration/migration.go

View check run for this annotation

Codecov / codecov/patch

migration/migration.go#L262

Added line #L262 was not covered by tests
}

var tx core.Transaction
if err = encoder.Unmarshal(val, &tx); err != nil {
return nil, utils.RunAndWrapOnError(iterator.Close, err)

Check warning on line 267 in migration/migration.go

View check run for this annotation

Codecov / codecov/patch

migration/migration.go#L267

Added line #L267 was not covered by tests
}

txs = append(txs, tx)
}

if err = iterator.Close(); err != nil {
return nil, err

Check warning on line 274 in migration/migration.go

View check run for this annotation

Codecov / codecov/patch

migration/migration.go#L274

Added line #L274 was not covered by tests
}

return txs, nil
}

0 comments on commit 5f2809d

Please sign in to comment.