Skip to content
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

fix: Extract aliased constants from identifiers #278

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ type DebugInfo struct {
}

type Identifier struct {
FullName string `json:"full_name"`
Members map[string]any `json:"members"`
Size int `json:"size"`
Decorators []string `json:"decorators"`
PC int `json:"pc"`
Type string `json:"type"`
CairoType string `json:"cairo_type"`
Value big.Int `json:"value"`
FullName string `json:"full_name"`
Members map[string]any `json:"members"`
Size int `json:"size"`
Decorators []string `json:"decorators"`
PC int `json:"pc"`
Type string `json:"type"`
CairoType string `json:"cairo_type"`
Value big.Int `json:"value"`
Destination string `json:"destination"`
}

type ApTrackingData struct {
Expand Down
39 changes: 30 additions & 9 deletions pkg/vm/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
)

type Identifier struct {
FullName string
Members map[string]any
Size int
Decorators []string
PC int
Type string
CairoType string
Value lambdaworks.Felt
FullName string
Members map[string]any
Size int
Decorators []string
PC int
Type string
CairoType string
Value lambdaworks.Felt
Destination string
}

type Program struct {
Expand Down Expand Up @@ -53,6 +54,7 @@ func DeserializeProgramJson(compiledProgram parser.CompiledJson) Program {
programIdentifier.Type = identifier.Type
programIdentifier.CairoType = identifier.CairoType
programIdentifier.Value = lambdaworks.FeltFromDecString(identifier.Value.String())
programIdentifier.Destination = identifier.Destination
program.Identifiers[key] = programIdentifier
}
program.Hints = compiledProgram.Hints
Expand All @@ -64,9 +66,28 @@ func DeserializeProgramJson(compiledProgram parser.CompiledJson) Program {
func (p *Program) ExtractConstants() map[string]lambdaworks.Felt {
constants := make(map[string]lambdaworks.Felt)
for name, identifier := range p.Identifiers {
if identifier.Type == "const" {
switch identifier.Type {
case "const":
constants[name] = identifier.Value
case "alias":
val, ok := searchConstFromAlias(identifier.Destination, &p.Identifiers)
if ok {
constants[name] = val
}
}
}
return constants
}

func searchConstFromAlias(destination string, identifiers *map[string]Identifier) (lambdaworks.Felt, bool) {
identifier, ok := (*identifiers)[destination]
if ok {
switch identifier.Type {
case "const":
return identifier.Value, true
case "alias":
return searchConstFromAlias(identifier.Destination, identifiers)
}
}
return lambdaworks.Felt{}, false
}
29 changes: 29 additions & 0 deletions pkg/vm/program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,34 @@ func TestExtractConstants(t *testing.T) {
if !reflect.DeepEqual(program.ExtractConstants(), expectedConstants) {
t.Errorf("Wrong Constants, expected %v, got %v", expectedConstants, program.ExtractConstants())
}
}

func TestExtractConstantsWithAliasedConstants(t *testing.T) {
program := vm.Program{
Identifiers: map[string]vm.Identifier{
"path.A": {
Value: lambdaworks.FeltFromUint64(7),
Type: "const",
},
"path.b": {
Value: lambdaworks.FeltFromUint64(17),
Type: "label",
},
"other_path.A": {
Destination: "path.A",
Type: "alias",
},
"other_path.b": {
Destination: "path.b",
Type: "alias",
},
},
}
expectedConstants := map[string]lambdaworks.Felt{
"path.A": lambdaworks.FeltFromUint64(7),
"other_path.A": lambdaworks.FeltFromUint64(7),
}
if !reflect.DeepEqual(program.ExtractConstants(), expectedConstants) {
t.Errorf("Wrong Constants, expected %v, got %v", expectedConstants, program.ExtractConstants())
}
}