Skip to content

Commit

Permalink
Add utils.OrderMap and use it in starknet_getCompiledCasm
Browse files Browse the repository at this point in the history
  • Loading branch information
kirugan committed Oct 29, 2024
1 parent 34024d5 commit 60d2443
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
6 changes: 5 additions & 1 deletion rpc/executables.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,15 @@ func adaptCairo0Class(class *core.Cairo0Class) (*CasmCompiledContractClass, erro
bytecode = append(bytecode, f)
}

hints, err := hintRunnerZero.GetZeroHints(&cairo0)
classHints, err := hintRunnerZero.GetZeroHints(&cairo0)
if err != nil {
return nil, err
}

var hints [][2]any // slice of 2-element tuples where first value is pc, and second value is slice of hints
for pc, hintItems := range utils.OrderMap(classHints) {
hints = append(hints, [2]any{pc, hintItems})
}
rawHints, err := json.Marshal(hints)
if err != nil {
return nil, err
Expand Down
26 changes: 26 additions & 0 deletions utils/ordered_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package utils

import (
"cmp"
"iter"
"slices"
)

func OrderMap[K cmp.Ordered, T any](m map[K]T) iter.Seq2[K, T] {
// 1. collect keys
keys := make([]K, 0, len(m))
for k := range m {
keys = append(keys, k)
}

// 2. sort them
slices.Sort(keys)
return func(yield func(K, T) bool) {
// 3. because keys are sorted now, we can iterate over them in order
for _, k := range keys {
if !yield(k, m[k]) {
return
}
}
}
}

0 comments on commit 60d2443

Please sign in to comment.