From 8373d099d1380394edb22d6f5bbbca259f2eb71a Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Tue, 27 Feb 2024 10:16:01 +0100 Subject: [PATCH 1/3] chore: cleanup wasm_hook.go Updated few comments and simplified the code. --- modules/ibc-hooks/wasm_hook.go | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/modules/ibc-hooks/wasm_hook.go b/modules/ibc-hooks/wasm_hook.go index 0aef149b..4233f28e 100644 --- a/modules/ibc-hooks/wasm_hook.go +++ b/modules/ibc-hooks/wasm_hook.go @@ -159,11 +159,7 @@ func jsonStringHasKey(memo, key string) (found bool, jsonObject map[string]inter // If the key doesn't exist, there's nothing to do on this hook. Continue by passing the packet // down the stack _, ok := jsonObject[key] - if !ok { - return false, jsonObject - } - - return true, jsonObject + return ok, jsonObject } func ValidateAndParseMemo(memo string, receiver string) (isWasmRouted bool, contractAddr sdk.AccAddress, msgBytes []byte, err error) { @@ -181,7 +177,6 @@ func ValidateAndParseMemo(memo string, receiver string) (isWasmRouted bool, cont fmt.Errorf(types.ErrBadMetadataFormatMsg, memo, "wasm metadata is not a valid JSON map object") } - // Get the contract contract, ok := wasm["contract"].(string) if !ok { // The tokens will be returned @@ -195,19 +190,16 @@ func ValidateAndParseMemo(memo string, receiver string) (isWasmRouted bool, cont fmt.Errorf(types.ErrBadMetadataFormatMsg, memo, `wasm["contract"] is not a valid bech32 address`) } - // The contract and the receiver should be the same for the packet to be valid if contract != receiver { return isWasmRouted, sdk.AccAddress{}, nil, fmt.Errorf(types.ErrBadMetadataFormatMsg, memo, `wasm["contract"] should be the same as the receiver of the packet`) } - // Ensure the message key is provided if wasm["msg"] == nil { return isWasmRouted, sdk.AccAddress{}, nil, fmt.Errorf(types.ErrBadMetadataFormatMsg, memo, `Could not find key wasm["msg"]`) } - // Make sure the msg key is a map. If it isn't, return an error _, ok = wasm["msg"].(map[string]interface{}) if !ok { return isWasmRouted, sdk.AccAddress{}, nil, @@ -236,7 +228,8 @@ func (h WasmHooks) SendPacketOverride(i ICS4Middleware, ctx sdk.Context, chanCap return i.channel.SendPacket(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data) // continue } - // We remove the callback metadata from the memo as it has already been processed. + // We remove the meta.callback provides instructions for post-send processing. This instruction are saved + // in the keepr (at the end of this function), and at the end - it is set as as it has already been processed. // If the only available key in the memo is the callback, we should remove the memo // from the data completely so the packet is sent without it. @@ -411,9 +404,9 @@ func MustExtractDenomFromPacketOnRecv(packet ibcexported.PacketI) string { var denom string if transfertypes.ReceiverChainIsSource(packet.GetSourcePort(), packet.GetSourceChannel(), data.Denom) { - // remove prefix added by sender chain + // if we receive back a token, that was originally sent from "this" chain, then we need to remove + // prefix added by the sender chain: port/channel/base_denom -> base_denom. voucherPrefix := transfertypes.GetDenomPrefix(packet.GetSourcePort(), packet.GetSourceChannel()) - unprefixedDenom := data.Denom[len(voucherPrefix):] // coin denomination used in sending from the escrow address @@ -422,7 +415,7 @@ func MustExtractDenomFromPacketOnRecv(packet ibcexported.PacketI) string { // The denomination used to send the coins is either the native denom or the hash of the path // if the denomination is not native. denomTrace := transfertypes.ParseDenomTrace(unprefixedDenom) - if denomTrace.Path != "" { + if !denomTrace.IsNativeDenom() { denom = denomTrace.IBCDenom() } } else { From f57c85d3ef679e758514f835c08805d7fca8b087 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Tue, 6 Aug 2024 17:33:01 -0500 Subject: [PATCH 2/3] fix: lint --- modules/ibc-hooks/wasm_hook.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ibc-hooks/wasm_hook.go b/modules/ibc-hooks/wasm_hook.go index 86277846..67ac735a 100644 --- a/modules/ibc-hooks/wasm_hook.go +++ b/modules/ibc-hooks/wasm_hook.go @@ -229,7 +229,7 @@ func (h WasmHooks) SendPacketOverride(i ICS4Middleware, ctx sdk.Context, chanCap return i.channel.SendPacket(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data) // continue } - // We remove the meta.callback provides instructions for post-send processing. This instruction are saved + // We remove the meta.callback provides instructions for post-send processing. This instruction are saved // in the keepr (at the end of this function), and at the end - it is set as as it has already been processed. // If the only available key in the memo is the callback, we should remove the memo From b320c21c9b3efeb760c1da5c4c1e8c938d243aeb Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Tue, 6 Aug 2024 17:34:01 -0500 Subject: [PATCH 3/3] nit: typo --- modules/ibc-hooks/wasm_hook.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ibc-hooks/wasm_hook.go b/modules/ibc-hooks/wasm_hook.go index 67ac735a..52a1fc6b 100644 --- a/modules/ibc-hooks/wasm_hook.go +++ b/modules/ibc-hooks/wasm_hook.go @@ -230,7 +230,7 @@ func (h WasmHooks) SendPacketOverride(i ICS4Middleware, ctx sdk.Context, chanCap } // We remove the meta.callback provides instructions for post-send processing. This instruction are saved - // in the keepr (at the end of this function), and at the end - it is set as as it has already been processed. + // in the keeper (at the end of this function), and at the end - it is set as as it has already been processed. // If the only available key in the memo is the callback, we should remove the memo // from the data completely so the packet is sent without it.