Skip to content

Commit

Permalink
Migrate more logs to new logging lib, add lib.UnwrapSDKContext (#1003)
Browse files Browse the repository at this point in the history
* migrate memclob file logs

* more error log migration

* migrate all k.Logger() to new logger lib

* Introduce lib.UnwrapSDKContext that adds log tags

* add the epoch event back, removed accidentally

* fix delay message tests

* lint
  • Loading branch information
jonfung-dydx committed Jan 23, 2024
1 parent 0f87843 commit 5ee11ed
Show file tree
Hide file tree
Showing 52 changed files with 364 additions and 223 deletions.
23 changes: 23 additions & 0 deletions protocol/lib/context.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
package lib

import (
"context"
"fmt"

"github.com/cometbft/cometbft/crypto/tmhash"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/lib/log"
)

type TxHash string

func GetTxHash(tx []byte) TxHash {
return TxHash(fmt.Sprintf("%X", tmhash.Sum(tx)))
}

// UnwrapSDKContext is a thin wrapper around cosmos sdk's unwrap function
// that extracts the cosmos context from the standard golang context.
// If moduleName is provided, it appends the persistent log tag with
// the module name to the logger in the context.
func UnwrapSDKContext(
goCtx context.Context,
moduleName string,
) sdk.Context {
ctx := sdk.UnwrapSDKContext(goCtx)
if moduleName != "" {
ctx = log.AddPersistentTagsToLogger(
ctx,
log.Module,
fmt.Sprintf("x/%s", moduleName),
)
}
return ctx
}
5 changes: 5 additions & 0 deletions protocol/lib/log/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ const (
// Do not have anything generic in here. For example, `Status` is too vague
// and can be clarified as `OrderStatus` or `DaemonHealthStatus`.
const (
Address = "address"
Module = "module"
TxMode = "tx_mode"
Operation = "operation"
OperationsQueue = "operations_queue"
Callback = "callback"
BlockHeight = "block_height"
Msg = "msg"
ProposerConsAddress = "proposer_cons_address"
Handler = "handler"
Tx = "tx"
Order = "order"
OrderId = "order_id"
OrderHash = "order_hash"
OrderStatus = "order_status"
Subaccount = "subaccount"
Expand All @@ -26,6 +30,7 @@ const (
StackTrace = "stack_trace"
Proposer = "proposer"
PrunableBlockHeight = "prunable_block_height"
StatusCode = "status_code"

OrderSizeOptimisticallyFilledFromMatchingQuantums = "order_size_optimistically_filled_from_matching_quantums"
NewLocalValidatorOperationsQueue = "new_local_validator_operations_queue"
Expand Down
4 changes: 1 addition & 3 deletions protocol/x/assets/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package types

import (
"github.com/dydxprotocol/v4-chain/protocol/lib"
)
import "github.com/dydxprotocol/v4-chain/protocol/lib"

const (
// UusdcDenom is the precomputed denom for IBC Micro USDC.
Expand Down
12 changes: 8 additions & 4 deletions protocol/x/clob/keeper/final_settlement.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
indexerevents "github.com/dydxprotocol/v4-chain/protocol/indexer/events"
"github.com/dydxprotocol/v4-chain/protocol/indexer/indexer_manager"
indexershared "github.com/dydxprotocol/v4-chain/protocol/indexer/shared"
"github.com/dydxprotocol/v4-chain/protocol/lib/log"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
)

Expand All @@ -29,11 +30,14 @@ func (k Keeper) mustCancelStatefulOrdersForFinalSettlement(ctx sdk.Context, clob
// rather than halt the chain.
removeStatefulOrderWithoutPanicing := func(ctx sdk.Context, orderId types.OrderId) {
defer func() {
if r := recover(); r != nil {
k.Logger(ctx).Error(
"mustCancelStatefulOrdersForFinalSettlement: Failed to remove stateful order with OrderId %+v: %v",
if err := recover(); err != nil {
log.ErrorLog(
ctx,
"mustCancelStatefulOrdersForFinalSettlement: Failed to remove stateful order",
"orderId",
orderId,
r,
"error",
err,
)
}
}()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package keeper

import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -16,7 +17,7 @@ func (k Keeper) BlockRateLimitConfiguration(
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(c)
ctx := lib.UnwrapSDKContext(c, types.ModuleName)
blockRateLimitConfig := k.GetBlockRateLimitConfiguration(ctx)

return &types.QueryBlockRateLimitConfigurationResponse{
Expand Down
6 changes: 3 additions & 3 deletions protocol/x/clob/keeper/grpc_query_clob_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"

"cosmossdk.io/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -20,7 +20,7 @@ func (k Keeper) ClobPairAll(
}

var clobPairs []types.ClobPair
ctx := sdk.UnwrapSDKContext(c)
ctx := lib.UnwrapSDKContext(c, types.ModuleName)

store := ctx.KVStore(k.storeKey)
clobPairStore := prefix.NewStore(store, []byte(types.ClobPairKeyPrefix))
Expand All @@ -46,7 +46,7 @@ func (k Keeper) ClobPair(c context.Context, req *types.QueryGetClobPairRequest)
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)
ctx := lib.UnwrapSDKContext(c, types.ModuleName)

val, found := k.GetClobPair(
ctx,
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/clob/keeper/grpc_query_equity_tier_limit_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"google.golang.org/grpc/status"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
"google.golang.org/grpc/codes"
)
Expand All @@ -17,7 +17,7 @@ func (k Keeper) EquityTierLimitConfiguration(
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)
ctx := lib.UnwrapSDKContext(c, types.ModuleName)

return &types.QueryEquityTierLimitConfigurationResponse{
EquityTierLimitConfig: k.GetEquityTierLimitConfiguration(ctx),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package keeper

import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand All @@ -17,7 +18,7 @@ func (k Keeper) LiquidationsConfiguration(
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(c)
ctx := lib.UnwrapSDKContext(c, types.ModuleName)
liquidationsConfig := k.GetLiquidationsConfig(ctx)
return &types.QueryLiquidationsConfigurationResponse{
LiquidationsConfig: liquidationsConfig,
Expand Down
3 changes: 2 additions & 1 deletion protocol/x/clob/keeper/grpc_query_mev_node_to_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/lib/log"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
"google.golang.org/grpc/codes"
Expand All @@ -18,7 +19,7 @@ func (k Keeper) MevNodeToNodeCalculation(
*types.MevNodeToNodeCalculationResponse,
error,
) {
ctx := sdk.UnwrapSDKContext(c)
ctx := lib.UnwrapSDKContext(c, types.ModuleName)

// Validate that the request is valid.
if err := validateMevNodeToNodeRequest(req); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions protocol/x/clob/keeper/mev.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func (k Keeper) RecordMevMetrics(
perpetualKeeper process.ProcessPerpetualKeeper,
msgProposedOperations *types.MsgProposedOperations,
) {
ctx = log.AddPersistentTagsToLogger(
ctx,
log.Module,
"x/clob/mev_telemetry",
)
defer metrics.ModuleMeasureSince(
types.ModuleName,
metrics.MevLatency,
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/keeper/msg_server_cancel_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (k msgServer) CancelOrder(
goCtx context.Context,
msg *types.MsgCancelOrder,
) (resp *types.MsgCancelOrderResponse, err error) {
ctx := sdk.UnwrapSDKContext(goCtx)
ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName)

// Attach various logging tags relative to this request. These should be static with no changes.
ctx = log.AddPersistentTagsToLogger(ctx,
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/clob/keeper/msg_server_create_clob_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
)
Expand All @@ -16,7 +16,7 @@ func (k msgServer) CreateClobPair(
goCtx context.Context,
msg *types.MsgCreateClobPair,
) (resp *types.MsgCreateClobPairResponse, err error) {
ctx := sdk.UnwrapSDKContext(goCtx)
ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName)

if !k.Keeper.HasAuthority(msg.Authority) {
return nil, errorsmod.Wrapf(
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/keeper/msg_server_place_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (k msgServer) PlaceOrder(goCtx context.Context, msg *types.MsgPlaceOrder) (
resp *types.MsgPlaceOrderResponse,
err error,
) {
ctx := sdk.UnwrapSDKContext(goCtx)
ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName)

// Attach various logging tags relative to this request. These should be static with no changes.
ctx = log.AddPersistentTagsToLogger(ctx,
Expand Down
2 changes: 1 addition & 1 deletion protocol/x/clob/keeper/msg_server_proposed_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (k msgServer) ProposedOperations(
goCtx context.Context,
msg *types.MsgProposedOperations,
) (resp *types.MsgProposedOperationsResponse, err error) {
ctx := sdk.UnwrapSDKContext(goCtx)
ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName)

// Attach various logging tags relative to this request. These should be static with no changes.
ctx = log.AddPersistentTagsToLogger(ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
)

Expand All @@ -15,7 +15,7 @@ func (k msgServer) UpdateBlockRateLimitConfiguration(
goCtx context.Context,
msg *types.MsgUpdateBlockRateLimitConfiguration,
) (resp *types.MsgUpdateBlockRateLimitConfigurationResponse, err error) {
ctx := sdk.UnwrapSDKContext(goCtx)
ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName)

if !k.Keeper.HasAuthority(msg.Authority) {
return nil, errorsmod.Wrapf(
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/clob/keeper/msg_server_update_clob_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import (

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
)

func (k msgServer) UpdateClobPair(
goCtx context.Context,
msg *types.MsgUpdateClobPair,
) (resp *types.MsgUpdateClobPairResponse, err error) {
ctx := sdk.UnwrapSDKContext(goCtx)
ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName)

if !k.Keeper.HasAuthority(msg.Authority) {
return nil, errorsmod.Wrapf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
)

Expand All @@ -15,7 +15,7 @@ func (k msgServer) UpdateEquityTierLimitConfiguration(
goCtx context.Context,
msg *types.MsgUpdateEquityTierLimitConfiguration,
) (resp *types.MsgUpdateEquityTierLimitConfigurationResponse, err error) {
ctx := sdk.UnwrapSDKContext(goCtx)
ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName)

if !k.Keeper.HasAuthority(msg.Authority) {
return nil, errorsmod.Wrapf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
)

Expand All @@ -15,7 +15,7 @@ func (k msgServer) UpdateLiquidationsConfig(
goCtx context.Context,
msg *types.MsgUpdateLiquidationsConfig,
) (resp *types.MsgUpdateLiquidationsConfigResponse, err error) {
ctx := sdk.UnwrapSDKContext(goCtx)
ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName)

if !k.Keeper.HasAuthority(msg.Authority) {
return nil, errorsmod.Wrapf(
Expand Down
Loading

0 comments on commit 5ee11ed

Please sign in to comment.