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

View File

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