From f9a4fcdb8e8c6ced272a164dd36029bf56250a3b Mon Sep 17 00:00:00 2001 From: sahithn Date: Tue, 6 Apr 2021 03:04:11 +0530 Subject: [PATCH 1/7] store supply as int bytes --- x/bank/keeper/keeper.go | 16 +++++++++++----- x/bank/legacy/v043/store.go | 3 ++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/x/bank/keeper/keeper.go b/x/bank/keeper/keeper.go index bb3bd1080c9c..14273bb97679 100644 --- a/x/bank/keeper/keeper.go +++ b/x/bank/keeper/keeper.go @@ -184,8 +184,9 @@ func (k BaseKeeper) GetSupply(ctx sdk.Context, denom string) sdk.Coin { } } - amount, ok := sdk.NewIntFromString(string(bz)) - if !ok { + var amount sdk.Int + err := amount.Unmarshal(bz) + if err != nil { panic("unexpected supply") } @@ -413,7 +414,11 @@ func (k BaseKeeper) setSupply(ctx sdk.Context, coin sdk.Coin) { store := ctx.KVStore(k.storeKey) supplyStore := prefix.NewStore(store, types.SupplyKey) - supplyStore.Set([]byte(coin.GetDenom()), []byte(coin.Amount.String())) + intBytes, err := coin.Amount.Marshal() + if err != nil { + panic("unexpected supply amount") + } + supplyStore.Set([]byte(coin.GetDenom()), intBytes) } func (k BaseKeeper) trackDelegation(ctx sdk.Context, addr sdk.AccAddress, balance, amt sdk.Coins) error { @@ -454,8 +459,9 @@ func (k BaseViewKeeper) IterateTotalSupply(ctx sdk.Context, cb func(sdk.Coin) bo defer iterator.Close() for ; iterator.Valid(); iterator.Next() { - amount, ok := sdk.NewIntFromString(string(iterator.Value())) - if !ok { + var amount sdk.Int + err := amount.Unmarshal(iterator.Value()) + if err != nil { panic("unexpected supply") } diff --git a/x/bank/legacy/v043/store.go b/x/bank/legacy/v043/store.go index 6dbc3ceab97f..292044e4e3be 100644 --- a/x/bank/legacy/v043/store.go +++ b/x/bank/legacy/v043/store.go @@ -34,8 +34,9 @@ func migrateSupply(store sdk.KVStore, cdc codec.BinaryMarshaler) error { // implementation. oldSupply := oldSupplyI.(*types.Supply) for i := range oldSupply.Total { + var amount sdk.Int coin := oldSupply.Total[i] - coinBz, err := cdc.MarshalBinaryBare(&coin) + coinBz, err := amount.Marshal() if err != nil { return err } From ddc9315d03613fe0b2c1f83e8161e137bf34aea5 Mon Sep 17 00:00:00 2001 From: sahithn Date: Tue, 6 Apr 2021 13:12:07 +0530 Subject: [PATCH 2/7] update migrate --- x/bank/legacy/v043/store.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/x/bank/legacy/v043/store.go b/x/bank/legacy/v043/store.go index 292044e4e3be..dfd021abd9ea 100644 --- a/x/bank/legacy/v043/store.go +++ b/x/bank/legacy/v043/store.go @@ -34,9 +34,8 @@ func migrateSupply(store sdk.KVStore, cdc codec.BinaryMarshaler) error { // implementation. oldSupply := oldSupplyI.(*types.Supply) for i := range oldSupply.Total { - var amount sdk.Int coin := oldSupply.Total[i] - coinBz, err := amount.Marshal() + coinBz, err := coin.Amount.Marshal() if err != nil { return err } From 7e8fc84e27a5358be671ab0ed3d9c4032bbd3932 Mon Sep 17 00:00:00 2001 From: sahithn Date: Tue, 6 Apr 2021 13:56:53 +0530 Subject: [PATCH 3/7] update migrate tests --- x/bank/legacy/v043/store_test.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/x/bank/legacy/v043/store_test.go b/x/bank/legacy/v043/store_test.go index 145db59f16cc..5f61e33dffde 100644 --- a/x/bank/legacy/v043/store_test.go +++ b/x/bank/legacy/v043/store_test.go @@ -36,12 +36,26 @@ func TestSupplyMigration(t *testing.T) { require.NoError(t, err) // New supply is indexed by denom. - var newFooCoin, newBarCoin sdk.Coin supplyStore := prefix.NewStore(store, types.SupplyKey) - encCfg.Marshaler.MustUnmarshalBinaryBare(supplyStore.Get([]byte("foo")), &newFooCoin) - encCfg.Marshaler.MustUnmarshalBinaryBare(supplyStore.Get([]byte("bar")), &newBarCoin) + bz := supplyStore.Get([]byte("foo")) + var amount sdk.Int + err = amount.Unmarshal(bz) + require.NoError(t, err) + newFooCoin := sdk.Coin{ + Denom: "foo", + Amount: amount, + } require.Equal(t, oldFooCoin, newFooCoin) + + bz = supplyStore.Get([]byte("bar")) + err = amount.Unmarshal(bz) + require.NoError(t, err) + + newBarCoin := sdk.Coin{ + Denom: "bar", + Amount: amount, + } require.Equal(t, oldBarCoin, newBarCoin) } From 76ae7facbdd2d12766c83b53a1da6e54634727ec Mon Sep 17 00:00:00 2001 From: sahithn Date: Tue, 6 Apr 2021 14:47:10 +0530 Subject: [PATCH 4/7] add error context --- x/bank/keeper/keeper.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/x/bank/keeper/keeper.go b/x/bank/keeper/keeper.go index 14273bb97679..edd14eaada90 100644 --- a/x/bank/keeper/keeper.go +++ b/x/bank/keeper/keeper.go @@ -1,6 +1,7 @@ package keeper import ( + "fmt" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" @@ -416,7 +417,7 @@ func (k BaseKeeper) setSupply(ctx sdk.Context, coin sdk.Coin) { intBytes, err := coin.Amount.Marshal() if err != nil { - panic("unexpected supply amount") + panic(fmt.Errorf("unable to marshal amount value %v", err)) } supplyStore.Set([]byte(coin.GetDenom()), intBytes) } @@ -462,7 +463,7 @@ func (k BaseViewKeeper) IterateTotalSupply(ctx sdk.Context, cb func(sdk.Coin) bo var amount sdk.Int err := amount.Unmarshal(iterator.Value()) if err != nil { - panic("unexpected supply") + panic(fmt.Errorf("unable to unmarshal supply value %v", err)) } balance := sdk.Coin{ From deb59fdee0316603877ecfe2a19e0eab6f1a6243 Mon Sep 17 00:00:00 2001 From: sahithn Date: Tue, 6 Apr 2021 14:50:43 +0530 Subject: [PATCH 5/7] fix lint --- x/bank/keeper/keeper.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/bank/keeper/keeper.go b/x/bank/keeper/keeper.go index edd14eaada90..e94f50245ad6 100644 --- a/x/bank/keeper/keeper.go +++ b/x/bank/keeper/keeper.go @@ -2,6 +2,7 @@ package keeper import ( "fmt" + "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/store/prefix" sdk "github.com/cosmos/cosmos-sdk/types" From b75525ba492f53210f8bbb0105557dfec13f06fa Mon Sep 17 00:00:00 2001 From: sahithn Date: Tue, 6 Apr 2021 15:29:01 +0530 Subject: [PATCH 6/7] update spec --- x/bank/keeper/keeper.go | 2 +- x/bank/spec/01_state.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/x/bank/keeper/keeper.go b/x/bank/keeper/keeper.go index e94f50245ad6..9704e1595e8d 100644 --- a/x/bank/keeper/keeper.go +++ b/x/bank/keeper/keeper.go @@ -189,7 +189,7 @@ func (k BaseKeeper) GetSupply(ctx sdk.Context, denom string) sdk.Coin { var amount sdk.Int err := amount.Unmarshal(bz) if err != nil { - panic("unexpected supply") + panic(fmt.Errorf("unable to unmarshal supply value %v", err)) } return sdk.Coin{ diff --git a/x/bank/spec/01_state.md b/x/bank/spec/01_state.md index 4b8a512489e6..5328e8b3caee 100644 --- a/x/bank/spec/01_state.md +++ b/x/bank/spec/01_state.md @@ -7,6 +7,6 @@ order: 1 The `x/bank` module keeps state of three primary objects, account balances, denom metadata and the total supply of all balances. -- Supply: `0x0 | byte(denom) -> ProtocolBuffer(coin)` +- Supply: `0x0 | byte(denom) -> byte(amount)` - Denom Metadata: `0x1 | byte(denom) -> ProtocolBuffer(Metadata)` - Balances: `0x2 | byte(address length) | []byte(address) | []byte(balance.Denom) -> ProtocolBuffer(balance)` From 13e8cc2e912ef6b34b89325784de96efb231a3ea Mon Sep 17 00:00:00 2001 From: sahithn Date: Tue, 6 Apr 2021 19:08:56 +0530 Subject: [PATCH 7/7] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c8fc67dd3e4..0a8f71dd0de4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -89,6 +89,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/bank) [\#8656](https://github.com/cosmos/cosmos-sdk/pull/8656) balance and supply are now correctly tracked via `coin_spent`, `coin_received`, `coinbase` and `burn` events. * (x/bank) [\#8517](https://github.com/cosmos/cosmos-sdk/pull/8517) Supply is now stored and tracked as `sdk.Coins` * (store) [\#8790](https://github.com/cosmos/cosmos-sdk/pull/8790) Reduce gas costs by 10x for transient store operations. +* (x/bank) [\#9051](https://github.com/cosmos/cosmos-sdk/pull/9051) Supply value is stored as `sdk.Int` rather than `string`. ### Improvements