From b68f71893ca7169fdc5f0d5c11004ac6b50a63a7 Mon Sep 17 00:00:00 2001 From: weiihann Date: Mon, 6 Jan 2025 13:07:42 +0800 Subject: [PATCH] Use `encoder/registry` to initialize encoder types add remove --- blockchain/blockchain.go | 1 - blockchain/init_test.go | 5 ++++ cmd/juno/init_test.go | 5 ++++ cmd/juno/juno.go | 1 + core/init_test.go | 5 ++++ core/state_test.go | 24 ------------------- .../registry/registry.go | 5 ++-- migration/init_pkg_test.go | 5 ++++ migration/migration.go | 1 - migration/migration_pkg_test.go | 1 - node/init_test.go | 5 ++++ plugin/init_test.go | 5 ++++ sync/init_test.go | 5 ++++ vm/init_test.go | 5 ++++ vm/vm_test.go | 8 ------- 15 files changed, 44 insertions(+), 37 deletions(-) create mode 100644 blockchain/init_test.go create mode 100644 cmd/juno/init_test.go create mode 100644 core/init_test.go rename blockchain/encoder_initializer.go => encoder/registry/registry.go (91%) create mode 100644 migration/init_pkg_test.go create mode 100644 node/init_test.go create mode 100644 plugin/init_test.go create mode 100644 sync/init_test.go create mode 100644 vm/init_test.go diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 4a4619385f..35b3bce603 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.go @@ -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, diff --git a/blockchain/init_test.go b/blockchain/init_test.go new file mode 100644 index 0000000000..b0639842c0 --- /dev/null +++ b/blockchain/init_test.go @@ -0,0 +1,5 @@ +package blockchain_test + +import ( + _ "github.com/NethermindEth/juno/encoder/registry" +) diff --git a/cmd/juno/init_test.go b/cmd/juno/init_test.go new file mode 100644 index 0000000000..ea0e5b1416 --- /dev/null +++ b/cmd/juno/init_test.go @@ -0,0 +1,5 @@ +package main_test + +import ( + _ "github.com/NethermindEth/juno/encoder/registry" +) diff --git a/cmd/juno/juno.go b/cmd/juno/juno.go index 1b973f8eff..86c6d027e5 100644 --- a/cmd/juno/juno.go +++ b/cmd/juno/juno.go @@ -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" diff --git a/core/init_test.go b/core/init_test.go new file mode 100644 index 0000000000..ccc2768178 --- /dev/null +++ b/core/init_test.go @@ -0,0 +1,5 @@ +package core_test + +import ( + _ "github.com/NethermindEth/juno/encoder/registry" +) diff --git a/core/state_test.go b/core/state_test.go index 6b96d64b3b..45364b9f6a 100644 --- a/core/state_test.go +++ b/core/state_test.go @@ -4,8 +4,6 @@ import ( "context" "encoding/json" "fmt" - "os" - "reflect" "testing" "github.com/NethermindEth/juno/clients/feeder" @@ -13,7 +11,6 @@ import ( "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" @@ -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) diff --git a/blockchain/encoder_initializer.go b/encoder/registry/registry.go similarity index 91% rename from blockchain/encoder_initializer.go rename to encoder/registry/registry.go index fbc82ab4d8..02ba18a17f 100644 --- a/blockchain/encoder_initializer.go +++ b/encoder/registry/registry.go @@ -1,4 +1,4 @@ -package blockchain +package registry import ( "reflect" @@ -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{}), diff --git a/migration/init_pkg_test.go b/migration/init_pkg_test.go new file mode 100644 index 0000000000..471c7b037c --- /dev/null +++ b/migration/init_pkg_test.go @@ -0,0 +1,5 @@ +package migration + +import ( + _ "github.com/NethermindEth/juno/encoder/registry" +) diff --git a/migration/migration.go b/migration/migration.go index 107bd40f10..c0a83883de 100644 --- a/migration/migration.go +++ b/migration/migration.go @@ -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 { diff --git a/migration/migration_pkg_test.go b/migration/migration_pkg_test.go index e2d5613c48..0bfb83fb7d 100644 --- a/migration/migration_pkg_test.go +++ b/migration/migration_pkg_test.go @@ -268,7 +268,6 @@ func TestMigrateTrieRootKeysFromBitsetToTrieKeys(t *testing.T) { } func TestMigrateCairo1CompiledClass(t *testing.T) { - blockchain.RegisterCoreTypesToEncoder() txn := db.NewMemTransaction() key := []byte("key") diff --git a/node/init_test.go b/node/init_test.go new file mode 100644 index 0000000000..cb01f2035a --- /dev/null +++ b/node/init_test.go @@ -0,0 +1,5 @@ +package node_test + +import ( + _ "github.com/NethermindEth/juno/encoder/registry" +) diff --git a/plugin/init_test.go b/plugin/init_test.go new file mode 100644 index 0000000000..7dfd97e7f6 --- /dev/null +++ b/plugin/init_test.go @@ -0,0 +1,5 @@ +package plugin_test + +import ( + _ "github.com/NethermindEth/juno/encoder/registry" +) diff --git a/sync/init_test.go b/sync/init_test.go new file mode 100644 index 0000000000..29deb6530c --- /dev/null +++ b/sync/init_test.go @@ -0,0 +1,5 @@ +package sync_test + +import ( + _ "github.com/NethermindEth/juno/encoder/registry" +) diff --git a/vm/init_test.go b/vm/init_test.go new file mode 100644 index 0000000000..e8dd094955 --- /dev/null +++ b/vm/init_test.go @@ -0,0 +1,5 @@ +package vm_test + +import ( + _ "github.com/NethermindEth/juno/encoder/registry" +) diff --git a/vm/vm_test.go b/vm/vm_test.go index 00832b6063..c5744ddf83 100644 --- a/vm/vm_test.go +++ b/vm/vm_test.go @@ -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" @@ -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, @@ -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, @@ -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,