diff --git a/app/params/amino.go b/app/params/amino.go index 364e917b3..e44665db0 100644 --- a/app/params/amino.go +++ b/app/params/amino.go @@ -16,7 +16,7 @@ func MakeTestEncodingConfig() EncodingConfig { return EncodingConfig{ InterfaceRegistry: interfaceRegistry, - Marshaler: codec, + Codec: codec, TxConfig: legacytx.StdTxConfig{Cdc: cdc}, Amino: cdc, } diff --git a/app/params/encoding.go b/app/params/encoding.go index 3d634abf1..8ff9ea04b 100644 --- a/app/params/encoding.go +++ b/app/params/encoding.go @@ -10,7 +10,7 @@ import ( // This is provided for compatibility between protobuf and amino implementations. type EncodingConfig struct { InterfaceRegistry types.InterfaceRegistry - Marshaler codec.Codec + Codec codec.Codec TxConfig client.TxConfig Amino *codec.LegacyAmino } diff --git a/app/params/proto.go b/app/params/proto.go index 2de22cebf..1af6215f6 100644 --- a/app/params/proto.go +++ b/app/params/proto.go @@ -33,7 +33,7 @@ func MakeEncodingConfig() EncodingConfig { txCfg := tx.NewTxConfig(cdc, tx.DefaultSignModes) return EncodingConfig{ InterfaceRegistry: interfaceRegistry, - Marshaler: cdc, + Codec: cdc, TxConfig: txCfg, Amino: amino, } diff --git a/cmd/bandd/cmd/root.go b/cmd/bandd/cmd/root.go index b43b99b2a..3a5073e3e 100644 --- a/cmd/bandd/cmd/root.go +++ b/cmd/bandd/cmd/root.go @@ -57,8 +57,8 @@ import ( // main function. func NewRootCmd() *cobra.Command { // we "pre"-instantiate the application for getting the injected/configured encoding configuration - initAppOptions := viper.New() tempDir := tempDir() + initAppOptions := viper.New() initAppOptions.Set(flags.FlagHome, tempDir) tempApplication := band.NewBandApp( log.NewNopLogger(), @@ -74,6 +74,9 @@ func NewRootCmd() *cobra.Command { if err := tempApplication.Close(); err != nil { panic(err) } + if tempDir != band.DefaultNodeHome { + os.RemoveAll(tempDir) + } }() initClientCtx := client.Context{}. @@ -429,7 +432,6 @@ var tempDir = func() string { if err != nil { dir = band.DefaultNodeHome } - defer os.RemoveAll(dir) return dir } diff --git a/cmd/cylinder/root.go b/cmd/cylinder/root.go index a2d4bb0bd..9b7230b24 100644 --- a/cmd/cylinder/root.go +++ b/cmd/cylinder/root.go @@ -99,8 +99,8 @@ func createPersistentPreRunE(rootCmd *cobra.Command, ctx *context.Context) func( } // init temporary application - initAppOptions := viper.New() tempDir := tempDir() + initAppOptions := viper.New() initAppOptions.Set(flags.FlagHome, tempDir) tempApplication := band.NewBandApp( log.NewNopLogger(), @@ -112,6 +112,14 @@ func createPersistentPreRunE(rootCmd *cobra.Command, ctx *context.Context) func( initAppOptions, 100, ) + defer func() { + if err := tempApplication.Close(); err != nil { + panic(err) + } + if tempDir != band.DefaultNodeHome { + os.RemoveAll(tempDir) + } + }() // set keyring keyring, err := keyring.New("band", keyring.BackendTest, home, nil, tempApplication.AppCodec()) @@ -150,7 +158,6 @@ var tempDir = func() string { if err != nil { dir = band.DefaultNodeHome } - defer os.RemoveAll(dir) return dir } diff --git a/cmd/grogu/cmd/run.go b/cmd/grogu/cmd/run.go index 21adecab3..c7e6ca15d 100644 --- a/cmd/grogu/cmd/run.go +++ b/cmd/grogu/cmd/run.go @@ -98,9 +98,9 @@ func createRunE(ctx *context.Context) func(cmd *cobra.Command, args []string) er } clientCtx = clientCtx.WithKeyring(ctx.Keyring). WithChainID(viper.GetString(flags.FlagChainID)). - WithCodec(ctx.BandApp.AppCodec()). - WithInterfaceRegistry(ctx.BandApp.InterfaceRegistry()). - WithTxConfig(ctx.BandApp.GetTxConfig()). + WithCodec(ctx.EncodingConfig.Codec). + WithInterfaceRegistry(ctx.EncodingConfig.InterfaceRegistry). + WithTxConfig(ctx.EncodingConfig.TxConfig). WithBroadcastMode(flags.BroadcastSync) nodeURIs := strings.Split(viper.GetString(flagNodes), ",") diff --git a/cmd/grogu/main.go b/cmd/grogu/main.go index 64d37980e..61537dae1 100644 --- a/cmd/grogu/main.go +++ b/cmd/grogu/main.go @@ -19,6 +19,7 @@ import ( "github.com/cosmos/cosmos-sdk/version" band "github.com/bandprotocol/chain/v3/app" + "github.com/bandprotocol/chain/v3/app/params" "github.com/bandprotocol/chain/v3/cmd/grogu/cmd" "github.com/bandprotocol/chain/v3/grogu/context" ) @@ -69,8 +70,8 @@ func createPersistentPreRunE(rootCmd *cobra.Command, ctx *context.Context) func( return err } - initAppOptions := viper.New() tempDir := tempDir() + initAppOptions := viper.New() initAppOptions.Set(flags.FlagHome, tempDir) tempApplication := band.NewBandApp( log.NewNopLogger(), @@ -82,7 +83,21 @@ func createPersistentPreRunE(rootCmd *cobra.Command, ctx *context.Context) func( initAppOptions, 100, ) - ctx.BandApp = tempApplication + defer func() { + if err := tempApplication.Close(); err != nil { + panic(err) + } + if tempDir != band.DefaultNodeHome { + os.RemoveAll(tempDir) + } + }() + + ctx.EncodingConfig = params.EncodingConfig{ + InterfaceRegistry: tempApplication.InterfaceRegistry(), + Codec: tempApplication.AppCodec(), + TxConfig: tempApplication.GetTxConfig(), + Amino: tempApplication.LegacyAmino(), + } ctx.Keyring, err = keyring.New("band", keyring.BackendTest, home, nil, tempApplication.AppCodec()) if err != nil { @@ -118,7 +133,6 @@ var tempDir = func() string { if err != nil { dir = band.DefaultNodeHome } - defer os.RemoveAll(dir) return dir } diff --git a/grogu/context/context.go b/grogu/context/context.go index bdcf5887d..483e48754 100644 --- a/grogu/context/context.go +++ b/grogu/context/context.go @@ -3,7 +3,7 @@ package context import ( "github.com/cosmos/cosmos-sdk/crypto/keyring" - band "github.com/bandprotocol/chain/v3/app" + "github.com/bandprotocol/chain/v3/app/params" "github.com/bandprotocol/chain/v3/pkg/logger" ) @@ -51,9 +51,9 @@ type Config struct { // Context holds the runtime context for the application. type Context struct { - Config Config - Keyring keyring.Keyring - Logger *logger.Logger - Home string - BandApp *band.BandApp + Config Config + Keyring keyring.Keyring + Logger *logger.Logger + Home string + EncodingConfig params.EncodingConfig } diff --git a/grogu/submitter/submitter_test.go b/grogu/submitter/submitter_test.go index 691ab2056..fecd4a736 100644 --- a/grogu/submitter/submitter_test.go +++ b/grogu/submitter/submitter_test.go @@ -2,7 +2,6 @@ package submitter import ( "context" - "os" "sync" "testing" "time" @@ -26,6 +25,7 @@ import ( codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" + sdktestutil "github.com/cosmos/cosmos-sdk/testutil" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" auth "github.com/cosmos/cosmos-sdk/x/auth/types" @@ -49,20 +49,10 @@ func TestSubmitterTestSuite(t *testing.T) { suite.Run(t, new(SubmitterTestSuite)) } -var tempDir = func() string { - dir, err := os.MkdirTemp("", ".band") - if err != nil { - dir = band.DefaultNodeHome - } - defer os.RemoveAll(dir) - - return dir -} - func (s *SubmitterTestSuite) SetupTest() { // Initialize encoding config + tempDir := sdktestutil.GetTempDir(s.T()) initAppOptions := viper.New() - tempDir := tempDir() initAppOptions.Set(flags.FlagHome, tempDir) tempApplication := band.NewBandApp( log.NewNopLogger(), diff --git a/yoda/context.go b/yoda/context.go index 9363cf68f..1a5d735c1 100644 --- a/yoda/context.go +++ b/yoda/context.go @@ -9,7 +9,7 @@ import ( "github.com/cosmos/cosmos-sdk/crypto/keyring" sdk "github.com/cosmos/cosmos-sdk/types" - band "github.com/bandprotocol/chain/v3/app" + "github.com/bandprotocol/chain/v3/app/params" "github.com/bandprotocol/chain/v3/pkg/filecache" "github.com/bandprotocol/chain/v3/x/oracle/types" "github.com/bandprotocol/chain/v3/yoda/executor" @@ -31,7 +31,7 @@ type ReportMsgWithKey struct { } type Context struct { - bandApp *band.BandApp + encodingConfig params.EncodingConfig client rpcclient.Client validator sdk.ValAddress gasPrices string diff --git a/yoda/execute.go b/yoda/execute.go index a37196368..ffcfcc6e3 100644 --- a/yoda/execute.go +++ b/yoda/execute.go @@ -26,10 +26,10 @@ func signAndBroadcast( ) (string, error) { clientCtx := client.Context{ Client: c.client, - Codec: c.bandApp.AppCodec(), - TxConfig: c.bandApp.GetTxConfig(), + Codec: c.encodingConfig.Codec, + TxConfig: c.encodingConfig.TxConfig, BroadcastMode: "sync", - InterfaceRegistry: c.bandApp.InterfaceRegistry(), + InterfaceRegistry: c.encodingConfig.InterfaceRegistry, } acc, err := queryAccount(clientCtx, key) if err != nil { @@ -133,8 +133,8 @@ func SubmitReport(c *Context, l *Logger, keyIndex int64, reports []ReportMsgWith clientCtx := client.Context{ Client: c.client, - TxConfig: c.bandApp.GetTxConfig(), - InterfaceRegistry: c.bandApp.InterfaceRegistry(), + TxConfig: c.encodingConfig.TxConfig, + InterfaceRegistry: c.encodingConfig.InterfaceRegistry, } gasLimit := estimateGas(c, l, msgs, feeEstimations) @@ -203,7 +203,7 @@ func GetExecutable(c *Context, l *Logger, hash string) ([]byte, error) { resValue, err := c.fileCache.GetFile(hash) if err != nil { l.Debug(":magnifying_glass_tilted_left: Fetching data source hash: %s from bandchain querier", hash) - bz := c.bandApp.AppCodec().MustMarshal(&types.QueryDataRequest{ + bz := c.encodingConfig.Codec.MustMarshal(&types.QueryDataRequest{ DataHash: hash, }) res, err := abciQuery(c, l, "/band.oracle.v1.Query/Data", bz) @@ -212,7 +212,7 @@ func GetExecutable(c *Context, l *Logger, hash string) ([]byte, error) { return nil, err } var dr types.QueryDataResponse - err = c.bandApp.AppCodec().Unmarshal(res.Response.GetValue(), &dr) + err = c.encodingConfig.Codec.Unmarshal(res.Response.GetValue(), &dr) if err != nil { l.Error(":exploding_head: Failed to unmarshal data source with error: %s", c, err.Error()) return nil, err @@ -236,7 +236,7 @@ func GetDataSourceHash(c *Context, l *Logger, id types.DataSourceID) (string, er } var d types.DataSource - c.bandApp.AppCodec().MustUnmarshal(res.Response.Value, &d) + c.encodingConfig.Codec.MustUnmarshal(res.Response.Value, &d) return d.Filename, nil } @@ -250,7 +250,7 @@ func GetRequest(c *Context, l *Logger, id types.RequestID) (types.Request, error } var r types.Request - c.bandApp.AppCodec().MustUnmarshal(res.Response.Value, &r) + c.encodingConfig.Codec.MustUnmarshal(res.Response.Value, &r) return r, nil } diff --git a/yoda/gas.go b/yoda/gas.go index 004cfbbe1..a91f8b1c6 100644 --- a/yoda/gas.go +++ b/yoda/gas.go @@ -110,7 +110,7 @@ func estimateReportHandlerGas(cdc codec.Codec, msg *types.MsgReportData, f FeeEs func estimateAuthAnteHandlerGas(c *Context, msgs []sdk.Msg) uint64 { gas := baseAuthAnteGas - txByteLength := getTxByteLength(c.bandApp.AppCodec(), msgs) + txByteLength := getTxByteLength(c.encodingConfig.Codec, msgs) gas += txCostPerByte * txByteLength if len(c.gasPrices) > 0 { @@ -128,7 +128,7 @@ func estimateGas(c *Context, l *Logger, msgs []sdk.Msg, feeEstimations []FeeEsti if !ok { panic("Don't support non-report data message") } - gas += estimateReportHandlerGas(c.bandApp.AppCodec(), msg, feeEstimations[i]) + gas += estimateReportHandlerGas(c.encodingConfig.Codec, msg, feeEstimations[i]) } l.Debug(":fuel_pump: Estimated gas is %d", gas) diff --git a/yoda/main.go b/yoda/main.go index 02a47de0f..65a923997 100644 --- a/yoda/main.go +++ b/yoda/main.go @@ -19,6 +19,7 @@ import ( "github.com/cosmos/cosmos-sdk/version" band "github.com/bandprotocol/chain/v3/app" + "github.com/bandprotocol/chain/v3/app/params" ) const ( @@ -97,8 +98,8 @@ func Main() { return err } - initAppOptions := viper.New() tempDir := tempDir() + initAppOptions := viper.New() initAppOptions.Set(flags.FlagHome, tempDir) tempApplication := band.NewBandApp( log.NewNopLogger(), @@ -110,7 +111,21 @@ func Main() { initAppOptions, 100, ) - ctx.bandApp = tempApplication + defer func() { + if err := tempApplication.Close(); err != nil { + panic(err) + } + if tempDir != band.DefaultNodeHome { + os.RemoveAll(tempDir) + } + }() + + ctx.encodingConfig = params.EncodingConfig{ + InterfaceRegistry: tempApplication.InterfaceRegistry(), + Codec: tempApplication.AppCodec(), + TxConfig: tempApplication.GetTxConfig(), + Amino: tempApplication.LegacyAmino(), + } kb, err = keyring.New("band", keyring.BackendTest, home, nil, tempApplication.AppCodec()) if err != nil { @@ -130,7 +145,6 @@ var tempDir = func() string { if err != nil { dir = band.DefaultNodeHome } - defer os.RemoveAll(dir) return dir } diff --git a/yoda/run.go b/yoda/run.go index f085a0712..2559fae9b 100644 --- a/yoda/run.go +++ b/yoda/run.go @@ -57,7 +57,7 @@ func runImpl(c *Context, l *Logger) error { waitingMsgs[i] = []ReportMsgWithKey{} } - bz := c.bandApp.AppCodec().MustMarshal(&types.QueryPendingRequestsRequest{ + bz := c.encodingConfig.Codec.MustMarshal(&types.QueryPendingRequestsRequest{ ValidatorAddress: c.validator.String(), }) resBz, err := c.client.ABCIQuery(context.Background(), "/band.oracle.v1.Query/PendingRequests", bz) @@ -65,7 +65,7 @@ func runImpl(c *Context, l *Logger) error { l.Error(":exploding_head: Failed to get pending requests with error: %s", c, err.Error()) } pendingRequests := types.QueryPendingRequestsResponse{} - c.bandApp.AppCodec().MustUnmarshal(resBz.Response.Value, &pendingRequests) + c.encodingConfig.Codec.MustUnmarshal(resBz.Response.Value, &pendingRequests) l.Info(":mag: Found %d pending requests", len(pendingRequests.RequestIDs)) for _, id := range pendingRequests.RequestIDs {