Skip to content

Commit

Permalink
add data max size and emit deposit hook events
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Jan 24, 2025
1 parent bb31a38 commit 5c1c229
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
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 @@ var (

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

/* MsgRecordBatch */
Expand Down Expand Up @@ -195,6 +196,10 @@ func (msg MsgInitiateTokenDeposit) Validate(ac address.Codec) error {
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

0 comments on commit 5c1c229

Please sign in to comment.