Skip to content

Commit 7ac436d

Browse files
jgimenoAlessio Tregliafdymyljamergify[bot]
authored
Improve set supply (#8950)
* 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 <alessio@tendermint.com> Co-authored-by: Frojdi Dymylja <33157909+fdymylja@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 5783de3 commit 7ac436d

File tree

5 files changed

+45
-53
lines changed

5 files changed

+45
-53
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
8383
* (crypto/types) [\#8600](https://github.com/cosmos/cosmos-sdk/pull/8600) `CompactBitArray`: optimize the `NumTrueBitsBefore` method and add an `Equal` method.
8484
* (x/upgrade) [\#8743](https://github.com/cosmos/cosmos-sdk/pull/8743) Add tracking module versions as per ADR-041
8585
* (types) [\#8962](https://github.com/cosmos/cosmos-sdk/issues/8962) Add `Abs()` method to `sdk.Int`.
86+
* (x/bank) [\#8950](https://github.com/cosmos/cosmos-sdk/pull/8950) Improve efficiency on supply updates.
8687

8788
### Bug Fixes
8889

contrib/rosetta/configuration/bootstrap.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"account_identifier": {
4-
"address":"cosmos1ujtnemf6jmfm995j000qdry064n5lq854gfe3j"
4+
"address":"cosmos1gykh2dsytj0lde8wr9msl9xd2nyj88duvmsnn7"
55
},
66
"currency":{
77
"symbol":"stake",

contrib/rosetta/node/data.tar.gz

1.98 KB
Binary file not shown.

x/bank/keeper/genesis.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ func (k BaseKeeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
3131
panic(fmt.Errorf("genesis supply is incorrect, expected %v, got %v", genState.Supply, totalSupply))
3232
}
3333

34-
k.setSupply(ctx, totalSupply)
34+
for _, supply := range totalSupply {
35+
k.setSupply(ctx, supply)
36+
}
3537

3638
for _, meta := range genState.DenomMetadata {
3739
k.SetDenomMetaData(ctx, meta)

x/bank/keeper/keeper.go

+40-51
Original file line numberDiff line numberDiff line change
@@ -175,39 +175,14 @@ func (k BaseKeeper) GetSupply(ctx sdk.Context, denom string) sdk.Coin {
175175
}
176176
}
177177

178-
var coin sdk.Coin
179-
err := k.cdc.UnmarshalBinaryBare(bz, &coin)
180-
if err != nil {
181-
panic(err)
178+
amount, ok := sdk.NewIntFromString(string(bz))
179+
if !ok {
180+
panic("unexpected supply")
182181
}
183182

184-
return coin
185-
}
186-
187-
// SetSupply sets the Supply to store
188-
func (k BaseKeeper) setSupply(ctx sdk.Context, supply sdk.Coins) {
189-
store := ctx.KVStore(k.storeKey)
190-
supplyStore := prefix.NewStore(store, types.SupplyKey)
191-
192-
var newSupply []sdk.Coin
193-
storeSupply := k.GetTotalSupply(ctx)
194-
195-
// update supply for coins which have non zero amount
196-
for _, coin := range storeSupply {
197-
if supply.AmountOf(coin.Denom).IsZero() {
198-
zeroCoin := &sdk.Coin{
199-
Denom: coin.Denom,
200-
Amount: sdk.NewInt(0),
201-
}
202-
bz := k.cdc.MustMarshalBinaryBare(zeroCoin)
203-
supplyStore.Set([]byte(coin.Denom), bz)
204-
}
205-
}
206-
newSupply = append(newSupply, supply...)
207-
208-
for i := range newSupply {
209-
bz := k.cdc.MustMarshalBinaryBare(&supply[i])
210-
supplyStore.Set([]byte(supply[i].Denom), bz)
183+
return sdk.Coin{
184+
Denom: denom,
185+
Amount: amount,
211186
}
212187
}
213188

@@ -359,7 +334,7 @@ func (k BaseKeeper) UndelegateCoinsFromModuleToAccount(
359334

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

372-
err := k.addCoins(ctx, acc.GetAddress(), amt)
347+
err := k.addCoins(ctx, acc.GetAddress(), amounts)
373348
if err != nil {
374349
return err
375350
}
376351

377-
// update total supply
378-
supply := k.GetTotalSupply(ctx)
379-
supply = supply.Add(amt...)
380-
381-
k.setSupply(ctx, supply)
352+
for _, amount := range amounts {
353+
supply := k.GetSupply(ctx, amount.GetDenom())
354+
supply = supply.Add(amount)
355+
k.setSupply(ctx, supply)
356+
}
382357

383358
logger := k.Logger(ctx)
384-
logger.Info("minted coins from module account", "amount", amt.String(), "from", moduleName)
359+
logger.Info("minted coins from module account", "amount", amounts.String(), "from", moduleName)
385360

386361
// emit mint event
387362
ctx.EventManager().EmitEvent(
388-
types.NewCoinMintEvent(acc.GetAddress(), amt),
363+
types.NewCoinMintEvent(acc.GetAddress(), amounts),
389364
)
390365

391366
return nil
392367
}
393368

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

406-
err := k.subUnlockedCoins(ctx, acc.GetAddress(), amt)
381+
err := k.subUnlockedCoins(ctx, acc.GetAddress(), amounts)
407382
if err != nil {
408383
return err
409384
}
410385

411-
// update total supply
412-
supply := k.GetTotalSupply(ctx)
413-
supply = supply.Sub(amt)
414-
415-
k.setSupply(ctx, supply)
386+
for _, amount := range amounts {
387+
supply := k.GetSupply(ctx, amount.GetDenom())
388+
supply = supply.Sub(amount)
389+
k.setSupply(ctx, supply)
390+
}
416391

417392
logger := k.Logger(ctx)
418-
logger.Info("burned tokens from module account", "amount", amt.String(), "from", moduleName)
393+
logger.Info("burned tokens from module account", "amount", amounts.String(), "from", moduleName)
419394

420395
// emit burn event
421396
ctx.EventManager().EmitEvent(
422-
types.NewCoinBurnEvent(acc.GetAddress(), amt),
397+
types.NewCoinBurnEvent(acc.GetAddress(), amounts),
423398
)
424399

425400
return nil
426401
}
427402

403+
func (k BaseKeeper) setSupply(ctx sdk.Context, coin sdk.Coin) {
404+
store := ctx.KVStore(k.storeKey)
405+
supplyStore := prefix.NewStore(store, types.SupplyKey)
406+
407+
supplyStore.Set([]byte(coin.GetDenom()), []byte(coin.Amount.String()))
408+
}
409+
428410
func (k BaseKeeper) trackDelegation(ctx sdk.Context, addr sdk.AccAddress, balance, amt sdk.Coins) error {
429411
acc := k.ak.GetAccount(ctx, addr)
430412
if acc == nil {
@@ -463,8 +445,15 @@ func (k BaseViewKeeper) IterateTotalSupply(ctx sdk.Context, cb func(sdk.Coin) bo
463445
defer iterator.Close()
464446

465447
for ; iterator.Valid(); iterator.Next() {
466-
var balance sdk.Coin
467-
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &balance)
448+
amount, ok := sdk.NewIntFromString(string(iterator.Value()))
449+
if !ok {
450+
panic("unexpected supply")
451+
}
452+
453+
balance := sdk.Coin{
454+
Denom: string(iterator.Key()),
455+
Amount: amount,
456+
}
468457

469458
if cb(balance) {
470459
break

0 commit comments

Comments
 (0)