Skip to content

Commit

Permalink
cannon: Consistent state serialization (#12151)
Browse files Browse the repository at this point in the history
* cannon: Consistent state serialization

* nosilent cmp

* fix run input

* add elf target dep
  • Loading branch information
Inphi authored Sep 26, 2024
1 parent a0eee5c commit bb87eef
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
23 changes: 22 additions & 1 deletion cannon/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ contract:
test: elf contract
go test -v ./...

diff-%-cannon: cannon elf
$$OTHER_CANNON load-elf --type $* --path ./testdata/example/bin/hello.elf --out ./bin/prestate-other.bin.gz --meta ""
./bin/cannon load-elf --type $* --path ./testdata/example/bin/hello.elf --out ./bin/prestate.bin.gz --meta ""
@cmp ./bin/prestate-other.bin.gz ./bin/prestate.bin.gz
@if [ $$? -eq 0 ]; then \
echo "Generated identical prestates"; \
else \
echo "Generated different prestates"; \
exit 1; \
fi
$$OTHER_CANNON run --proof-at '=0' --stop-at '=100000000' --input=./bin/prestate.bin.gz --output ./bin/out-other.bin.gz --meta ""
./bin/cannon run --proof-at '=0' --stop-at '=100000000' --input=./bin/prestate.bin.gz --output ./bin/out.bin.gz --meta ""
@cmp ./bin/out-other.bin.gz ./bin/out.bin.gz
@if [ $$? -eq 0 ]; then \
echo "Generated identical states"; \
else \
echo "Generated different prestates"; \
exit 1; \
fi

fuzz:
# Common vm tests
go test $(FUZZLDFLAGS) -run NOTAREALTEST -v -fuzztime 10s -fuzz=FuzzStateSyscallBrk ./mipsevm/tests
Expand All @@ -65,4 +85,5 @@ fuzz:
clean \
test \
lint \
fuzz
fuzz \
diff-%-cannon
8 changes: 7 additions & 1 deletion cannon/mipsevm/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"fmt"
"io"
"math/bits"
"slices"
"sort"

"github.com/ethereum/go-ethereum/crypto"
"golang.org/x/exp/maps"
)

// Note: 2**12 = 4 KiB, the min phys page size in the Go runtime.
Expand Down Expand Up @@ -299,7 +301,11 @@ func (m *Memory) Serialize(out io.Writer) error {
if err := binary.Write(out, binary.BigEndian, uint32(m.PageCount())); err != nil {
return err
}
for pageIndex, page := range m.pages {
indexes := maps.Keys(m.pages)
// iterate sorted map keys for consistent serialization
slices.Sort(indexes)
for _, pageIndex := range indexes {
page := m.pages[pageIndex]
if err := binary.Write(out, binary.BigEndian, pageIndex); err != nil {
return err
}
Expand Down

0 comments on commit bb87eef

Please sign in to comment.