From 3d666cee2928a1b4478d33b2f40816ca00523032 Mon Sep 17 00:00:00 2001 From: Carlton N Hanna Date: Fri, 6 Oct 2023 13:21:36 -0600 Subject: [PATCH] more refactors --- app/app.go | 2 +- x/ibchooks/hooks.go | 6 +++--- x/ibchooks/ibc_hooks.go | 32 +++++++++++++++++--------------- x/ibchooks/ics4_middleware.go | 4 ++-- x/ibchooks/keeper/keeper.go | 6 +++--- x/ibchooks/types/types.go | 10 ++++++++++ x/ibchooks/wasm_hook.go | 13 ++++++------- 7 files changed, 42 insertions(+), 31 deletions(-) diff --git a/app/app.go b/app/app.go index 2484741872..a23352c81a 100644 --- a/app/app.go +++ b/app/app.go @@ -650,7 +650,7 @@ func New( app.IBCHooksKeeper.ContractKeeper = app.ContractKeeper app.Ics20MarkerHooks.MarkerKeeper = &app.MarkerKeeper - app.IbcHooks.PreSendPacketDataProcessingFns = []ibchookstypes.PreSendPacketDataProcessingFn{app.Ics20MarkerHooks.ProcessMarkerMemoFn, app.Ics20WasmHooks.GetPreSendPacketDataProcessingFns} + app.IbcHooks.SendPacketPreProcessors = []ibchookstypes.PreSendPacketDataProcessingFn{app.Ics20MarkerHooks.ProcessMarkerMemoFn, app.Ics20WasmHooks.GetWasmSendPacketPreProcessor} app.ScopedOracleKeeper = scopedOracleKeeper app.OracleKeeper = *oraclekeeper.NewKeeper( diff --git a/x/ibchooks/hooks.go b/x/ibchooks/hooks.go index 2a4d910d71..6a524a924d 100644 --- a/x/ibchooks/hooks.go +++ b/x/ibchooks/hooks.go @@ -12,9 +12,9 @@ import ( type Hooks interface{} -// GetPreSendPacketDataProcessingFns returns a list of ordered functions to be executed before ibc's SendPacket function in middleware -type GetPreSendPacketDataProcessingFns interface { - GetPreSendPacketDataProcessingFns() []types.PreSendPacketDataProcessingFn +// SendPacketPreProcessors returns a list of ordered functions to be executed before ibc's SendPacket function in middleware +type SendPacketPreProcessors interface { + GetSendPacketPreProcessors() []types.PreSendPacketDataProcessingFn } type OnChanOpenInitOverrideHooks interface { diff --git a/x/ibchooks/ibc_hooks.go b/x/ibchooks/ibc_hooks.go index ba64d24572..09a18d9553 100644 --- a/x/ibchooks/ibc_hooks.go +++ b/x/ibchooks/ibc_hooks.go @@ -14,22 +14,22 @@ import ( ) type IbcHooks struct { - cdc codec.BinaryCodec - ibcKeeper *ibckeeper.Keeper - ibcHooksKeeper *keeper.Keeper - wasmHooks *WasmHooks - markerHooks *MarkerHooks - PreSendPacketDataProcessingFns []types.PreSendPacketDataProcessingFn + cdc codec.BinaryCodec + ibcKeeper *ibckeeper.Keeper + ibcHooksKeeper *keeper.Keeper + wasmHooks *WasmHooks + markerHooks *MarkerHooks + SendPacketPreProcessors []types.PreSendPacketDataProcessingFn } func NewIbcHooks(cdc codec.BinaryCodec, ibcHooksKeeper *keeper.Keeper, ibcKeeper *ibckeeper.Keeper, wasmHooks *WasmHooks, markerHooks *MarkerHooks, preSendPacketDataProcessingFns []types.PreSendPacketDataProcessingFn) IbcHooks { return IbcHooks{ - cdc: cdc, - ibcKeeper: ibcKeeper, - ibcHooksKeeper: ibcHooksKeeper, - wasmHooks: wasmHooks, - markerHooks: markerHooks, - PreSendPacketDataProcessingFns: preSendPacketDataProcessingFns, + cdc: cdc, + ibcKeeper: ibcKeeper, + ibcHooksKeeper: ibcHooksKeeper, + wasmHooks: wasmHooks, + markerHooks: markerHooks, + SendPacketPreProcessors: preSendPacketDataProcessingFns, } } @@ -38,9 +38,9 @@ func (h IbcHooks) ProperlyConfigured() bool { return h.wasmHooks.ProperlyConfigured() && h.markerHooks.ProperlyConfigured() && h.ibcHooksKeeper != nil } -// GetPreSendPacketDataProcessingFns returns a list of ordered functions to be executed before ibc's SendPacket function in middleware -func (h IbcHooks) GetPreSendPacketDataProcessingFns() []types.PreSendPacketDataProcessingFn { - return h.PreSendPacketDataProcessingFns +// GetSendPacketPreProcessors returns a list of ordered functions to be executed before ibc's SendPacket function in middleware +func (h IbcHooks) GetSendPacketPreProcessors() []types.PreSendPacketDataProcessingFn { + return h.SendPacketPreProcessors } // OnRecvPacketOverride executes wasm or marker hooks for Ics20 packets, if not ics20 packet it will continue to process packet with no override @@ -76,10 +76,12 @@ func (h IbcHooks) SendPacketAfterHook(ctx sdktypes.Context, h.wasmHooks.SendPacketAfterHook(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data, sequence, err, processData) } +// OnTimeoutPacketOverride returns impl of wasm hook for OnTimeoutPacketOverride func (h IbcHooks) OnTimeoutPacketOverride(im IBCMiddleware, ctx sdktypes.Context, packet channeltypes.Packet, relayer sdktypes.AccAddress) error { return h.wasmHooks.OnTimeoutPacketOverride(im, ctx, packet, relayer) } +// OnAcknowledgementPacketOverride returns impl of wasm OnAcknowledgementPacketOverride func (h IbcHooks) OnAcknowledgementPacketOverride(im IBCMiddleware, ctx sdktypes.Context, packet channeltypes.Packet, acknowledgement []byte, relayer sdktypes.AccAddress) error { return h.wasmHooks.OnAcknowledgementPacketOverride(im, ctx, packet, acknowledgement, relayer) } diff --git a/x/ibchooks/ics4_middleware.go b/x/ibchooks/ics4_middleware.go index 84f126d191..f746767ba4 100644 --- a/x/ibchooks/ics4_middleware.go +++ b/x/ibchooks/ics4_middleware.go @@ -42,8 +42,8 @@ func (i ICS4Middleware) SendPacket( } processingStateData := make(map[string]interface{}) - if hook, ok := i.Hooks.(GetPreSendPacketDataProcessingFns); ok { - for _, packetDataProcessingFn := range hook.GetPreSendPacketDataProcessingFns() { + if hook, ok := i.Hooks.(SendPacketPreProcessors); ok { + for _, packetDataProcessingFn := range hook.GetSendPacketPreProcessors() { data, err = packetDataProcessingFn(ctx, data, processingStateData) if err != nil { return 0, err diff --git a/x/ibchooks/keeper/keeper.go b/x/ibchooks/keeper/keeper.go index 05a0cbcdbd..0b43bfa67f 100644 --- a/x/ibchooks/keeper/keeper.go +++ b/x/ibchooks/keeper/keeper.go @@ -236,7 +236,7 @@ func (k Keeper) EmitIBCAck(ctx sdk.Context, sender, channel string, packetSequen newAck = channeltypes.NewResultAcknowledgement(jsonAck) case "ack_error": packet = ack.AckError.Packet - newAck = NewSuccessAckRepresentingAnError(ctx, types.ErrAckFromContract, []byte(ack.AckError.ErrorResponse), ack.AckError.ErrorDescription) + newAck = NewSuccessAckError(ctx, types.ErrAckFromContract, []byte(ack.AckError.ErrorResponse), ack.AckError.ErrorDescription) default: return nil, sdkerrors.Wrap(err, "could not unmarshal into IBCAckResponse or IBCAckError") } @@ -274,10 +274,10 @@ func hashPacket(packet channeltypes.Packet) (string, error) { return hex.EncodeToString(packetHash), nil } -// NewSuccessAckRepresentingAnError creates a new success acknowledgement that represents an error. +// NewSuccessAckError creates a new success acknowledgement that represents an error. // This is useful for notifying the sender that an error has occurred in a way that does not allow // the received tokens to be reverted (which means they shouldn't be released by the sender's ics20 escrow) -func NewSuccessAckRepresentingAnError(ctx sdk.Context, err error, errorContent []byte, errorContexts ...string) channeltypes.Acknowledgement { +func NewSuccessAckError(ctx sdk.Context, err error, errorContent []byte, errorContexts ...string) channeltypes.Acknowledgement { logger := ctx.Logger().With("module", "ibc-acknowledgement-error") attributes := make([]sdk.Attribute, len(errorContexts)+1) diff --git a/x/ibchooks/types/types.go b/x/ibchooks/types/types.go index f1d7e7e958..d9647e0676 100644 --- a/x/ibchooks/types/types.go +++ b/x/ibchooks/types/types.go @@ -53,6 +53,7 @@ type IBCAckError struct { ErrorResponse string `json:"error_response"` } +// IBCAck is the parent IBC ack response structure type IBCAck struct { Type string `json:"type"` Content json.RawMessage `json:"content"` @@ -63,6 +64,7 @@ type IBCAck struct { AckError *IBCAckError `json:"error,omitempty"` } +// UnmarshalIBCAck unmashals Ack to either response or error type func UnmarshalIBCAck(bz []byte) (*IBCAck, error) { var ack IBCAck if err := json.Unmarshal(bz, &ack); err != nil { @@ -85,6 +87,7 @@ func UnmarshalIBCAck(bz []byte) (*IBCAck, error) { return &ack, nil } +// IbcAck ibc ack struct with json fields defined type IbcAck struct { Channel string `json:"channel"` Sequence uint64 `json:"sequence"` @@ -92,31 +95,38 @@ type IbcAck struct { Success bool `json:"success"` } +// IbcLifecycleCompleteAck ibc lifcycle complete ack with json fields defined type IbcLifecycleCompleteAck struct { IbcAck IbcAck `json:"ibc_ack"` } +// IbcTimeout ibc timeout struct with json fields defined type IbcTimeout struct { Channel string `json:"channel"` Sequence uint64 `json:"sequence"` } +// IbcLifecycleCompleteTimeout ibc lifecycle complete struct with json fields defined type IbcLifecycleCompleteTimeout struct { IbcTimeout IbcTimeout `json:"ibc_timeout"` } +// IbcLifecycleComplete ibc lifecycle complete struct with json fields defined type IbcLifecycleComplete struct { IbcLifecycleComplete interface{} `json:"ibc_lifecycle_complete"` } +// MarkerMemo parent marker struct for memo json type MarkerMemo struct { Marker MarkerPayload `json:"marker"` } +// MarkerPayload child structure for marker memo type MarkerPayload struct { TransferAuths []string `json:"transfer-auths"` } +// NewMarkerPayload returns a marker payload with transfer authorities func NewMarkerPayload(transferAuthAddrs []sdk.AccAddress) MarkerPayload { addresses := make([]string, len(transferAuthAddrs)) for i := 0; i < len(transferAuthAddrs); i++ { diff --git a/x/ibchooks/wasm_hook.go b/x/ibchooks/wasm_hook.go index f30fde1f0d..930594c111 100644 --- a/x/ibchooks/wasm_hook.go +++ b/x/ibchooks/wasm_hook.go @@ -297,7 +297,7 @@ func (h WasmHooks) SendPacketOverride( return seq, nil } -func (h WasmHooks) GetPreSendPacketDataProcessingFns( +func (h WasmHooks) GetWasmSendPacketPreProcessor( _ sdktypes.Context, data []byte, processData map[string]interface{}, @@ -513,7 +513,6 @@ func MustExtractDenomFromPacketOnRecv(packet ibcexported.PacketI) string { panic("unable to unmarshal ICS20 packet data") } - var denom string if transfertypes.ReceiverChainIsSource(packet.GetSourcePort(), packet.GetSourceChannel(), data.Denom) { // remove prefix added by sender chain voucherPrefix := transfertypes.GetDenomPrefix(packet.GetSourcePort(), packet.GetSourceChannel()) @@ -521,7 +520,7 @@ func MustExtractDenomFromPacketOnRecv(packet ibcexported.PacketI) string { unprefixedDenom := data.Denom[len(voucherPrefix):] // coin denomination used in sending from the escrow address - denom = unprefixedDenom + denom := unprefixedDenom // The denomination used to send the coins is either the native denom or the hash of the path // if the denomination is not native. @@ -529,9 +528,9 @@ func MustExtractDenomFromPacketOnRecv(packet ibcexported.PacketI) string { if denomTrace.Path != "" { denom = denomTrace.IBCDenom() } - } else { - prefixedDenom := transfertypes.GetDenomPrefix(packet.GetDestPort(), packet.GetDestChannel()) + data.Denom - denom = transfertypes.ParseDenomTrace(prefixedDenom).IBCDenom() + return denom } - return denom + + prefixedDenom := transfertypes.GetDenomPrefix(packet.GetDestPort(), packet.GetDestChannel()) + data.Denom + return transfertypes.ParseDenomTrace(prefixedDenom).IBCDenom() }