Skip to content

Commit

Permalink
fix emit reason event only at failure
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Sep 10, 2024
1 parent a055501 commit e9f4748
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 6 additions & 2 deletions x/opchild/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,14 @@ func (ms MsgServer) FinalizeTokenDeposit(ctx context.Context, req *types.MsgFina
if depositSuccess && len(req.Data) > 0 {
hookSuccess, reason = ms.handleBridgeHook(sdkCtx, req.Data)
event = event.AppendAttributes(sdk.NewAttribute(types.AttributeKeySuccess, strconv.FormatBool(hookSuccess)))
event = event.AppendAttributes(sdk.NewAttribute(types.AttributeKeyReason, "hook failed; "+reason))
if !hookSuccess {
event = event.AppendAttributes(sdk.NewAttribute(types.AttributeKeyReason, "hook failed; "+reason))
}
} else {
event = event.AppendAttributes(sdk.NewAttribute(types.AttributeKeySuccess, strconv.FormatBool(depositSuccess)))
event = event.AppendAttributes(sdk.NewAttribute(types.AttributeKeyReason, reason))
if !depositSuccess {
event = event.AppendAttributes(sdk.NewAttribute(types.AttributeKeyReason, "deposit failed; "+reason))
}
}

// emit deposit event
Expand Down
11 changes: 10 additions & 1 deletion x/opchild/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,22 @@ func Test_MsgServer_Deposit_ToModuleAccount(t *testing.T) {
_, err := ms.FinalizeTokenDeposit(ctx, msg)
require.NoError(t, err)

for _, event := range sdk.UnwrapSDKContext(ctx).EventManager().Events() {
if event.Type == types.EventTypeFinalizeTokenDeposit {
attrIdx := slices.Index(event.Attributes, sdk.NewAttribute(types.AttributeKeySuccess, "false").ToKVPair())
require.Positive(t, attrIdx)
require.Equal(t, event.Attributes[attrIdx+1].Key, types.AttributeKeyReason)
require.Contains(t, event.Attributes[attrIdx+1].Value, "deposit failed;")
}
}

afterToBalance := input.BankKeeper.GetBalance(ctx, addrs[1], denom)
require.Equal(t, math.ZeroInt(), afterToBalance.Amount)

afterModuleBalance := input.BankKeeper.GetBalance(ctx, opchildModuleAddress, denom)
require.True(t, afterModuleBalance.Amount.IsZero())

// token withdrawal inititated
// token withdrawal initiated
events := sdk.UnwrapSDKContext(ctx).EventManager().Events()
lastEvent := events[len(events)-1]
require.Equal(t, sdk.NewEvent(
Expand Down

0 comments on commit e9f4748

Please sign in to comment.