Skip to content

Commit

Permalink
use wasmtime instead wat2wasm binary (#374)
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerKSI committed May 3, 2024
1 parent d46e1d9 commit b6ed5a9
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 55 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ jobs:
with:
go-version: '1.21.7'

- name: Install Wabt (wat2wasm)
run: |
wget https://github.com/WebAssembly/wabt/releases/download/1.0.17/wabt-1.0.17-ubuntu.tar.gz
tar -zxf wabt-1.0.17-ubuntu.tar.gz
sudo cp wabt-1.0.17/bin/wat2wasm /usr/local/bin
- name: Check go mod cache
uses: actions/cache@v4
with:
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/simulation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ jobs:
with:
go-version: "1.21.7"
check-latest: true
- name: Install Wabt (wat2wasm)
run: |
wget https://github.com/WebAssembly/wabt/releases/download/1.0.17/wabt-1.0.17-ubuntu.tar.gz
tar -zxf wabt-1.0.17-ubuntu.tar.gz
sudo cp wabt-1.0.17/bin/wat2wasm ~/go/bin
- name: Install runsim
run: go install github.com/cosmos/tools/cmd/runsim@v1.0.0
- uses: actions/cache@v4
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
cosmossdk.io/errors v1.0.1
cosmossdk.io/math v1.3.0
github.com/bandprotocol/go-owasm v0.3.1
github.com/bytecodealliance/wasmtime-go/v20 v20.0.0
github.com/cometbft/cometbft v0.37.5
github.com/cometbft/cometbft-db v0.10.0
github.com/confio/ics23/go v0.9.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOF
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
github.com/bytecodealliance/wasmtime-go/v20 v20.0.0 h1:xO8EMdztxRALMRoru7WCIlr10co225tFFUoJ/Ygzdv4=
github.com/bytecodealliance/wasmtime-go/v20 v20.0.0/go.mod h1:Va362hmt7aqwyb2Vu73yHbmx6NkSvGmvHOzJa2xMECQ=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.8.0 h1:ea0Xadu+sHlu7x5O3gKhRpQ1IKiMrSiHttPF0ybECuA=
github.com/bytedance/sonic v1.8.0/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
Expand Down
4 changes: 2 additions & 2 deletions testing/testdata/wasm_1_simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package testdata
// CALL ask_external_data with EID 3 DID 3 CALLDATA "beeb"
// EXECUTE:
// CALL set_return_date with RETDATE "beeb"
var Wasm1 = wat2wasm([]byte(`
var Wasm1 = wat2wasm(`
(module
(type $t0 (func))
(type $t1 (func (param i64 i64 i64 i64)))
Expand Down Expand Up @@ -50,4 +50,4 @@ var Wasm1 = wat2wasm([]byte(`
(table $T0 1 1 funcref)
(memory $memory (export "memory") 17)
(data (i32.const 1024) "beeb"))
`))
`)
4 changes: 2 additions & 2 deletions testing/testdata/wasm_2_return_in_prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package testdata
// CALL set_return_data with RETDATA "beeb" -- Not allowed during prepare
// EXECUTE:
// DO NOTHING
var Wasm2 []byte = wat2wasm([]byte(`
var Wasm2 []byte = wat2wasm(`
(module
(type $t0 (func))
(type $t2 (func (param i64 i64)))
Expand All @@ -18,4 +18,4 @@ var Wasm2 []byte = wat2wasm([]byte(`
(func $execute (export "execute"))
(memory $memory (export "memory") 17)
(data (i32.const 1024) "beeb"))
`))
`)
4 changes: 2 additions & 2 deletions testing/testdata/wasm_3_do_nothing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package testdata
// DO NOTHING
// EXECUTE:
// DO NOTHING
var Wasm3 []byte = wat2wasm([]byte(`
var Wasm3 []byte = wat2wasm(`
(module
(type $t0 (func))
(type $t1 (func (param i64 i64 i64 i64)))
Expand All @@ -18,4 +18,4 @@ var Wasm3 []byte = wat2wasm([]byte(`
(table $T0 1 1 funcref)
(memory $memory (export "memory") 17)
(data (i32.const 1024) "beeb"))
`))
`)
4 changes: 2 additions & 2 deletions testing/testdata/wasm_56_computation.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
// EXECUTE:
// Loop for `time` times and set return data "new beeb".
func Wasm56(time int) []byte {
return wat2wasm([]byte(fmt.Sprintf(`(module
return wat2wasm(fmt.Sprintf(`(module
(type $t0 (func))
(type $t1 (func (param i64 i64 i64 i64)))
(type $t2 (func (param i64 i64)))
Expand Down Expand Up @@ -49,5 +49,5 @@ func Wasm56(time int) []byte {
call $set_return_data)
(table $T0 1 1 funcref)
(memory $memory (export "memory") 17)
(data (i32.const 1024) "new beeb"))`, time, time)))
(data (i32.const 1024) "new beeb"))`, time, time))
}
4 changes: 2 additions & 2 deletions testing/testdata/wasm_78_large_calldata.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func Wasm78(time int) []byte {
for idx := 0; idx < time; idx++ {
b.Write([]byte("b"))
}
return wat2wasm([]byte(fmt.Sprintf(`(module
return wat2wasm(fmt.Sprintf(`(module
(type $t0 (func))
(type $t1 (func (param i64 i64 i64 i64)))
(type $t2 (func (param i64 i64)))
Expand All @@ -39,5 +39,5 @@ func Wasm78(time int) []byte {
(table $T0 1 1 funcref)
(memory $memory (export "memory") 200)
(data (i32.const 1024) "%s"))
`, time, time, b.String())))
`, time, time, b.String()))
}
4 changes: 2 additions & 2 deletions testing/testdata/wasm_9_set_data_several_times.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package testdata

var Wasm9 []byte = wat2wasm([]byte(`
var Wasm9 []byte = wat2wasm(`
(module
(type $t0 (func))
(type $t1 (func (param i64 i64 i64 i64)))
Expand All @@ -20,4 +20,4 @@ var Wasm9 []byte = wat2wasm([]byte(`
(table $T0 1 1 funcref)
(memory $memory (export "memory") 17)
(data (i32.const 1024) "beeb"))
`))
`)
8 changes: 4 additions & 4 deletions testing/testdata/wasm_extras.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// WasmExtra1 is an extra Owasm code to test creating or editing oracle scripts.
var WasmExtra1 []byte = wat2wasm([]byte(`
var WasmExtra1 []byte = wat2wasm(`
(module
(type $t0 (func))
(type $t2 (func (param i64 i64)))
Expand All @@ -15,10 +15,10 @@ var WasmExtra1 []byte = wat2wasm([]byte(`
(func $execute (export "execute") (type $t0))
(memory $memory (export "memory") 17))
`))
`)

// WasmExtra2 is another extra Owasm code to test creating or editing oracle scripts.
var WasmExtra2 []byte = wat2wasm([]byte(`
var WasmExtra2 []byte = wat2wasm(`
(module
(type $t0 (func))
(type $t1 (func (param i64 i64 i64 i64)))
Expand All @@ -28,7 +28,7 @@ var WasmExtra2 []byte = wat2wasm([]byte(`
(func $prepare (export "prepare") (type $t0))
(func $execute (export "execute") (type $t0))
(memory $memory (export "memory") 17))
`))
`)

var (
WasmExtra1FileName string
Expand Down
30 changes: 4 additions & 26 deletions testing/testdata/wasm_util.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package testdata

import (
"os"
"os/exec"

owasm "github.com/bandprotocol/go-owasm/api"
"github.com/bytecodealliance/wasmtime-go/v20"

"github.com/bandprotocol/chain/v2/x/oracle/types"
)
Expand All @@ -23,31 +21,11 @@ func Compile(code []byte) []byte {
return compiled
}

func wat2wasm(wat []byte) []byte {
inputFile, err := os.CreateTemp("", "input")
if err != nil {
panic(err)
}
defer os.Remove(inputFile.Name())

outputFile, err := os.CreateTemp("", "output")
if err != nil {
panic(err)
}
defer os.Remove(outputFile.Name())

if _, err := inputFile.Write(wat); err != nil {
panic(err)
}

if err := exec.Command("wat2wasm", inputFile.Name(), "-o", outputFile.Name()).Run(); err != nil { //nolint:gosec
panic(err)
}

output, err := os.ReadFile(outputFile.Name())
func wat2wasm(wat string) []byte {
wasm, err := wasmtime.Wat2Wasm(wat)
if err != nil {
panic(err)
}

return output
return wasm
}
4 changes: 2 additions & 2 deletions x/oracle/simulation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (suite *SimTestSuite) TestSimulateMsgCreateOracleScript() {
suite.Require().
Equal("nDQfwRLGIWozYaOAilMBcObErwgTDNGWnwQMUgFFSKtPDMEoEQCTKVREqrXZSGLqwTMcxHfWotDllNkIJPMbXzjDVjPOOjCFuIvT", msg.SourceCodeURL)
suite.Require().
Equal("0061736d0100000001100360000060047e7e7e7e0060027e7e00022f0203656e761161736b5f65787465726e616c5f64617461000103656e760f7365745f72657475726e5f6461746100020303020000040501700101010503010011071e030770726570617265000207657865637574650003066d656d6f727902000a4e022601017e42014201418008ad22004204100042024202200042041000420342032000420410000b2501017f4100210002400340200041016a2100200041e400490d000b0b418008ad420410010b0b0b01004180080b0462656562", hex.EncodeToString(msg.Code))
Equal("0061736d0100000001100360000060047e7e7e7e0060027e7e00022f0203656e761161736b5f65787465726e616c5f64617461000103656e760f7365745f72657475726e5f6461746100020303020000040501700101010503010011071e030770726570617265000207657865637574650003066d656d6f727902000a4e022601017e42014201418008ad22004204100042024202200042041000420342032000420410000b2501017f4100210002400340200041016a2100200041e400490d000b0b418008ad420410010b0b0b01004180080b0462656562006f046e616d65013704001161736b5f65787465726e616c5f64617461010f7365745f72657475726e5f64617461020770726570617265030765786563757465020e02020100026c3003010003696478040d030002743001027431020274320505010002543006090100066d656d6f7279", hex.EncodeToString(msg.Code))
suite.Require().Equal("band1n5sqxutsmk6eews5z9z673wv7n9wah8hjlxyuf", msg.Owner)
suite.Require().Equal(types.TypeMsgCreateOracleScript, msg.Type())
suite.Require().Equal(types.ModuleName, msg.Route())
Expand Down Expand Up @@ -330,7 +330,7 @@ func (suite *SimTestSuite) TestSimulateMsgEditOracleScript() {
suite.Require().
Equal("WozYaOAilMBcObErwgTDNGWnwQMUgFFSKtPDMEoEQCTKVREqrXZSGLqwTMcxHfWotDllNkIJPMbXzjDVjPOOjCFuIvTyhXKLyhUS", msg.SourceCodeURL)
suite.Require().
Equal("0061736d0100000001100360000060047e7e7e7e0060027e7e00022f0203656e761161736b5f65787465726e616c5f64617461000103656e760f7365745f72657475726e5f6461746100020303020000040501700101010503010011071e030770726570617265000207657865637574650003066d656d6f727902000a4e022601017e42014201418008ad22004204100042024202200042041000420342032000420410000b2501017f4100210002400340200041016a2100200041e400490d000b0b418008ad420410010b0b0b01004180080b0462656562", hex.EncodeToString(msg.Code))
Equal("0061736d0100000001100360000060047e7e7e7e0060027e7e00022f0203656e761161736b5f65787465726e616c5f64617461000103656e760f7365745f72657475726e5f6461746100020303020000040501700101010503010011071e030770726570617265000207657865637574650003066d656d6f727902000a4e022601017e42014201418008ad22004204100042024202200042041000420342032000420410000b2501017f4100210002400340200041016a2100200041e400490d000b0b418008ad420410010b0b0b01004180080b0462656562006f046e616d65013704001161736b5f65787465726e616c5f64617461010f7365745f72657475726e5f64617461020770726570617265030765786563757465020e02020100026c3003010003696478040d030002743001027431020274320505010002543006090100066d656d6f7279", hex.EncodeToString(msg.Code))
suite.Require().Equal("band1tnh2q55v8wyygtt9srz5safamzdengsneky62e", msg.Owner)
suite.Require().Equal(types.TypeMsgEditOracleScript, msg.Type())
suite.Require().Equal(types.ModuleName, msg.Route())
Expand Down

0 comments on commit b6ed5a9

Please sign in to comment.