-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Improve set supply #8950
Improve set supply #8950
Changes from 16 commits
006852a
00416d3
34c1bf8
bb0511f
4daba57
9daa707
a00dd17
af5c523
463a7ec
141626a
5d5be8c
06c3d60
b51e816
174d9bf
2e22746
9c33f45
aed0e8c
0267d24
f265a5a
0bdc7a3
34666bc
2ec3fa3
2f8ca8f
2591510
f223213
7b5645f
58f2b4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,33 +184,6 @@ func (k BaseKeeper) GetSupply(ctx sdk.Context, denom string) sdk.Coin { | |
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) | ||
} | ||
} | ||
|
||
// GetDenomMetaData retrieves the denomination metadata | ||
func (k BaseKeeper) GetDenomMetaData(ctx sdk.Context, denom string) (types.Metadata, bool) { | ||
store := ctx.KVStore(k.storeKey) | ||
|
@@ -354,7 +327,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)) | ||
|
@@ -364,31 +337,33 @@ 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) | ||
var newSupply sdk.Coins | ||
for _, amount := range amounts { | ||
supply := k.GetSupply(ctx, amount.GetDenom()) | ||
supply = supply.Add(amount) | ||
newSupply = append(newSupply, supply) | ||
} | ||
k.setSupply(ctx, newSupply) | ||
|
||
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)) | ||
|
@@ -398,28 +373,40 @@ 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) | ||
var newSupply sdk.Coins | ||
for _, amount := range amounts { | ||
supply := k.GetSupply(ctx, amount.GetDenom()) | ||
supply = supply.Sub(amount) | ||
newSupply = append(newSupply, supply) | ||
} | ||
k.setSupply(ctx, newSupply) | ||
|
||
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, coins sdk.Coins) { | ||
aaronc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
store := ctx.KVStore(k.storeKey) | ||
supplyStore := prefix.NewStore(store, types.SupplyKey) | ||
|
||
for i := range coins { | ||
bz := k.cdc.MustMarshalBinaryBare(&coins[i]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also noticing that this doesn't seem quite right. There's no need to serialize the whole There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the first approach I took, setSupply to get one coin, I can revert it. I agree on the coin serialization. Thanks! |
||
supplyStore.Set([]byte(coins[i].GetDenom()), bz) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not keeping a loop here?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An argument could be made for doing all of this logic inline inside There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jgimeno I wonder if there's value in making this |
||
|
||
func (k BaseKeeper) trackDelegation(ctx sdk.Context, addr sdk.AccAddress, balance, amt sdk.Coins) error { | ||
acc := k.ak.GetAccount(ctx, addr) | ||
if acc == nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see a need to bundle this into Coins at all. Instead I think we should just be setting supply by denom in the above for loop. The whole Coins abstraction is just unnecessary here IMHO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for inlining. The function is used only 2 times, so not a big trade off.