diff --git a/cmd/planetmint-god/cmd/root.go b/cmd/planetmint-god/cmd/root.go index 6a12df5..f583571 100644 --- a/cmd/planetmint-god/cmd/root.go +++ b/cmd/planetmint-god/cmd/root.go @@ -36,10 +36,12 @@ import ( "github.com/spf13/cast" "github.com/spf13/cobra" "github.com/spf13/pflag" + // this line is used by starport scaffolding # root/moduleImport "planetmint-go/app" appparams "planetmint-go/app/params" + planetmintconfig "planetmint-go/config" ) // NewRootCmd creates a new root command for a Cosmos SDK application @@ -234,6 +236,10 @@ func (a appCreator) newApp( ) servertypes.Application { var cache sdk.MultiStorePersistentCache + // Get [planetmint] section from app.toml + plmntConfig := planetmintconfig.GetConfig() + plmntConfig.SetWatchmenConfig(appOpts.Get("planetmint")) + if cast.ToBool(appOpts.Get(server.FlagInterBlockCache)) { cache = store.NewCommitKVStoreCacheManager() } @@ -344,6 +350,7 @@ func initAppConfig() (string, interface{}) { type CustomAppConfig struct { serverconfig.Config + PlmntConfig planetmintconfig.Config } // Optionally allow the chain developer to overwrite the SDK's default @@ -363,10 +370,13 @@ func initAppConfig() (string, interface{}) { // In simapp, we set the min gas prices to 0. srvCfg.MinGasPrices = "0stake" + plmntCfg := planetmintconfig.DefaultConfig() + customAppConfig := CustomAppConfig{ - Config: *srvCfg, + Config: *srvCfg, + PlmntConfig: *plmntCfg, } - customAppTemplate := serverconfig.DefaultConfigTemplate + customAppTemplate := serverconfig.DefaultConfigTemplate + planetmintconfig.DefaultConfigTemplate return customAppTemplate, customAppConfig } diff --git a/config/config.dev.json b/config/config.dev.json deleted file mode 100644 index eb43211..0000000 --- a/config/config.dev.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "WATCHMEN_ENDPOINT": "localhost", - "WATCHMEN_PORT": 7401 -} diff --git a/config/config.go b/config/config.go index 1b77cf5..11c04f9 100644 --- a/config/config.go +++ b/config/config.go @@ -1,36 +1,65 @@ package app import ( - "path" - "path/filepath" - "runtime" - "strings" - - "github.com/tkanos/gonfig" + "encoding/json" + "fmt" + "sync" ) -type Configuration struct { - WATCHMEN_ENDPOINT string - WATCHMEN_PORT int +const DefaultConfigTemplate = ` +############################################################################### +### Planetmint ### +############################################################################### + +[planetmint] +watchmen-endpoint = "{{ .PlmntConfig.WatchmenConfig.Endpoint }}" +watchmen-port = {{ .PlmntConfig.WatchmenConfig.Port }} +` + +// Config defines Planetmint's top level configuration +type Config struct { + WatchmenConfig WatchmenConfig `mapstructure:"watchmen-config" json:"watchmen-config"` } -func GetConfig(params ...string) Configuration { - configuration := Configuration{} - env := "dev" +// WatchmenConfig defines Planetmint's watchmen configuration +type WatchmenConfig struct { + Endpoint string `mapstructure:"watchmen-endpoint" json:"watchmen-endpoint"` + Port int `mapstructure:"watchmen-port" json:"watchmen-port"` +} - if len(params) > 0 { - env = params[0] +// cosmos-sdk wide global singleton +var ( + plmntConfig *Config + initConfig sync.Once +) + +// DefaultConfig returns planetmint's default configuration. +func DefaultConfig() *Config { + return &Config{ + WatchmenConfig: WatchmenConfig{ + Endpoint: "localhost", + Port: 7401, + }, } +} - filename := []string{"config.", env, ".json"} - _, dirname, _, _ := runtime.Caller(0) - filePath := path.Join(filepath.Dir(dirname), strings.Join(filename, "")) - - err := gonfig.GetConf(filePath, &configuration) +// GetConfig returns the config instance for the SDK. +func GetConfig() *Config { + initConfig.Do(func() { + plmntConfig = DefaultConfig() + }) + return plmntConfig +} +// SetWatchmenConfig sets Planetmint's watchmen configuration +func (config *Config) SetWatchmenConfig(watchmenConfig interface{}) { + jsonWatchmenConfig, err := json.Marshal(watchmenConfig) if err != nil { panic(err) } - - return configuration + err = json.Unmarshal(jsonWatchmenConfig, &config.WatchmenConfig) + if err != nil { + panic(err) + } + fmt.Printf("%+v\n", config.WatchmenConfig.Port) } diff --git a/config/config.prod.json b/config/config.prod.json deleted file mode 100644 index eb43211..0000000 --- a/config/config.prod.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "WATCHMEN_ENDPOINT": "localhost", - "WATCHMEN_PORT": 7401 -} diff --git a/go.mod b/go.mod index a7dff93..bcaba53 100644 --- a/go.mod +++ b/go.mod @@ -23,7 +23,6 @@ require ( github.com/spf13/cobra v1.6.1 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.2 - github.com/tkanos/gonfig v0.0.0-20210106201359-53e13348de2f google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 google.golang.org/grpc v1.55.0 gopkg.in/yaml.v2 v2.4.0 diff --git a/go.sum b/go.sum index 7626275..8af7b56 100644 --- a/go.sum +++ b/go.sum @@ -960,8 +960,6 @@ github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2l github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME= github.com/tidwall/btree v1.6.0 h1:LDZfKfQIBHGHWSwckhXI0RPSXzlo+KYdjK7FWSqOzzg= github.com/tidwall/btree v1.6.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY= -github.com/tkanos/gonfig v0.0.0-20210106201359-53e13348de2f h1:xDFq4NVQD34ekH5UsedBSgfxsBuPU2aZf7v4t0tH2jY= -github.com/tkanos/gonfig v0.0.0-20210106201359-53e13348de2f/go.mod h1:DaZPBuToMc2eezA9R9nDAnmS2RMwL7yEa5YD36ESQdI= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= diff --git a/x/machine/keeper/msg_server_attest_machine.go b/x/machine/keeper/msg_server_attest_machine.go index 023206e..e809894 100644 --- a/x/machine/keeper/msg_server_attest_machine.go +++ b/x/machine/keeper/msg_server_attest_machine.go @@ -46,7 +46,7 @@ func validateIssuerLiquid(issuerLiquid string) bool { func (k msgServer) reissueMachine(machine *types.Machine) error { conf := config.GetConfig() - client := osc.NewClient(conf.WATCHMEN_ENDPOINT, int(conf.WATCHMEN_PORT)) + client := osc.NewClient(conf.WatchmenConfig.Endpoint, conf.WatchmenConfig.Port) msg := osc.NewMessage("/rddl/*") msg.Append(machine.Name) msg.Append(machine.Ticker)