Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store supply as Int bytes rather than string bytes #9051

Merged
merged 11 commits into from
Apr 6, 2021
22 changes: 15 additions & 7 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package keeper

import (
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -184,9 +186,10 @@ func (k BaseKeeper) GetSupply(ctx sdk.Context, denom string) sdk.Coin {
}
}

amount, ok := sdk.NewIntFromString(string(bz))
if !ok {
panic("unexpected supply")
var amount sdk.Int
err := amount.Unmarshal(bz)
if err != nil {
panic(fmt.Errorf("unable to unmarshal supply value %v", err))
}

return sdk.Coin{
Expand Down Expand Up @@ -413,7 +416,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(fmt.Errorf("unable to marshal amount value %v", err))
}
supplyStore.Set([]byte(coin.GetDenom()), intBytes)
}

func (k BaseKeeper) trackDelegation(ctx sdk.Context, addr sdk.AccAddress, balance, amt sdk.Coins) error {
Expand Down Expand Up @@ -454,9 +461,10 @@ 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 {
panic("unexpected supply")
var amount sdk.Int
err := amount.Unmarshal(iterator.Value())
if err != nil {
panic(fmt.Errorf("unable to unmarshal supply value %v", err))
}

balance := sdk.Coin{
Expand Down
2 changes: 1 addition & 1 deletion x/bank/legacy/v043/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func migrateSupply(store sdk.KVStore, cdc codec.BinaryMarshaler) error {
oldSupply := oldSupplyI.(*types.Supply)
for i := range oldSupply.Total {
coin := oldSupply.Total[i]
coinBz, err := cdc.MarshalBinaryBare(&coin)
coinBz, err := coin.Amount.Marshal()
if err != nil {
return err
}
Expand Down
20 changes: 17 additions & 3 deletions x/bank/legacy/v043/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion x/bank/spec/01_state.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`