From 5048dc48531f8145f4aedefe99d208408af35a56 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 5 Jul 2023 10:32:28 +0200 Subject: [PATCH] fix: accaddr cachefix (backport #15433) (#16823) Co-authored-by: KyleMoser Co-authored-by: HuangYi --- CHANGELOG.md | 1 + types/address.go | 42 +++++++++++++++++++++++++++--------------- types/address_test.go | 30 ++++++++++++++++-------------- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2cb2cd1c0a6..cce16035b5e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/types/address.go b/types/address.go index a18513743a1d..824039106a1f 100644 --- a/types/address.go +++ b/types/address.go @@ -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) } @@ -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) } @@ -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) } diff --git a/types/address_test.go b/types/address_test.go index 7b51ce0b09ce..27446f754965 100644 --- a/types/address_test.go +++ b/types/address_test.go @@ -158,12 +158,6 @@ 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. @@ -171,19 +165,23 @@ 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()) @@ -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()