Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan committed Dec 5, 2023
1 parent 8ac60f1 commit f628aa1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 18 deletions.
12 changes: 10 additions & 2 deletions migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -85,6 +85,14 @@ type OldCairo1Class struct {
Compiled json.RawMessage
}

func (o *oldCairo1Class) Version() uint64 {
return 1

Check warning on line 89 in migration/migration.go

View check run for this annotation

Codecov / codecov/patch

migration/migration.go#L88-L89

Added lines #L88 - L89 were not covered by tests
}

func (o *oldCairo1Class) Hash() (*felt.Felt, error) {
return nil, nil

Check warning on line 93 in migration/migration.go

View check run for this annotation

Codecov / codecov/patch

migration/migration.go#L92-L93

Added lines #L92 - L93 were not covered by tests
}

func migrateCairo1CompiledClass(txn db.Transaction, key, value []byte, _ utils.Network) error {
var class declaredClass
err := encoder.Unmarshal(value, &class)
Expand Down
74 changes: 58 additions & 16 deletions migration/migration_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"math/rand"
"testing"

"github.com/NethermindEth/juno/blockchain"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit f628aa1

Please sign in to comment.