From e8897c465e0d273419f62b02a19ed511e6271ff7 Mon Sep 17 00:00:00 2001 From: Spoorthi Satheesha <9302666+spoo-bar@users.noreply.github.com> Date: Mon, 13 Mar 2023 14:36:23 +0000 Subject: [PATCH] adding code comments/documentation --- x/mint/abci.go | 6 +++--- x/mint/abci_test.go | 6 +++--- x/mint/keeper/keeper.go | 1 + x/mint/keeper/minter.go | 2 +- x/mint/types/expected_keepers.go | 2 +- x/mint/types/keys.go | 2 ++ x/rewards/keeper/keeper.go | 3 +++ 7 files changed, 14 insertions(+), 8 deletions(-) diff --git a/x/mint/abci.go b/x/mint/abci.go index b0e9f5ca..89f10450 100644 --- a/x/mint/abci.go +++ b/x/mint/abci.go @@ -39,16 +39,16 @@ func mintAndDistribute(k keeper.Keeper, ctx sdk.Context, tokenToMint sdk.Dec) { denom := k.BondDenom(ctx) mintCoin := sdk.NewInt64Coin(denom, tokenToMint.BigInt().Int64()) // as sdk.Coin - err := k.MintCoins(ctx, sdk.NewCoins(mintCoin)) + err := k.MintCoins(ctx, sdk.NewCoins(mintCoin)) // mint the tokens into x/mint if err != nil { panic(err) } for _, distribution := range mintParams.GetInflationRecipients() { - amount := distribution.Ratio.MulInt(mintCoin.Amount) // totalCoinsToMint * distribution.Ratio + amount := distribution.Ratio.MulInt(mintCoin.Amount) // distribution.Ratio * mintedCoins coin := sdk.NewCoin(denom, amount.TruncateInt()) // as sdk.Coin - err := k.SendCoinsToModule(ctx, distribution.Recipient, sdk.NewCoins(coin)) + err := k.SendCoinsToModule(ctx, distribution.Recipient, sdk.NewCoins(coin)) // distribute the tokens from x/mint if err != nil { panic(err) } diff --git a/x/mint/abci_test.go b/x/mint/abci_test.go index a1706224..5f6b667d 100644 --- a/x/mint/abci_test.go +++ b/x/mint/abci_test.go @@ -10,7 +10,7 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" ) -const REWARDSMODULE string = "rewards" +const REWARDS_MODULE string = "rewards" func (s *KeeperTestSuite) TestBeginBlocker() { currentTime := time.Now() @@ -52,7 +52,7 @@ func (s *KeeperTestSuite) TestBeginBlocker() { s.Require().True(found) s.Require().True(rewardsCollected.Amount.GT(sdk.ZeroInt())) - s.Require().True(feeCollected.IsGTE(rewardsCollected)) // feeCollected should be greater than rewards + s.Require().True(feeCollected.IsGTE(rewardsCollected)) // feeCollected should be greater than rewards cuz we set up inflation distribution that way }) } @@ -66,7 +66,7 @@ func getTestParams() types.Params { Recipient: authtypes.FeeCollectorName, Ratio: sdk.MustNewDecFromStr("0.9"), // 90% }, { - Recipient: REWARDSMODULE, + Recipient: REWARDS_MODULE, Ratio: sdk.MustNewDecFromStr("0.1"), // 10% }}) return params diff --git a/x/mint/keeper/keeper.go b/x/mint/keeper/keeper.go index 45f4787a..f32a3883 100644 --- a/x/mint/keeper/keeper.go +++ b/x/mint/keeper/keeper.go @@ -55,6 +55,7 @@ func (k Keeper) BondDenom(ctx sdk.Context) string { return k.stakingKeeper.BondDenom(ctx) } +// SendCoinsFromModuleToModule sends the given number of coins from one module account to another module account func (k Keeper) SendCoinsToModule(ctx sdk.Context, recipientModule string, amt sdk.Coins) error { return k.bankKeeper.SendCoinsFromModuleToModule(ctx, types.ModuleName, recipientModule, amt) } diff --git a/x/mint/keeper/minter.go b/x/mint/keeper/minter.go index 8e6f272a..3b0ea05f 100644 --- a/x/mint/keeper/minter.go +++ b/x/mint/keeper/minter.go @@ -24,6 +24,7 @@ func (k Keeper) GetInflationForRecipient(ctx sdk.Context, recipientName string) return mintAmount, true } +// SetInflationForRecipient sets the sdk.Coin distributed to the given module for the current block func (k Keeper) SetInflationForRecipient(ctx sdk.Context, recipientName string, mintAmount sdk.Coin) { store := ctx.KVStore(k.storeKey) value := k.cdc.MustMarshal(&mintAmount) @@ -59,7 +60,6 @@ func (k Keeper) GetBlockProvisions(ctx sdk.Context) (tokens sdk.Dec, blockInflat bondedTokenSupply := k.GetBondedTokenSupply(ctx) tokens = blockInflation.MulInt(bondedTokenSupply.Amount).Mul(sdk.NewDecFromBigInt(big.NewInt(int64(elapsed.Seconds()))).QuoInt64(int64(Year.Seconds()))) // amount := (inflation * bondedTokenSupply) * (elapsed/Year) - //tokens = sdk.NewInt64Coin(bondDenom, tokenAmount.BigInt().Int64()) // as sdk.Coin return } diff --git a/x/mint/types/expected_keepers.go b/x/mint/types/expected_keepers.go index 6eb042cf..7b506f7f 100644 --- a/x/mint/types/expected_keepers.go +++ b/x/mint/types/expected_keepers.go @@ -11,7 +11,7 @@ type BankKeeper interface { MintCoins(ctx sdk.Context, name string, amt sdk.Coins) error // GetSupply retrieves the given token supply from store GetSupply(ctx sdk.Context, denom string) sdk.Coin - + // SendCoinsFromModuleToModule sends the given number of coins from one module account to another module account SendCoinsFromModuleToModule(ctx sdk.Context, senderModule, recipientModule string, amt sdk.Coins) error } diff --git a/x/mint/types/keys.go b/x/mint/types/keys.go index f3928eba..b435c43c 100644 --- a/x/mint/types/keys.go +++ b/x/mint/types/keys.go @@ -19,6 +19,8 @@ var ( MintDistribution = []byte{0x00} ) +// GetMintDistributionRecipientKey gets the store prefix to fetch the inflation distribution for the recipient +// returns MintDistribution + currentBlockHeight + recipientName func GetMintDistributionRecipientKey(blockHeight int64, recipientName string) []byte { return append(append(MintDistribution, byte(blockHeight)), []byte(recipientName)...) } diff --git a/x/rewards/keeper/keeper.go b/x/rewards/keeper/keeper.go index c4d20314..0c83a418 100644 --- a/x/rewards/keeper/keeper.go +++ b/x/rewards/keeper/keeper.go @@ -38,7 +38,9 @@ type BankKeeperExpected interface { SendCoinsFromModuleToModule(ctx sdk.Context, senderModule string, recipientModule string, amt sdk.Coins) error } +// BankKeeperExpected defines the interface for the x/mint module dependency. type MintKeeperExpected interface { + // GetInflationForRecipient gets the sdk.Coin distributed to the given module in the current block GetInflationForRecipient(ctx sdk.Context, recipientName string) (sdk.Coin, bool) } @@ -110,6 +112,7 @@ func (k Keeper) GetRewardsRecords(ctx sdk.Context, rewardsAddr sdk.AccAddress, p return k.state.RewardsRecord(ctx).GetRewardsRecordByRewardsAddressPaginated(rewardsAddr, pageReq) } +// GetInflationForRecipient gets the sdk.Coin distributed to the x/rewards in the current block func (k Keeper) GetInflationForRewards(ctx sdk.Context) (sdk.Coin, bool) { return k.mintKeeper.GetInflationForRecipient(ctx, types.ModuleName) }