-
Notifications
You must be signed in to change notification settings - Fork 231
Refactor: Refactored p2p2core adapater #3225
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
Changes from 3 commits
cdc5873
4fa3939
6008f67
3b21214
45641ff
43f902e
040e1c5
95369f4
b1bbd75
5c916b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,16 +18,11 @@ func AdaptSierraClass(cairo1 *class.Cairo1Class) (core.SierraClass, error) { | |
| abiHash := crypto.StarknetKeccak([]byte(cairo1.Abi)) | ||
|
|
||
| program := utils.Map(cairo1.Program, AdaptFelt) | ||
| compiled, err := createCompiledClass(cairo1) | ||
| compiled, err := compileToCasm(cairo1) | ||
| if err != nil { | ||
| return core.SierraClass{}, fmt.Errorf("invalid compiled class: %w", err) | ||
| } | ||
|
|
||
| adaptEP := func(points []*class.SierraEntryPoint) []core.SierraEntryPoint { | ||
| // usage of NonNilSlice is essential because relevant core class fields are non nil | ||
| return utils.Map(utils.NonNilSlice(points), adaptSierra) | ||
| } | ||
|
|
||
| entryPoints := cairo1.EntryPoints | ||
| return core.SierraClass{ | ||
| Abi: cairo1.Abi, | ||
|
|
@@ -37,35 +32,31 @@ func AdaptSierraClass(cairo1 *class.Cairo1Class) (core.SierraClass, error) { | |
| External []core.SierraEntryPoint | ||
| L1Handler []core.SierraEntryPoint | ||
| }{ | ||
| Constructor: adaptEP(entryPoints.Constructors), | ||
| External: adaptEP(entryPoints.Externals), | ||
| L1Handler: adaptEP(entryPoints.L1Handlers), | ||
| Constructor: adaptSierraEntryPoints(entryPoints.Constructors), | ||
| External: adaptSierraEntryPoints(entryPoints.Externals), | ||
| L1Handler: adaptSierraEntryPoints(entryPoints.L1Handlers), | ||
| }, | ||
| Program: program, | ||
| ProgramHash: crypto.PoseidonArray(program...), | ||
| SemanticVersion: cairo1.ContractClassVersion, | ||
| Compiled: compiled, | ||
| Compiled: &compiled, | ||
| }, nil | ||
| } | ||
|
|
||
| func AdaptClass(cls *class.Class) (core.ClassDefinition, error) { | ||
| func AdaptClassDefinition(cls *class.Class) (core.ClassDefinition, error) { | ||
| if cls == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| switch cls := cls.Class.(type) { | ||
| case *class.Class_Cairo0: | ||
| adaptEP := func(points []*class.EntryPoint) []core.DeprecatedEntryPoint { | ||
| // usage of NonNilSlice is essential because relevant core class fields are non nil | ||
| return utils.Map(utils.NonNilSlice(points), adaptEntryPoint) | ||
| } | ||
|
|
||
| deprecatedCairo := cls.Cairo0 | ||
| return &core.DeprecatedCairoClass{ | ||
| Abi: json.RawMessage(deprecatedCairo.Abi), | ||
| Externals: adaptEP(deprecatedCairo.Externals), | ||
| L1Handlers: adaptEP(deprecatedCairo.L1Handlers), | ||
| Constructors: adaptEP(deprecatedCairo.Constructors), | ||
| Externals: adaptEntryPoints(deprecatedCairo.Externals), | ||
| L1Handlers: adaptEntryPoints(deprecatedCairo.L1Handlers), | ||
| Constructors: adaptEntryPoints(deprecatedCairo.Constructors), | ||
| Program: deprecatedCairo.Program, | ||
| }, nil | ||
| case *class.Class_Cairo1: | ||
|
|
@@ -76,23 +67,42 @@ func AdaptClass(cls *class.Class) (core.ClassDefinition, error) { | |
| } | ||
| } | ||
|
|
||
| func adaptSierra(e *class.SierraEntryPoint) core.SierraEntryPoint { | ||
| func adaptSierraEntryPoints(points []*class.SierraEntryPoint) []core.SierraEntryPoint { | ||
| sierraEntryPoints := make([]core.SierraEntryPoint, len(points)) | ||
| for index := range points { | ||
| sierraEntryPoints[index] = adaptSierraEntryPoint(points[index]) | ||
| } | ||
| return sierraEntryPoints | ||
| } | ||
|
|
||
| func adaptSierraEntryPoint(e *class.SierraEntryPoint) core.SierraEntryPoint { | ||
| return core.SierraEntryPoint{ | ||
| Index: e.Index, | ||
| Selector: AdaptFelt(e.Selector), | ||
| } | ||
| } | ||
|
|
||
| func adaptEntryPoints(points []*class.EntryPoint) []core.DeprecatedEntryPoint { | ||
| deprecatedEntryPoints := make([]core.DeprecatedEntryPoint, len(points)) | ||
| for index := range points { | ||
| deprecatedEntryPoints[index] = adaptEntryPoint(points[index]) | ||
| } | ||
| return deprecatedEntryPoints | ||
| } | ||
|
|
||
| func adaptEntryPoint(e *class.EntryPoint) core.DeprecatedEntryPoint { | ||
| return core.DeprecatedEntryPoint{ | ||
| Selector: AdaptFelt(e.Selector), | ||
| Offset: new(felt.Felt).SetUint64(e.Offset), | ||
| } | ||
| } | ||
|
|
||
| func createCompiledClass(cairo1 *class.Cairo1Class) (*core.CasmClass, error) { | ||
| // todo should this be a method of class.Cairo1Class | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a requirement: |
||
|
|
||
| func compileToCasm(cairo1 *class.Cairo1Class) (core.CasmClass, error) { | ||
| if cairo1 == nil { | ||
| return nil, nil | ||
| //nolint:exhaustruct // intentionally returning an empty CasmClass | ||
| return core.CasmClass{}, nil | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if this is correct because we instantiate a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that now we're only calling this function when |
||
| } | ||
|
|
||
| adapt := func(ep *class.SierraEntryPoint) starknet.SierraEntryPoint { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cant we reuse
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. |
||
|
|
@@ -117,7 +127,7 @@ func createCompiledClass(cairo1 *class.Cairo1Class) (*core.CasmClass, error) { | |
|
|
||
| compiledClass, err := compiler.Compile(def) | ||
| if err != nil { | ||
| return nil, err | ||
| return core.CasmClass{}, err | ||
| } | ||
|
|
||
| return sn2core.AdaptCompiledClass(compiledClass) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -304,7 +304,7 @@ func AdaptSierraClass( | |
| Abi: response.Abi, | ||
| AbiHash: crypto.StarknetKeccak([]byte(response.Abi)), | ||
|
|
||
| Compiled: coreCompiledClass, | ||
| Compiled: &coreCompiledClass, | ||
|
|
||
| EntryPoints: struct { | ||
| Constructor []core.SierraEntryPoint | ||
|
|
@@ -327,9 +327,10 @@ func AdaptSierraClass( | |
| }, nil | ||
| } | ||
|
|
||
| func AdaptCompiledClass(compiledClass *starknet.CasmClass) (*core.CasmClass, error) { | ||
| func AdaptCompiledClass(compiledClass *starknet.CasmClass) (core.CasmClass, error) { | ||
| if compiledClass == nil { | ||
| return nil, nil | ||
| //nolint:exhaustruct // intentionally returning an empty CasmClass | ||
| return core.CasmClass{}, nil | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if this is correct because we instantiate a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that now we're only calling this function when |
||
| } | ||
|
|
||
| var casm core.CasmClass | ||
|
|
@@ -342,15 +343,16 @@ func AdaptCompiledClass(compiledClass *starknet.CasmClass) (*core.CasmClass, err | |
| var ok bool | ||
| casm.Prime, ok = new(big.Int).SetString(compiledClass.Prime, 0) | ||
| if !ok { | ||
| return nil, fmt.Errorf("couldn't convert prime value to big.Int: %d", casm.Prime) | ||
| return core.CasmClass{}, | ||
| fmt.Errorf("couldn't convert prime value to big.Int: %d", casm.Prime) | ||
| } | ||
|
|
||
| entryPoints := compiledClass.EntryPoints | ||
| casm.External = utils.Map(entryPoints.External, adaptCompiledEntryPoint) | ||
| casm.L1Handler = utils.Map(entryPoints.L1Handler, adaptCompiledEntryPoint) | ||
| casm.Constructor = utils.Map(entryPoints.Constructor, adaptCompiledEntryPoint) | ||
|
|
||
| return &casm, nil | ||
| return casm, nil | ||
| } | ||
|
|
||
| func AdaptSegmentLengths(l starknet.SegmentLengths) core.SegmentLengths { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -573,10 +573,21 @@ func TestClassV1(t *testing.T) { | |
| assert.Equal(t, compiled.Hints, v1Class.Compiled.Hints) | ||
| assert.Equal(t, compiled.CompilerVersion, v1Class.Compiled.CompilerVersion) | ||
| assert.Equal(t, len(compiled.EntryPoints.External), len(v1Class.Compiled.External)) | ||
| assert.Equal(t, len(compiled.EntryPoints.Constructor), len(v1Class.Compiled.Constructor)) | ||
| assert.Equal(t, len(compiled.EntryPoints.L1Handler), len(v1Class.Compiled.L1Handler)) | ||
| assert.Equal( | ||
| t, | ||
| len(compiled.EntryPoints.Constructor), | ||
| len(v1Class.Compiled.Constructor), | ||
| ) | ||
| assert.Equal(t, | ||
| len(compiled.EntryPoints.L1Handler), | ||
| len(v1Class.Compiled.L1Handler), | ||
| ) | ||
|
|
||
| assert.Equal(t, len(feederClass.Sierra.EntryPoints.External), len(v1Class.EntryPoints.External)) | ||
| assert.Equal( | ||
| t, | ||
| len(feederClass.Sierra.EntryPoints.External), | ||
| len(v1Class.EntryPoints.External), | ||
| ) | ||
| for i, v := range feederClass.Sierra.EntryPoints.External { | ||
| assert.Equal(t, v.Selector, v1Class.EntryPoints.External[i].Selector) | ||
| assert.Equal(t, v.Index, v1Class.EntryPoints.External[i].Index) | ||
|
|
@@ -629,7 +640,7 @@ func TestClassV1(t *testing.T) { | |
| func TestAdaptCompiledClass(t *testing.T) { | ||
| result, err := sn2core.AdaptCompiledClass(nil) | ||
| require.NoError(t, err) | ||
| assert.Nil(t, result) | ||
| assert.Empty(t, result) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we change the behaviour?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the reason: #3225 (comment)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we change the behaviour? |
||
| } | ||
|
|
||
| func TestAdaptPreConfirmed(t *testing.T) { | ||
|
|
@@ -670,7 +681,10 @@ func TestAdaptPreConfirmed(t *testing.T) { | |
| } | ||
|
|
||
| for _, test := range tests { | ||
| response, err := client.PreConfirmedBlock(t.Context(), strconv.FormatUint(test.blockNumber, 10)) | ||
| response, err := client.PreConfirmedBlock( | ||
| t.Context(), | ||
| strconv.FormatUint(test.blockNumber, 10), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| expectedEventCount, expectedPreConfirmedTxCount := countEventsAndTxs(response.Receipts) | ||
|
|
@@ -680,7 +694,8 @@ func TestAdaptPreConfirmed(t *testing.T) { | |
| adapted, err := sn2core.AdaptPreConfirmedBlock(response, test.blockNumber) | ||
| require.NoError(t, err) | ||
|
|
||
| assertPreConfirmedBlockBasics(t, | ||
| assertPreConfirmedBlockBasics( | ||
| t, | ||
| &adapted, | ||
| test.blockNumber, | ||
| response, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -785,7 +785,7 @@ func migrateCairo1CompiledClass2(txn db.KeyValueWriter, key, value []byte, _ *ut | |
| return err | ||
| } | ||
|
|
||
| var casmClass *core.CasmClass | ||
| var casmClass core.CasmClass | ||
| if deprecated, _ := starknet.IsDeprecatedCompiledClassDefinition(class.Class.Compiled); !deprecated { | ||
| var starknetCompiledClass starknet.CasmClass | ||
| err = json.Unmarshal(class.Class.Compiled, &starknetCompiledClass) | ||
|
|
@@ -808,7 +808,7 @@ func migrateCairo1CompiledClass2(txn db.KeyValueWriter, key, value []byte, _ *ut | |
| Program: class.Class.Program, | ||
| ProgramHash: class.Class.ProgramHash, | ||
| SemanticVersion: class.Class.SemanticVersion, | ||
| Compiled: casmClass, | ||
| Compiled: &casmClass, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This means we have a zero
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, looks like we should have nil instead |
||
| }, | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -349,7 +349,7 @@ func TestMigrateCairo1CompiledClass(t *testing.T) { | |
| if test.checkCompiledExists { | ||
| assert.NotNil(t, actualClass.Compiled) | ||
| } else { | ||
| assert.Nil(t, actualClass.Compiled) | ||
| assert.Empty(t, actualClass.Compiled) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we change the behaviour?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the reason: #3225 (comment)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we change the behaviour? |
||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -215,7 +215,7 @@ func TestClassV1(t *testing.T) { | |
| if test.hasCompiledClass { | ||
| assert.NotNil(t, adaptedResponse.Compiled) | ||
| } else { | ||
| assert.Nil(t, adaptedResponse.Compiled) | ||
| assert.Empty(t, adaptedResponse.Compiled) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we change the behaviour?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the reason: #3225 (comment)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we change the behaviour?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the requirement: #3204
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's asking to return value type instead of reference type in
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @infrmtcs but if we are adapting Sierra classes, Cairo 0 wouldn't be adapted to it. Where do we adapt Cairo zero and set the Casm to nil? |
||
| } | ||
| } | ||
| } | ||
|
|
||

Uh oh!
There was an error while loading. Please reload this page.