Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: guarantee ack & timeout to be sccuess #45

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions app/ibc-hooks/ack.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,27 @@
if !isWasmRouted || hookData.AsyncCallback == "" {
return nil
} else if err != nil {
return err
h.wasmKeeper.Logger(ctx).Error("failed to parse memo", "error", err)
return nil

Check warning on line 33 in app/ibc-hooks/ack.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/ack.go#L32-L33

Added lines #L32 - L33 were not covered by tests
}

// 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); err != nil {
return err
if allowed, err := h.checkACL(im, cacheCtx, callback); err != nil {
h.wasmKeeper.Logger(cacheCtx).Error("failed to check ACL", "error", err)
return nil

Check warning on line 43 in app/ibc-hooks/ack.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/ack.go#L42-L43

Added lines #L42 - L43 were not covered by tests
} else if !allowed {
h.wasmKeeper.Logger(cacheCtx).Error("failed to check ACL", "not allowed")
return nil
}

contractAddr, err := h.ac.StringToBytes(callback)
if err != nil {
return errorsmod.Wrap(err, "Ack callback error")
h.wasmKeeper.Logger(cacheCtx).Error("invalid contract address", "error", err)
return nil

Check warning on line 52 in app/ibc-hooks/ack.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/ack.go#L51-L52

Added lines #L51 - L52 were not covered by tests
}

success := "false"
Expand All @@ -52,18 +60,21 @@
// Notify the sender that the ack has been received
ackAsJson, err := json.Marshal(acknowledgement)
if err != nil {
// If the ack is not a json object, error
return err
h.wasmKeeper.Logger(cacheCtx).Error("ack is not json object", "error", err)
return nil

Check warning on line 64 in app/ibc-hooks/ack.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/ack.go#L63-L64

Added lines #L63 - L64 were not covered by tests
}

sudoMsg := []byte(fmt.Sprintf(
`{"ibc_lifecycle_complete": {"ibc_ack": {"channel": "%s", "sequence": %d, "ack": %s, "success": %s}}}`,
packet.SourceChannel, packet.Sequence, ackAsJson, success))
_, err = h.wasmKeeper.Sudo(ctx, contractAddr, sudoMsg)
_, err = h.wasmKeeper.Sudo(cacheCtx, contractAddr, sudoMsg)
if err != nil {
return errorsmod.Wrap(err, "Ack callback error")
}

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

return nil
}

Expand All @@ -83,19 +94,27 @@
if !isWasmRouted || hookData.AsyncCallback == "" {
return nil
} else if err != nil {
return err
h.wasmKeeper.Logger(ctx).Error("failed to parse memo", "error", err)
return nil

Check warning on line 98 in app/ibc-hooks/ack.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/ack.go#L97-L98

Added lines #L97 - L98 were not covered by tests
}

// 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); err != nil {
return err
if allowed, err := h.checkACL(im, cacheCtx, callback); err != nil {
h.wasmKeeper.Logger(cacheCtx).Error("failed to check ACL", "error", err)
return nil

Check warning on line 108 in app/ibc-hooks/ack.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/ack.go#L107-L108

Added lines #L107 - L108 were not covered by tests
} else if !allowed {
h.wasmKeeper.Logger(cacheCtx).Error("failed to check ACL", "not allowed")
return nil
}

contractAddr, err := h.ac.StringToBytes(callback)
if err != nil {
return errorsmod.Wrap(err, "Ack callback error")
h.wasmKeeper.Logger(cacheCtx).Error("invalid contract address", "error", err)
return nil

Check warning on line 117 in app/ibc-hooks/ack.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/ack.go#L116-L117

Added lines #L116 - L117 were not covered by tests
}

success := "false"
Expand All @@ -106,17 +125,21 @@
// Notify the sender that the ack has been received
ackAsJson, err := json.Marshal(acknowledgement)
if err != nil {
// If the ack is not a json object, error
return err
h.wasmKeeper.Logger(cacheCtx).Error("ack is not json object", "error", err)
return nil

Check warning on line 129 in app/ibc-hooks/ack.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/ack.go#L128-L129

Added lines #L128 - L129 were not covered by tests
}

sudoMsg := []byte(fmt.Sprintf(
`{"ibc_lifecycle_complete": {"ibc_ack": {"channel": "%s", "sequence": %d, "ack": %s, "success": %s}}}`,
packet.SourceChannel, packet.Sequence, ackAsJson, success))
_, err = h.wasmKeeper.Sudo(ctx, contractAddr, sudoMsg)
_, err = h.wasmKeeper.Sudo(cacheCtx, contractAddr, sudoMsg)
if err != nil {
return errorsmod.Wrap(err, "Ack callback error")
h.wasmKeeper.Logger(cacheCtx).Error("failed to execute callback", "error", err)
return nil

Check warning on line 138 in app/ibc-hooks/ack.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/ack.go#L137-L138

Added lines #L137 - L138 were not covered by tests
}

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

