Skip to content

Commit

Permalink
Merge pull request #557 from bandprotocol/fix-tempdir-delete
Browse files Browse the repository at this point in the history
[Main] Fix tempdir deletion issue
  • Loading branch information
RogerKSI authored Jan 23, 2025
2 parents 17a74d4 + 26313d5 commit 1457631
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 49 deletions.
2 changes: 1 addition & 1 deletion app/params/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func MakeTestEncodingConfig() EncodingConfig {

return EncodingConfig{
InterfaceRegistry: interfaceRegistry,
Marshaler: codec,
Codec: codec,
TxConfig: legacytx.StdTxConfig{Cdc: cdc},
Amino: cdc,
}
Expand Down
2 changes: 1 addition & 1 deletion app/params/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion app/params/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
6 changes: 4 additions & 2 deletions cmd/bandd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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{}.
Expand Down Expand Up @@ -429,7 +432,6 @@ var tempDir = func() string {
if err != nil {
dir = band.DefaultNodeHome
}
defer os.RemoveAll(dir)

return dir
}
11 changes: 9 additions & 2 deletions cmd/cylinder/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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())
Expand Down Expand Up @@ -150,7 +158,6 @@ var tempDir = func() string {
if err != nil {
dir = band.DefaultNodeHome
}
defer os.RemoveAll(dir)

return dir
}
6 changes: 3 additions & 3 deletions cmd/grogu/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), ",")
Expand Down
20 changes: 17 additions & 3 deletions cmd/grogu/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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(),
Expand All @@ -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 {
Expand Down Expand Up @@ -118,7 +133,6 @@ var tempDir = func() string {
if err != nil {
dir = band.DefaultNodeHome
}
defer os.RemoveAll(dir)

return dir
}
12 changes: 6 additions & 6 deletions grogu/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
14 changes: 2 additions & 12 deletions grogu/submitter/submitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package submitter

import (
"context"
"os"
"sync"
"testing"
"time"
Expand All @@ -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"
Expand All @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions yoda/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -31,7 +31,7 @@ type ReportMsgWithKey struct {
}

type Context struct {
bandApp *band.BandApp
encodingConfig params.EncodingConfig
client rpcclient.Client
validator sdk.ValAddress
gasPrices string
Expand Down
18 changes: 9 additions & 9 deletions yoda/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions yoda/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
20 changes: 17 additions & 3 deletions yoda/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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(),
Expand All @@ -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 {
Expand All @@ -130,7 +145,6 @@ var tempDir = func() string {
if err != nil {
dir = band.DefaultNodeHome
}
defer os.RemoveAll(dir)

return dir
}
4 changes: 2 additions & 2 deletions yoda/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ 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)
if err != nil {
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 {
Expand Down

0 comments on commit 1457631

Please sign in to comment.