Skip to content

Commit

Permalink
update all references using ctx to context in the keeper and abci. So…
Browse files Browse the repository at this point in the history
…me calls were passing ctx when it should have been context. Adding more logs for the unbinding entries when it udpates the store.
  • Loading branch information
dekm committed Mar 19, 2024
1 parent 7a10aa9 commit 75c0694
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
27 changes: 14 additions & 13 deletions x/gridnode/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -70,7 +71,7 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
}

// DelegateTokens locks the tokens for gridnode delegation
func (k Keeper) DelegateTokens(ctx sdk.Context, delegator sdk.AccAddress, amount sdkmath.Int) error {
func (k Keeper) DelegateTokens(ctx context.Context, delegator sdk.AccAddress, amount sdkmath.Int) error {
// Retrieve the available balance of the delegator account
availableBalance := k.bankKeeper.GetBalance(ctx, delegator, "uugd")
fmt.Println("availableBalance: ", availableBalance)
Expand Down Expand Up @@ -116,9 +117,9 @@ func (k Keeper) DelegateTokens(ctx sdk.Context, delegator sdk.AccAddress, amount
lockedBalance = lockedBalance.Add(amount)
fmt.Println("Locked balance after adding: ", lockedBalance) // Log the locked balance after adding the new amount
k.SetLockedBalance(ctx, delegator, lockedBalance, pubKeyHex)

uwCtx := sdk.UnwrapSDKContext(ctx)
// Emitting events
ctx.EventManager().EmitEvent(sdk.NewEvent(
uwCtx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeDelegate,
sdk.NewAttribute(types.AttributeKeyDelegator, delegator.String()),
sdk.NewAttribute(types.AttributeKeyAmount, amount.String()),
Expand All @@ -128,7 +129,7 @@ func (k Keeper) DelegateTokens(ctx sdk.Context, delegator sdk.AccAddress, amount
}

// UndelegateTokens unlocks the tokens from gridnode delegation
func (k Keeper) UndelegateTokens(ctx sdk.Context, account sdk.AccAddress, amount sdkmath.Int) error {
func (k Keeper) UndelegateTokens(ctx context.Context, account sdk.AccAddress, amount sdkmath.Int) error {
// ... similar logic to release the tokens
fmt.Println("UndelegateTokens: ", account, amount)
// Retrieve the current unbonding entries for the account
Expand Down Expand Up @@ -156,9 +157,9 @@ func (k Keeper) UndelegateTokens(ctx sdk.Context, account sdk.AccAddress, amount
if totalUnbonding.GT(delegatedAmount) {
return errors.Wrapf(types.ErrOverUnbond, "attempting to unbond more than the delegated amount")
}

uwCtx := sdk.UnwrapSDKContext(ctx)
// Retrieve current block time
blockTime := ctx.BlockTime()
blockTime := uwCtx.BlockTime()

// Define the unbonding period, 21 days TODO: enable this for mainnet
//unbondingPeriod := time.Hour * 24 * 21
Expand All @@ -182,7 +183,7 @@ func (k Keeper) UndelegateTokens(ctx sdk.Context, account sdk.AccAddress, amount
}

// Emit an event or log the unbonding
ctx.EventManager().EmitEvent(
uwCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeUnbond,
sdk.NewAttribute(types.AttributeKeyDelegator, account.String()),
Expand All @@ -206,7 +207,7 @@ func (k Keeper) GetStoreService() store.KVStoreService {
return k.storeService
}

func (k Keeper) GetLockedBalance(ctx sdk.Context, delegator sdk.AccAddress) sdkmath.Int {
func (k Keeper) GetLockedBalance(ctx context.Context, delegator sdk.AccAddress) sdkmath.Int {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, []byte(types.StoreKey))
key := k.keyForDelegator(delegator)
Expand All @@ -228,7 +229,7 @@ func (k Keeper) GetLockedBalance(ctx sdk.Context, delegator sdk.AccAddress) sdkm
return delegationData.LockedBalance
}

func (k Keeper) QueryAllDelegations(ctx sdk.Context) ([]types.DelegationInfo, error) {
func (k Keeper) QueryAllDelegations(ctx context.Context) ([]types.DelegationInfo, error) {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, []byte(types.StoreKey))

Expand Down Expand Up @@ -332,7 +333,7 @@ func (k Keeper) QueryAllDelegations(ctx sdk.Context) ([]types.DelegationInfo, er
return delegations, nil
}

func (k Keeper) SetLockedBalance(ctx sdk.Context, delegator sdk.AccAddress, amount sdkmath.Int, pubKey string) {
func (k Keeper) SetLockedBalance(ctx context.Context, delegator sdk.AccAddress, amount sdkmath.Int, pubKey string) {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, []byte(types.StoreKey))
key := k.keyForDelegator(delegator)
Expand Down Expand Up @@ -367,7 +368,7 @@ func (k Keeper) keyForUnBonding(delegator sdk.AccAddress) []byte {
return []byte(fmt.Sprintf("%s%s", bondingPrefix, delegator.String()))
}

func (k Keeper) GetDelegatedAmount(ctx sdk.Context, delegator sdk.AccAddress) sdkmath.Int {
func (k Keeper) GetDelegatedAmount(ctx context.Context, delegator sdk.AccAddress) sdkmath.Int {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, []byte(types.StoreKey))
byteValue := store.Get(k.keyForDelegator(delegator))
Expand All @@ -386,7 +387,7 @@ func (k Keeper) GetDelegatedAmount(ctx sdk.Context, delegator sdk.AccAddress) sd
return delegationData.LockedBalance
}

func (k Keeper) SetDelegatedAmount(ctx sdk.Context, delegator sdk.AccAddress, amount sdkmath.Int) {
func (k Keeper) SetDelegatedAmount(ctx context.Context, delegator sdk.AccAddress, amount sdkmath.Int) {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, []byte(types.StoreKey))
if amount.IsNegative() {
Expand All @@ -398,7 +399,7 @@ func (k Keeper) SetDelegatedAmount(ctx sdk.Context, delegator sdk.AccAddress, am
}

// AddUnbondingEntry adds a new unbonding entry for a given account.
func (k Keeper) AddUnbondingEntry(ctx sdk.Context, entry types.UnbondingEntry) error {
func (k Keeper) AddUnbondingEntry(ctx context.Context, entry types.UnbondingEntry) error {
storeAdapter := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
store := prefix.NewStore(storeAdapter, []byte(types.StoreKey))
delegatorAddr, err := sdk.AccAddressFromBech32(entry.Account)
Expand Down
14 changes: 9 additions & 5 deletions x/gridnode/module/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func BeginBlocker(goCtx context.Context, k keeper.Keeper) {
//fmt.Println("BeginBlocker started. Current block time:", currentTime)
//fmt.Println("CTX BlockHeight:", ctx.BlockHeight())
// Iterate over all unbonding entries
storeAdapter := runtime.KVStoreAdapter(k.GetStoreService().OpenKVStore(ctx))
storeAdapter := runtime.KVStoreAdapter(k.GetStoreService().OpenKVStore(goCtx))
store := prefix.NewStore(storeAdapter, []byte(types.StoreKey))

iterator := storetypes.KVStorePrefixIterator(store, []byte(k.GetBondingPrefix()))
Expand All @@ -53,7 +53,7 @@ func BeginBlocker(goCtx context.Context, k keeper.Keeper) {
timestamp := time.Unix(entry.CompletionTime, 0)

if currentTime.After(timestamp) {
//fmt.Printf("Processing unbonding for delegator: %s, amount: %s\n", entry.Account, entry.Amount)
fmt.Printf("Processing unbonding for delegator: %s, amount: %d\n", entry.Account, entry.Amount)
bankKeeper := k.GetBankKeeper()
// Process the unbonding
delegatorAddr, err := sdk.AccAddressFromBech32(entry.Account)
Expand All @@ -63,15 +63,15 @@ func BeginBlocker(goCtx context.Context, k keeper.Keeper) {
}
amount := math.NewInt(entry.Amount)
coin := sdk.NewCoin("uugd", amount)
snd := bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, delegatorAddr, sdk.NewCoins(coin))
snd := bankKeeper.SendCoinsFromModuleToAccount(goCtx, types.ModuleName, delegatorAddr, sdk.NewCoins(coin))
if snd != nil {
fmt.Println("Error sending coins from module to account:", err)
continue
}
// Reduce the delegated amount from the store
currentDelegatedAmount := k.GetDelegatedAmount(ctx, delegatorAddr)
currentDelegatedAmount := k.GetDelegatedAmount(goCtx, delegatorAddr)
newDelegatedAmount := currentDelegatedAmount.Sub(amount)
k.SetDelegatedAmount(ctx, delegatorAddr, newDelegatedAmount)
k.SetDelegatedAmount(goCtx, delegatorAddr, newDelegatedAmount)

// Placeholder to call hedgehog
fmt.Printf("Placeholder: Notify hedgehog that account %s is unbonding %d tokens.\n", entry.Account, entry.Amount)
Expand Down Expand Up @@ -100,6 +100,10 @@ func BeginBlocker(goCtx context.Context, k keeper.Keeper) {
fmt.Printf("Error marshalling new entries for key %x: %v\n", key, err)
continue
}

// Log the data that is about to be stored
fmt.Printf("Updating store for key %x with data: %s\n", key, string(newBz))

store.Set(key, newBz)
fmt.Printf("Updated store for key %x\n", key)
}
Expand Down

0 comments on commit 75c0694

Please sign in to comment.