Skip to content

Commit c38957b

Browse files
committed
add comment to all function
1 parent 165be0f commit c38957b

File tree

6 files changed

+44
-1
lines changed

6 files changed

+44
-1
lines changed

x/restake/client/cli/query.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func GetQueryCmd() *cobra.Command {
3232
return queryCmd
3333
}
3434

35+
// GetQueryCmdKey implements the key query command.
3536
func GetQueryCmdKey() *cobra.Command {
3637
cmd := &cobra.Command{
3738
Use: "key [name]",
@@ -60,6 +61,7 @@ func GetQueryCmdKey() *cobra.Command {
6061
return cmd
6162
}
6263

64+
// GetQueryCmdKeys implements the keys query command.
6365
func GetQueryCmdKeys() *cobra.Command {
6466
cmd := &cobra.Command{
6567
Use: "keys",
@@ -89,6 +91,7 @@ func GetQueryCmdKeys() *cobra.Command {
8991
return cmd
9092
}
9193

94+
// GetQueryCmdRewards implements the rewards query command.
9295
func GetQueryCmdRewards() *cobra.Command {
9396
cmd := &cobra.Command{
9497
Use: "rewards [locker_address]",
@@ -115,6 +118,7 @@ func GetQueryCmdRewards() *cobra.Command {
115118
return cmd
116119
}
117120

121+
// GetQueryCmdReward implements the reward query command.
118122
func GetQueryCmdReward() *cobra.Command {
119123
cmd := &cobra.Command{
120124
Use: "reward [locker_address] [key_name]",
@@ -141,6 +145,7 @@ func GetQueryCmdReward() *cobra.Command {
141145
return cmd
142146
}
143147

148+
// GetQueryCmdLocks implements the locks query command.
144149
func GetQueryCmdLocks() *cobra.Command {
145150
cmd := &cobra.Command{
146151
Use: "locks [locker_address]",
@@ -167,6 +172,7 @@ func GetQueryCmdLocks() *cobra.Command {
167172
return cmd
168173
}
169174

175+
// GetQueryCmdLock implements the lock query command.
170176
func GetQueryCmdLock() *cobra.Command {
171177
cmd := &cobra.Command{
172178
Use: "lock [locker_address] [key_name]",

x/restake/keeper/grpc_query.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import (
1212
"github.com/bandprotocol/chain/v2/x/restake/types"
1313
)
1414

15-
// Querier is used as Keeper will have duplicate methods if used directly, and gRPC names take precedence over keeper
15+
// Querier is used as Keeper will have duplicate methods if used directly, and gRPC names take precedence over keeper.
1616
type Querier struct {
1717
*Keeper
1818
}
1919

2020
var _ types.QueryServer = Querier{}
2121

22+
// Keys queries all keys with pagination.
2223
func (k Querier) Keys(
2324
c context.Context,
2425
req *types.QueryKeysRequest,
@@ -43,6 +44,7 @@ func (k Querier) Keys(
4344
return &types.QueryKeysResponse{Keys: filteredKeys, Pagination: pageRes}, nil
4445
}
4546

47+
// Key queries info about a key.
4648
func (k Querier) Key(
4749
c context.Context,
4850
req *types.QueryKeyRequest,
@@ -57,6 +59,7 @@ func (k Querier) Key(
5759
return &types.QueryKeyResponse{Key: key}, nil
5860
}
5961

62+
// Rewards queries all rewards with pagination.
6063
func (k Querier) Rewards(
6164
c context.Context,
6265
req *types.QueryRewardsRequest,
@@ -87,6 +90,7 @@ func (k Querier) Rewards(
8790
return &types.QueryRewardsResponse{Rewards: filteredRewards, Pagination: pageRes}, nil
8891
}
8992

93+
// Reward queries info about a reward by using address and key
9094
func (k Querier) Reward(
9195
c context.Context,
9296
req *types.QueryRewardRequest,
@@ -108,6 +112,7 @@ func (k Querier) Reward(
108112
}, nil
109113
}
110114

115+
// Locks queries all locks with pagination.
111116
func (k Querier) Locks(
112117
c context.Context,
113118
req *types.QueryLocksRequest,
@@ -144,6 +149,7 @@ func (k Querier) Locks(
144149
return &types.QueryLocksResponse{Locks: filteredLocks, Pagination: pageRes}, nil
145150
}
146151

152+
// Lock queries info about a lock by using address and key
147153
func (k Querier) Lock(
148154
c context.Context,
149155
req *types.QueryLockRequest,

x/restake/keeper/hooks.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (h Hooks) BeforeDelegationSharesModified(_ sdk.Context, _ sdk.AccAddress, _
4646
return nil
4747
}
4848

49+
// check if after delegation is removed, the locked power is still less than total delegation
4950
func (h Hooks) BeforeDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error {
5051
delegated := h.k.stakingKeeper.GetDelegatorBonded(ctx, delAddr)
5152

@@ -68,6 +69,7 @@ func (h Hooks) BeforeDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAddress,
6869
return h.isAbleToUnbond(ctx, delAddr, delegated)
6970
}
7071

72+
// check if after delegation is modified, the locked power is still less than total delegation
7173
func (h Hooks) AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress, _ sdk.ValAddress) error {
7274
// get total delegation
7375
delegated := h.k.stakingKeeper.GetDelegatorBonded(ctx, delAddr)
@@ -84,6 +86,7 @@ func (h Hooks) AfterUnbondingInitiated(_ sdk.Context, _ uint64) error {
8486
return nil
8587
}
8688

89+
// isAbleToUnbond checks if the new total delegation is still more than locked power in the module.
8790
func (h Hooks) isAbleToUnbond(ctx sdk.Context, addr sdk.AccAddress, delegated sdkmath.Int) error {
8891
iterator := sdk.KVStoreReversePrefixIterator(ctx.KVStore(h.k.storeKey), types.LocksByAmountIndexKey(addr))
8992
defer iterator.Close()

x/restake/keeper/keeper_key.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/bandprotocol/chain/v2/x/restake/types"
99
)
1010

11+
// GetOrCreateKey get the key object by using key name. If the key doesn't exist, it will initialize the new key.
1112
func (k Keeper) GetOrCreateKey(ctx sdk.Context, keyName string) (types.Key, error) {
1213
key, err := k.GetKey(ctx, keyName)
1314
if err != nil {
@@ -74,6 +75,7 @@ func (k Keeper) AddRewards(ctx sdk.Context, sender sdk.AccAddress, keyName strin
7475
return nil
7576
}
7677

78+
// IsActiveKey checks whether the key is active or not.
7779
func (k Keeper) IsActiveKey(ctx sdk.Context, keyName string) bool {
7880
key, err := k.GetKey(ctx, keyName)
7981
if err != nil {
@@ -83,6 +85,7 @@ func (k Keeper) IsActiveKey(ctx sdk.Context, keyName string) bool {
8385
return key.IsActive
8486
}
8587

88+
// DeactivateKey deactivates the key.
8689
func (k Keeper) DeactivateKey(ctx sdk.Context, keyName string) error {
8790
key, err := k.GetKey(ctx, keyName)
8891
if err != nil {
@@ -106,6 +109,7 @@ func (k Keeper) DeactivateKey(ctx sdk.Context, keyName string) error {
106109
return nil
107110
}
108111

112+
// createKeyAccount creates a key account by using name and block hash.
109113
func (k Keeper) createKeyAccount(ctx sdk.Context, key string) (sdk.AccAddress, error) {
110114
header := ctx.BlockHeader()
111115

@@ -143,10 +147,12 @@ func (k Keeper) createKeyAccount(ctx sdk.Context, key string) (sdk.AccAddress, e
143147
// store part
144148
// -------------------------------
145149

150+
// GetKeysIterator gets iterator of key store.
146151
func (k Keeper) GetKeysIterator(ctx sdk.Context) sdk.Iterator {
147152
return sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), types.KeyStoreKeyPrefix)
148153
}
149154

155+
// GetKeys gets all keys in the store.
150156
func (k Keeper) GetKeys(ctx sdk.Context) (keys []types.Key) {
151157
iterator := k.GetKeysIterator(ctx)
152158
defer iterator.Close()
@@ -160,10 +166,13 @@ func (k Keeper) GetKeys(ctx sdk.Context) (keys []types.Key) {
160166
return keys
161167
}
162168

169+
// HasKey checks if key exists in the store.
163170
func (k Keeper) HasKey(ctx sdk.Context, keyName string) bool {
164171
return ctx.KVStore(k.storeKey).Has(types.KeyStoreKey(keyName))
165172
}
166173

174+
// MustGetKey gets a key from store by name.
175+
// Panics if can't get the key.
167176
func (k Keeper) MustGetKey(ctx sdk.Context, keyName string) types.Key {
168177
key, err := k.GetKey(ctx, keyName)
169178
if err != nil {
@@ -173,6 +182,7 @@ func (k Keeper) MustGetKey(ctx sdk.Context, keyName string) types.Key {
173182
return key
174183
}
175184

185+
// GetKey gets a key from store by name.
176186
func (k Keeper) GetKey(ctx sdk.Context, keyName string) (types.Key, error) {
177187
bz := ctx.KVStore(k.storeKey).Get(types.KeyStoreKey(keyName))
178188
if bz == nil {
@@ -185,6 +195,7 @@ func (k Keeper) GetKey(ctx sdk.Context, keyName string) (types.Key, error) {
185195
return key, nil
186196
}
187197

198+
// SetKey sets a key to the store.
188199
func (k Keeper) SetKey(ctx sdk.Context, key types.Key) {
189200
ctx.KVStore(k.storeKey).Set(types.KeyStoreKey(key.Name), k.cdc.MustMarshal(&key))
190201
}

x/restake/keeper/keeper_lock.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,14 @@ func (k Keeper) GetLockedPower(ctx sdk.Context, lockerAddr sdk.AccAddress, keyNa
8686
return lock.Amount, nil
8787
}
8888

89+
// getAccumulatedRewards gets the accumulatedRewards of a lock if they lock since beginning.
8990
func (k Keeper) getAccumulatedRewards(ctx sdk.Context, lock types.Lock) sdk.DecCoins {
9091
key := k.MustGetKey(ctx, lock.Key)
9192

9293
return key.RewardPerPowers.MulDecTruncate(sdkmath.LegacyNewDecFromInt(lock.Amount))
9394
}
9495

96+
// getReward gets the reward of a lock by using accumulated rewards and reward debts.
9597
func (k Keeper) getReward(ctx sdk.Context, lock types.Lock) types.Reward {
9698
totalRewards := k.getAccumulatedRewards(ctx, lock)
9799

@@ -105,14 +107,17 @@ func (k Keeper) getReward(ctx sdk.Context, lock types.Lock) types.Reward {
105107
// store part
106108
// -------------------------------
107109

110+
// GetLocksIterator gets iterator of lock store.
108111
func (k Keeper) GetLocksIterator(ctx sdk.Context) sdk.Iterator {
109112
return sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), types.LockStoreKeyPrefix)
110113
}
111114

115+
// GetLocksByAddressIterator gets iterator of locks of the speicfic address.
112116
func (k Keeper) GetLocksByAddressIterator(ctx sdk.Context, addr sdk.AccAddress) sdk.Iterator {
113117
return sdk.KVStorePrefixIterator(ctx.KVStore(k.storeKey), types.LocksStoreKey(addr))
114118
}
115119

120+
// GetLocksByAddress gets all locks of the address.
116121
func (k Keeper) GetLocksByAddress(ctx sdk.Context, addr sdk.AccAddress) (locks []types.Lock) {
117122
iterator := k.GetLocksByAddressIterator(ctx, addr)
118123
defer iterator.Close()
@@ -126,6 +131,7 @@ func (k Keeper) GetLocksByAddress(ctx sdk.Context, addr sdk.AccAddress) (locks [
126131
return locks
127132
}
128133

134+
// GetLocks gets all locks in the store.
129135
func (k Keeper) GetLocks(ctx sdk.Context) (locks []types.Lock) {
130136
iterator := k.GetLocksIterator(ctx)
131137
defer iterator.Close()
@@ -139,10 +145,12 @@ func (k Keeper) GetLocks(ctx sdk.Context) (locks []types.Lock) {
139145
return locks
140146
}
141147

148+
// HasLock checks if lock exists in the store.
142149
func (k Keeper) HasLock(ctx sdk.Context, addr sdk.AccAddress, keyName string) bool {
143150
return ctx.KVStore(k.storeKey).Has(types.LockStoreKey(addr, keyName))
144151
}
145152

153+
// GetLock gets a lock from store by address and key name.
146154
func (k Keeper) GetLock(ctx sdk.Context, addr sdk.AccAddress, keyName string) (types.Lock, error) {
147155
bz := ctx.KVStore(k.storeKey).Get(types.LockStoreKey(addr, keyName))
148156
if bz == nil {
@@ -159,6 +167,7 @@ func (k Keeper) GetLock(ctx sdk.Context, addr sdk.AccAddress, keyName string) (t
159167
return lock, nil
160168
}
161169

170+
// SetLock sets a lock to the store.
162171
func (k Keeper) SetLock(ctx sdk.Context, lock types.Lock) {
163172
addr := sdk.MustAccAddressFromBech32(lock.LockerAddress)
164173
k.DeleteLock(ctx, addr, lock.Key)
@@ -167,6 +176,7 @@ func (k Keeper) SetLock(ctx sdk.Context, lock types.Lock) {
167176
k.setLockByAmount(ctx, lock)
168177
}
169178

179+
// DeleteLock deletes a lock from the store.
170180
func (k Keeper) DeleteLock(ctx sdk.Context, addr sdk.AccAddress, keyName string) {
171181
lock, err := k.GetLock(ctx, addr, keyName)
172182
if err != nil {
@@ -176,10 +186,12 @@ func (k Keeper) DeleteLock(ctx sdk.Context, addr sdk.AccAddress, keyName string)
176186
k.deleteLockByAmount(ctx, lock)
177187
}
178188

189+
// setLockByAmount sets a lock by amount to the store.
179190
func (k Keeper) setLockByAmount(ctx sdk.Context, lock types.Lock) {
180191
ctx.KVStore(k.storeKey).Set(types.LockByAmountIndexKey(lock), []byte(lock.Key))
181192
}
182193

194+
// deleteLockByAmount deletes a lock by amount from the store.
183195
func (k Keeper) deleteLockByAmount(ctx sdk.Context, lock types.Lock) {
184196
ctx.KVStore(k.storeKey).Delete(types.LockByAmountIndexKey(lock))
185197
}

x/restake/types/keys.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,27 @@ var (
3333
LocksByAmountIndexKeyPrefix = []byte{0x10}
3434
)
3535

36+
// KeyStoreKey returns the key to retrieve a specific key from the store.
3637
func KeyStoreKey(keyName string) []byte {
3738
return append(KeyStoreKeyPrefix, []byte(keyName)...)
3839
}
3940

41+
// LocksStoreKey returns the key to retrieve all locks of an address from the store.
4042
func LocksStoreKey(addr sdk.AccAddress) []byte {
4143
return append(LockStoreKeyPrefix, address.MustLengthPrefix(addr)...)
4244
}
4345

46+
// LockStoreKey returns the key to retrieve a lock of an address and the key from the store.
4447
func LockStoreKey(addr sdk.AccAddress, keyName string) []byte {
4548
return append(LocksStoreKey(addr), []byte(keyName)...)
4649
}
4750

51+
// LocksByAmountIndexKey returns the key to retrieve all locks of an address ordering by locked amount from the store.
4852
func LocksByAmountIndexKey(addr sdk.AccAddress) []byte {
4953
return append(LocksByAmountIndexKeyPrefix, address.MustLengthPrefix(addr)...)
5054
}
5155

56+
// LockByAmountIndexKey returns the key to retrieve a lock by amount from the store.
5257
func LockByAmountIndexKey(lock Lock) []byte {
5358
address := sdk.MustAccAddressFromBech32(lock.LockerAddress)
5459

0 commit comments

Comments
 (0)