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

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
Jürgen Eckel 2024-03-11 09:21:41 +01:00
parent c7ca04e1c3
commit 0715d9a87a
No known key found for this signature in database
3 changed files with 29 additions and 29 deletions

View File

@ -10,13 +10,13 @@ import (
// Config defines library top level configuration. // Config defines library top level configuration.
type Config struct { type Config struct {
ChainID string `json:"chain-id" mapstructure:"chain-id"` chainID string `json:"chain-id" mapstructure:"chain-id"`
ClientCtx client.Context `json:"client-ctx" mapstructure:"client-ctx"` clientCtx client.Context `json:"client-ctx" mapstructure:"client-ctx"`
EncodingConfig params.EncodingConfig `json:"encoding-config" mapstructure:"encoding-config"` encodingConfig params.EncodingConfig `json:"encoding-config" mapstructure:"encoding-config"`
FeeDenom string `json:"fee-denom" mapstructure:"fee-denom"` feeDenom string `json:"fee-denom" mapstructure:"fee-denom"`
RootDir string `json:"root-dir" mapstructure:"root-dir"` rootDir string `json:"root-dir" mapstructure:"root-dir"`
RPCEndpoint string `json:"rpc-endpoint" mapstructure:"rpc-endpoint"` rpcEndpoint string `json:"rpc-endpoint" mapstructure:"rpc-endpoint"`
TxGas uint64 `json:"tx-gas" mapstructure:"tx-gas"` txGas uint64 `json:"tx-gas" mapstructure:"tx-gas"`
} }
// lib wide global singleton // lib wide global singleton
@ -30,13 +30,13 @@ var (
// DefaultConfig returns library default configuration. // DefaultConfig returns library default configuration.
func DefaultConfig() *Config { func DefaultConfig() *Config {
return &Config{ return &Config{
ChainID: "planetmint-testnet-1", chainID: "planetmint-testnet-1",
ClientCtx: client.Context{}, clientCtx: client.Context{},
EncodingConfig: params.EncodingConfig{}, encodingConfig: params.EncodingConfig{},
FeeDenom: "plmnt", feeDenom: "plmnt",
RootDir: "~/.planetmint-go/", rootDir: "~/.planetmint-go/",
RPCEndpoint: "http://127.0.0.1:26657", rpcEndpoint: "http://127.0.0.1:26657",
TxGas: 200000, txGas: 200000,
} }
} }
@ -65,7 +65,7 @@ func (config *Config) SetBech32PrefixForAccount(bech32Prefix string) *Config {
func (config *Config) SetEncodingConfig(encodingConfig params.EncodingConfig) *Config { func (config *Config) SetEncodingConfig(encodingConfig params.EncodingConfig) *Config {
changeLock.Lock() changeLock.Lock()
defer changeLock.Unlock() defer changeLock.Unlock()
config.EncodingConfig = encodingConfig config.encodingConfig = encodingConfig
return config return config
} }
@ -73,7 +73,7 @@ func (config *Config) SetEncodingConfig(encodingConfig params.EncodingConfig) *C
func (config *Config) SetChainID(chainID string) *Config { func (config *Config) SetChainID(chainID string) *Config {
changeLock.Lock() changeLock.Lock()
defer changeLock.Unlock() defer changeLock.Unlock()
config.ChainID = chainID config.chainID = chainID
return config return config
} }
@ -81,7 +81,7 @@ func (config *Config) SetChainID(chainID string) *Config {
func (config *Config) SetClientCtx(clientCtx client.Context) *Config { func (config *Config) SetClientCtx(clientCtx client.Context) *Config {
changeLock.Lock() changeLock.Lock()
defer changeLock.Unlock() defer changeLock.Unlock()
config.ClientCtx = clientCtx config.clientCtx = clientCtx
return config return config
} }
@ -89,7 +89,7 @@ func (config *Config) SetClientCtx(clientCtx client.Context) *Config {
func (config *Config) SetFeeDenom(feeDenom string) *Config { func (config *Config) SetFeeDenom(feeDenom string) *Config {
changeLock.Lock() changeLock.Lock()
defer changeLock.Unlock() defer changeLock.Unlock()
config.FeeDenom = feeDenom config.feeDenom = feeDenom
return config return config
} }
@ -97,7 +97,7 @@ func (config *Config) SetFeeDenom(feeDenom string) *Config {
func (config *Config) SetRoot(root string) *Config { func (config *Config) SetRoot(root string) *Config {
changeLock.Lock() changeLock.Lock()
defer changeLock.Unlock() defer changeLock.Unlock()
config.RootDir = root config.rootDir = root
return config return config
} }
@ -105,7 +105,7 @@ func (config *Config) SetRoot(root string) *Config {
func (config *Config) SetRPCEndpoint(rpcEndpoint string) *Config { func (config *Config) SetRPCEndpoint(rpcEndpoint string) *Config {
changeLock.Lock() changeLock.Lock()
defer changeLock.Unlock() defer changeLock.Unlock()
config.RPCEndpoint = rpcEndpoint config.rpcEndpoint = rpcEndpoint
return config return config
} }
@ -113,6 +113,6 @@ func (config *Config) SetRPCEndpoint(rpcEndpoint string) *Config {
func (config *Config) SetTxGas(txGas uint64) *Config { func (config *Config) SetTxGas(txGas uint64) *Config {
changeLock.Lock() changeLock.Lock()
defer changeLock.Unlock() defer changeLock.Unlock()
config.TxGas = txGas config.txGas = txGas
return config return config
} }

View File

@ -38,7 +38,7 @@ func getAccountNumberAndSequence(clientCtx client.Context) (accountNumber, seque
} }
func getClientContextAndTxFactory(fromAddress sdk.AccAddress) (clientCtx client.Context, txf tx.Factory, err error) { 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 // 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 // it would be better to check for an empty client context, but that does not work at the moment
if clientCtx.AccountRetriever == nil { if clientCtx.AccountRetriever == nil {
@ -70,17 +70,17 @@ func getTxFactoryWithAccountNumberAndSequence(clientCtx client.Context, accountN
WithAccountRetriever(clientCtx.AccountRetriever). WithAccountRetriever(clientCtx.AccountRetriever).
WithChainID(clientCtx.ChainID). WithChainID(clientCtx.ChainID).
WithFeeGranter(clientCtx.FeeGranter). WithFeeGranter(clientCtx.FeeGranter).
WithGas(GetConfig().TxGas). WithGas(GetConfig().txGas).
WithGasPrices("0.000005" + GetConfig().FeeDenom). WithGasPrices("0.000005" + GetConfig().feeDenom).
WithKeybase(clientCtx.Keyring). WithKeybase(clientCtx.Keyring).
WithSequence(sequence). WithSequence(sequence).
WithTxConfig(clientCtx.TxConfig) WithTxConfig(clientCtx.TxConfig)
} }
func getClientContext(fromAddress sdk.AccAddress) (clientCtx client.Context, err error) { 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 input := os.Stdin
codec := encodingConfig.Marshaler codec := encodingConfig.Marshaler
keyringOptions := []keyring.Option{} keyringOptions := []keyring.Option{}
@ -95,7 +95,7 @@ func getClientContext(fromAddress sdk.AccAddress) (clientCtx client.Context, err
return return
} }
remote := GetConfig().RPCEndpoint remote := GetConfig().rpcEndpoint
wsClient, err := comethttp.New(remote, "/websocket") wsClient, err := comethttp.New(remote, "/websocket")
if err != nil { if err != nil {
return return
@ -106,7 +106,7 @@ func getClientContext(fromAddress sdk.AccAddress) (clientCtx client.Context, err
clientCtx = client.Context{ clientCtx = client.Context{
AccountRetriever: authtypes.AccountRetriever{}, AccountRetriever: authtypes.AccountRetriever{},
BroadcastMode: "sync", BroadcastMode: "sync",
ChainID: GetConfig().ChainID, ChainID: GetConfig().chainID,
Client: wsClient, Client: wsClient,
Codec: codec, Codec: codec,
From: fromAddress.String(), From: fromAddress.String(),

View File

@ -60,7 +60,7 @@ func createSequenceDirectory() (path string, err error) {
return return
} }
homeDir := usr.HomeDir homeDir := usr.HomeDir
path = filepath.Join(GetConfig().RootDir, "sequence") path = filepath.Join(GetConfig().rootDir, "sequence")
// expand tilde to user's home directory // expand tilde to user's home directory
if strings.HasPrefix(path, "~/") { if strings.HasPrefix(path, "~/") {
path = filepath.Join(homeDir, path[2:]) path = filepath.Join(homeDir, path[2:])