diff --git a/CHANGELOG_UNRELEASED.md b/CHANGELOG_UNRELEASED.md index 368f6a9620..753ce80cb1 100644 --- a/CHANGELOG_UNRELEASED.md +++ b/CHANGELOG_UNRELEASED.md @@ -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 @@ -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 diff --git a/chain/app/app.go b/chain/app/app.go index 573f58ddfb..c9b245b408 100644 --- a/chain/app/app.go +++ b/chain/app/app.go @@ -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 ( @@ -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...) @@ -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) @@ -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. diff --git a/chain/cmd/bandd/genos.go b/chain/cmd/bandd/genos.go index ac30094b6b..a611129783 100644 --- a/chain/cmd/bandd/genos.go +++ b/chain/cmd/bandd/genos.go @@ -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. @@ -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 } diff --git a/chain/cmd/bandd/main.go b/chain/cmd/bandd/main.go index 4f2e74b164..91bb71c3dc 100644 --- a/chain/cmd/bandd/main.go +++ b/chain/cmd/bandd/main.go @@ -38,6 +38,7 @@ const ( flagEnableFastSync = "enable-fast-sync" flagWithPricer = "with-pricer" flagWithRequestSearch = "with-request-search" + flagWithOwasmCacheSize = "oracle-script-cache-size" ) var invCheckPeriod uint @@ -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) @@ -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)), @@ -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 @@ -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) } diff --git a/chain/go.mod b/chain/go.mod index 2a76d94729..c0be7f2fb2 100644 --- a/chain/go.mod +++ b/chain/go.mod @@ -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 @@ -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 diff --git a/chain/go.sum b/chain/go.sum index 969b1df830..00d7cf6573 100644 --- a/chain/go.sum +++ b/chain/go.sum @@ -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= @@ -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= diff --git a/chain/x/oracle/keeper/keeper.go b/chain/x/oracle/keeper/keeper.go index bf136e380c..dce3546379 100644 --- a/chain/x/oracle/keeper/keeper.go +++ b/chain/x/oracle/keeper/keeper.go @@ -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 ( @@ -25,6 +26,7 @@ type Keeper struct { supplyKeeper types.SupplyKeeper stakingKeeper types.StakingKeeper distrKeeper types.DistrKeeper + owasmVM *owasm.Vm } // NewKeeper creates a new oracle Keeper instance. @@ -32,6 +34,7 @@ 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()) @@ -45,6 +48,7 @@ func NewKeeper( supplyKeeper: supplyKeeper, stakingKeeper: stakingKeeper, distrKeeper: distrKeeper, + owasmVM: owasmVM, } } diff --git a/chain/x/oracle/keeper/oracle_script.go b/chain/x/oracle/keeper/oracle_script.go index c61654cb60..7702a135b3 100644 --- a/chain/x/oracle/keeper/oracle_script.go +++ b/chain/x/oracle/keeper/oracle_script.go @@ -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. @@ -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()) } diff --git a/chain/x/oracle/keeper/oracle_script_test.go b/chain/x/oracle/keeper/oracle_script_test.go index abb55acd61..1d8b104b37 100644 --- a/chain/x/oracle/keeper/oracle_script_test.go +++ b/chain/x/oracle/keeper/oracle_script_test.go @@ -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) { @@ -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) diff --git a/chain/x/oracle/keeper/owasm.go b/chain/x/oracle/keeper/owasm.go index 5e56b655d8..56505341d2 100644 --- a/chain/x/oracle/keeper/owasm.go +++ b/chain/x/oracle/keeper/owasm.go @@ -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 @@ -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()) } @@ -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 { diff --git a/chain/x/oracle/testapp/setup.go b/chain/x/oracle/testapp/setup.go index 80d9fbdd86..d455ccfc1e 100644 --- a/chain/x/oracle/testapp/setup.go +++ b/chain/x/oracle/testapp/setup.go @@ -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. @@ -48,6 +49,7 @@ var ( Validator3 Account DataSources []types.DataSource OracleScripts []types.OracleScript + OwasmVM *owasm.Vm ) // nolint @@ -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 { @@ -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{ diff --git a/chain/x/oracle/testapp/wasm_util.go b/chain/x/oracle/testapp/wasm_util.go index 17a6effd9d..e81d66d24c 100644 --- a/chain/x/oracle/testapp/wasm_util.go +++ b/chain/x/oracle/testapp/wasm_util.go @@ -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) } diff --git a/chain/x/oracle/types/exec_env.go b/chain/x/oracle/types/exec_env.go index de9a2a9e80..58db221209 100644 --- a/chain/x/oracle/types/exec_env.go +++ b/chain/x/oracle/types/exec_env.go @@ -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, diff --git a/chain/x/oracle/types/exec_env_test.go b/chain/x/oracle/types/exec_env_test.go index e150626d99..0a86d882f8 100644 --- a/chain/x/oracle/types/exec_env_test.go +++ b/chain/x/oracle/types/exec_env_test.go @@ -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 (