Skip to content

Commit

Permalink
add simualtion test for oracle module
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerKSI committed Aug 12, 2023
1 parent 41ab8bc commit 23d190c
Show file tree
Hide file tree
Showing 25 changed files with 1,512 additions and 37 deletions.
26 changes: 25 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ VERSION := $(shell echo $(shell git describe --tags) | sed 's/^v//')
COMMIT := $(shell git log -1 --format='%H')
LEDGER_ENABLED ?= true
BINDIR ?= $(GOPATH)/bin
APP = ./app

DOCKER := $(shell which docker)
DOCKER_BUF := $(DOCKER) run --rm -v $(CURDIR):/workspace --workdir /workspace bufbuild/buf
Expand Down Expand Up @@ -36,6 +37,8 @@ ldflags := $(strip $(ldflags))

BUILD_FLAGS := -tags "$(build_tags_comma_sep)" -ldflags '$(ldflags)'

include contrib/devtools/Makefile

all: install

install: go.sum
Expand Down Expand Up @@ -156,4 +159,25 @@ proto-update-deps:
## Issue link: https://github.com/confio/ics23/issues/32
@sed -i '4ioption go_package = "github.com/confio/ics23/go";' $(CONFIO_TYPES)/proofs.proto

.PHONY: proto-all proto-gen proto-gen-any proto-swagger-gen proto-format proto-lint proto-check-breaking proto-update-deps
###############################################################################
### Simulation ###
###############################################################################

test-sim-import-export: runsim
@echo "Running application import/export simulation. This may take several minutes..."
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(APP) -ExitOnFail 50 5 TestAppImportExport

test-sim-multi-seed-short: runsim
@echo "Running short multi-seed application simulation. This may take awhile!"
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(APP) -ExitOnFail 50 5 TestFullAppSimulation

test-sim-after-import: runsim
@echo "Running application simulation-after-import. This may take several minutes..."
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(APP) -ExitOnFail 50 5 TestAppSimulationAfterImport

test-sim-deterministic: runsim
@echo "Running application deterministic simulation. This may take awhile!"
@$(BINDIR)/runsim -Jobs=4 -SimAppPkg=$(APP) -ExitOnFail 1 1 TestAppStateDeterminism

.PHONY: proto-all proto-gen proto-gen-any proto-swagger-gen proto-format proto-lint proto-check-breaking proto-update-deps \
test-sim-import-export test-sim-multi-seed-short test-sim-after-import test-sim-deterministic
8 changes: 7 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,13 @@ func NewBandApp(
scopedOracleKeeper,
owasmVM,
)
oracleModule := oracle.NewAppModule(app.OracleKeeper)
oracleModule := oracle.NewAppModule(
appCodec,
app.OracleKeeper,
app.AccountKeeper,
app.BankKeeper,
app.StakingKeeper,
)
oracleIBCModule := oracle.NewIBCModule(app.OracleKeeper)

// Create static IBC router, add transfer route, then set and seal it
Expand Down
36 changes: 36 additions & 0 deletions contrib/devtools/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## To test locally:
# docker build --pull --rm -f "contrib/devtools/Dockerfile" -t cosmossdk-proto:latest "contrib/devtools"
# docker run --rm -v $(pwd):/workspace --workdir /workspace cosmossdk-proto sh ./scripts/protocgen.sh

FROM bufbuild/buf:1.9.0 as BUILDER
FROM golang:1.19-alpine

RUN apk add --no-cache \
nodejs \
npm \
git \
make \
clang-extra-tools

RUN npm install -g swagger-combine

ARG UNAME=protobuild
ARG UID=1000
RUN adduser -u $UID -s /bin/sh $UNAME -D
USER $UNAME

ENV GOLANG_PROTOBUF_VERSION=1.28.1 \
GRPC_GATEWAY_VERSION=1.16.0

