Skip to content
54 changes: 32 additions & 22 deletions adapters/p2p2core/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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 {
Comment thread
ndatta-nethermind marked this conversation as resolved.
Outdated
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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this todo a leftover?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a requirement:
Add a todo on top which asks: "should this be a method of class.Cairo1Class"


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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if this is correct because we instantiate a core.CasmClass in the Cairo 0 case, where *class.Cairo1Class is nil.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that now we're only calling this function when compiledClass is non-nil, so we can throw an error here instead.

}

adapt := func(ep *class.SierraEntryPoint) starknet.SierraEntryPoint {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cant we reuse adaptSierra or adaptSierraEntryPoints here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. adaptSierra return core.SierraEntrypoint.

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion adapters/p2p2core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func AdaptStateDiff(
)

for _, cls := range classes {
class, err := AdaptClass(cls)
class, err := AdaptClassDefinition(cls)
if err != nil {
return nil, fmt.Errorf("unsupported class: %w", err)
}
Expand Down
13 changes: 10 additions & 3 deletions adapters/p2p2core/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ func AdaptDeclareV3WithClass(

declareCommon, err := AdaptDeclareV3TxnCommon(tx.Common, classHash, txnHash)
if err != nil {
return nil, nil, fmt.Errorf("failed to adapt declare v3 transaction common: %w", err)
return nil,
nil,
fmt.Errorf("failed to adapt declare v3 transaction common: %w", err)
}
if *class.Compiled.Hash() != *declareCommon.CompiledClassHash {
err := fmt.Errorf("compiled class hash mismatch: expected %s, got %s", class.Compiled.Hash(), declareCommon.CompiledClassHash)
compiledClassHash := class.Compiled.Hash()
if !compiledClassHash.Equal(declareCommon.CompiledClassHash) {
err := fmt.Errorf(
"compiled class hash mismatch: expected %s, got %s",
compiledClassHash,
declareCommon.CompiledClassHash,
)
return nil, nil, err
}

Expand Down
12 changes: 7 additions & 5 deletions adapters/sn2core/sn2core.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func AdaptSierraClass(
Abi: response.Abi,
AbiHash: crypto.StarknetKeccak([]byte(response.Abi)),

Compiled: coreCompiledClass,
Compiled: &coreCompiledClass,

EntryPoints: struct {
Constructor []core.SierraEntryPoint
Expand All @@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if this is correct because we instantiate a core.CasmClass in the Cairo 0 case, where *class.Cairo1Class is nil.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that now we're only calling this function when compiledClass is non-nil, so we can throw an error here instead.

}

var casm core.CasmClass
Expand All @@ -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 {
Expand Down
27 changes: 21 additions & 6 deletions adapters/sn2core/sn2core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we change the behaviour?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the reason: #3225 (comment)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we change the behaviour?

}

func TestAdaptPreConfirmed(t *testing.T) {
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion core/state_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ func declaredClassesDigest(
}
}

func deprecatedDeclaredClassesDigest(declaredV0Classes []*felt.Felt, digest *crypto.PoseidonDigest) {
func deprecatedDeclaredClassesDigest(
declaredV0Classes []*felt.Felt,
digest *crypto.PoseidonDigest,
) {
numOfDeclaredV0Classes := uint64(len(declaredV0Classes))
digest.Update(new(felt.Felt).SetUint64(numOfDeclaredV0Classes))

Expand Down
4 changes: 2 additions & 2 deletions migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means we have a zero core.CasmClass instead of nil in deprecated case.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, looks like we should have nil instead

},
}

Expand Down
2 changes: 1 addition & 1 deletion migration/migration_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we change the behaviour?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the reason: #3225 (comment)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we change the behaviour?

}
}
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (s *BlockFetcher) adaptAndSanityCheckBlock(

newClasses := make(map[felt.Felt]core.ClassDefinition)
for _, cls := range classes {
coreC, err := p2p2core.AdaptClass(cls)
coreC, err := p2p2core.AdaptClassDefinition(cls)
if err != nil {
bodyCh <- BlockBody{Err: fmt.Errorf("failed to adapt class: %w", err)}
return
Expand Down
4 changes: 3 additions & 1 deletion rpc/v7/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,9 @@ func TestTraceBlockTransactions(t *testing.T) {
})

t.Run("regular block", func(t *testing.T) {
blockHash := felt.NewUnsafeFromString[felt.Felt]("0x37b244ea7dc6b3f9735fba02d183ef0d6807a572dd91a63cc1b14b923c1ac0")
blockHash := felt.NewUnsafeFromString[felt.Felt](
"0x37b244ea7dc6b3f9735fba02d183ef0d6807a572dd91a63cc1b14b923c1ac0",
)
tx := &core.DeclareTransaction{
TransactionHash: felt.NewUnsafeFromString[felt.Felt]("0x000000001"),
ClassHash: felt.NewUnsafeFromString[felt.Felt]("0x000000000"),
Expand Down
2 changes: 1 addition & 1 deletion starknetdata/feeder/feeder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we change the behaviour?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the reason: #3225 (comment)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we change the behaviour?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the requirement: #3204

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the screenshot of the issue:
image

None of the above is saying that we should set Compiled to &CasmClass{} instead of nil if the declared class is Cairo V0.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's asking to return value type instead of reference type in CompileToCasm method into adapters/p2p2core/class.go.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • If Cairo V0: Set Compiled to nil
  • If Cairo V1: Set Compiled to &casmClass

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

}
}
}
Expand Down
Loading