-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
x/bank: create reverse prefix for denom<->address (#9611)
- Loading branch information
1 parent
c061cc1
commit 9ea1fee
Showing
17 changed files
with
248 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,38 @@ | ||
package v043 | ||
|
||
import ( | ||
"errors" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/address" | ||
) | ||
|
||
const ( | ||
// ModuleName is the name of the module | ||
ModuleName = "bank" | ||
) | ||
|
||
// KVStore keys | ||
var ( | ||
BalancesPrefix = []byte{0x02} | ||
SupplyKey = []byte{0x00} | ||
BalancesPrefix = []byte{0x02} | ||
|
||
ErrInvalidKey = errors.New("invalid key") | ||
) | ||
|
||
func CreateAccountBalancesPrefix(addr []byte) []byte { | ||
return append(BalancesPrefix, address.MustLengthPrefix(addr)...) | ||
} | ||
|
||
func AddressFromBalancesStore(key []byte) (sdk.AccAddress, error) { | ||
if len(key) == 0 { | ||
return nil, ErrInvalidKey | ||
} | ||
|
||
addrLen := key[0] | ||
bound := int(addrLen) | ||
|
||
if len(key)-1 < bound { | ||
return nil, ErrInvalidKey | ||
} | ||
|
||
return key[1 : bound+1], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package v044 | ||
|
||
var ( | ||
DenomAddressPrefix = []byte{0x03} | ||
) | ||
|
||
func CreateAddressDenomPrefix(denom string) []byte { | ||
key := append(DenomAddressPrefix, []byte(denom)...) | ||
return append(key, 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package v044 | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/address" | ||
v043 "github.com/cosmos/cosmos-sdk/x/bank/migrations/v043" | ||
) | ||
|
||
// MigrateStore performs in-place store migrations from v0.43 to v0.44. The | ||
// migration includes: | ||
// | ||
// - Add an additional reverse index from denomination to address. | ||
func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error { | ||
store := ctx.KVStore(storeKey) | ||
return addDenomReverseIndex(store, cdc) | ||
} | ||
|
||
func addDenomReverseIndex(store sdk.KVStore, cdc codec.BinaryCodec) error { | ||
oldBalancesStore := prefix.NewStore(store, v043.BalancesPrefix) | ||
|
||
oldBalancesIter := oldBalancesStore.Iterator(nil, nil) | ||
defer oldBalancesIter.Close() | ||
|
||
denomPrefixStores := make(map[string]prefix.Store) // memoize prefix stores | ||
|
||
for ; oldBalancesIter.Valid(); oldBalancesIter.Next() { | ||
var balance sdk.Coin | ||
if err := cdc.Unmarshal(oldBalancesIter.Value(), &balance); err != nil { | ||
return err | ||
} | ||
|
||
addr, err := v043.AddressFromBalancesStore(oldBalancesIter.Key()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
denomPrefixStore, ok := denomPrefixStores[balance.Denom] | ||
if !ok { | ||
denomPrefixStore = prefix.NewStore(store, CreateAddressDenomPrefix(balance.Denom)) | ||
denomPrefixStores[balance.Denom] = denomPrefixStore | ||
} | ||
|
||
// Store a reverse index from denomination to account address with a | ||
// sentinel value. | ||
denomPrefixStore.Set(address.MustLengthPrefix(addr), []byte{0}) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package v044_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/cosmos-sdk/simapp" | ||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
"github.com/cosmos/cosmos-sdk/testutil" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/address" | ||
v043 "github.com/cosmos/cosmos-sdk/x/bank/migrations/v043" | ||
v044 "github.com/cosmos/cosmos-sdk/x/bank/migrations/v044" | ||
) | ||
|
||
func TestMigrateStore(t *testing.T) { | ||
encCfg := simapp.MakeTestEncodingConfig() | ||
bankKey := sdk.NewKVStoreKey("bank") | ||
ctx := testutil.DefaultContext(bankKey, sdk.NewTransientStoreKey("transient_test")) | ||
store := ctx.KVStore(bankKey) | ||
|
||
addr := sdk.AccAddress([]byte("addr________________")) | ||
prefixAccStore := prefix.NewStore(store, v043.CreateAccountBalancesPrefix(addr)) | ||
|
||
balances := sdk.NewCoins( | ||
sdk.NewCoin("foo", sdk.NewInt(10000)), | ||
sdk.NewCoin("bar", sdk.NewInt(20000)), | ||
) | ||
|
||
for _, b := range balances { | ||
bz, err := encCfg.Codec.Marshal(&b) | ||
require.NoError(t, err) | ||
|
||
prefixAccStore.Set([]byte(b.Denom), bz) | ||
} | ||
|
||
require.NoError(t, v044.MigrateStore(ctx, bankKey, encCfg.Codec)) | ||
|
||
for _, b := range balances { | ||
denomPrefixStore := prefix.NewStore(store, v044.CreateAddressDenomPrefix(b.Denom)) | ||
bz := denomPrefixStore.Get(address.MustLengthPrefix(addr)) | ||
require.NotNil(t, bz) | ||
} | ||
} |
Oops, something went wrong.