diff --git a/migration/migration.go b/migration/migration.go index 54ac4b0c69..e7d85ae1da 100644 --- a/migration/migration.go +++ b/migration/migration.go @@ -68,10 +68,10 @@ var defaultMigrations = []Migration{ type declaredClass struct { At uint64 - Class OldCairo1Class + Class oldCairo1Class } -type OldCairo1Class struct { +type oldCairo1Class struct { Abi string AbiHash *felt.Felt EntryPoints struct { @@ -85,6 +85,14 @@ type OldCairo1Class struct { Compiled json.RawMessage } +func (o *oldCairo1Class) Version() uint64 { + return 1 +} + +func (o *oldCairo1Class) Hash() (*felt.Felt, error) { + return nil, nil +} + func migrateCairo1CompiledClass(txn db.Transaction, key, value []byte, _ utils.Network) error { var class declaredClass err := encoder.Unmarshal(value, &class) diff --git a/migration/migration_pkg_test.go b/migration/migration_pkg_test.go index cfed77b157..c2cba4b265 100644 --- a/migration/migration_pkg_test.go +++ b/migration/migration_pkg_test.go @@ -6,7 +6,7 @@ import ( "encoding/binary" "encoding/json" "errors" - "fmt" + "math/rand" "testing" "github.com/NethermindEth/juno/blockchain" @@ -208,46 +208,77 @@ func TestMigrateTrieRootKeysFromBitsetToTrieKeys(t *testing.T) { } func TestMigrateCairo1CompiledClass(t *testing.T) { + blockchain.RegisterCoreTypesToEncoder() txn := db.NewMemTransaction() + compiledValues := map[string]any{ + "prime": "123", + } + compiledJSON, err := json.Marshal(compiledValues) + require.NoError(t, err) + key := []byte("key") - // todo rename - class := OldCairo1Class{ - Abi: "Some_ABI", + class := oldCairo1Class{ + Abi: "some cairo abi", AbiHash: randFelt(t), EntryPoints: struct { Constructor []core.SierraEntryPoint External []core.SierraEntryPoint L1Handler []core.SierraEntryPoint }{ - Constructor: nil, - External: nil, - L1Handler: nil, + Constructor: []core.SierraEntryPoint{ + { + Index: 0, + Selector: randFelt(t), + }, + }, + External: []core.SierraEntryPoint{ + { + Index: 0, + Selector: randFelt(t), + }, + }, + L1Handler: []core.SierraEntryPoint{ + { + Index: 0, + Selector: randFelt(t), + }, + }, }, - Program: nil, + Program: randSlice(t), ProgramHash: randFelt(t), SemanticVersion: "0.1.0", - Compiled: json.RawMessage(`{"prime": "323"}`), + Compiled: json.RawMessage(compiledJSON), } - oldDeclaredClass := declaredClass{ + expectedDeclared := declaredClass{ At: 777, Class: class, } - bytes, err := encoder.Marshal(oldDeclaredClass) + classBytes, err := encoder.Marshal(expectedDeclared) require.NoError(t, err) - err = txn.Set(key, bytes) + err = txn.Set(key, classBytes) require.NoError(t, err) - require.NoError(t, migrateCairo1CompiledClass(txn, key, bytes, utils.Mainnet)) + require.NoError(t, migrateCairo1CompiledClass(txn, key, classBytes, utils.Mainnet)) - var declaredClass core.DeclaredClass + var actualDeclared core.DeclaredClass err = txn.Get(key, func(bytes []byte) error { - return encoder.Unmarshal(bytes, &declaredClass) + return encoder.Unmarshal(bytes, &actualDeclared) }) require.NoError(t, err) - fmt.Printf("Data: %v\n", declaredClass) + assert.Equal(t, actualDeclared.At, expectedDeclared.At) + + actualClass := actualDeclared.Class.(*core.Cairo1Class) + expectedClass := expectedDeclared.Class + assert.Equal(t, expectedClass.Abi, actualClass.Abi) + assert.Equal(t, expectedClass.AbiHash, actualClass.AbiHash) + assert.Equal(t, expectedClass.EntryPoints, actualClass.EntryPoints) + assert.Equal(t, expectedClass.Program, actualClass.Program) + assert.Equal(t, expectedClass.ProgramHash, actualClass.ProgramHash) + assert.Equal(t, expectedClass.SemanticVersion, actualClass.SemanticVersion) + assert.Equal(t, compiledValues["prime"], actualClass.Compiled.Prime.String()) } func TestMigrateTrieNodesFromBitsetToTrieKey(t *testing.T) { @@ -438,6 +469,17 @@ func TestMigrateIfNeededInternal(t *testing.T) { }) } +func randSlice(t *testing.T) []*felt.Felt { + n := rand.Intn(10) + sl := make([]*felt.Felt, n) + + for i := range sl { + sl[i] = randFelt(t) + } + + return sl +} + func randFelt(t *testing.T) *felt.Felt { t.Helper()