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

feat: add data max size and emit deposit hook events #133

Merged
merged 1 commit into from
Jan 28, 2025
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
5 changes: 4 additions & 1 deletion x/opchild/keeper/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ func (k Keeper) handleBridgeHook(ctx sdk.Context, data []byte, hookMaxGas uint64
return
}

_, err = handler(cacheCtx, msg)
res, err := handler(cacheCtx, msg)
if err != nil {
reason = fmt.Sprintf("Failed to execute Msg: %s", err)
return
}

// emit events
cacheCtx.EventManager().EmitEvents(res.GetEvents())
}

commit()
Expand Down
65 changes: 65 additions & 0 deletions x/opchild/keeper/deposit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package keeper_test

import (
"encoding/hex"
"testing"

"cosmossdk.io/math"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/initia-labs/OPinit/x/opchild/keeper"
"github.com/initia-labs/OPinit/x/opchild/types"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/sha3"
)

func Test_MsgServer_Deposit_HookEvents(t *testing.T) {
ctx, input := createDefaultTestInput(t)
ms := keeper.NewMsgServerImpl(&input.OPChildKeeper)

bz := sha3.Sum256([]byte("test_token"))
denom := "l2/" + hex.EncodeToString(bz[:])

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

// empty deposit to create account
priv, _, addr := keyPubAddr()
msg := types.NewMsgFinalizeTokenDeposit(addrsStr[0], addrsStr[1], addr.String(), sdk.NewCoin(denom, math.ZeroInt()), 1, 1, "test_token", nil)
_, err := ms.FinalizeTokenDeposit(ctx, msg)
require.NoError(t, err)

// create hook data
acc := input.AccountKeeper.GetAccount(ctx, addr)
require.NotNil(t, acc)
privs, accNums, accSeqs := []cryptotypes.PrivKey{priv}, []uint64{acc.GetAccountNumber()}, []uint64{0}
from, _ := input.AccountKeeper.AddressCodec().BytesToString(addr)
to, _ := input.AccountKeeper.AddressCodec().BytesToString(addrs[2])

signedTxBz, err := input.EncodingConfig.TxConfig.TxEncoder()(generateTestTx(
t, input,
[]sdk.Msg{
types.NewMsgInitiateTokenWithdrawal(from, to, sdk.NewCoin(denom, math.NewInt(50))),
},
privs, accNums, accSeqs, sdk.UnwrapSDKContext(ctx).ChainID(),
))
require.NoError(t, err)

// valid deposit
ctx = sdk.UnwrapSDKContext(ctx).WithEventManager(sdk.NewEventManager())
msg = types.NewMsgFinalizeTokenDeposit(addrsStr[0], addrsStr[1], addr.String(), sdk.NewCoin(denom, math.NewInt(100)), 2, 1, "test_token", signedTxBz)
_, err = ms.FinalizeTokenDeposit(ctx, msg)
require.NoError(t, err)

withdrawalEventFound := false
for _, event := range sdk.UnwrapSDKContext(ctx).EventManager().Events() {
if event.Type == types.EventTypeInitiateTokenWithdrawal {
withdrawalEventFound = true
} else if event.Type == types.EventTypeFinalizeTokenDeposit {
require.Equal(t, event.Attributes[len(event.Attributes)-1].Key, types.AttributeKeySuccess)
require.Equal(t, event.Attributes[len(event.Attributes)-1].Value, "true")
}
}

// but no event was emitted by hooks
require.True(t, withdrawalEventFound)
}
1 change: 1 addition & 0 deletions x/ophost/types/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ var (
ErrInvalidChallengerUpdate = errorsmod.Register(ModuleName, 15, "invalid challenger update")
ErrEmptyBatchBytes = errorsmod.Register(ModuleName, 16, "empty batch bytes")
ErrBridgeNotFound = errorsmod.Register(ModuleName, 17, "bridge not found")
ErrInvalidData = errorsmod.Register(ModuleName, 18, "invalid data")
)
5 changes: 5 additions & 0 deletions x/ophost/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

const (
MaxMetadataLength = 1024 * 5
MaxDataLength = 1024 * 10
)

/* MsgRecordBatch */
Expand Down Expand Up @@ -195,6 +196,10 @@
return ErrInvalidBridgeId
}

if len(msg.Data) > MaxDataLength {
return ErrInvalidData.Wrapf("data length exceeds %d", MaxDataLength)
}

Check warning on line 201 in x/ophost/types/tx.go

View check run for this annotation

Codecov / codecov/patch

x/ophost/types/tx.go#L199-L201

Added lines #L199 - L201 were not covered by tests

return nil
}

Expand Down
Loading