return nil
}
49 changes: 36 additions & 13 deletions app/ibc-hooks/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import (
"fmt"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
Expand All @@ -27,29 +26,41 @@
if !isWasmRouted || hookData.AsyncCallback == "" {
return nil
} else if err != nil {
return err
h.wasmKeeper.Logger(ctx).Error("failed to parse memo", "error", err)
return nil

Check warning on line 30 in app/ibc-hooks/timeout.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/timeout.go#L29-L30

Added lines #L29 - L30 were not covered by tests
}

// 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); err != nil {
return err
if allowed, err := h.checkACL(im, cacheCtx, callback); err != nil {
h.wasmKeeper.Logger(cacheCtx).Error("failed to check ACL", "error", err)
return nil

Check warning on line 40 in app/ibc-hooks/timeout.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/timeout.go#L39-L40

Added lines #L39 - L40 were not covered by tests
} else if !allowed {
h.wasmKeeper.Logger(cacheCtx).Error("failed to check ACL", "not allowed")
return nil
}

contractAddr, err := h.ac.StringToBytes(callback)
if err != nil {
return errorsmod.Wrap(err, "Ack callback error")
h.wasmKeeper.Logger(cacheCtx).Error("invalid contract address", "error", err)
return nil

Check warning on line 49 in app/ibc-hooks/timeout.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/timeout.go#L48-L49

Added lines #L48 - L49 were not covered by tests
}

sudoMsg := []byte(fmt.Sprintf(
`{"ibc_lifecycle_complete": {"ibc_timeout": {"channel": "%s", "sequence": %d}}}`,
packet.SourceChannel, packet.Sequence))
_, err = h.wasmKeeper.Sudo(ctx, contractAddr, sudoMsg)
_, err = h.wasmKeeper.Sudo(cacheCtx, contractAddr, sudoMsg)
if err != nil {
return errorsmod.Wrap(err, "Ack callback error")
h.wasmKeeper.Logger(cacheCtx).Error("failed to execute callback", "error", err)
return nil

Check warning on line 58 in app/ibc-hooks/timeout.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/timeout.go#L57-L58

Added lines #L57 - L58 were not covered by tests
}

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

return nil
}

Expand All @@ -68,28 +79,40 @@
if !isWasmRouted || hookData.AsyncCallback == "" {
return nil
} else if err != nil {
return err
h.wasmKeeper.Logger(ctx).Error("failed to parse memo", "error", err)
return nil

Check warning on line 83 in app/ibc-hooks/timeout.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/timeout.go#L82-L83

Added lines #L82 - L83 were not covered by tests
}

// 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); err != nil {
return err
if allowed, err := h.checkACL(im, cacheCtx, callback); err != nil {
h.wasmKeeper.Logger(cacheCtx).Error("failed to check ACL", "error", err)
return nil

Check warning on line 93 in app/ibc-hooks/timeout.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/timeout.go#L92-L93

Added lines #L92 - L93 were not covered by tests
} else if !allowed {
h.wasmKeeper.Logger(cacheCtx).Error("failed to check ACL", "not allowed")
return nil
}

contractAddr, err := h.ac.StringToBytes(callback)
if err != nil {
return errorsmod.Wrap(err, "Ack callback error")
h.wasmKeeper.Logger(cacheCtx).Error("invalid contract address", "error", err)
return nil

Check warning on line 102 in app/ibc-hooks/timeout.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/timeout.go#L101-L102

Added lines #L101 - L102 were not covered by tests
}

sudoMsg := []byte(fmt.Sprintf(
`{"ibc_lifecycle_complete": {"ibc_timeout": {"channel": "%s", "sequence": %d}}}`,
packet.SourceChannel, packet.Sequence))
_, err = h.wasmKeeper.Sudo(ctx, contractAddr, sudoMsg)
_, err = h.wasmKeeper.Sudo(cacheCtx, contractAddr, sudoMsg)
if err != nil {
return errorsmod.Wrap(err, "Ack callback error")
h.wasmKeeper.Logger(cacheCtx).Error("failed to execute callback", "error", err)
return nil

Check warning on line 111 in app/ibc-hooks/timeout.go

View check run for this annotation

Codecov / codecov/patch

app/ibc-hooks/timeout.go#L110-L111

Added lines #L110 - L111 were not covered by tests
}

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

return nil
}
Loading