Skip to content

Commit

Permalink
fix: accaddr cachefix (backport cosmos#15433) (cosmos#16823)
Browse files Browse the repository at this point in the history
Co-authored-by: KyleMoser <KyleMoser@users.noreply.github.com>
Co-authored-by: HuangYi <huang@crypto.com>
  • Loading branch information
3 people authored and JeancarloBarrios committed Sep 28, 2024
1 parent d545254 commit 5048dc4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

* (x/auth) [#16554](https://github.com/cosmos/cosmos-sdk/pull/16554) `ModuleAccount.Validate` now reports a nil `.BaseAccount` instead of panicking.
* [#16588](https://github.com/cosmos/cosmos-sdk/pull/16588) Propogate the Snapshotter's failure to the caller, (it will create a empty snapshot silently before).
* (types) [#15433](https://github.com/cosmos/cosmos-sdk/pull/15433) Allow disabling of account address caches (for printing bech32 account addresses).

## [v0.46.13](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.13) - 2023-06-08

Expand Down
42 changes: 27 additions & 15 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,15 @@ func (aa AccAddress) String() string {
}

key := conv.UnsafeBytesToStr(aa)
accAddrMu.Lock()
defer accAddrMu.Unlock()
addr, ok := accAddrCache.Get(key)
if ok {
return addr.(string)

if IsAddrCacheEnabled() {
accAddrMu.Lock()
defer accAddrMu.Unlock()

addr, ok := accAddrCache.Get(key)
if ok {
return addr.(string)
}
}
return cacheBech32Addr(GetConfig().GetBech32AccountAddrPrefix(), aa, accAddrCache, key)
}
Expand Down Expand Up @@ -458,11 +462,15 @@ func (va ValAddress) String() string {
}

key := conv.UnsafeBytesToStr(va)
valAddrMu.Lock()
defer valAddrMu.Unlock()
addr, ok := valAddrCache.Get(key)
if ok {
return addr.(string)

if IsAddrCacheEnabled() {
valAddrMu.Lock()
defer valAddrMu.Unlock()

addr, ok := valAddrCache.Get(key)
if ok {
return addr.(string)
}
}
return cacheBech32Addr(GetConfig().GetBech32ValidatorAddrPrefix(), va, valAddrCache, key)
}
Expand Down Expand Up @@ -603,11 +611,15 @@ func (ca ConsAddress) String() string {
}

key := conv.UnsafeBytesToStr(ca)
consAddrMu.Lock()
defer consAddrMu.Unlock()
addr, ok := consAddrCache.Get(key)
if ok {
return addr.(string)

if IsAddrCacheEnabled() {
consAddrMu.Lock()
defer consAddrMu.Unlock()

addr, ok := consAddrCache.Get(key)
if ok {
return addr.(string)
}
}
return cacheBech32Addr(GetConfig().GetBech32ConsensusAddrPrefix(), ca, consAddrCache, key)
}
Expand Down
30 changes: 16 additions & 14 deletions types/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,32 +158,30 @@ func (s *addressTestSuite) TestUnmarshalYAMLWithInvalidInput() {
s.Require().Equal(types.ErrEmptyHexAddress, err)
}

func setConfigWithPrefix(conf *types.Config, prefix string) {
conf.SetBech32PrefixForAccount(prefix, types.GetBech32PrefixAccPub(prefix))
conf.SetBech32PrefixForValidator(types.GetBech32PrefixValAddr(prefix), types.GetBech32PrefixValPub(prefix))
conf.SetBech32PrefixForConsensusNode(types.GetBech32PrefixConsAddr(prefix), types.GetBech32PrefixConsPub(prefix))
}

// Test that the account address cache ignores the bech32 prefix setting, retrieving bech32 addresses from the cache.
// This will cause the AccAddress.String() to print out unexpected prefixes if the config was changed between bech32 lookups.
// See https://github.com/cosmos/cosmos-sdk/issues/15317.
func (s *addressTestSuite) TestAddrCache() {
// Use a random key
pubBz := make([]byte, ed25519.PubKeySize)
pub := &ed25519.PubKey{Key: pubBz}
_, err := rand.Read(pub.Key)
s.Require().NoError(err)
rand.Read(pub.Key)

// Set SDK bech32 prefixes to 'osmo'
prefix := "osmo"
conf := types.GetConfig()
setConfigWithPrefix(conf, prefix)
conf.SetBech32PrefixForAccount(prefix, prefix+"pub")
conf.SetBech32PrefixForValidator(prefix+"valoper", prefix+"valoperpub")
conf.SetBech32PrefixForConsensusNode(prefix+"valcons", prefix+"valconspub")

acc := types.AccAddress(pub.Address())
osmoAddrBech32 := acc.String()

// Set SDK bech32 to 'cosmos'
prefix = "cosmos"
setConfigWithPrefix(conf, prefix)
conf.SetBech32PrefixForAccount(prefix, prefix+"pub")
conf.SetBech32PrefixForValidator(prefix+"valoper", prefix+"valoperpub")
conf.SetBech32PrefixForConsensusNode(prefix+"valcons", prefix+"valconspub")

// We name this 'addrCosmos' to prove a point, but the bech32 address will still begin with 'osmo' due to the cache behavior.
addrCosmos := types.AccAddress(pub.Address())
Expand All @@ -204,19 +202,23 @@ func (s *addressTestSuite) TestAddrCacheDisabled() {
// Use a random key
pubBz := make([]byte, ed25519.PubKeySize)
pub := &ed25519.PubKey{Key: pubBz}
_, err := rand.Read(pub.Key)
s.Require().NoError(err)
rand.Read(pub.Key)

// Set SDK bech32 prefixes to 'osmo'
prefix := "osmo"
conf := types.GetConfig()
setConfigWithPrefix(conf, prefix)
conf.SetBech32PrefixForAccount(prefix, prefix+"pub")
conf.SetBech32PrefixForValidator(prefix+"valoper", prefix+"valoperpub")
conf.SetBech32PrefixForConsensusNode(prefix+"valcons", prefix+"valconspub")

acc := types.AccAddress(pub.Address())
osmoAddrBech32 := acc.String()

// Set SDK bech32 to 'cosmos'
prefix = "cosmos"
setConfigWithPrefix(conf, prefix)
conf.SetBech32PrefixForAccount(prefix, prefix+"pub")
conf.SetBech32PrefixForValidator(prefix+"valoper", prefix+"valoperpub")
conf.SetBech32PrefixForConsensusNode(prefix+"valcons", prefix+"valconspub")

addrCosmos := types.AccAddress(pub.Address())
cosmosAddrBech32 := addrCosmos.String()
Expand Down

0 comments on commit 5048dc4

Please sign in to comment.