Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Update go-owasm #3195

Merged
merged 10 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG_UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

### Chain (Non-consensus)

- (impv) [\#3195](https://github.com/bandprotocol/bandchain/pull/3195) Patch go owasm
- (impv) [\#2912](https://github.com/bandprotocol/bandchain/pull/2912) proof contain the real block time instead of time hash

### Yoda
Expand All @@ -34,7 +35,7 @@
- (bugs) [\#2927](https://github.com/bandprotocol/bandchain/pull/2927) Fix default tab on Route's search
- (feat) [\#2854](https://github.com/bandprotocol/bandchain/pull/2854) Implement reinvest
- (chore) [\#2793](https://github.com/bandprotocol/bandchain/pull/2793) Remove legacy row and column
- (impv) [\#2686](https://github.com/bandprotocol/bandchain/pull/2686) Support array on generateDecodeLibSolidity
- (impv) [\#2686](https://github.com/bandprotocol/bandchain/pull/2686) Support array on generateDecodeLibSolidity

### Bridges

Expand Down
9 changes: 7 additions & 2 deletions chain/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/bandprotocol/bandchain/chain/x/oracle"
bandante "github.com/bandprotocol/bandchain/chain/x/oracle/ante"
bandsupply "github.com/bandprotocol/bandchain/chain/x/supply"
"github.com/bandprotocol/go-owasm/api"
)

const (
Expand Down Expand Up @@ -138,7 +139,7 @@ func SetBech32AddressPrefixesAndBip44CoinType(config *sdk.Config) {
func NewBandApp(
logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool,
invCheckPeriod uint, skipUpgradeHeights map[int64]bool, home string,
disableFeelessReports bool, baseAppOptions ...func(*bam.BaseApp),
disableFeelessReports bool, owasmCacheSize uint32, baseAppOptions ...func(*bam.BaseApp),
) *BandApp {
cdc := MakeCodec()
bApp := bam.NewBaseApp(AppName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...)
Expand All @@ -157,6 +158,10 @@ func NewBandApp(
keys: keys,
tKeys: tKeys,
}
owasmVM, err := api.NewVm(owasmCacheSize)
if err != nil {
panic(err)
}
// Initialize params keeper and module subspaces.
app.ParamsKeeper = params.NewKeeper(cdc, keys[params.StoreKey], tKeys[params.TStoreKey])
authSubspace := app.ParamsKeeper.Subspace(auth.DefaultParamspace)
Expand All @@ -183,7 +188,7 @@ func NewBandApp(
app.CrisisKeeper = crisis.NewKeeper(crisisSubspace, invCheckPeriod, app.SupplyKeeper, auth.FeeCollectorName)
app.SlashingKeeper = slashing.NewKeeper(cdc, keys[slashing.StoreKey], &stakingKeeper, slashingSubspace)
app.UpgradeKeeper = upgrade.NewKeeper(skipUpgradeHeights, keys[upgrade.StoreKey], cdc)
app.OracleKeeper = oracle.NewKeeper(cdc, keys[oracle.StoreKey], filepath.Join(viper.GetString(cli.HomeFlag), "files"), auth.FeeCollectorName, oracleSubspace, app.SupplyKeeper, &stakingKeeper, app.DistrKeeper)
app.OracleKeeper = oracle.NewKeeper(cdc, keys[oracle.StoreKey], filepath.Join(viper.GetString(cli.HomeFlag), "files"), auth.FeeCollectorName, oracleSubspace, app.SupplyKeeper, &stakingKeeper, app.DistrKeeper, owasmVM)
// Register the proposal types.
govRouter := gov.NewRouter()
govRouter.
Expand Down
8 changes: 6 additions & 2 deletions chain/cmd/bandd/genos.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/bandprotocol/bandchain/chain/pkg/filecache"
"github.com/bandprotocol/bandchain/chain/x/oracle"
"github.com/bandprotocol/bandchain/chain/x/oracle/types"
"github.com/bandprotocol/bandchain/go-owasm/api"
"github.com/bandprotocol/go-owasm/api"
)

// AddGenesisOracleScriptCmd returns add-oracle-script cobra Command.
Expand All @@ -33,7 +33,11 @@ func AddGenesisOracleScriptCmd(ctx *server.Context, cdc *codec.Codec, defaultNod
if err != nil {
return err
}
compiledData, err := api.Compile(data, types.MaxCompiledWasmCodeSize)
vm, err := api.NewVm(0) // The compilation doesn't use cache
if err != nil {
return err
}
compiledData, err := vm.Compile(data, types.MaxCompiledWasmCodeSize)
if err != nil {
return err
}
Expand Down
7 changes: 5 additions & 2 deletions chain/cmd/bandd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
flagEnableFastSync = "enable-fast-sync"
flagWithPricer = "with-pricer"
flagWithRequestSearch = "with-request-search"
flagWithOwasmCacheSize = "oracle-script-cache-size"
)

var invCheckPeriod uint
Expand Down Expand Up @@ -74,6 +75,7 @@ func main() {
rootCmd.PersistentFlags().Bool(flagEnableFastSync, false, "[Experimental] Enable fast sync mode")
rootCmd.PersistentFlags().String(flagWithRequestSearch, "", "[Experimental] Enable mode to save request in sql database")
rootCmd.PersistentFlags().String(flagWithPricer, "", "[Experimental] Enable mode to save price in level db")
rootCmd.PersistentFlags().Uint(flagWithOwasmCacheSize, 100, "[Experimental] Number of oracle scripts to cache")
err := executor.Execute()
if err != nil {
panic(err)
Expand All @@ -100,6 +102,7 @@ func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer) abci.Application
logger, db, traceStore, true, invCheckPeriod, skipUpgradeHeights,
viper.GetString(flags.FlagHome),
viper.GetBool(flagDisableFeelessReports),
viper.GetUint32(flagWithOwasmCacheSize),
baseapp.SetPruning(pruningOpts),
baseapp.SetMinGasPrices(viper.GetString(server.FlagMinGasPrices)),
baseapp.SetHaltHeight(viper.GetUint64(server.FlagHaltHeight)),
Expand Down Expand Up @@ -139,7 +142,7 @@ func exportAppStateAndTMValidators(
) (json.RawMessage, []tmtypes.GenesisValidator, error) {

if height != -1 {
bandApp := app.NewBandApp(logger, db, traceStore, false, uint(1), map[int64]bool{}, "", viper.GetBool(flagDisableFeelessReports))
bandApp := app.NewBandApp(logger, db, traceStore, false, uint(1), map[int64]bool{}, "", viper.GetBool(flagDisableFeelessReports), viper.GetUint32(flagWithOwasmCacheSize))
err := bandApp.LoadHeight(height)
if err != nil {
return nil, nil, err
Expand All @@ -148,6 +151,6 @@ func exportAppStateAndTMValidators(
return bandApp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList)
}

bandApp := app.NewBandApp(logger, db, traceStore, true, uint(1), map[int64]bool{}, "", viper.GetBool(flagDisableFeelessReports))
bandApp := app.NewBandApp(logger, db, traceStore, true, uint(1), map[int64]bool{}, "", viper.GetBool(flagDisableFeelessReports), viper.GetUint32(flagWithOwasmCacheSize))
return bandApp.ExportAppStateAndValidators(forZeroHeight, jailWhiteList)
}
4 changes: 1 addition & 3 deletions chain/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/bandprotocol/bandchain/chain
go 1.13

require (
github.com/bandprotocol/bandchain/go-owasm v0.0.0-00010101000000-000000000000
github.com/bandprotocol/go-owasm v0.0.0-20210311072328-a6859c27139c
github.com/cosmos/cosmos-sdk v0.39.2
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d
github.com/ethereum/go-ethereum v1.9.19
Expand Down Expand Up @@ -38,5 +38,3 @@ require (
)

replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.2-alpha.regen.1

replace github.com/bandprotocol/bandchain/go-owasm => ../go-owasm
3 changes: 2 additions & 1 deletion chain/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQ
github.com/aws/aws-sdk-go v1.25.48/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/bandprotocol/go-owasm v0.0.0-20210311072328-a6859c27139c h1:G0NLmoFYgPJqH9M2HLkGHayY9euzrEdOOt30/Pqbfqo=
github.com/bandprotocol/go-owasm v0.0.0-20210311072328-a6859c27139c/go.mod h1:buKhSOd32eN0zPbZ/k+4sa6znl33cDg+8JnORQD3/c8=
github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d h1:1aAija9gr0Hyv4KfQcRcwlmFIrhkDmIj2dz5bkg/s/8=
github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d/go.mod h1:icNx/6QdFblhsEjZehARqbNumymUT/ydwlLojFdv7Sk=
github.com/benbjohnson/clock v1.0.2/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
Expand Down Expand Up @@ -669,7 +671,6 @@ github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5Fsn
github.com/prometheus/client_golang v1.3.0/go.mod h1:hJaj2vgQTGQmVCsAACORcieXFeDPbaTKGT+JTgUa3og=
github.com/prometheus/client_golang v1.5.1 h1:bdHYieyGlH+6OLEk2YQha8THib30KP0/yD0YH9m6xcA=
github.com/prometheus/client_golang v1.5.1/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU=
github.com/prometheus/client_golang v1.8.0 h1:zvJNkoCFAnYFNC24FV8nW4JdRJ3GIFcLbg65lL/JDcw=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down
4 changes: 4 additions & 0 deletions chain/x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/bandprotocol/bandchain/chain/pkg/filecache"
"github.com/bandprotocol/bandchain/chain/x/oracle/types"
owasm "github.com/bandprotocol/go-owasm/api"
)

const (
Expand All @@ -25,13 +26,15 @@ type Keeper struct {
supplyKeeper types.SupplyKeeper
stakingKeeper types.StakingKeeper
distrKeeper types.DistrKeeper
owasmVM *owasm.Vm
}

// NewKeeper creates a new oracle Keeper instance.
func NewKeeper(
cdc *codec.Codec, key sdk.StoreKey, fileDir string, feeCollectorName string,
paramSpace params.Subspace, supplyKeeper types.SupplyKeeper,
stakingKeeper types.StakingKeeper, distrKeeper types.DistrKeeper,
owasmVM *owasm.Vm,
) Keeper {
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(ParamKeyTable())
Expand All @@ -45,6 +48,7 @@ func NewKeeper(
supplyKeeper: supplyKeeper,
stakingKeeper: stakingKeeper,
distrKeeper: distrKeeper,
owasmVM: owasmVM,
}
}

Expand Down
3 changes: 1 addition & 2 deletions chain/x/oracle/keeper/oracle_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/bandprotocol/bandchain/chain/x/oracle/types"
"github.com/bandprotocol/bandchain/go-owasm/api"
)

// HasOracleScript checks if the oracle script of this ID exists in the storage.
Expand Down Expand Up @@ -79,7 +78,7 @@ func (k Keeper) AddOracleScriptFile(file []byte) (string, error) {
if bytes.Equal(file, types.DoNotModifyBytes) {
return types.DoNotModify, nil
}
compiledFile, err := api.Compile(file, types.MaxCompiledWasmCodeSize)
compiledFile, err := k.owasmVM.Compile(file, types.MaxCompiledWasmCodeSize)
if err != nil {
return "", sdkerrors.Wrapf(types.ErrOwasmCompilation, "with error: %s", err.Error())
}
Expand Down
3 changes: 1 addition & 2 deletions chain/x/oracle/keeper/oracle_script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/bandprotocol/bandchain/chain/x/oracle/testapp"
"github.com/bandprotocol/bandchain/chain/x/oracle/types"
"github.com/bandprotocol/bandchain/go-owasm/api"
)

func TestHasOracleScript(t *testing.T) {
Expand Down Expand Up @@ -141,7 +140,7 @@ func TestGetAllOracleScripts(t *testing.T) {
func TestAddOracleScriptFile(t *testing.T) {
_, _, k := testapp.CreateTestInput(true)
// Code should be perferctly compilable.
compiledCode, err := api.Compile(testapp.WasmExtra1, types.MaxCompiledWasmCodeSize)
compiledCode, err := testapp.OwasmVM.Compile(testapp.WasmExtra1, types.MaxCompiledWasmCodeSize)
require.NoError(t, err)
// We start by adding the Owasm content to the storage.
filename, err := k.AddOracleScriptFile(testapp.WasmExtra1)
Expand Down
5 changes: 2 additions & 3 deletions chain/x/oracle/keeper/owasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/bandprotocol/bandchain/chain/pkg/bandrng"
"github.com/bandprotocol/bandchain/chain/x/oracle/types"
owasm "github.com/bandprotocol/bandchain/go-owasm/api"
)

// GetRandomValidators returns a pseudorandom subset of active validators. Each validator has
Expand Down Expand Up @@ -70,7 +69,7 @@ func (k Keeper) PrepareRequest(ctx sdk.Context, r types.RequestSpec) error {
return err
}
code := k.GetFile(script.Filename)
output, err := owasm.Prepare(code, types.WasmPrepareGas, types.MaxDataSize, env)
output, err := k.owasmVM.Prepare(code, types.WasmPrepareGas, types.MaxDataSize, env)
if err != nil {
return sdkerrors.Wrapf(types.ErrBadWasmExecution, err.Error())
}
Expand Down Expand Up @@ -120,7 +119,7 @@ func (k Keeper) ResolveRequest(ctx sdk.Context, reqID types.RequestID) {
env := types.NewExecuteEnv(req, k.GetReports(ctx, reqID))
script := k.MustGetOracleScript(ctx, req.OracleScriptID)
code := k.GetFile(script.Filename)
output, err := owasm.Execute(code, types.WasmExecuteGas, types.MaxDataSize, env)
output, err := k.owasmVM.Execute(code, types.WasmExecuteGas, types.MaxDataSize, env)
if err != nil {
k.ResolveFailure(ctx, reqID, err.Error())
} else if env.Retdata == nil {
Expand Down
9 changes: 8 additions & 1 deletion chain/x/oracle/testapp/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/bandprotocol/bandchain/chain/x/oracle"
me "github.com/bandprotocol/bandchain/chain/x/oracle/keeper"
"github.com/bandprotocol/bandchain/chain/x/oracle/types"
owasm "github.com/bandprotocol/go-owasm/api"
)

// Account is a data structure to store key of test account.
Expand All @@ -48,6 +49,7 @@ var (
Validator3 Account
DataSources []types.DataSource
OracleScripts []types.OracleScript
OwasmVM *owasm.Vm
)

// nolint
Expand All @@ -67,6 +69,11 @@ func init() {
Validator1 = createArbitraryAccount(r)
Validator2 = createArbitraryAccount(r)
Validator3 = createArbitraryAccount(r)
owasmVM, err := owasm.NewVm(10)
if err != nil {
panic(err)
}
OwasmVM = owasmVM
}

func createArbitraryAccount(r *rand.Rand) Account {
Expand Down Expand Up @@ -147,7 +154,7 @@ func NewSimApp(chainID string, logger log.Logger) *bandapp.BandApp {
}
viper.Set(cli.HomeFlag, dir)
db := dbm.NewMemDB()
app := bandapp.NewBandApp(logger, db, nil, true, 0, map[int64]bool{}, "", false)
app := bandapp.NewBandApp(logger, db, nil, true, 0, map[int64]bool{}, "", false, 0)
genesis := bandapp.NewDefaultGenesisState()
// Fund seed accounts and validators with 1000000uband and 100000000uband initially.
authGenesis := auth.NewGenesisState(auth.DefaultParams(), []authexported.GenesisAccount{
Expand Down
3 changes: 1 addition & 2 deletions chain/x/oracle/testapp/wasm_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"os/exec"

"github.com/bandprotocol/bandchain/chain/x/oracle/types"
"github.com/bandprotocol/bandchain/go-owasm/api"
)

func compile(code []byte) []byte {
compiled, err := api.Compile(code, types.MaxCompiledWasmCodeSize)
compiled, err := OwasmVM.Compile(code, types.MaxCompiledWasmCodeSize)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion chain/x/oracle/types/exec_env.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package types

import (
"github.com/bandprotocol/bandchain/go-owasm/api"
"github.com/bandprotocol/go-owasm/api"
)

// BaseEnv combines shared functions used in prepare and execution Owasm program,
Expand Down
2 changes: 1 addition & 1 deletion chain/x/oracle/types/exec_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/secp256k1"

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

var (
Expand Down