Skip to content

Commit

Permalink
fix: do not return error in ack and timeout (#204)
Browse files Browse the repository at this point in the history
* do not return error in ack and timeout

* change error log
  • Loading branch information
beer-1 authored Jun 14, 2024
1 parent 867edd5 commit a877b19
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 37 deletions.
54 changes: 36 additions & 18 deletions x/ibc-hooks/move-hooks/ack.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,31 @@ func (h MoveHooks) onAckIcs20Packet(
if !isMoveRouted || hookData.AsyncCallback == nil {
return nil
} else if err != nil {
return err
h.moveKeeper.Logger(ctx).Error("failed to parse memo", "error", err)
return nil
}

// create a new cache context to ignore errors during
// the execution of the callback
cacheCtx, write := ctx.CacheContext()

callback := hookData.AsyncCallback
if allowed, err := h.checkACL(im, ctx, callback.ModuleAddress); err != nil {
return err
if allowed, err := h.checkACL(im, cacheCtx, callback.ModuleAddress); err != nil {
h.moveKeeper.Logger(cacheCtx).Error("failed to check ACL", "error", err)
return nil
} else if !allowed {
h.moveKeeper.Logger(cacheCtx).Error("failed to check ACL", "not allowed")
return nil
}

callbackIdBz, err := vmtypes.SerializeUint64(callback.Id)
if err != nil {
return err
return nil
}
successBz, err := vmtypes.SerializeBool(!isAckError(h.codec, acknowledgement))
if err != nil {
return err
return nil
}

_, err = h.execMsg(ctx, &movetypes.MsgExecute{
_, err = h.execMsg(cacheCtx, &movetypes.MsgExecute{
Sender: data.Sender,
ModuleAddress: callback.ModuleAddress,
ModuleName: callback.ModuleName,
Expand All @@ -55,9 +60,13 @@ func (h MoveHooks) onAckIcs20Packet(
Args: [][]byte{callbackIdBz, successBz},
})
if err != nil {
return err
h.moveKeeper.Logger(cacheCtx).Error("failed to execute callback", "error", err)
return nil
}

// write the cache context only if the callback execution was successful
write()

return nil
}

Expand All @@ -77,26 +86,31 @@ func (h MoveHooks) onAckIcs721Packet(
if !isMoveRouted || hookData.AsyncCallback == nil {
return nil
} else if err != nil {
return err
h.moveKeeper.Logger(ctx).Error("failed to parse memo", "error", err)
return nil
}

// create a new cache context to ignore errors during
// the execution of the callback
cacheCtx, write := ctx.CacheContext()

callback := hookData.AsyncCallback
if allowed, err := h.checkACL(im, ctx, callback.ModuleAddress); err != nil {
return err
if allowed, err := h.checkACL(im, cacheCtx, callback.ModuleAddress); err != nil {
h.moveKeeper.Logger(cacheCtx).Error("failed to check ACL", "error", err)
return nil
} else if !allowed {
h.moveKeeper.Logger(cacheCtx).Error("failed to check ACL", "not allowed")
return nil
}

callbackIdBz, err := vmtypes.SerializeUint64(callback.Id)
if err != nil {
return err
return nil
}
successBz, err := vmtypes.SerializeBool(!isAckError(h.codec, acknowledgement))
if err != nil {
return err
return nil
}

_, err = h.execMsg(ctx, &movetypes.MsgExecute{
_, err = h.execMsg(cacheCtx, &movetypes.MsgExecute{
Sender: data.Sender,
ModuleAddress: callback.ModuleAddress,
ModuleName: callback.ModuleName,
Expand All @@ -105,8 +119,12 @@ func (h MoveHooks) onAckIcs721Packet(
Args: [][]byte{callbackIdBz, successBz},
})
if err != nil {
return err
h.moveKeeper.Logger(cacheCtx).Error("failed to execute callback", "error", err)
return nil
}

// write the cache context only if the callback execution was successful
write()

return nil
}
5 changes: 2 additions & 3 deletions x/ibc-hooks/move-hooks/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
ibchooks "github.com/initia-labs/initia/x/ibc-hooks"
movekeeper "github.com/initia-labs/initia/x/move/keeper"
movetypes "github.com/initia-labs/initia/x/move/types"

vmtypes "github.com/initia-labs/movevm/types"
)

var (
Expand Down Expand Up @@ -71,10 +69,11 @@ func (h MoveHooks) OnTimeoutPacketOverride(im ibchooks.IBCMiddleware, ctx sdk.Co
}

func (h MoveHooks) checkACL(im ibchooks.IBCMiddleware, ctx sdk.Context, addrStr string) (bool, error) {
vmAddr, err := vmtypes.NewAccountAddress(addrStr)
vmAddr, err := movetypes.AccAddressFromString(h.ac, addrStr)
if err != nil {
return false, err
}

sdkAddr := movetypes.ConvertVMAddressToSDKAddress(vmAddr)
return im.HooksKeeper.GetAllowed(ctx, sdkAddr)
}
50 changes: 34 additions & 16 deletions x/ibc-hooks/move-hooks/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,27 @@ func (h MoveHooks) onTimeoutIcs20Packet(
if !isMoveRouted || hookData.AsyncCallback == nil {
return nil
} else if err != nil {
return err
h.moveKeeper.Logger(ctx).Error("failed to parse memo", "error", err)
return nil
}

// create a new cache context to ignore errors during
// the execution of the callback
cacheCtx, write := ctx.CacheContext()

callback := hookData.AsyncCallback
if allowed, err := h.checkACL(im, ctx, callback.ModuleAddress); err != nil {
return err
if allowed, err := h.checkACL(im, cacheCtx, callback.ModuleAddress); err != nil {
h.moveKeeper.Logger(cacheCtx).Error("failed to check ACL", "error", err)
return nil
} else if !allowed {
h.moveKeeper.Logger(cacheCtx).Error("failed to check ACL", "not allowed")
return nil
}

callbackIdBz, err := vmtypes.SerializeUint64(callback.Id)
if err != nil {
return err
return nil
}

_, err = h.execMsg(ctx, &movetypes.MsgExecute{
_, err = h.execMsg(cacheCtx, &movetypes.MsgExecute{
Sender: data.Sender,
ModuleAddress: callback.ModuleAddress,
ModuleName: callback.ModuleName,
Expand All @@ -50,9 +55,13 @@ func (h MoveHooks) onTimeoutIcs20Packet(
Args: [][]byte{callbackIdBz},
})
if err != nil {
return err
h.moveKeeper.Logger(cacheCtx).Error("failed to execute callback", "error", err)
return nil
}

// write the cache context only if the callback execution was successful
write()

return nil
}

Expand All @@ -71,22 +80,27 @@ func (h MoveHooks) onTimeoutIcs721Packet(
if !isMoveRouted || hookData.AsyncCallback == nil {
return nil
} else if err != nil {
return err
h.moveKeeper.Logger(ctx).Error("failed to parse memo", "error", err)
return nil
}

// create a new cache context to ignore errors during
// the execution of the callback
cacheCtx, write := ctx.CacheContext()

callback := hookData.AsyncCallback
if allowed, err := h.checkACL(im, ctx, callback.ModuleAddress); err != nil {
return err
if allowed, err := h.checkACL(im, cacheCtx, callback.ModuleAddress); err != nil {
h.moveKeeper.Logger(cacheCtx).Error("failed to check ACL", "error", err)
return nil
} else if !allowed {
h.moveKeeper.Logger(cacheCtx).Error("failed to check ACL", "not allowed")
return nil
}

callbackIdBz, err := vmtypes.SerializeUint64(callback.Id)
if err != nil {
return err
return nil
}

_, err = h.execMsg(ctx, &movetypes.MsgExecute{
_, err = h.execMsg(cacheCtx, &movetypes.MsgExecute{
Sender: data.Sender,
ModuleAddress: callback.ModuleAddress,
ModuleName: callback.ModuleName,
Expand All @@ -95,8 +109,12 @@ func (h MoveHooks) onTimeoutIcs721Packet(
Args: [][]byte{callbackIdBz},
})
if err != nil {
return err
h.moveKeeper.Logger(cacheCtx).Error("failed to execute callback", "error", err)
return nil
}

// write the cache context only if the callback execution was successful
write()

return nil
}

0 comments on commit a877b19

Please sign in to comment.