Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make internal blockchain methods private #2386

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ func (b *Blockchain) Height() (uint64, error) {
var height uint64
return height, b.database.View(func(txn db.Transaction) error {
var err error
height, err = ChainHeight(txn)
height, err = chainHeight(txn)
return err
})
}

func ChainHeight(txn db.Transaction) (uint64, error) {
func chainHeight(txn db.Transaction) (uint64, error) {
var height uint64
return height, txn.Get(db.ChainHeight.Key(), func(val []byte) error {
height = binary.BigEndian.Uint64(val)
Expand Down Expand Up @@ -163,15 +163,15 @@ func (b *Blockchain) HeadsHeader() (*core.Header, error) {
}

func head(txn db.Transaction) (*core.Block, error) {
height, err := ChainHeight(txn)
height, err := chainHeight(txn)
if err != nil {
return nil, err
}
return BlockByNumber(txn, height)
return blockByNumber(txn, height)
}

func headsHeader(txn db.Transaction) (*core.Header, error) {
height, err := ChainHeight(txn)
height, err := chainHeight(txn)
if err != nil {
return nil, err
}
Expand All @@ -184,7 +184,7 @@ func (b *Blockchain) BlockByNumber(number uint64) (*core.Block, error) {
var block *core.Block
return block, b.database.View(func(txn db.Transaction) error {
var err error
block, err = BlockByNumber(txn, number)
block, err = blockByNumber(txn, number)
return err
})
}
Expand Down Expand Up @@ -339,7 +339,7 @@ func (b *Blockchain) Store(block *core.Block, blockCommitments *core.BlockCommit
if err := core.NewState(txn).Update(block.Number, stateUpdate, newClasses); err != nil {
return err
}
if err := StoreBlockHeader(txn, block.Header); err != nil {
if err := storeBlockHeader(txn, block.Header); err != nil {
return err
}

Expand All @@ -354,11 +354,11 @@ func (b *Blockchain) Store(block *core.Block, blockCommitments *core.BlockCommit
return err
}

if err := StoreBlockCommitments(txn, block.Number, blockCommitments); err != nil {
if err := storeBlockCommitments(txn, block.Number, blockCommitments); err != nil {
return err
}

if err := StoreL1HandlerMsgHashes(txn, block.Transactions); err != nil {
if err := storeL1HandlerMsgHashes(txn, block.Transactions); err != nil {
return err
}

Expand Down Expand Up @@ -402,7 +402,7 @@ func verifyBlock(txn db.Transaction, block *core.Block) error {
return nil
}

func StoreBlockCommitments(txn db.Transaction, blockNumber uint64, commitments *core.BlockCommitments) error {
func storeBlockCommitments(txn db.Transaction, blockNumber uint64, commitments *core.BlockCommitments) error {
numBytes := core.MarshalBlockNumber(blockNumber)

commitmentBytes, err := encoder.Marshal(commitments)
Expand Down Expand Up @@ -445,7 +445,7 @@ func blockCommitmentsByNumber(txn db.Transaction, blockNumber uint64) (*core.Blo
// "[]" is the db prefix to represent a bucket
// "()" are additional keys appended to the prefix or multiple values marshalled together
// "->" represents a key value pair.
func StoreBlockHeader(txn db.Transaction, header *core.Header) error {
func storeBlockHeader(txn db.Transaction, header *core.Header) error {
numBytes := core.MarshalBlockNumber(header.Number)

if err := txn.Set(db.BlockHeaderNumbersByHash.Key(header.Hash.Marshal()), numBytes); err != nil {
Expand Down Expand Up @@ -483,16 +483,16 @@ func blockHeaderByHash(txn db.Transaction, hash *felt.Felt) (*core.Header, error
})
}

// BlockByNumber retrieves a block from database by its number
func BlockByNumber(txn db.Transaction, number uint64) (*core.Block, error) {
// blockByNumber retrieves a block from database by its number
func blockByNumber(txn db.Transaction, number uint64) (*core.Block, error) {
header, err := blockHeaderByNumber(txn, number)
if err != nil {
return nil, err
}

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 @@ -504,7 +504,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) {
numBytes := core.MarshalBlockNumber(number)
prefix := db.TransactionsByBlockNumberAndIndex.Key(numBytes)

Expand Down Expand Up @@ -576,12 +576,12 @@ func blockByHash(txn db.Transaction, hash *felt.Felt) (*core.Block, error) {
var block *core.Block
return block, txn.Get(db.BlockHeaderNumbersByHash.Key(hash.Marshal()), func(val []byte) error {
var err error
block, err = BlockByNumber(txn, binary.BigEndian.Uint64(val))
block, err = blockByNumber(txn, binary.BigEndian.Uint64(val))
return err
})
}

func StoreL1HandlerMsgHashes(dbTxn db.Transaction, blockTxns []core.Transaction) error {
func storeL1HandlerMsgHashes(dbTxn db.Transaction, blockTxns []core.Transaction) error {
for _, txn := range blockTxns {
if l1Handler, ok := (txn).(*core.L1HandlerTransaction); ok {
err := dbTxn.Set(db.L1HandlerTxnHashByMsgHash.Key(l1Handler.MessageHash()), txn.Hash().Marshal())
Expand Down Expand Up @@ -773,7 +773,7 @@ func (b *Blockchain) HeadState() (core.StateReader, StateCloser, error) {
return nil, nil, err
}

_, err = ChainHeight(txn)
_, err = chainHeight(txn)
if err != nil {
return nil, nil, utils.RunAndWrapOnError(txn.Discard, err)
}
Expand Down Expand Up @@ -827,7 +827,7 @@ func (b *Blockchain) EventFilter(from *felt.Felt, keys [][]felt.Felt) (EventFilt
return nil, err
}

latest, err := ChainHeight(txn)
latest, err := chainHeight(txn)
if err != nil {
return nil, err
}
Expand All @@ -843,7 +843,7 @@ func (b *Blockchain) RevertHead() error {
func (b *Blockchain) GetReverseStateDiff() (*core.StateDiff, error) {
var reverseStateDiff *core.StateDiff
return reverseStateDiff, b.database.View(func(txn db.Transaction) error {
blockNumber, err := ChainHeight(txn)
blockNumber, err := chainHeight(txn)
if err != nil {
return err
}
Expand All @@ -858,7 +858,7 @@ func (b *Blockchain) GetReverseStateDiff() (*core.StateDiff, error) {
}

func (b *Blockchain) revertHead(txn db.Transaction) error {
blockNumber, err := ChainHeight(txn)
blockNumber, err := chainHeight(txn)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion blockchain/event_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ type FilteredEvent struct {
//nolint:gocyclo
func (e *EventFilter) Events(cToken *ContinuationToken, chunkSize uint64) ([]*FilteredEvent, *ContinuationToken, error) {
var matchedEvents []*FilteredEvent
latest, err := ChainHeight(e.txn)
latest, err := chainHeight(e.txn)
if err != nil {
return nil, nil, err
}
Expand Down
165 changes: 165 additions & 0 deletions migration/blockchain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package migration

import (
"bytes"
"encoding/binary"

"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/encoder"
"github.com/NethermindEth/juno/utils"
)

func blockByNumber(txn db.Transaction, number uint64) (*core.Block, error) {
header, err := blockHeaderByNumber(txn, number)
if err != nil {
return nil, err
}

block := new(core.Block)
block.Header = header
block.Transactions, err = transactionsByBlockNumber(txn, number)
if err != nil {
return nil, err
}

Check warning on line 24 in migration/blockchain.go

View check run for this annotation

Codecov / codecov/patch

migration/blockchain.go#L23-L24

Added lines #L23 - L24 were not covered by tests

block.Receipts, err = receiptsByBlockNumber(txn, number)
if err != nil {
return nil, err
}

Check warning on line 29 in migration/blockchain.go

View check run for this annotation

Codecov / codecov/patch

migration/blockchain.go#L28-L29

Added lines #L28 - L29 were not covered by tests
return block, nil
}

func blockHeaderByNumber(txn db.Transaction, number uint64) (*core.Header, error) {
numBytes := core.MarshalBlockNumber(number)

var header *core.Header
if err := txn.Get(db.BlockHeadersByNumber.Key(numBytes), func(val []byte) error {
header = new(core.Header)
return encoder.Unmarshal(val, header)
}); err != nil {
return nil, err
}
return header, nil
}

func head(txn db.Transaction) (*core.Block, error) {
height, err := chainHeight(txn)
if err != nil {
return nil, err
}
return blockByNumber(txn, height)
}

func chainHeight(txn db.Transaction) (uint64, error) {
var height uint64
return height, txn.Get(db.ChainHeight.Key(), func(val []byte) error {
height = binary.BigEndian.Uint64(val)
return nil
})
}

func transactionsByBlockNumber(txn db.Transaction, number uint64) ([]core.Transaction, error) {
numBytes := core.MarshalBlockNumber(number)
prefix := db.TransactionsByBlockNumberAndIndex.Key(numBytes)

iterator, err := txn.NewIterator(prefix, true)
if err != nil {
return nil, err
}

Check warning on line 69 in migration/blockchain.go

View check run for this annotation

Codecov / codecov/patch

migration/blockchain.go#L68-L69

Added lines #L68 - L69 were not covered by tests

var txs []core.Transaction
for iterator.First(); iterator.Valid(); iterator.Next() {
val, vErr := iterator.Value()
if vErr != nil {
return nil, utils.RunAndWrapOnError(iterator.Close, vErr)
}

Check warning on line 76 in migration/blockchain.go

View check run for this annotation

Codecov / codecov/patch

migration/blockchain.go#L75-L76

Added lines #L75 - L76 were 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 81 in migration/blockchain.go

View check run for this annotation

Codecov / codecov/patch

migration/blockchain.go#L80-L81

Added lines #L80 - L81 were not covered by tests

txs = append(txs, tx)
}

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

Check warning on line 88 in migration/blockchain.go

View check run for this annotation

Codecov / codecov/patch

migration/blockchain.go#L87-L88

Added lines #L87 - L88 were not covered by tests

return txs, nil
}

func receiptsByBlockNumber(txn db.Transaction, number uint64) ([]*core.TransactionReceipt, error) {
numBytes := core.MarshalBlockNumber(number)
prefix := db.ReceiptsByBlockNumberAndIndex.Key(numBytes)

iterator, err := txn.NewIterator(prefix, true)
if err != nil {
return nil, err
}

Check warning on line 100 in migration/blockchain.go

View check run for this annotation

Codecov / codecov/patch

migration/blockchain.go#L99-L100

Added lines #L99 - L100 were not covered by tests

var receipts []*core.TransactionReceipt

for iterator.First(); iterator.Valid(); iterator.Next() {
if !bytes.HasPrefix(iterator.Key(), prefix) {
break

Check warning on line 106 in migration/blockchain.go

View check run for this annotation

Codecov / codecov/patch

migration/blockchain.go#L106

Added line #L106 was not covered by tests
}

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

Check warning on line 112 in migration/blockchain.go

View check run for this annotation

Codecov / codecov/patch

migration/blockchain.go#L111-L112

Added lines #L111 - L112 were not covered by tests

receipt := new(core.TransactionReceipt)
if err = encoder.Unmarshal(val, receipt); err != nil {
return nil, utils.RunAndWrapOnError(iterator.Close, err)
}

Check warning on line 117 in migration/blockchain.go

View check run for this annotation

Codecov / codecov/patch

migration/blockchain.go#L116-L117

Added lines #L116 - L117 were not covered by tests

receipts = append(receipts, receipt)
}

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

Check warning on line 124 in migration/blockchain.go

View check run for this annotation

Codecov / codecov/patch

migration/blockchain.go#L123-L124

Added lines #L123 - L124 were not covered by tests

return receipts, nil
}

func storeBlockHeader(txn db.Transaction, header *core.Header) error {
numBytes := core.MarshalBlockNumber(header.Number)

if err := txn.Set(db.BlockHeaderNumbersByHash.Key(header.Hash.Marshal()), numBytes); err != nil {
return err
}

Check warning on line 134 in migration/blockchain.go

View check run for this annotation

Codecov / codecov/patch

migration/blockchain.go#L133-L134

Added lines #L133 - L134 were not covered by tests

headerBytes, err := encoder.Marshal(header)
if err != nil {
return err
}

Check warning on line 139 in migration/blockchain.go

View check run for this annotation

Codecov / codecov/patch

migration/blockchain.go#L138-L139

Added lines #L138 - L139 were not covered by tests

return txn.Set(db.BlockHeadersByNumber.Key(numBytes), headerBytes)
}

func storeBlockCommitments(txn db.Transaction, blockNumber uint64, commitments *core.BlockCommitments) error {
numBytes := core.MarshalBlockNumber(blockNumber)

commitmentBytes, err := encoder.Marshal(commitments)
if err != nil {
return err
}

Check warning on line 150 in migration/blockchain.go

View check run for this annotation

Codecov / codecov/patch

migration/blockchain.go#L149-L150

Added lines #L149 - L150 were not covered by tests

return txn.Set(db.BlockCommitments.Key(numBytes), commitmentBytes)
}

func storeL1HandlerMsgHashes(dbTxn db.Transaction, blockTxns []core.Transaction) error {
for _, txn := range blockTxns {
if l1Handler, ok := (txn).(*core.L1HandlerTransaction); ok {
err := dbTxn.Set(db.L1HandlerTxnHashByMsgHash.Key(l1Handler.MessageHash()), txn.Hash().Marshal())
if err != nil {
return err
}

Check warning on line 161 in migration/blockchain.go

View check run for this annotation

Codecov / codecov/patch

migration/blockchain.go#L160-L161

Added lines #L160 - L161 were not covered by tests
}
}
return nil
}
Loading
Loading