Skip to content

Commit 06efd1d

Browse files
feat: scoped config (cosmos#24668)
2 parents bb324ff + 4025458 commit 06efd1d

File tree

3 files changed

+101
-67
lines changed

3 files changed

+101
-67
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ Ref: https://keepachangelog.com/en/1.0.0/
3939
## [Unreleased]
4040

4141
### Features
42+
4243
* (server) [#24720](https://github.com/cosmos/cosmos-sdk/pull/24720) add `verbose_log_level` flag for configuring the log level when switching to verbose logging mode during sensitive operations (such as chain upgrades).
4344

4445
### Improvements
4546

47+
* (types) [#24668](https://github.com/cosmos/cosmos-sdk/pull/24668) Scope the global config to a particular binary so that multiple SDK binaries can be properly run on the same machine.
4648
* (baseapp) [#24655](https://github.com/cosmos/cosmos-sdk/pull/24655) Add mutex locks for `state` and make `lastCommitInfo` atomic to prevent race conditions between `Commit` and `CreateQueryContext`.
4749
* (proto) [#24161](https://github.com/cosmos/cosmos-sdk/pull/24161) Remove unnecessary annotations from `x/staking` authz proto.
4850

types/config.go

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,57 @@ package types
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"sync"
78

89
"github.com/cosmos/cosmos-sdk/version"
910
)
1011

11-
// DefaultKeyringServiceName defines a default service name for the keyring.
12-
const DefaultKeyringServiceName = "cosmos"
12+
const (
13+
DefaultKeyringServiceName = "cosmos"
14+
EnvConfigScope = "COSMOS_SDK_CONFIG_SCOPE"
15+
)
1316

14-
// Config is the structure that holds the SDK configuration parameters.
15-
// This could be used to initialize certain configuration parameters for the SDK.
1617
type Config struct {
1718
fullFundraiserPath string
1819
bech32AddressPrefix map[string]string
1920
txEncoder TxEncoder
2021
addressVerifier func([]byte) error
2122
mtx sync.RWMutex
2223

23-
// SLIP-44 related
2424
purpose uint32
2525
coinType uint32
2626

2727
sealed bool
2828
sealedch chan struct{}
2929
}
3030

31-
// cosmos-sdk wide global singleton
3231
var (
33-
sdkConfig *Config
34-
initConfig sync.Once
32+
configRegistry = make(map[string]*Config)
33+
registryMutex sync.Mutex
3534
)
3635

36+
// getConfigKey returns a unique config scope identifier.
37+
// It uses ENV override, or defaults to "hostname|binary|pid".
38+
func getConfigKey() string {
39+
if id := os.Getenv(EnvConfigScope); id != "" {
40+
return id
41+
}
42+
43+
exe, errExec := os.Executable()
44+
host, errHost := os.Hostname()
45+
pid := os.Getpid()
46+
47+
if errExec != nil {
48+
exe = "unknown-exe"
49+
}
50+
if errHost != nil {
51+
host = "unknown-host"
52+
}
53+
54+
return fmt.Sprintf("%s|%s|%d", host, exe, pid)
55+
}
56+
3757
// NewConfig returns a new Config with default values.
3858
func NewConfig() *Config {
3959
return &Config{
@@ -47,74 +67,59 @@ func NewConfig() *Config {
4767
"consensus_pub": Bech32PrefixConsPub,
4868
},
4969
fullFundraiserPath: FullFundraiserPath,
50-
51-
purpose: Purpose,
52-
coinType: CoinType,
53-
txEncoder: nil,
70+
purpose: Purpose,
71+
coinType: CoinType,
5472
}
5573
}
5674

57-
// GetConfig returns the config instance for the SDK.
75+
// GetConfig returns a per-scope config instance.
5876
func GetConfig() *Config {
59-
initConfig.Do(func() {
60-
sdkConfig = NewConfig()
61-
})
62-
return sdkConfig
63-
}
77+
key := getConfigKey()
6478

65-
// GetSealedConfig returns the config instance for the SDK if/once it is sealed.
66-
func GetSealedConfig(ctx context.Context) (*Config, error) {
67-
config := GetConfig()
68-
select {
69-
case <-config.sealedch:
70-
return config, nil
71-
case <-ctx.Done():
72-
return nil, ctx.Err()
79+
registryMutex.Lock()
80+
defer registryMutex.Unlock()
81+
82+
if cfg, exists := configRegistry[key]; exists {
83+
return cfg
7384
}
85+
86+
cfg := NewConfig()
87+
configRegistry[key] = cfg
88+
89+
return cfg
7490
}
7591

7692
func (config *Config) assertNotSealed() {
7793
config.mtx.RLock()
7894
defer config.mtx.RUnlock()
79-
8095
if config.sealed {
8196
panic("Config is sealed")
8297
}
8398
}
8499

85-
// SetBech32PrefixForAccount builds the Config with Bech32 addressPrefix and publKeyPrefix for accounts
86-
// and returns the config instance
87100
func (config *Config) SetBech32PrefixForAccount(addressPrefix, pubKeyPrefix string) {
88101
config.assertNotSealed()
89102
config.bech32AddressPrefix["account_addr"] = addressPrefix
90103
config.bech32AddressPrefix["account_pub"] = pubKeyPrefix
91104
}
92105

93-
// SetBech32PrefixForValidator builds the Config with Bech32 addressPrefix and publKeyPrefix for validators
94-
//
95-
// and returns the config instance
96106
func (config *Config) SetBech32PrefixForValidator(addressPrefix, pubKeyPrefix string) {
97107
config.assertNotSealed()
98108
config.bech32AddressPrefix["validator_addr"] = addressPrefix
99109
config.bech32AddressPrefix["validator_pub"] = pubKeyPrefix
100110
}
101111

102-
// SetBech32PrefixForConsensusNode builds the Config with Bech32 addressPrefix and publKeyPrefix for consensus nodes
103-
// and returns the config instance
104112
func (config *Config) SetBech32PrefixForConsensusNode(addressPrefix, pubKeyPrefix string) {
105113
config.assertNotSealed()
106114
config.bech32AddressPrefix["consensus_addr"] = addressPrefix
107115
config.bech32AddressPrefix["consensus_pub"] = pubKeyPrefix
108116
}
109117

110-
// SetTxEncoder builds the Config with TxEncoder used to marshal StdTx to bytes
111118
func (config *Config) SetTxEncoder(encoder TxEncoder) {
112119
config.assertNotSealed()
113120
config.txEncoder = encoder
114121
}
115122

116-
// SetAddressVerifier builds the Config with the provided function for verifying that addresses
117-
// have the correct format
118123
func (config *Config) SetAddressVerifier(addressVerifier func([]byte) error) {
119124
config.assertNotSealed()
120125
config.addressVerifier = addressVerifier
@@ -140,81 +145,64 @@ func (config *Config) SetCoinType(coinType uint32) {
140145
config.coinType = coinType
141146
}
142147

143-
// Seal seals the config such that the config state could not be modified further
144148
func (config *Config) Seal() *Config {
145149
config.mtx.Lock()
150+
defer config.mtx.Unlock()
146151

147152
if config.sealed {
148-
config.mtx.Unlock()
149153
return config
150154
}
151155

152-
// signal sealed after state exposed/unlocked
153156
config.sealed = true
154-
config.mtx.Unlock()
155157
close(config.sealedch)
156158

157159
return config
158160
}
159161

160-
// GetBech32AccountAddrPrefix returns the Bech32 prefix for account address
161162
func (config *Config) GetBech32AccountAddrPrefix() string {
162163
return config.bech32AddressPrefix["account_addr"]
163164
}
164165

165-
// GetBech32ValidatorAddrPrefix returns the Bech32 prefix for validator address
166166
func (config *Config) GetBech32ValidatorAddrPrefix() string {
167167
return config.bech32AddressPrefix["validator_addr"]
168168
}
169169

170-
// GetBech32ConsensusAddrPrefix returns the Bech32 prefix for consensus node address
171170
func (config *Config) GetBech32ConsensusAddrPrefix() string {
172171
return config.bech32AddressPrefix["consensus_addr"]
173172
}
174173

175-
// GetBech32AccountPubPrefix returns the Bech32 prefix for account public key
176174
func (config *Config) GetBech32AccountPubPrefix() string {
177175
return config.bech32AddressPrefix["account_pub"]
178176
}
179177

180-
// GetBech32ValidatorPubPrefix returns the Bech32 prefix for validator public key
181178
func (config *Config) GetBech32ValidatorPubPrefix() string {
182179
return config.bech32AddressPrefix["validator_pub"]
183180
}
184181

185-
// GetBech32ConsensusPubPrefix returns the Bech32 prefix for consensus node public key
186182
func (config *Config) GetBech32ConsensusPubPrefix() string {
187183
return config.bech32AddressPrefix["consensus_pub"]
188184
}
189185

190-
// GetTxEncoder return function to encode transactions
191186
func (config *Config) GetTxEncoder() TxEncoder {
192187
return config.txEncoder
193188
}
194189

195-
// GetAddressVerifier returns the function to verify that addresses have the correct format
196190
func (config *Config) GetAddressVerifier() func([]byte) error {
197191
return config.addressVerifier
198192
}
199193

200-
// GetPurpose returns the BIP-0044 Purpose code on the config.
201194
func (config *Config) GetPurpose() uint32 {
202195
return config.purpose
203196
}
204197

205-
// GetCoinType returns the BIP-0044 CoinType code on the config.
206198
func (config *Config) GetCoinType() uint32 {
207199
return config.coinType
208200
}
209201

210-
// GetFullFundraiserPath returns the BIP44Prefix.
211-
//
212-
// Deprecated: This method is supported for backward compatibility only and will be removed in a future release. Use GetFullBIP44Path instead.
213202
func (config *Config) GetFullFundraiserPath() string {
214203
return config.fullFundraiserPath
215204
}
216205

217-
// GetFullBIP44Path returns the BIP44Prefix.
218206
func (config *Config) GetFullBIP44Path() string {
219207
return fmt.Sprintf("m/%d'/%d'/0'/0/0", config.purpose, config.coinType)
220208
}
@@ -225,3 +213,15 @@ func KeyringServiceName() string {
225213
}
226214
return version.Name
227215
}
216+
217+
// Optional: expose sealed config with timeout
218+
219+
func GetSealedConfig(ctx context.Context) (*Config, error) {
220+
config := GetConfig()
221+
select {
222+
case <-config.sealedch:
223+
return config, nil
224+
case <-ctx.Done():
225+
return nil, ctx.Err()
226+
}
227+
}

types/config_test.go

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
package types_test
1+
package types
22

33
import (
44
"errors"
5+
"strings"
56
"testing"
67

78
"github.com/stretchr/testify/suite"
8-
9-
sdk "github.com/cosmos/cosmos-sdk/types"
109
)
1110

1211
type configTestSuite struct {
@@ -17,8 +16,8 @@ func TestConfigTestSuite(t *testing.T) {
1716
suite.Run(t, new(configTestSuite))
1817
}
1918

20-
func (s *contextTestSuite) TestConfig_SetPurpose() {
21-
config := sdk.NewConfig()
19+
func (s *configTestSuite) TestConfig_SetPurpose() {
20+
config := NewConfig()
2221
config.SetPurpose(44)
2322
s.Require().Equal(uint32(44), config.GetPurpose())
2423

@@ -30,7 +29,7 @@ func (s *contextTestSuite) TestConfig_SetPurpose() {
3029
}
3130

3231
func (s *configTestSuite) TestConfig_SetCoinType() {
33-
config := sdk.NewConfig()
32+
config := NewConfig()
3433
config.SetCoinType(1)
3534
s.Require().Equal(uint32(1), config.GetCoinType())
3635
config.SetCoinType(99)
@@ -42,19 +41,19 @@ func (s *configTestSuite) TestConfig_SetCoinType() {
4241

4342
func (s *configTestSuite) TestConfig_SetTxEncoder() {
4443
mockErr := errors.New("test")
45-
config := sdk.NewConfig()
44+
config := NewConfig()
4645
s.Require().Nil(config.GetTxEncoder())
47-
encFunc := sdk.TxEncoder(func(tx sdk.Tx) ([]byte, error) { return nil, nil })
46+
encFunc := TxEncoder(func(tx Tx) ([]byte, error) { return nil, mockErr })
4847
config.SetTxEncoder(encFunc)
49-
_, err := config.GetTxEncoder()(sdk.Tx(nil))
50-
s.Require().Error(mockErr, err)
48+
_, err := config.GetTxEncoder()(Tx(nil))
49+
s.Require().Equal(mockErr, err)
5150

5251
config.Seal()
5352
s.Require().Panics(func() { config.SetTxEncoder(encFunc) })
5453
}
5554

5655
func (s *configTestSuite) TestConfig_SetFullFundraiserPath() {
57-
config := sdk.NewConfig()
56+
config := NewConfig()
5857
config.SetFullFundraiserPath("test/path")
5958
s.Require().Equal("test/path", config.GetFullFundraiserPath())
6059

@@ -66,5 +65,38 @@ func (s *configTestSuite) TestConfig_SetFullFundraiserPath() {
6665
}
6766

6867
func (s *configTestSuite) TestKeyringServiceName() {
69-
s.Require().Equal(sdk.DefaultKeyringServiceName, sdk.KeyringServiceName())
68+
s.Require().Equal(DefaultKeyringServiceName, KeyringServiceName())
69+
}
70+
71+
func (s *configTestSuite) TestConfig_ScopePerBinary_DefaultBehavior() {
72+
cfg1 := GetConfig()
73+
cfg2 := GetConfig()
74+
s.Require().Equal(cfg1, cfg2, "configs should be identical in same binary by default")
75+
}
76+
77+
func (s *configTestSuite) TestConfig_ScopePerBinary_EnvOverride() {
78+
s.T().Setenv(EnvConfigScope, "test-scope-A")
79+
cfgA := GetConfig()
80+
81+
s.T().Setenv(EnvConfigScope, "test-scope-B")
82+
cfgB := GetConfig()
83+
84+
s.Require().NotEqual(cfgA, cfgB, "configs should differ for different env scopes")
85+
}
86+
87+
func (s *configTestSuite) TestConfig_ScopePerBinary_EnvRestoration() {
88+
envKey := EnvConfigScope
89+
90+
s.T().Setenv(envKey, "test-scope-Restore")
91+
cfg1 := GetConfig()
92+
93+
s.T().Setenv(envKey, "test-scope-Restore")
94+
cfg2 := GetConfig()
95+
96+
s.Require().Equal(cfg1, cfg2, "config should remain stable with same env scope")
97+
}
98+
99+
func (s *configTestSuite) TestConfig_ScopeKeyFormat() {
100+
key := getConfigKey()
101+
s.Require().True(strings.Count(key, "|") == 2, "scope key should have 2 pipe separators")
70102
}

0 commit comments

Comments
 (0)