From d10eeec8bd004c19ebc6491a0e82b45b06328b57 Mon Sep 17 00:00:00 2001 From: Daniel Wedul Date: Tue, 3 Oct 2023 16:30:53 -0600 Subject: [PATCH] [1658]: Create emitEvent and emitEvents that just logs any errors instead of returning them. --- x/exchange/keeper/fulfillment.go | 9 ++++++--- x/exchange/keeper/grpc_query.go | 5 +---- x/exchange/keeper/keeper.go | 20 ++++++++++++++++++++ x/exchange/keeper/market.go | 24 ++++++++++++++++-------- x/exchange/keeper/msg_server.go | 16 ++++------------ x/exchange/keeper/orders.go | 9 ++++++--- 6 files changed, 53 insertions(+), 30 deletions(-) diff --git a/x/exchange/keeper/fulfillment.go b/x/exchange/keeper/fulfillment.go index 8581175770..ed9f8f94fb 100644 --- a/x/exchange/keeper/fulfillment.go +++ b/x/exchange/keeper/fulfillment.go @@ -154,7 +154,8 @@ func (k Keeper) FillBids(ctx sdk.Context, msg *exchange.MsgFillBidsRequest) erro events[i] = exchange.NewEventOrderFilled(order.OrderId) } - return ctx.EventManager().EmitTypedEvents(events...) + k.emitEvents(ctx, events) + return nil } // FillAsks settles one or more ask orders for a buyer. @@ -281,7 +282,8 @@ func (k Keeper) FillAsks(ctx sdk.Context, msg *exchange.MsgFillAsksRequest) erro events[i] = exchange.NewEventOrderFilled(order.OrderId) } - return ctx.EventManager().EmitTypedEvents(events...) + k.emitEvents(ctx, events) + return nil } // SettleOrders attempts to settle all the provided orders. @@ -411,5 +413,6 @@ func (k Keeper) SettleOrders(ctx sdk.Context, marketID uint32, askOrderIDs, bidO )) } - return ctx.EventManager().EmitTypedEvents(events...) + k.emitEvents(ctx, events) + return nil } diff --git a/x/exchange/keeper/grpc_query.go b/x/exchange/keeper/grpc_query.go index 7f8b4edad8..d91d1cf4b8 100644 --- a/x/exchange/keeper/grpc_query.go +++ b/x/exchange/keeper/grpc_query.go @@ -381,10 +381,7 @@ func (k QueryServer) QueryValidateManageFees(goCtx context.Context, req *exchang buyerRatios, msg.AddFeeBuyerSettlementRatios, msg.RemoveFeeBuyerSettlementRatios)...) } - if err := k.UpdateFees(ctx, msg); err != nil { - // The only error this might be would be about event emission. - errs = append(errs, err) - } + k.UpdateFees(ctx, msg) if err := k.ValidateMarket(ctx, msg.MarketId); err != nil { errs = append(errs, err) } diff --git a/x/exchange/keeper/keeper.go b/x/exchange/keeper/keeper.go index b412f48f1b..5ed8cd3e53 100644 --- a/x/exchange/keeper/keeper.go +++ b/x/exchange/keeper/keeper.go @@ -3,6 +3,8 @@ package keeper import ( "fmt" + "github.com/gogo/protobuf/proto" + sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/codec" @@ -53,6 +55,24 @@ func (k Keeper) logErrorf(ctx sdk.Context, msg string, args ...interface{}) { ctx.Logger().Error(fmt.Sprintf(msg, args...), "module", "x/"+exchange.ModuleName) } +// emitEvent emits the provided event and writes any error to the error log. +// See Also emitEvents. +func (k Keeper) emitEvent(ctx sdk.Context, event proto.Message) { + err := ctx.EventManager().EmitTypedEvent(event) + if err != nil { + k.logErrorf(ctx, "error emitting event %#v: %v", event, err) + } +} + +// emitEvents emits the provided events and writes any error to the error log. +// See Also emitEvent. +func (k Keeper) emitEvents(ctx sdk.Context, events []proto.Message) { + err := ctx.EventManager().EmitTypedEvents(events...) + if err != nil { + k.logErrorf(ctx, "error emitting events %#v: %v", events, err) + } +} + // wrongAuthErr returns the error to use when a message's authority isn't what's required. func (k Keeper) wrongAuthErr(badAuthority string) error { return govtypes.ErrInvalidSigner.Wrapf("expected %s got %s", k.GetAuthority(), badAuthority) diff --git a/x/exchange/keeper/market.go b/x/exchange/keeper/market.go index 9264ebbdc1..cf64aaacd0 100644 --- a/x/exchange/keeper/market.go +++ b/x/exchange/keeper/market.go @@ -664,7 +664,7 @@ func (k Keeper) ValidateBuyerSettlementFee(ctx sdk.Context, marketID uint32, pri } // UpdateFees updates all the fees as provided in the MsgGovManageFeesRequest. -func (k Keeper) UpdateFees(ctx sdk.Context, msg *exchange.MsgGovManageFeesRequest) error { +func (k Keeper) UpdateFees(ctx sdk.Context, msg *exchange.MsgGovManageFeesRequest) { store := k.getStore(ctx) updateCreateAskFlatFees(store, msg.MarketId, msg.RemoveFeeCreateAskFlat, msg.AddFeeCreateAskFlat) updateCreateBidFlatFees(store, msg.MarketId, msg.RemoveFeeCreateBidFlat, msg.AddFeeCreateBidFlat) @@ -672,7 +672,8 @@ func (k Keeper) UpdateFees(ctx sdk.Context, msg *exchange.MsgGovManageFeesReques updateSellerSettlementRatios(store, msg.MarketId, msg.RemoveFeeSellerSettlementRatios, msg.AddFeeSellerSettlementRatios) updateBuyerSettlementFlatFees(store, msg.MarketId, msg.RemoveFeeBuyerSettlementFlat, msg.AddFeeBuyerSettlementFlat) updateBuyerSettlementRatios(store, msg.MarketId, msg.RemoveFeeBuyerSettlementRatios, msg.AddFeeBuyerSettlementRatios) - return ctx.EventManager().EmitTypedEvent(exchange.NewEventMarketFeesUpdated(msg.MarketId)) + + k.emitEvent(ctx, exchange.NewEventMarketFeesUpdated(msg.MarketId)) } // isMarketActive returns true if the provided market is accepting orders. @@ -721,7 +722,8 @@ func (k Keeper) UpdateMarketActive(ctx sdk.Context, marketID uint32, active bool return fmt.Errorf("market %d already has accepting-orders %t", marketID, active) } setMarketActive(store, marketID, active) - return ctx.EventManager().EmitTypedEvent(exchange.NewEventMarketActiveUpdated(marketID, updatedBy, active)) + k.emitEvent(ctx, exchange.NewEventMarketActiveUpdated(marketID, updatedBy, active)) + return nil } // IsUserSettlementAllowed gets whether user-settlement is allowed for a market. @@ -738,7 +740,8 @@ func (k Keeper) UpdateUserSettlementAllowed(ctx sdk.Context, marketID uint32, al return fmt.Errorf("market %d already has allow-user-settlement %t", marketID, allow) } setUserSettlementAllowed(store, marketID, allow) - return ctx.EventManager().EmitTypedEvent(exchange.NewEventMarketUserSettleUpdated(marketID, updatedBy, allow)) + k.emitEvent(ctx, exchange.NewEventMarketUserSettleUpdated(marketID, updatedBy, allow)) + return nil } // storeHasPermission returns true if there is an entry in the store for the given market, address, and permissions. @@ -918,7 +921,8 @@ func (k Keeper) UpdatePermissions(ctx sdk.Context, msg *exchange.MsgMarketManage return errors.Join(errs...) } - return ctx.EventManager().EmitTypedEvent(exchange.NewEventMarketPermissionsUpdated(marketID, admin)) + k.emitEvent(ctx, exchange.NewEventMarketPermissionsUpdated(marketID, admin)) + return nil } // reqAttrKeyMaker is a function that returns a key for required attributes. @@ -1085,7 +1089,8 @@ func (k Keeper) UpdateReqAttrs(ctx sdk.Context, msg *exchange.MsgMarketManageReq return errors.Join(errs...) } - return ctx.EventManager().EmitTypedEvent(exchange.NewEventMarketReqAttrUpdated(marketID, admin)) + k.emitEvent(ctx, exchange.NewEventMarketReqAttrUpdated(marketID, admin)) + return nil } // getMarketAccountByAddr gets a market's account given its address. @@ -1135,8 +1140,9 @@ func (k Keeper) UpdateMarketDetails(ctx sdk.Context, marketID uint32, marketDeta marketAcc.MarketDetails = marketDetails k.accountKeeper.SetAccount(ctx, marketAcc) + k.emitEvent(ctx, exchange.NewEventMarketDetailsUpdated(marketID, updatedBy)) - return ctx.EventManager().EmitTypedEvent(exchange.NewEventMarketDetailsUpdated(marketID, updatedBy)) + return nil } // storeMarket writes all the market fields to the state store (except MarketDetails which are in the account). @@ -1215,6 +1221,7 @@ func (k Keeper) CreateMarket(ctx sdk.Context, market exchange.Market) (uint32, e k.accountKeeper.SetAccount(ctx, marketAcc) storeMarket(store, market) + k.emitEvent(ctx, exchange.NewEventMarketCreated(market.MarketId)) return market.MarketId, nil } @@ -1278,7 +1285,8 @@ func (k Keeper) WithdrawMarketFunds(ctx sdk.Context, marketID uint32, toAddr sdk if err != nil { return fmt.Errorf("failed to withdraw %s from market %d: %w", amount, marketID, err) } - return ctx.EventManager().EmitTypedEvent(exchange.NewEventMarketWithdraw(marketID, amount, toAddr, withdrawnBy)) + k.emitEvent(ctx, exchange.NewEventMarketWithdraw(marketID, amount, toAddr, withdrawnBy)) + return nil } // ValidateMarket checks the setup of the provided market, making sure there aren't any possibly problematic settings. diff --git a/x/exchange/keeper/msg_server.go b/x/exchange/keeper/msg_server.go index 0498cf21e8..e5c2a998a9 100644 --- a/x/exchange/keeper/msg_server.go +++ b/x/exchange/keeper/msg_server.go @@ -180,14 +180,11 @@ func (k MsgServer) GovCreateMarket(goCtx context.Context, msg *exchange.MsgGovCr } ctx := sdk.UnwrapSDKContext(goCtx) - marketID, err := k.CreateMarket(ctx, msg.Market) - if err != nil { - return nil, sdkerrors.ErrInvalidRequest.Wrap(err.Error()) - } - err = ctx.EventManager().EmitTypedEvent(exchange.NewEventMarketCreated(marketID)) + _, err := k.CreateMarket(ctx, msg.Market) if err != nil { return nil, sdkerrors.ErrInvalidRequest.Wrap(err.Error()) } + return &exchange.MsgGovCreateMarketResponse{}, nil } @@ -198,10 +195,7 @@ func (k MsgServer) GovManageFees(goCtx context.Context, msg *exchange.MsgGovMana } ctx := sdk.UnwrapSDKContext(goCtx) - err := k.UpdateFees(ctx, msg) - if err != nil { - return nil, sdkerrors.ErrInvalidRequest.Wrap(err.Error()) - } + k.UpdateFees(ctx, msg) return &exchange.MsgGovManageFeesResponse{}, nil } @@ -214,9 +208,7 @@ func (k MsgServer) GovUpdateParams(goCtx context.Context, msg *exchange.MsgGovUp ctx := sdk.UnwrapSDKContext(goCtx) k.SetParams(ctx, &msg.Params) + k.emitEvent(ctx, exchange.NewEventParamsUpdated()) - if err := ctx.EventManager().EmitTypedEvent(exchange.NewEventParamsUpdated()); err != nil { - return nil, sdkerrors.ErrInvalidRequest.Wrap(err.Error()) - } return &exchange.MsgGovUpdateParamsResponse{}, nil } diff --git a/x/exchange/keeper/orders.go b/x/exchange/keeper/orders.go index cce16155ab..3197ca8a8b 100644 --- a/x/exchange/keeper/orders.go +++ b/x/exchange/keeper/orders.go @@ -602,7 +602,8 @@ func (k Keeper) CreateAskOrder(ctx sdk.Context, askOrder exchange.AskOrder, crea return 0, err } - return orderID, ctx.EventManager().EmitTypedEvent(exchange.NewEventOrderCreated(order)) + k.emitEvent(ctx, exchange.NewEventOrderCreated(order)) + return orderID, nil } // CreateBidOrder creates a bid order, collects the creation fee, and places all needed holds. @@ -642,7 +643,8 @@ func (k Keeper) CreateBidOrder(ctx sdk.Context, bidOrder exchange.BidOrder, crea return 0, err } - return orderID, ctx.EventManager().EmitTypedEvent(exchange.NewEventOrderCreated(order)) + k.emitEvent(ctx, exchange.NewEventOrderCreated(order)) + return orderID, nil } // CancelOrder releases an order's held funds and deletes it. @@ -669,8 +671,9 @@ func (k Keeper) CancelOrder(ctx sdk.Context, orderID uint64, signer string) erro } deleteAndDeIndexOrder(k.getStore(ctx), *order) + k.emitEvent(ctx, exchange.NewEventOrderCancelled(orderID, signerAddr)) - return ctx.EventManager().EmitTypedEvent(exchange.NewEventOrderCancelled(orderID, signerAddr)) + return nil } // IterateOrders iterates over all orders. An error is returned if there was a problem