Skip to content

Commit

Permalink
chore: add logs in hook handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
ironman0x7b2 committed Dec 3, 2024
1 parent 012b6bf commit 000045f
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 22 deletions.
6 changes: 6 additions & 0 deletions x/lease/client/cli/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cli

const (
flagDenom = "denom"
flagRenewable = "renewable"
)
34 changes: 24 additions & 10 deletions x/lease/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func txEndLease() *cobra.Command {

func txRenewLease() *cobra.Command {
cmd := &cobra.Command{
Use: "renew-lease [id] [hours] [denom]",
Use: "renew-lease [id] [hours]",
Short: "Renew an existing lease for a specified duration",
Args: cobra.ExactArgs(3),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -66,11 +66,16 @@ func txRenewLease() *cobra.Command {
return err
}

denom, err := cmd.Flags().GetString(flagDenom)
if err != nil {
return err
}

msg := v1.NewMsgRenewLeaseRequest(
ctx.FromAddress.Bytes(),
id,
hours,
args[2],
denom,
)
if err = msg.ValidateBasic(); err != nil {
return err
Expand All @@ -81,15 +86,16 @@ func txRenewLease() *cobra.Command {
}

flags.AddTxFlagsToCmd(cmd)
cmd.Flags().String(flagDenom, "", "Specify the payment denomination for the lease")

return cmd
}

func txStartLease() *cobra.Command {
cmd := &cobra.Command{
Use: "start-lease [node-addr] [hours] [denom] [renewable]",
Use: "start-lease [node-addr] [hours]",
Short: "Start a lease with a node for the specified duration",
Args: cobra.ExactArgs(4),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -106,7 +112,12 @@ func txStartLease() *cobra.Command {
return err
}

renewable, err := strconv.ParseBool(args[3])
denom, err := cmd.Flags().GetString(flagDenom)
if err != nil {
return err
}

renewable, err := cmd.Flags().GetBool(flagRenewable)
if err != nil {
return err
}
Expand All @@ -115,7 +126,7 @@ func txStartLease() *cobra.Command {
ctx.FromAddress.Bytes(),
nodeAddr,
hours,
args[2],
denom,
renewable,
)
if err = msg.ValidateBasic(); err != nil {
Expand All @@ -127,15 +138,17 @@ func txStartLease() *cobra.Command {
}

flags.AddTxFlagsToCmd(cmd)
cmd.Flags().String(flagDenom, "", "Specify the payment denomination for the lease")
cmd.Flags().Bool(flagRenewable, false, "Specify if the lease is renewable")

return cmd
}

func txUpdateLease() *cobra.Command {
cmd := &cobra.Command{
Use: "update-lease [id] [renewable]",
Use: "update-lease [id]",
Short: "Update the details of an existing lease",
Args: cobra.ExactArgs(2),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := client.GetClientTxContext(cmd)
if err != nil {
Expand All @@ -147,7 +160,7 @@ func txUpdateLease() *cobra.Command {
return err
}

renewable, err := strconv.ParseBool(args[1])
renewable, err := cmd.Flags().GetBool(flagRenewable)
if err != nil {
return err
}
Expand All @@ -166,6 +179,7 @@ func txUpdateLease() *cobra.Command {
}

flags.AddTxFlagsToCmd(cmd)
cmd.Flags().Bool(flagRenewable, false, "Specify if the lease is renewable")

return cmd
}
4 changes: 4 additions & 0 deletions x/lease/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

// NodeInactivePreHook is triggered when a node becomes inactive. It ends all leases associated with the specified node address.
func (k *Keeper) NodeInactivePreHook(ctx sdk.Context, addr base.NodeAddress) error {
k.Logger(ctx).Info("Running node inactive pre-hook", "address", addr.String())

// Iterate through all leases associated with the given node address
return k.IterateLeasesForNode(ctx, addr, func(_ int, item v1.Lease) (bool, error) {
// Create a message to end the lease
Expand Down Expand Up @@ -39,6 +41,8 @@ func (k *Keeper) NodeInactivePreHook(ctx sdk.Context, addr base.NodeAddress) err

// ProviderInactivePreHook is triggered when a provider becomes inactive. It ends all leases associated with the specified provider address.
func (k *Keeper) ProviderInactivePreHook(ctx sdk.Context, addr base.ProvAddress) error {
k.Logger(ctx).Info("Running provider inactive pre-hook", "address", addr.String())

// Iterate through all leases associated with the given provider address
return k.IterateLeasesForProvider(ctx, addr, func(_ int, item v1.Lease) (bool, error) {
// Create a message to end the lease
Expand Down
7 changes: 3 additions & 4 deletions x/lease/services/v1/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/sentinel-official/hub/v12/x/lease/keeper"
"github.com/sentinel-official/hub/v12/x/lease/types/v1"
Expand All @@ -28,8 +26,9 @@ func (m *msgServer) MsgEndLease(c context.Context, req *v1.MsgEndLeaseRequest) (
return m.HandleMsgEndLease(ctx, req)
}

func (m *msgServer) MsgRenewLease(_ context.Context, _ *v1.MsgRenewLeaseRequest) (*v1.MsgRenewLeaseResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
func (m *msgServer) MsgRenewLease(c context.Context, req *v1.MsgRenewLeaseRequest) (*v1.MsgRenewLeaseResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
return m.HandleMsgRenewLease(ctx, req)
}

func (m *msgServer) MsgStartLease(c context.Context, req *v1.MsgStartLeaseRequest) (*v1.MsgStartLeaseResponse, error) {
Expand Down
2 changes: 2 additions & 0 deletions x/node/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

// SessionInactivePreHook handles operations when a session transitions to the inactive state.
func (k *Keeper) SessionInactivePreHook(ctx sdk.Context, id uint64) error {
k.Logger(ctx).Info("Running session inactive pre-hook", "id", id)

// Retrieve the session by ID and return an error if not found.
item, found := k.GetSession(ctx, id)
if !found {
Expand Down
8 changes: 4 additions & 4 deletions x/oracle/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ func (k *Keeper) BeginBlock(ctx sdk.Context) {

portID := k.GetPortID(ctx)
if portID == "" {
ctx.Logger().Info("PortID is empty, skipping BeginBlock execution")
k.Logger(ctx).Info("PortID is empty, skipping BeginBlock execution")
return
}

channelID := k.GetChannelID(ctx)
if channelID == "" {
ctx.Logger().Info("ChannelID is empty, skipping BeginBlock execution")
k.Logger(ctx).Info("ChannelID is empty, skipping BeginBlock execution")
return
}

Expand All @@ -33,7 +33,7 @@ func (k *Keeper) BeginBlock(ctx sdk.Context) {
// Get the channel capability to ensure we have the authority to send packets.
channelCap, found := k.capability.GetCapability(ctx, ibchost.ChannelCapabilityPath(portID, channelID))
if !found {
ctx.Logger().Info("Channel capability not found, skipping BeginBlock execution")
k.Logger(ctx).Info("Channel capability not found, skipping BeginBlock execution")
return
}

Expand All @@ -53,7 +53,7 @@ func (k *Keeper) BeginBlock(ctx sdk.Context) {
// Send the GetProtoRevPool query packet over IBC.
sequence, err := k.SendQueryPacket(ctx, channelCap, portID, channelID, uint64(timeout), req)
if err != nil {
ctx.Logger().Error("Failed to send query packet", "asset", item.Denom, "error", err)
k.Logger(ctx).Error("Failed to send query packet", "asset", item.Denom, "error", err)
return false
}

Expand Down
7 changes: 7 additions & 0 deletions x/oracle/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package keeper

import (
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
ibcporttypes "github.com/cosmos/ibc-go/v7/modules/core/05-port/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"

"github.com/sentinel-official/hub/v12/x/oracle/types"
)

type Keeper struct {
Expand Down Expand Up @@ -34,6 +37,10 @@ func (k *Keeper) GetAuthority() string {
return k.authority
}

func (k *Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", "x/"+types.ModuleName)
}

func (k *Keeper) Store(ctx sdk.Context) sdk.KVStore {
return ctx.KVStore(k.key)
}
4 changes: 4 additions & 0 deletions x/plan/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

// LeaseInactivePreHook handles the necessary operations when a lease becomes inactive.
func (k *Keeper) LeaseInactivePreHook(ctx sdk.Context, id uint64) error {
k.Logger(ctx).Info("Running lease inactive pre-hook", "id", id)

// Retrieve the lease by ID and check if it exists, return an error if not found.
lease, found := k.GetLease(ctx, id)
if !found {
Expand Down Expand Up @@ -58,6 +60,8 @@ func (k *Keeper) LeaseInactivePreHook(ctx sdk.Context, id uint64) error {

// ProviderInactivePreHook handles the necessary operations when a provider becomes inactive.
func (k *Keeper) ProviderInactivePreHook(ctx sdk.Context, addr base.ProvAddress) error {
k.Logger(ctx).Info("Running provider inactive pre-hook", "address", addr.String())

// Iterate through all plans associated with the given provider address.
return k.IteratePlansForProvider(ctx, addr, func(_ int, item v3.Plan) (bool, error) {
// Check if the plan status is active; if not, skip to the next plan.
Expand Down
6 changes: 6 additions & 0 deletions x/session/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

// NodeInactivePreHook handles the necessary operations when a node becomes inactive.
func (k *Keeper) NodeInactivePreHook(ctx sdk.Context, addr base.NodeAddress) error {
k.Logger(ctx).Info("Running node inactive pre-hook", "address", addr.String())

// Iterate through all active sessions associated with the given node address.
return k.IterateSessionsForNode(ctx, addr, func(_ int, item v3.Session) (bool, error) {
// Skip the session if it is not active.
Expand Down Expand Up @@ -45,6 +47,8 @@ func (k *Keeper) NodeInactivePreHook(ctx sdk.Context, addr base.NodeAddress) err

// SubscriptionInactivePendingPreHook handles the necessary operations when a subscription becomes inactive pending.
func (k *Keeper) SubscriptionInactivePendingPreHook(ctx sdk.Context, id uint64) error {
k.Logger(ctx).Info("Running subscription inactive pending pre-hook", "id", id)

// Iterate through all active sessions associated with the given subscription ID.
return k.IterateSessionsForSubscription(ctx, id, func(_ int, item v3.Session) (bool, error) {
// Skip the session if it is not active.
Expand Down Expand Up @@ -78,6 +82,8 @@ func (k *Keeper) SubscriptionInactivePendingPreHook(ctx sdk.Context, id uint64)

// PlanUnlinkNodePreHook handles the necessary operations when unlinking a node from a plan.
func (k *Keeper) PlanUnlinkNodePreHook(ctx sdk.Context, id uint64, addr base.NodeAddress) error {
k.Logger(ctx).Info("Running plan unlink node pre-hook", "id", id, "address", addr.String())

// Iterate through all active sessions associated with the given plan ID and node address.
return k.IterateSessionsForPlanByNode(ctx, id, addr, func(_ int, item v3.Session) (bool, error) {
// Skip the session if it is not active.
Expand Down
2 changes: 2 additions & 0 deletions x/subscription/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

// SessionInactivePreHook handles the necessary operations when a session becomes inactive.
func (k *Keeper) SessionInactivePreHook(ctx sdk.Context, id uint64) error {
k.Logger(ctx).Info("Running session inactive pre-hook", "id", id)

// Retrieve the session by ID; return an error if not found.
item, found := k.GetSession(ctx, id)
if !found {
Expand Down
7 changes: 3 additions & 4 deletions x/subscription/services/v3/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/sentinel-official/hub/v12/x/subscription/keeper"
"github.com/sentinel-official/hub/v12/x/subscription/types/v3"
Expand All @@ -28,8 +26,9 @@ func (m *msgServer) MsgCancelSubscription(c context.Context, req *v3.MsgCancelSu
return m.HandleMsgCancelSubscription(ctx, req)
}

func (m *msgServer) MsgRenewSubscription(_ context.Context, _ *v3.MsgRenewSubscriptionRequest) (*v3.MsgRenewSubscriptionResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
func (m *msgServer) MsgRenewSubscription(c context.Context, req *v3.MsgRenewSubscriptionRequest) (*v3.MsgRenewSubscriptionResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
return m.HandleMsgRenewSubscription(ctx, req)
}

func (m *msgServer) MsgShareSubscription(c context.Context, req *v3.MsgShareSubscriptionRequest) (*v3.MsgShareSubscriptionResponse, error) {
Expand Down

0 comments on commit 000045f

Please sign in to comment.