added more sync objects to avoid accidential data races

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
Jürgen Eckel 2024-03-08 16:20:23 +01:00
parent d5d86997f3
commit bc64dd2a25
No known key found for this signature in database
3 changed files with 25 additions and 1 deletions

View File

@ -24,6 +24,7 @@ var (
libConfig *Config libConfig *Config
sdkConfig *sdk.Config sdkConfig *sdk.Config
initConfig sync.Once initConfig sync.Once
changeLock sync.Mutex
) )
// DefaultConfig returns library default configuration. // DefaultConfig returns library default configuration.
@ -42,6 +43,8 @@ func DefaultConfig() *Config {
// GetConfig returns the config instance for the SDK. // GetConfig returns the config instance for the SDK.
func GetConfig() *Config { func GetConfig() *Config {
initConfig.Do(func() { initConfig.Do(func() {
changeLock.Lock()
defer changeLock.Unlock()
libConfig = DefaultConfig() libConfig = DefaultConfig()
sdkConfig = sdk.GetConfig() sdkConfig = sdk.GetConfig()
libConfig.SetBech32PrefixForAccount("plmnt") libConfig.SetBech32PrefixForAccount("plmnt")
@ -54,48 +57,64 @@ func GetConfig() *Config {
// SetBech32PrefixForAccount sets the bech32 account prefix. // SetBech32PrefixForAccount sets the bech32 account prefix.
func (config *Config) SetBech32PrefixForAccount(bech32Prefix string) *Config { func (config *Config) SetBech32PrefixForAccount(bech32Prefix string) *Config {
changeLock.Lock()
defer changeLock.Unlock()
sdkConfig.SetBech32PrefixForAccount(bech32Prefix, "pub") sdkConfig.SetBech32PrefixForAccount(bech32Prefix, "pub")
return config return config
} }
// SetEncodingConfig sets the encoding config and must not be nil. // SetEncodingConfig sets the encoding config and must not be nil.
func (config *Config) SetEncodingConfig(encodingConfig params.EncodingConfig) *Config { func (config *Config) SetEncodingConfig(encodingConfig params.EncodingConfig) *Config {
changeLock.Lock()
defer changeLock.Unlock()
config.EncodingConfig = encodingConfig config.EncodingConfig = encodingConfig
return config return config
} }
// SetChainID sets the chain ID parameter. // SetChainID sets the chain ID parameter.
func (config *Config) SetChainID(chainID string) *Config { func (config *Config) SetChainID(chainID string) *Config {
changeLock.Lock()
defer changeLock.Unlock()
config.ChainID = chainID config.ChainID = chainID
return config return config
} }
// SetClientCtx sets the client context parameter. // SetClientCtx sets the client context parameter.
func (config *Config) SetClientCtx(clientCtx client.Context) *Config { func (config *Config) SetClientCtx(clientCtx client.Context) *Config {
changeLock.Lock()
defer changeLock.Unlock()
config.ClientCtx = clientCtx config.ClientCtx = clientCtx
return config return config
} }
// SetFeeDenom sets the fee denominator parameter. // SetFeeDenom sets the fee denominator parameter.
func (config *Config) SetFeeDenom(feeDenom string) *Config { func (config *Config) SetFeeDenom(feeDenom string) *Config {
changeLock.Lock()
defer changeLock.Unlock()
config.FeeDenom = feeDenom config.FeeDenom = feeDenom
return config return config
} }
// SetRoot sets the root directory where to find the keyring. // SetRoot sets the root directory where to find the keyring.
func (config *Config) SetRoot(root string) *Config { func (config *Config) SetRoot(root string) *Config {
changeLock.Lock()
defer changeLock.Unlock()
config.RootDir = root config.RootDir = root
return config return config
} }
// SetRPCEndpoint sets the RPC endpoint to send requests to. // SetRPCEndpoint sets the RPC endpoint to send requests to.
func (config *Config) SetRPCEndpoint(rpcEndpoint string) *Config { func (config *Config) SetRPCEndpoint(rpcEndpoint string) *Config {
changeLock.Lock()
defer changeLock.Unlock()
config.RPCEndpoint = rpcEndpoint config.RPCEndpoint = rpcEndpoint
return config return config
} }
// SetTxGas sets the amount of Gas for the TX that is send to the network // SetTxGas sets the amount of Gas for the TX that is send to the network
func (config *Config) SetTxGas(txGas uint64) *Config { func (config *Config) SetTxGas(txGas uint64) *Config {
changeLock.Lock()
defer changeLock.Unlock()
config.TxGas = txGas config.TxGas = txGas
return config return config
} }

View File

@ -61,7 +61,7 @@ func (s *E2ETestSuite) TestBankSendBroadcastTxWithFileLock() {
assert.Equal(s.T(), "received wrong fee denom; got: plmnt required: stake: invalid coins", txResponse.RawLog) assert.Equal(s.T(), "received wrong fee denom; got: plmnt required: stake: invalid coins", txResponse.RawLog)
libConfig := lib.GetConfig() libConfig := lib.GetConfig()
libConfig.FeeDenom = "stake" libConfig.SetFeeDenom("stake")
// incorrect coin // incorrect coin
out, err = lib.BroadcastTxWithFileLock(val.Address, msg) out, err = lib.BroadcastTxWithFileLock(val.Address, msg)

View File

@ -16,6 +16,7 @@ var (
globalApplicationLoggerTag string globalApplicationLoggerTag string
appLogger *AppLogger appLogger *AppLogger
initAppLogger sync.Once initAppLogger sync.Once
syncTestingLog sync.Mutex
) )
func init() { func init() {
@ -33,7 +34,9 @@ func GetAppLogger() *AppLogger {
} }
func (logger *AppLogger) SetTestingLogger(testingLogger *testing.T) *AppLogger { func (logger *AppLogger) SetTestingLogger(testingLogger *testing.T) *AppLogger {
syncTestingLog.Lock()
logger.testingLogger = testingLogger logger.testingLogger = testingLogger
syncTestingLog.Unlock()
return logger return logger
} }
@ -49,7 +52,9 @@ func (logger *AppLogger) testingLog(msg string, keyvals ...interface{}) {
return return
} }
msg = format(msg, keyvals...) msg = format(msg, keyvals...)
syncTestingLog.Lock()
logger.testingLogger.Logf(msg) logger.testingLogger.Logf(msg)
syncTestingLog.Unlock()
} }
func (logger *AppLogger) Info(ctx sdk.Context, msg string, keyvals ...interface{}) { func (logger *AppLogger) Info(ctx sdk.Context, msg string, keyvals ...interface{}) {