Skip to content

Commit

Permalink
test-cases/go: add stylus contract
Browse files Browse the repository at this point in the history
this is the only test that checks 5 recursive stylus calls with one-step-proofs for each relevant SwitchThread
  • Loading branch information
tsahee committed May 11, 2024
1 parent df5dcb5 commit f2448fe
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ $(arbitrator_jit): $(DEP_PREDICATE) $(jit_files)
$(arbitrator_cases)/rust/$(wasm32_wasi)/%.wasm: $(arbitrator_cases)/rust/src/bin/%.rs $(arbitrator_cases)/rust/src/lib.rs
cargo build --manifest-path $(arbitrator_cases)/rust/Cargo.toml --release --target wasm32-wasi --bin $(patsubst $(arbitrator_cases)/rust/$(wasm32_wasi)/%.wasm,%, $@)

$(arbitrator_cases)/go/testcase.wasm: $(arbitrator_cases)/go/*.go
$(arbitrator_cases)/go/testcase.wasm: $(arbitrator_cases)/go/*.go .make/solgen
cd $(arbitrator_cases)/go && GOOS=wasip1 GOARCH=wasm go build -o testcase.wasm

$(arbitrator_generated_header): $(DEP_PREDICATE) $(stylus_files)
Expand Down Expand Up @@ -439,8 +439,12 @@ target/testdata/preimages.bin:
contracts/test/prover/proofs/rust-%.json: $(arbitrator_cases)/rust/$(wasm32_wasi)/%.wasm $(prover_bin) $(arbitrator_wasm_libs) target/testdata/preimages.bin
$(prover_bin) $< $(arbitrator_wasm_lib_flags) -o $@ -b --allow-hostapi --require-success --inbox-add-stub-headers --inbox $(arbitrator_cases)/rust/data/msg0.bin --inbox $(arbitrator_cases)/rust/data/msg1.bin --delayed-inbox $(arbitrator_cases)/rust/data/msg0.bin --delayed-inbox $(arbitrator_cases)/rust/data/msg1.bin --preimages target/testdata/preimages.bin

contracts/test/prover/proofs/go.json: $(arbitrator_cases)/go/testcase.wasm $(prover_bin) $(arbitrator_wasm_libs) target/testdata/preimages.bin $(arbitrator_tests_link_deps)
$(prover_bin) $< $(arbitrator_wasm_lib_flags) -o $@ -i 50000000 --require-success --preimages target/testdata/preimages.bin
contracts/test/prover/proofs/go.json: $(arbitrator_cases)/go/testcase.wasm $(prover_bin) $(arbitrator_wasm_libs) target/testdata/preimages.bin $(arbitrator_tests_link_deps) $(arbitrator_cases)/user.wasm
$(prover_bin) $< $(arbitrator_wasm_lib_flags) -o $@ -b --require-success --preimages target/testdata/preimages.bin --stylus-modules $(arbitrator_cases)/user.wasm

# avoid testing user.wasm in onestepproofs. It can only run as stylus program.
contracts/test/prover/proofs/user.json:
echo "[]" > $@

# avoid testing read-inboxmsg-10 in onestepproofs. It's used for go challenge testing.
contracts/test/prover/proofs/read-inboxmsg-10.json:
Expand Down
44 changes: 44 additions & 0 deletions arbitrator/prover/test-cases/go/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright 2021-2024, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE

//go:build wasm
// +build wasm

package main

import (
Expand All @@ -19,6 +22,7 @@ import (
merkletree "github.com/wealdtech/go-merkletree"

"github.com/offchainlabs/nitro/arbcompress"
"github.com/offchainlabs/nitro/arbos/programs"
"github.com/offchainlabs/nitro/arbutil"
"github.com/offchainlabs/nitro/wavmio"
)
Expand Down Expand Up @@ -69,11 +73,51 @@ const BYTES_PER_FIELD_ELEMENT = 32

var BLS_MODULUS, _ = new(big.Int).SetString("52435875175126190479447740508185965837690552500527637822603658699938581184513", 10)

var stylusModuleHash = common.HexToHash("a149cf8113ff9c95f2c8c2a1423575367de86dd422d87114bb9ea47baf535dd7") // user.wat

func callStylusProgram(recurse int) {
evmData := programs.EvmData{}
progParams := programs.ProgParams{
MaxDepth: 10000,
InkPrice: 1,
DebugMode: true,
}
reqHandler := func(req programs.RequestType, input []byte) ([]byte, []byte, uint64) {
fmt.Printf("got request type %d req %v\n", req, input)
if req == programs.GetBytes32 {
if recurse > 0 {
callStylusProgram(recurse - 1)
}
answer := common.Hash{}
return answer[:], nil, 1
}

panic("unsupported call")
}
calldata := common.Hash{}.Bytes()
_, _, err := programs.CallProgramLoop(
stylusModuleHash,
calldata,
160000000,
&evmData,
&progParams,
reqHandler)
if err != nil {
panic(err)
}
}

func main() {
fmt.Printf("starting executable with %v arg(s): %v\n", len(os.Args), os.Args)
runtime.GC()
time.Sleep(time.Second)

fmt.Printf("Stylus test\n")

callStylusProgram(5)

fmt.Printf("Stylus test done!\n")

// Data for the tree
data := [][]byte{
[]byte("Foo"),
Expand Down

0 comments on commit f2448fe

Please sign in to comment.