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
16 changes: 11 additions & 5 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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")
sahith-narahari marked this conversation as resolved.
Show resolved Hide resolved
}
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,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")
sahith-narahari marked this conversation as resolved.
Show resolved Hide resolved
}

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