From 0715d9a87a0dc76fe0835f015fac965194f3013b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Eckel?= Date: Mon, 11 Mar 2024 09:21:41 +0100 Subject: [PATCH] made lib.config.Config varialbes private so that they cannot be tampered with (multithreading). Please introduce Get-methods to retrive the varialbes outside of the package if you need it. This way, the race conditions and unexpected change of the global object state can be protected MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jürgen Eckel --- lib/config.go | 42 +++++++++++++++++++++--------------------- lib/tx.go | 14 +++++++------- lib/utils.go | 2 +- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/lib/config.go b/lib/config.go index 4509457..aacbbef 100644 --- a/lib/config.go +++ b/lib/config.go @@ -10,13 +10,13 @@ import ( // Config defines library top level configuration. type Config struct { - ChainID string `json:"chain-id" mapstructure:"chain-id"` - ClientCtx client.Context `json:"client-ctx" mapstructure:"client-ctx"` - EncodingConfig params.EncodingConfig `json:"encoding-config" mapstructure:"encoding-config"` - FeeDenom string `json:"fee-denom" mapstructure:"fee-denom"` - RootDir string `json:"root-dir" mapstructure:"root-dir"` - RPCEndpoint string `json:"rpc-endpoint" mapstructure:"rpc-endpoint"` - TxGas uint64 `json:"tx-gas" mapstructure:"tx-gas"` + chainID string `json:"chain-id" mapstructure:"chain-id"` + clientCtx client.Context `json:"client-ctx" mapstructure:"client-ctx"` + encodingConfig params.EncodingConfig `json:"encoding-config" mapstructure:"encoding-config"` + feeDenom string `json:"fee-denom" mapstructure:"fee-denom"` + rootDir string `json:"root-dir" mapstructure:"root-dir"` + rpcEndpoint string `json:"rpc-endpoint" mapstructure:"rpc-endpoint"` + txGas uint64 `json:"tx-gas" mapstructure:"tx-gas"` } // lib wide global singleton @@ -30,13 +30,13 @@ var ( // DefaultConfig returns library default configuration. func DefaultConfig() *Config { return &Config{ - ChainID: "planetmint-testnet-1", - ClientCtx: client.Context{}, - EncodingConfig: params.EncodingConfig{}, - FeeDenom: "plmnt", - RootDir: "~/.planetmint-go/", - RPCEndpoint: "http://127.0.0.1:26657", - TxGas: 200000, + chainID: "planetmint-testnet-1", + clientCtx: client.Context{}, + encodingConfig: params.EncodingConfig{}, + feeDenom: "plmnt", + rootDir: "~/.planetmint-go/", + rpcEndpoint: "http://127.0.0.1:26657", + txGas: 200000, } } @@ -65,7 +65,7 @@ func (config *Config) SetBech32PrefixForAccount(bech32Prefix string) *Config { func (config *Config) SetEncodingConfig(encodingConfig params.EncodingConfig) *Config { changeLock.Lock() defer changeLock.Unlock() - config.EncodingConfig = encodingConfig + config.encodingConfig = encodingConfig return config } @@ -73,7 +73,7 @@ func (config *Config) SetEncodingConfig(encodingConfig params.EncodingConfig) *C func (config *Config) SetChainID(chainID string) *Config { changeLock.Lock() defer changeLock.Unlock() - config.ChainID = chainID + config.chainID = chainID return config } @@ -81,7 +81,7 @@ func (config *Config) SetChainID(chainID string) *Config { func (config *Config) SetClientCtx(clientCtx client.Context) *Config { changeLock.Lock() defer changeLock.Unlock() - config.ClientCtx = clientCtx + config.clientCtx = clientCtx return config } @@ -89,7 +89,7 @@ func (config *Config) SetClientCtx(clientCtx client.Context) *Config { func (config *Config) SetFeeDenom(feeDenom string) *Config { changeLock.Lock() defer changeLock.Unlock() - config.FeeDenom = feeDenom + config.feeDenom = feeDenom return config } @@ -97,7 +97,7 @@ func (config *Config) SetFeeDenom(feeDenom string) *Config { func (config *Config) SetRoot(root string) *Config { changeLock.Lock() defer changeLock.Unlock() - config.RootDir = root + config.rootDir = root return config } @@ -105,7 +105,7 @@ func (config *Config) SetRoot(root string) *Config { func (config *Config) SetRPCEndpoint(rpcEndpoint string) *Config { changeLock.Lock() defer changeLock.Unlock() - config.RPCEndpoint = rpcEndpoint + config.rpcEndpoint = rpcEndpoint return config } @@ -113,6 +113,6 @@ func (config *Config) SetRPCEndpoint(rpcEndpoint string) *Config { func (config *Config) SetTxGas(txGas uint64) *Config { changeLock.Lock() defer changeLock.Unlock() - config.TxGas = txGas + config.txGas = txGas return config } diff --git a/lib/tx.go b/lib/tx.go index 6c8e486..7bbe213 100644 --- a/lib/tx.go +++ b/lib/tx.go @@ -38,7 +38,7 @@ func getAccountNumberAndSequence(clientCtx client.Context) (accountNumber, seque } func getClientContextAndTxFactory(fromAddress sdk.AccAddress) (clientCtx client.Context, txf tx.Factory, err error) { - clientCtx = GetConfig().ClientCtx + clientCtx = GetConfig().clientCtx // at least we need an account retriever // it would be better to check for an empty client context, but that does not work at the moment if clientCtx.AccountRetriever == nil { @@ -70,17 +70,17 @@ func getTxFactoryWithAccountNumberAndSequence(clientCtx client.Context, accountN WithAccountRetriever(clientCtx.AccountRetriever). WithChainID(clientCtx.ChainID). WithFeeGranter(clientCtx.FeeGranter). - WithGas(GetConfig().TxGas). - WithGasPrices("0.000005" + GetConfig().FeeDenom). + WithGas(GetConfig().txGas). + WithGasPrices("0.000005" + GetConfig().feeDenom). WithKeybase(clientCtx.Keyring). WithSequence(sequence). WithTxConfig(clientCtx.TxConfig) } func getClientContext(fromAddress sdk.AccAddress) (clientCtx client.Context, err error) { - encodingConfig := GetConfig().EncodingConfig + encodingConfig := GetConfig().encodingConfig - rootDir := GetConfig().RootDir + rootDir := GetConfig().rootDir input := os.Stdin codec := encodingConfig.Marshaler keyringOptions := []keyring.Option{} @@ -95,7 +95,7 @@ func getClientContext(fromAddress sdk.AccAddress) (clientCtx client.Context, err return } - remote := GetConfig().RPCEndpoint + remote := GetConfig().rpcEndpoint wsClient, err := comethttp.New(remote, "/websocket") if err != nil { return @@ -106,7 +106,7 @@ func getClientContext(fromAddress sdk.AccAddress) (clientCtx client.Context, err clientCtx = client.Context{ AccountRetriever: authtypes.AccountRetriever{}, BroadcastMode: "sync", - ChainID: GetConfig().ChainID, + ChainID: GetConfig().chainID, Client: wsClient, Codec: codec, From: fromAddress.String(), diff --git a/lib/utils.go b/lib/utils.go index 4d4c7fc..2174cd1 100644 --- a/lib/utils.go +++ b/lib/utils.go @@ -60,7 +60,7 @@ func createSequenceDirectory() (path string, err error) { return } homeDir := usr.HomeDir - path = filepath.Join(GetConfig().RootDir, "sequence") + path = filepath.Join(GetConfig().rootDir, "sequence") // expand tilde to user's home directory if strings.HasPrefix(path, "~/") { path = filepath.Join(homeDir, path[2:])