Skip to content

Commit

Permalink
shield: update variable names and comments (#85)
Browse files Browse the repository at this point in the history
* Updated naming and comments for the shield module.

* Removed unused methods from expected keeper interface.
  • Loading branch information
dshe-certik authored Oct 18, 2020
1 parent aec1be1 commit 9b3f981
Show file tree
Hide file tree
Showing 18 changed files with 132 additions and 121 deletions.
14 changes: 7 additions & 7 deletions x/shield/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/certikfoundation/shentu/x/shield/types"
)

// BeginBlock executes logics to begin a block
// BeginBlock executes logics to begin a block.
func BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock, k Keeper) {
}

Expand All @@ -23,18 +23,18 @@ func EndBlocker(ctx sdk.Context, k Keeper) {
k.ClosePool(ctx, pool)
continue
}
// compute premiums for current block
// Compute premiums for current block.
var currentBlockPremium types.MixedDecCoins
if ctx.BlockTime().After(pool.EndTime) {
// must spend all premium
// Must spend all premiums
currentBlockPremium = pool.Premium
} else {
timeUntilEnd := pool.EndTime.Sub(ctx.BlockTime())
blocksUntilEnd := sdk.MaxDec(sdk.NewDec(int64(timeUntilEnd/time.Second)).QuoInt64(int64(common.SecondsPerBlock)), sdk.OneDec())
currentBlockPremium = pool.Premium.QuoDec(blocksUntilEnd)
}

