Skip to content

Commit

Permalink
Use encoder/registry to initialize encoder types
Browse files Browse the repository at this point in the history
add
  • Loading branch information
weiihann committed Jan 6, 2025
1 parent 6b4d132 commit 5c2a241
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 37 deletions.
1 change: 0 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ type Blockchain struct {
}

func New(database db.DB, network *utils.Network, pendingBlockFn func() *core.Block) *Blockchain {
RegisterCoreTypesToEncoder()
return &Blockchain{
database: database,
network: network,
Expand Down
5 changes: 5 additions & 0 deletions blockchain/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package blockchain_test

import (
_ "github.com/NethermindEth/juno/encoder/registry"
)
5 changes: 5 additions & 0 deletions cmd/juno/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main_test

import (
_ "github.com/NethermindEth/juno/encoder/registry"
)
1 change: 1 addition & 0 deletions cmd/juno/juno.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"syscall"
"time"

_ "github.com/NethermindEth/juno/encoder/registry"
_ "github.com/NethermindEth/juno/jemalloc"
"github.com/NethermindEth/juno/node"
"github.com/NethermindEth/juno/utils"
Expand Down
5 changes: 5 additions & 0 deletions core/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package core_test

import (
_ "github.com/NethermindEth/juno/encoder/registry"
)
24 changes: 0 additions & 24 deletions core/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"reflect"
"testing"

"github.com/NethermindEth/juno/clients/feeder"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/db/pebble"
"github.com/NethermindEth/juno/encoder"
adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder"
"github.com/NethermindEth/juno/utils"
"github.com/stretchr/testify/assert"
Expand All @@ -26,27 +23,6 @@ var (
su1FirstDeployedAddress = *_su1FirstDeployedAddress
)

func TestMain(m *testing.M) {
txTypes := []core.Transaction{
&core.DeclareTransaction{},
&core.DeployTransaction{},
&core.InvokeTransaction{},
&core.L1HandlerTransaction{},
&core.DeployAccountTransaction{},
}

for _, tx := range txTypes {
_ = encoder.RegisterType(reflect.TypeOf(tx))
}

_ = encoder.RegisterType(reflect.TypeOf(core.Cairo0Class{}))
_ = encoder.RegisterType(reflect.TypeOf(core.Cairo1Class{}))

code := m.Run()

os.Exit(code)
}

func TestUpdate(t *testing.T) {
client := feeder.NewTestClient(t, &utils.Mainnet)
gw := adaptfeeder.New(client)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package blockchain
package registry

import (
"reflect"
Expand All @@ -10,7 +10,8 @@ import (

var once sync.Once

func RegisterCoreTypesToEncoder() {
//nolint:gochecknoinits
func init() {
once.Do(func() {
types := []reflect.Type{
reflect.TypeOf(core.DeclareTransaction{}),
Expand Down
5 changes: 5 additions & 0 deletions migration/init_pkg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package migration

import (
_ "github.com/NethermindEth/juno/encoder/registry"
)
1 change: 0 additions & 1 deletion migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ func relocateContractStorageRootKeys(txn db.Transaction, _ *utils.Network) error

// recalculateBloomFilters updates bloom filters in block headers to match what the most recent implementation expects
func recalculateBloomFilters(txn db.Transaction, _ *utils.Network) error {
blockchain.RegisterCoreTypesToEncoder()
for blockNumber := uint64(0); ; blockNumber++ {
block, err := blockchain.BlockByNumber(txn, blockNumber)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion migration/migration_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ func TestMigrateTrieRootKeysFromBitsetToTrieKeys(t *testing.T) {
}

func TestMigrateCairo1CompiledClass(t *testing.T) {
blockchain.RegisterCoreTypesToEncoder()
txn := db.NewMemTransaction()

key := []byte("key")
Expand Down
5 changes: 5 additions & 0 deletions node/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package node_test

import (
_ "github.com/NethermindEth/juno/encoder/registry"
)
5 changes: 5 additions & 0 deletions plugin/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package plugin_test

import (
_ "github.com/NethermindEth/juno/encoder/registry"
)
5 changes: 5 additions & 0 deletions sync/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sync_test

import (
_ "github.com/NethermindEth/juno/encoder/registry"
)
11 changes: 11 additions & 0 deletions utils/testing/testing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package testing

import (
"testing"

_ "github.com/NethermindEth/juno/encoder/registry" // Register types
)

type T struct {
*testing.T
}
5 changes: 5 additions & 0 deletions vm/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package vm_test

import (
_ "github.com/NethermindEth/juno/encoder/registry"
)
8 changes: 0 additions & 8 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package vm
import (
"context"
"os"
"reflect"
"testing"

"github.com/NethermindEth/juno/clients/feeder"
"github.com/NethermindEth/juno/core"
"github.com/NethermindEth/juno/core/felt"
"github.com/NethermindEth/juno/db/pebble"
"github.com/NethermindEth/juno/encoder"
adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder"
"github.com/NethermindEth/juno/utils"
"github.com/stretchr/testify/assert"
Expand All @@ -33,8 +31,6 @@ func TestV0Call(t *testing.T) {
simpleClass, err := gw.Class(context.Background(), classHash)
require.NoError(t, err)

require.NoError(t, encoder.RegisterType(reflect.TypeOf(core.Cairo0Class{})))

testState := core.NewState(txn)
require.NoError(t, testState.Update(0, &core.StateUpdate{
OldRoot: &felt.Zero,
Expand Down Expand Up @@ -95,8 +91,6 @@ func TestV1Call(t *testing.T) {
simpleClass, err := gw.Class(context.Background(), classHash)
require.NoError(t, err)

require.NoError(t, encoder.RegisterType(reflect.TypeOf(core.Cairo1Class{})))

testState := core.NewState(txn)
require.NoError(t, testState.Update(0, &core.StateUpdate{
OldRoot: &felt.Zero,
Expand Down Expand Up @@ -165,8 +159,6 @@ func TestCall_MaxSteps(t *testing.T) {
simpleClass, err := gw.Class(context.Background(), classHash)
require.NoError(t, err)

encoder.RegisterType(reflect.TypeOf(core.Cairo0Class{})) //nolint:errcheck

testState := core.NewState(txn)
require.NoError(t, testState.Update(0, &core.StateUpdate{
OldRoot: &felt.Zero,
Expand Down

0 comments on commit 5c2a241

Please sign in to comment.