Skip to content

Commit

Permalink
Improve set supply (#8950)
Browse files Browse the repository at this point in the history
* temp commit

* remove set supply

* fix supply

* remove keys

* improve supply set

* update changelog

* improve linter

* update setSupply to get only one coin

* update genesis

* remove dirt

* save only supply

* go fmt

* update rosetta test bootstrap

Co-authored-by: Alessio Treglia <[email protected]>
Co-authored-by: Frojdi Dymylja <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored Mar 25, 2021
1 parent 5783de3 commit 7ac436d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 53 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (crypto/types) [\#8600](https://github.com/cosmos/cosmos-sdk/pull/8600) `CompactBitArray`: optimize the `NumTrueBitsBefore` method and add an `Equal` method.
* (x/upgrade) [\#8743](https://github.com/cosmos/cosmos-sdk/pull/8743) Add tracking module versions as per ADR-041
* (types) [\#8962](https://github.com/cosmos/cosmos-sdk/issues/8962) Add `Abs()` method to `sdk.Int`.
* (x/bank) [\#8950](https://github.com/cosmos/cosmos-sdk/pull/8950) Improve efficiency on supply updates.

### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion contrib/rosetta/configuration/bootstrap.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"account_identifier": {
"address":"cosmos1ujtnemf6jmfm995j000qdry064n5lq854gfe3j"
"address":"cosmos1gykh2dsytj0lde8wr9msl9xd2nyj88duvmsnn7"
},
"currency":{
"symbol":"stake",
Expand Down
Binary file modified contrib/rosetta/node/data.tar.gz
Binary file not shown.
4 changes: 3 additions & 1 deletion x/bank/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ func (k BaseKeeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
panic(fmt.Errorf("genesis supply is incorrect, expected %v, got %v", genState.Supply, totalSupply))
}

k.setSupply(ctx, totalSupply)
for _, supply := range totalSupply {
k.setSupply(ctx, supply)
}

for _, meta := range genState.DenomMetadata {
k.SetDenomMetaData(ctx, meta)
Expand Down
91 changes: 40 additions & 51 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,39 +175,14 @@ func (k BaseKeeper) GetSupply(ctx sdk.Context, denom string) sdk.Coin {
}
}

var coin sdk.Coin
err := k.cdc.UnmarshalBinaryBare(bz, &coin)
if err != nil {
panic(err)
amount, ok := sdk.NewIntFromString(string(bz))
if !ok {
panic("unexpected supply")
}

return coin
}

// SetSupply sets the Supply to store
func (k BaseKeeper) setSupply(ctx sdk.Context, supply sdk.Coins) {
store := ctx.KVStore(k.storeKey)
supplyStore := prefix.NewStore(store, types.SupplyKey)

var newSupply []sdk.Coin
storeSupply := k.GetTotalSupply(ctx)

// update supply for coins which have non zero amount
for _, coin := range storeSupply {
if supply.AmountOf(coin.Denom).IsZero() {
zeroCoin := &sdk.Coin{
Denom: coin.Denom,
Amount: sdk.NewInt(0),
}
bz := k.cdc.MustMarshalBinaryBare(zeroCoin)
supplyStore.Set([]byte(coin.Denom), bz)
}
}
newSupply = append(newSupply, supply...)

for i := range newSupply {
bz := k.cdc.MustMarshalBinaryBare(&supply[i])
supplyStore.Set([]byte(supply[i].Denom), bz)
return sdk.Coin{
Denom: denom,
Amount: amount,
}
}

Expand Down Expand Up @@ -359,7 +334,7 @@ func (k BaseKeeper) UndelegateCoinsFromModuleToAccount(

// MintCoins creates new coins from thin air and adds it to the module account.
// It will panic if the module account does not exist or is unauthorized.
func (k BaseKeeper) MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error {
func (k BaseKeeper) MintCoins(ctx sdk.Context, moduleName string, amounts sdk.Coins) error {
acc := k.ak.GetModuleAccount(ctx, moduleName)
if acc == nil {
panic(sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "module account %s does not exist", moduleName))
Expand All @@ -369,31 +344,31 @@ func (k BaseKeeper) MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins)
panic(sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "module account %s does not have permissions to mint tokens", moduleName))
}

err := k.addCoins(ctx, acc.GetAddress(), amt)
err := k.addCoins(ctx, acc.GetAddress(), amounts)
if err != nil {
return err
}

// update total supply
supply := k.GetTotalSupply(ctx)
supply = supply.Add(amt...)

k.setSupply(ctx, supply)
for _, amount := range amounts {
supply := k.GetSupply(ctx, amount.GetDenom())
supply = supply.Add(amount)
k.setSupply(ctx, supply)
}

logger := k.Logger(ctx)
logger.Info("minted coins from module account", "amount", amt.String(), "from", moduleName)
logger.Info("minted coins from module account", "amount", amounts.String(), "from", moduleName)

// emit mint event
ctx.EventManager().EmitEvent(
types.NewCoinMintEvent(acc.GetAddress(), amt),
types.NewCoinMintEvent(acc.GetAddress(), amounts),
)

return nil
}

// BurnCoins burns coins deletes coins from the balance of the module account.
// It will panic if the module account does not exist or is unauthorized.
func (k BaseKeeper) BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error {
func (k BaseKeeper) BurnCoins(ctx sdk.Context, moduleName string, amounts sdk.Coins) error {
acc := k.ak.GetModuleAccount(ctx, moduleName)
if acc == nil {
panic(sdkerrors.Wrapf(sdkerrors.ErrUnknownAddress, "module account %s does not exist", moduleName))
Expand All @@ -403,28 +378,35 @@ func (k BaseKeeper) BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins)
panic(sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "module account %s does not have permissions to burn tokens", moduleName))
}

err := k.subUnlockedCoins(ctx, acc.GetAddress(), amt)
err := k.subUnlockedCoins(ctx, acc.GetAddress(), amounts)
if err != nil {
return err
}

// update total supply
supply := k.GetTotalSupply(ctx)
supply = supply.Sub(amt)

k.setSupply(ctx, supply)
for _, amount := range amounts {
supply := k.GetSupply(ctx, amount.GetDenom())
supply = supply.Sub(amount)
k.setSupply(ctx, supply)
}

logger := k.Logger(ctx)
logger.Info("burned tokens from module account", "amount", amt.String(), "from", moduleName)
logger.Info("burned tokens from module account", "amount", amounts.String(), "from", moduleName)

// emit burn event
ctx.EventManager().EmitEvent(
types.NewCoinBurnEvent(acc.GetAddress(), amt),
types.NewCoinBurnEvent(acc.GetAddress(), amounts),
)

return nil
}

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()))
}

func (k BaseKeeper) trackDelegation(ctx sdk.Context, addr sdk.AccAddress, balance, amt sdk.Coins) error {
acc := k.ak.GetAccount(ctx, addr)
if acc == nil {
Expand Down Expand Up @@ -463,8 +445,15 @@ func (k BaseViewKeeper) IterateTotalSupply(ctx sdk.Context, cb func(sdk.Coin) bo
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var balance sdk.Coin
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &balance)
amount, ok := sdk.NewIntFromString(string(iterator.Value()))
if !ok {
panic("unexpected supply")
}

balance := sdk.Coin{
Denom: string(iterator.Key()),
Amount: amount,
}

if cb(balance) {
break
Expand Down

0 comments on commit 7ac436d

Please sign in to comment.