// distribute to A and C in proportion
// Distribute to A and C in proportion.
totalCollateralAmount := pool.TotalCollateral.Add(pool.TotalLocked)
recipients := k.GetAllPoolCollaterals(ctx, pool)
for _, recipient := range recipients {
Expand All @@ -49,7 +49,7 @@ func EndBlocker(ctx sdk.Context, k Keeper) {
k.AddRewards(ctx, recipient.Provider, rewards)
}

// pass remaining to the pool admin if pool has ended
// Pass remaining to the pool admin if pool has ended.
if pool.EndTime.Before(ctx.BlockTime()) {
k.AddRewards(ctx, k.GetAdmin(ctx), pool.Premium)
pool.Premium.Native = sdk.NewDecCoins()
Expand All @@ -59,9 +59,9 @@ func EndBlocker(ctx sdk.Context, k Keeper) {
k.SetPool(ctx, pool)
} // for each pool

// remove expired purchases
// Remove expired purchases.
k.RemoveExpiredPurchases(ctx)

// process completed withdraws
// Process completed withdraws.
k.DequeueCompletedWithdrawQueue(ctx)
}
2 changes: 1 addition & 1 deletion x/shield/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func InitGenesis(ctx sdk.Context, k Keeper, data GenesisState) []abci.ValidatorU
for _, purchaseList := range data.PurchaseLists {
k.SetPurchaseList(ctx, purchaseList)
for _, entry := range purchaseList.Entries {
k.InsertPurchaseQueue(ctx, purchaseList, entry.ExpirationTime)
k.InsertPurchaseQueue(ctx, purchaseList, entry.DeleteTime)
}
}
for _, provider := range data.Providers {
Expand Down
2 changes: 1 addition & 1 deletion x/shield/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/certikfoundation/shentu/x/shield/types"
)

// NewHandler creates an sdk.Handler for all the shield type messages
// NewHandler creates an sdk.Handler for all the shield type messages.
func NewHandler(k Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) {
ctx = ctx.WithEventManager(sdk.NewEventManager())
Expand Down
11 changes: 5 additions & 6 deletions x/shield/keeper/collateral.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (k Keeper) IterateCollaterals(ctx sdk.Context, callback func(collateral typ
}
}

// IteratePoolCollaterals iterates through collaterals in a pool
// IteratePoolCollaterals iterates through collaterals in a pool.
func (k Keeper) IteratePoolCollaterals(ctx sdk.Context, pool types.Pool, callback func(collateral types.Collateral) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.GetPoolCollateralsKey(pool.PoolID))
Expand Down Expand Up @@ -115,7 +115,7 @@ func (k Keeper) DepositCollateral(ctx sdk.Context, from sdk.AccAddress, id uint6
return types.ErrNoPoolFound
}

// check eligibility
// Check eligibility.
provider, found := k.GetProvider(ctx, from)
if !found {
provider = k.addProvider(ctx, from)
Expand All @@ -126,7 +126,7 @@ func (k Keeper) DepositCollateral(ctx sdk.Context, from sdk.AccAddress, id uint6
}
provider.Available = provider.Available.Sub(amount)

// update the pool, collateral and provider
// Update the pool, collateral and provider.
collateral, found := k.GetCollateral(ctx, pool, from)
if !found {
collateral = types.NewCollateral(pool, from, amount)
Expand All @@ -152,8 +152,7 @@ func (k Keeper) WithdrawCollateral(ctx sdk.Context, from sdk.AccAddress, id uint
return types.ErrNoPoolFound
}

// retrieve the particular collateral to ensure that
// amount is less than collateral minus collateral withdraw
// Retrieve the particular collateral to ensure that amount is less than collateral minus collateral withdraw.
collateral, found := k.GetCollateral(ctx, pool, from)
if !found {
return types.ErrNoCollateralFound
Expand All @@ -163,7 +162,7 @@ func (k Keeper) WithdrawCollateral(ctx sdk.Context, from sdk.AccAddress, id uint
return types.ErrOverWithdraw
}

// update the pool available coins, but not pool total collateral or community which should be updated 21 days later
// Update the pool available coins, but not pool total collateral or community which should be updated 21 days later.
pool.Available = pool.Available.Sub(amount)
k.SetPool(ctx, pool)

Expand Down
4 changes: 2 additions & 2 deletions x/shield/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/certikfoundation/shentu/x/shield/types"
)

// RegisterInvariants registers all shield invariants
// RegisterInvariants registers all shield invariants.
func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
ir.RegisterRoute(types.ModuleName, "account-collaterals",
AccountCollateralsInvariants(k))
Expand All @@ -26,7 +26,7 @@ func AllInvariants(k Keeper) sdk.Invariant {
}

// AccountCollateralInvariants checks that the total collaterals for an accounts has to equal the sum of
// the account's registered collaterals
// the account's registered collaterals.
func AccountCollateralsInvariants(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
broken := false
Expand Down
4 changes: 3 additions & 1 deletion x/shield/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/certikfoundation/shentu/x/shield/types"
)

// Keeper implements the shield keeper.
type Keeper struct {
storeKey sdk.StoreKey
cdc *codec.Codec
Expand All @@ -33,6 +34,7 @@ func NewKeeper(cdc *codec.Codec, shieldStoreKey sdk.StoreKey, sk types.StakingKe
}
}

// GetValidator returns info of a validator given its operator address.
func (k Keeper) GetValidator(ctx sdk.Context, addr sdk.ValAddress) (staking.ValidatorI, bool) {
return k.sk.GetValidator(ctx, addr)
}
Expand Down Expand Up @@ -76,7 +78,7 @@ func (k Keeper) DepositNativePremium(ctx sdk.Context, premium sdk.Coins, from sd
return k.supplyKeeper.SendCoinsFromAccountToModule(ctx, from, types.ModuleName, premium)
}

// BondDenom returns staking bond denomination
// BondDenom returns staking bond denomination.
func (k Keeper) BondDenom(ctx sdk.Context) string {
return k.sk.BondDenom(ctx)
}
Expand Down
32 changes: 19 additions & 13 deletions x/shield/keeper/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (
"github.com/certikfoundation/shentu/x/shield/types"
)

// SetPool sets data of a pool in kv-store.
func (k Keeper) SetPool(ctx sdk.Context, pool types.Pool) {
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryLengthPrefixed(pool)
store.Set(types.GetPoolKey(pool.PoolID), bz)
}

// GetPool gets data of a pool given pool ID.
func (k Keeper) GetPool(ctx sdk.Context, id uint64) (types.Pool, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetPoolKey(id))
Expand All @@ -26,6 +28,7 @@ func (k Keeper) GetPool(ctx sdk.Context, id uint64) (types.Pool, bool) {
return pool, true
}

// CreatePool creates a pool and sponsor's shield.
func (k Keeper) CreatePool(ctx sdk.Context, creator sdk.AccAddress,
shield sdk.Coins, deposit types.MixedCoins, sponsor string,
sponsorAddr sdk.AccAddress, poolLifeTime time.Duration) (types.Pool, error) {
Expand All @@ -42,7 +45,7 @@ func (k Keeper) CreatePool(ctx sdk.Context, creator sdk.AccAddress,
return types.Pool{}, types.ErrPoolLifeTooShort
}

// check if shield is backed by admin's delegations
// Check if shield is backed by admin's delegations.
provider, found := k.GetProvider(ctx, admin)
if !found {
provider = k.addProvider(ctx, admin)
Expand All @@ -61,12 +64,12 @@ func (k Keeper) CreatePool(ctx sdk.Context, creator sdk.AccAddress,

pool := types.NewPool(shield, shieldAmt, depositDec, sponsor, sponsorAddr, endTime, id)

// transfer deposit
// Transfer deposit to the Shield module account.
if err := k.DepositNativePremium(ctx, deposit.Native, creator); err != nil {
return types.Pool{}, err
}

// make a pseudo-purchase for B, equal to shield
// Make a pseudo-purchase for B.
purchaseID := k.GetNextPurchaseID(ctx)
expirationTime := pool.EndTime.Add(-k.gk.GetVotingParams(ctx).VotingPeriod * 2)
purchase := types.NewPurchase(purchaseID, shield, ctx.BlockHeight(), expirationTime, expirationTime, expirationTime, "shield for sponsor")
Expand All @@ -83,13 +86,14 @@ func (k Keeper) CreatePool(ctx sdk.Context, creator sdk.AccAddress,
return pool, nil
}

// UpdatePool updates pool info and sponsor's shield.
func (k Keeper) UpdatePool(ctx sdk.Context, updater sdk.AccAddress, shield sdk.Coins, deposit types.MixedCoins, id uint64, addTime time.Duration, description string) (types.Pool, error) {
admin := k.GetAdmin(ctx)
if !updater.Equals(admin) {
return types.Pool{}, types.ErrNotShieldAdmin
}

// check if shield is backed by admin's delegations
// Check if shield is backed by admin's delegations.
provider, found := k.GetProvider(ctx, admin)
if !found {
return types.Pool{}, types.ErrNoDelegationAmount
Expand Down Expand Up @@ -119,28 +123,28 @@ func (k Keeper) UpdatePool(ctx sdk.Context, updater sdk.AccAddress, shield sdk.C
pool.Description = description
}

// transfer deposit and store
// Transfer deposit and store.
if err := k.DepositNativePremium(ctx, deposit.Native, admin); err != nil {
return types.Pool{}, err
}

// update sponsor purchase
// Update sponsor purchase.
sponsorPurchase, found := k.GetPurchaseList(ctx, id, pool.SponsorAddr)

purchaseEndTime := pool.EndTime.Add(-k.gk.GetVotingParams(ctx).VotingPeriod * 2)
if purchaseEndTime.Before(ctx.BlockTime()) {
return types.Pool{}, types.ErrPoolLifeTooShort
}
// assume there is only one purchase from sponsor address, and add in any if B's purchase expired.
// Assume there is only one purchase from sponsor address, and add in any if B's purchase expired.
var purchase types.Purchase
if !found {
purchaseID := k.GetNextPurchaseID(ctx)
purchase = types.NewPurchase(purchaseID, sdk.NewCoins(), ctx.BlockHeight(), purchaseEndTime, purchaseEndTime, purchaseEndTime, "shield for sponsor")
k.SetNextPurchaseID(ctx, purchaseID+1)
} else {
purchase = sponsorPurchase.Entries[0]
k.DequeuePurchase(ctx, sponsorPurchase, purchase.ExpirationTime)
purchase.ExpirationTime = purchaseEndTime
k.DequeuePurchase(ctx, sponsorPurchase, purchase.DeleteTime)
purchase.DeleteTime = purchaseEndTime
purchase.ClaimPeriodEndTime = purchaseEndTime
purchase.ProtectionEndTime = purchaseEndTime
}
Expand All @@ -156,6 +160,7 @@ func (k Keeper) UpdatePool(ctx sdk.Context, updater sdk.AccAddress, shield sdk.C
return pool, nil
}

// PausePool sets an active pool to be inactive.
func (k Keeper) PausePool(ctx sdk.Context, updater sdk.AccAddress, id uint64) (types.Pool, error) {
admin := k.GetAdmin(ctx)
if !updater.Equals(admin) {
Expand All @@ -173,6 +178,7 @@ func (k Keeper) PausePool(ctx sdk.Context, updater sdk.AccAddress, id uint64) (t
return pool, nil
}

// ResumePool sets an inactive pool to be active.
func (k Keeper) ResumePool(ctx sdk.Context, updater sdk.AccAddress, id uint64) (types.Pool, error) {
admin := k.GetAdmin(ctx)
if !updater.Equals(admin) {
Expand All @@ -199,12 +205,12 @@ func (k Keeper) GetAllPools(ctx sdk.Context) (pools []types.Pool) {
return pools
}

// PoolEnded returns if pool has reached ending time and block height
// PoolEnded returns if pool has reached ending time and block height.
func (k Keeper) PoolEnded(ctx sdk.Context, pool types.Pool) bool {
return ctx.BlockTime().After(pool.EndTime)
}

// ClosePool closes the pool
// ClosePool closes the pool.
func (k Keeper) ClosePool(ctx sdk.Context, pool types.Pool) {
// TODO: make sure nothing else needs to be done
k.FreeCollaterals(ctx, pool)
Expand All @@ -228,7 +234,7 @@ func (k Keeper) IterateAllPools(ctx sdk.Context, callback func(pool types.Pool)
}
}

// ValidatePoolDuration validates new pool duration to be valid
// ValidatePoolDuration validates new pool duration to be valid.
func (k Keeper) ValidatePoolDuration(ctx sdk.Context, timeDuration time.Duration) bool {
poolParams := k.GetPoolParams(ctx)
minPoolDuration := poolParams.MinPoolLife
Expand All @@ -246,7 +252,7 @@ func (k Keeper) WithdrawFromPools(ctx sdk.Context, addr sdk.AccAddress, amount s
panic(types.ErrNotEnoughCollateral)
}

// initiate proportional withdraws from all of the address's collaterals.
// Initiate proportional withdraws from all of the address's collaterals.
addrCollaterals := k.GetProviderCollaterals(ctx, addr)
remainingWithdraw := amount
for i, collateral := range addrCollaterals {
Expand Down
Loading

0 comments on commit 9b3f981

Please sign in to comment.