Store supply as Int bytes rather than string bytes (#9051)
* store supply as int bytes * update migrate * update migrate tests * add error context * fix lint * update spec * add changelog Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Jonathan Gimeno <jgimeno@gmail.com>
This commit is contained in:
parent
5247a557d5
commit
4b2fc3bd43
@ -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
|
||||
|
||||
|
||||
@ -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"
|
||||
@ -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{
|
||||
@ -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 {
|
||||
@ -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{
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
|
||||
@ -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)`
|
||||
|
||||
Loading…
Reference in New Issue
Block a user