RUN go install github.com/cosmos/cosmos-proto/cmd/protoc-gen-go-pulsar@latest && \
go install google.golang.org/protobuf/cmd/protoc-gen-go@v${GOLANG_PROTOBUF_VERSION} && \
go install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway@v${GRPC_GATEWAY_VERSION} \
github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger@v${GRPC_GATEWAY_VERSION}

# install all gogo protobuf binaries
RUN git clone https://github.com/cosmos/gogoproto.git; \
cd gogoproto; \
go mod download; \
make install

COPY --from=BUILDER /usr/local/bin /usr/local/bin
76 changes: 76 additions & 0 deletions contrib/devtools/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
###
# Find OS and Go environment
# GO contains the Go binary
# FS contains the OS file separator
###
ifeq ($(OS),Windows_NT)
GO := $(shell where go.exe 2> NUL)
FS := "\\"
else
GO := $(shell command -v go 2> /dev/null)
FS := "/"
endif

ifeq ($(GO),)
$(error could not find go. Is it in PATH? $(GO))
endif

###############################################################################
### Functions ###
###############################################################################

go_get = $(if $(findstring Windows_NT,$(OS)),\
IF NOT EXIST $(GITHUBDIR)$(FS)$(1)$(FS) ( mkdir $(GITHUBDIR)$(FS)$(1) ) else (cd .) &\
IF NOT EXIST $(GITHUBDIR)$(FS)$(1)$(FS)$(2)$(FS) ( cd $(GITHUBDIR)$(FS)$(1) && git clone https://github.com/$(1)/$(2) ) else (cd .) &\
,\
mkdir -p $(GITHUBDIR)$(FS)$(1) &&\
(test ! -d $(GITHUBDIR)$(FS)$(1)$(FS)$(2) && cd $(GITHUBDIR)$(FS)$(1) && git clone https://github.com/$(1)/$(2)) || true &&\
)\
cd $(GITHUBDIR)$(FS)$(1)$(FS)$(2) && git fetch origin && git checkout -q $(3)

mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
mkfile_dir := $(shell cd $(shell dirname $(mkfile_path)); pwd)


###############################################################################
### Tools ###
###############################################################################

PREFIX ?= /usr/local
BIN ?= $(PREFIX)/bin
UNAME_S ?= $(shell uname -s)
UNAME_M ?= $(shell uname -m)

GOPATH ?= $(shell $(GO) env GOPATH)
GITHUBDIR := $(GOPATH)$(FS)src$(FS)github.com

BUF_VERSION ?= 0.11.0

TOOLS_DESTDIR ?= $(GOPATH)/bin
STATIK = $(TOOLS_DESTDIR)/statik
RUNSIM = $(TOOLS_DESTDIR)/runsim

tools: tools-stamp
tools-stamp: statik runsim
# Create dummy file to satisfy dependency and avoid
# rebuilding when this Makefile target is hit twice
# in a row.
touch $@

# Install the runsim binary
statik: $(STATIK)
$(STATIK):
@echo "Installing statik..."
@go install github.com/rakyll/statik@v0.1.6

# Install the runsim binary
runsim: $(RUNSIM)
$(RUNSIM):
@echo "Installing runsim..."
@go install github.com/cosmos/tools/cmd/runsim@v1.0.0

tools-clean:
rm -f $(STATIK) $(GOLANGCI_LINT) $(RUNSIM)
rm -f tools-stamp

.PHONY: tools-clean statik runsim
6 changes: 6 additions & 0 deletions contrib/devtools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Contributors

Thanks to the entire Cosmos SDK team and the contributors who put their efforts into making simulation testing
easier to implement. 🤗

<https://github.com/cosmos/cosmos-sdk/blob/master/contrib/devtools/Makefile>
5 changes: 3 additions & 2 deletions testing/testapp/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
owasm "github.com/bandprotocol/go-owasm/api"

bandapp "github.com/bandprotocol/chain/v2/app"
"github.com/bandprotocol/chain/v2/testing/testdata"
"github.com/bandprotocol/chain/v2/x/oracle/keeper"
"github.com/bandprotocol/chain/v2/x/oracle/types"
)
Expand Down Expand Up @@ -193,11 +194,11 @@ func getGenesisOracleScripts(homePath string) []types.OracleScript {
fc := filecache.New(dir)
OracleScripts = []types.OracleScript{{}} // 0th index should be ignored
wasms := [][]byte{
Wasm1, Wasm2, Wasm3, Wasm4, Wasm56(10), Wasm56(10000000), Wasm78(10), Wasm78(2000), Wasm9,
testdata.Wasm1, testdata.Wasm2, testdata.Wasm3, testdata.Wasm4, testdata.Wasm56(10), testdata.Wasm56(10000000), testdata.Wasm78(10), testdata.Wasm78(2000), testdata.Wasm9,
}
for idx := 0; idx < len(wasms); idx++ {
idxStr := fmt.Sprintf("%d", idx+1)
hash := fc.AddFile(compile(wasms[idx]))
hash := fc.AddFile(testdata.Compile(wasms[idx]))
OracleScripts = append(OracleScripts, types.NewOracleScript(
Owner.Address, "name"+idxStr, "desc"+idxStr, hash, "schema"+idxStr, "url"+idxStr,
))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testapp
package testdata

// A simple Owasm script with the following specification:
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testapp
package testdata

// A bad Owasm script with the following specification:
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testapp
package testdata

// A silly oracle script, primarily to test that you must make at least one raw request:
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testapp
package testdata

import (
"encoding/hex"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testapp
package testdata

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testapp
package testdata

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testapp
package testdata

var Wasm9 []byte = wat2wasm([]byte(`
(module
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package testapp
package testdata

import (
"crypto/sha256"
Expand Down Expand Up @@ -34,8 +34,8 @@ var WasmExtra1FileName string
var WasmExtra2FileName string

func init() {
wasm1CompiledHash := sha256.Sum256(compile(WasmExtra1))
wasm2CompiledHash := sha256.Sum256(compile(WasmExtra2))
wasm1CompiledHash := sha256.Sum256(Compile(WasmExtra1))
wasm2CompiledHash := sha256.Sum256(Compile(WasmExtra2))
WasmExtra1FileName = hex.EncodeToString(wasm1CompiledHash[:])
WasmExtra2FileName = hex.EncodeToString(wasm2CompiledHash[:])
}
13 changes: 10 additions & 3 deletions testing/testapp/wasm_util.go → testing/testdata/wasm_util.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package testapp
package testdata

import (
"os"
"os/exec"

owasm "github.com/bandprotocol/go-owasm/api"

"github.com/bandprotocol/chain/v2/x/oracle/types"
)

func compile(code []byte) []byte {
compiled, err := OwasmVM.Compile(code, types.MaxCompiledWasmCodeSize)
func Compile(code []byte) []byte {
owasmVM, err := owasm.NewVm(10)
if err != nil {
panic(err)
}

compiled, err := owasmVM.Compile(code, types.MaxCompiledWasmCodeSize)
if err != nil {
panic(err)
}
Expand Down
7 changes: 5 additions & 2 deletions x/oracle/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
// handleBeginBlock re-calculates and saves the rolling seed value based on block hashes.
func handleBeginBlock(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
// Update rolling seed used for pseudorandom oracle provider selection.
rollingSeed := k.GetRollingSeed(ctx)
k.SetRollingSeed(ctx, append(rollingSeed[1:], req.GetHash()[0]))
hash := req.GetHash()
if len(hash) > 0 {
rollingSeed := k.GetRollingSeed(ctx)
k.SetRollingSeed(ctx, append(rollingSeed[1:], req.GetHash()[0]))
}
// Reward a portion of block rewards (inflation + tx fee) to active oracle validators.
k.AllocateTokens(ctx, req.LastCommitInfo.GetVotes())
}
Expand Down
19 changes: 10 additions & 9 deletions x/oracle/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/bandprotocol/go-owasm/api"

"github.com/bandprotocol/chain/v2/testing/testapp"
"github.com/bandprotocol/chain/v2/testing/testdata"
"github.com/bandprotocol/chain/v2/x/oracle"
"github.com/bandprotocol/chain/v2/x/oracle/types"
)
Expand Down Expand Up @@ -183,7 +184,7 @@ func TestCreateOracleScriptSuccess(t *testing.T) {
osCount := k.GetOracleScriptCount(ctx)
name := "os_1"
description := "beeb"
code := testapp.WasmExtra1
code := testdata.WasmExtra1
schema := "schema"
url := "url"
msg := types.NewMsgCreateOracleScript(
Expand All @@ -201,7 +202,7 @@ func TestCreateOracleScriptSuccess(t *testing.T) {
require.NoError(t, err)
require.Equal(
t,
types.NewOracleScript(testapp.Owner.Address, name, description, testapp.WasmExtra1FileName, schema, url),
types.NewOracleScript(testapp.Owner.Address, name, description, testdata.WasmExtra1FileName, schema, url),
os,
)

Expand All @@ -223,7 +224,7 @@ func TestCreateGzippedOracleScriptSuccess(t *testing.T) {
url := "url"
var buf bytes.Buffer
zw := gz.NewWriter(&buf)
zw.Write(testapp.WasmExtra1)
zw.Write(testdata.WasmExtra1)
zw.Close()
msg := types.NewMsgCreateOracleScript(
name,
Expand All @@ -240,7 +241,7 @@ func TestCreateGzippedOracleScriptSuccess(t *testing.T) {
require.NoError(t, err)
require.Equal(
t,
types.NewOracleScript(testapp.Owner.Address, name, description, testapp.WasmExtra1FileName, schema, url),
types.NewOracleScript(testapp.Owner.Address, name, description, testdata.WasmExtra1FileName, schema, url),
os,
)

Expand Down Expand Up @@ -275,7 +276,7 @@ func TestCreateOracleScriptFail(t *testing.T) {
// Bad Gzip
var buf bytes.Buffer
zw := gz.NewWriter(&buf)
zw.Write(testapp.WasmExtra1)
zw.Write(testdata.WasmExtra1)
zw.Close()
msg = types.NewMsgCreateOracleScript(
name,
Expand All @@ -295,7 +296,7 @@ func TestEditOracleScriptSuccess(t *testing.T) {
_, ctx, k := testapp.CreateTestInput(false)
newName := "os_2"
newDescription := "beebbeeb"
newCode := testapp.WasmExtra2
newCode := testdata.WasmExtra2
newSchema := "new_schema"
newURL := "new_url"
msg := types.NewMsgEditOracleScript(
Expand All @@ -318,7 +319,7 @@ func TestEditOracleScriptSuccess(t *testing.T) {
testapp.Alice.Address,
newName,
newDescription,
testapp.WasmExtra2FileName,
testdata.WasmExtra2FileName,
newSchema,
newURL,
),
Expand All @@ -336,7 +337,7 @@ func TestEditOracleScriptFail(t *testing.T) {
_, ctx, k := testapp.CreateTestInput(false)
newName := "os_2"
newDescription := "beebbeeb"
newCode := testapp.WasmExtra2
newCode := testdata.WasmExtra2
newSchema := "new_schema"
newURL := "new_url"
// Bad ID
Expand Down Expand Up @@ -384,7 +385,7 @@ func TestEditOracleScriptFail(t *testing.T) {
// Bad Gzip
var buf bytes.Buffer
zw := gz.NewWriter(&buf)
zw.Write(testapp.WasmExtra2)
zw.Write(testdata.WasmExtra2)
zw.Close()
msg = types.NewMsgEditOracleScript(
1,
Expand Down
Loading

0 comments on commit 23d190c

Please sign in to comment.