Skip to content

Commit

Permalink
add ante redundant bridge decorator (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-cha authored Jul 9, 2024
1 parent b555a4d commit 89d9d95
Show file tree
Hide file tree
Showing 11 changed files with 1,062 additions and 372 deletions.
463 changes: 263 additions & 200 deletions api/opinit/opchild/v1/tx.pulsar.go

Large diffs are not rendered by default.

131 changes: 99 additions & 32 deletions api/opinit/opchild/v1/types.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion proto/opinit/opchild/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ message MsgFinalizeTokenDeposit {
}

// MsgFinalizeTokenDepositResponse returns deposit result data
message MsgFinalizeTokenDepositResponse {}
message MsgFinalizeTokenDepositResponse {
ResponseResultType result = 1;
}

// MsgInitiateTokenWithdrawal is a message to withdraw a new token from L2 to L1.
message MsgInitiateTokenWithdrawal {
Expand Down
12 changes: 12 additions & 0 deletions proto/opinit/opchild/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,16 @@ message CoinsWrapper {
(amino.dont_omitempty) = true,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
}

// ResponseResultType defines the possible outcomes of the execution of a message
enum ResponseResultType {
option (gogoproto.goproto_enum_prefix) = false;

// Default zero value enumeration
RESPONSE_RESULT_TYPE_UNSPECIFIED = 0 [(gogoproto.enumvalue_customname) = "UNSPECIFIED"];
// The message did not execute msg operation (because, for example, deposit had already been finalized)
RESPONSE_RESULT_TYPE_NOOP = 1 [(gogoproto.enumvalue_customname) = "NOOP"];
// The message was executed successfully
RESPONSE_RESULT_TYPE_SUCCESS = 2 [(gogoproto.enumvalue_customname) = "SUCCESS"];
}
42 changes: 42 additions & 0 deletions x/opchild/ante/ante.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ante

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/initia-labs/OPinit/x/opchild/keeper"
"github.com/initia-labs/OPinit/x/opchild/types"
)

type RedundantBridgeDecorator struct {
ms keeper.MsgServer
}

func NewRedundantBridgeDecorator(k keeper.Keeper) RedundantBridgeDecorator {
return RedundantBridgeDecorator{
ms: keeper.NewMsgServerImpl(k),
}
}

func (rbd RedundantBridgeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
if (ctx.IsCheckTx() || ctx.IsReCheckTx()) && !simulate {
redundancies := 0
packetMsgs := 0
for _, m := range tx.GetMsgs() {
switch msg := m.(type) {
case *types.MsgFinalizeTokenDeposit:
response, err := rbd.ms.FinalizeTokenDeposit(ctx, msg)
if err != nil {
return ctx, err
}
if response.Result == types.NOOP {
redundancies++
}
packetMsgs++
}
}

if redundancies == packetMsgs && packetMsgs > 0 {
return ctx, types.ErrRedundantTx
}
}
return next(ctx, tx, simulate)
}
72 changes: 72 additions & 0 deletions x/opchild/ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import (
"context"
"testing"

"github.com/initia-labs/OPinit/x/opchild/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"google.golang.org/protobuf/reflect/protoreflect"

"cosmossdk.io/log"
"cosmossdk.io/math"
"cosmossdk.io/simapp"
dbm "github.com/cosmos/cosmos-db"

Expand All @@ -22,6 +26,10 @@ import (
xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"

errorsmod "cosmossdk.io/errors"

"github.com/initia-labs/OPinit/x/opchild/ante"
)

// AnteTestSuite is a test suite to be used with ante handler tests.
Expand Down Expand Up @@ -122,3 +130,67 @@ func (suite *AnteTestSuite) CreateTestTx(
func TestAnteTestSuite(t *testing.T) {
suite.Run(t, new(AnteTestSuite))
}

var _ sdk.Tx = testTx{}

type testTx struct {
msgs []sdk.Msg
}

func (t testTx) GetMsgsV2() ([]protoreflect.ProtoMessage, error) {
return nil, nil
}

func (t testTx) GetMsgs() []sdk.Msg {
return t.msgs
}

func TestRedundantTx(t *testing.T) {
ctx, input := createTestInput(t, true)
rbd := ante.NewRedundantBridgeDecorator(input.OPChildKeeper)

// input.Faucet.Mint(ctx, addrs[0], sdk.NewCoin(testDenoms[0], math.NewInt(100000)))

tx := testTx{
msgs: []sdk.Msg{
types.NewMsgFinalizeTokenDeposit(
addrsStr[0], addrsStr[0], addrsStr[1], sdk.NewCoin(testDenoms[0], math.NewInt(100)), 1, 1, "l1_test0", []byte(""),
),
types.NewMsgFinalizeTokenDeposit(
addrsStr[0], addrsStr[0], addrsStr[1], sdk.NewCoin(testDenoms[0], math.NewInt(100)), 2, 1, "l1_test0", []byte(""),
),
},
}

sdkCtx := sdk.UnwrapSDKContext(ctx)
_, err := rbd.AnteHandle(sdkCtx, tx, false, func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) { return sdk.Context{}, nil })
require.NoError(t, err)

_, err = rbd.AnteHandle(sdkCtx, tx, false, func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) { return sdk.Context{}, nil })
require.True(t, errorsmod.IsOf(err, types.ErrRedundantTx))

tx = testTx{
msgs: []sdk.Msg{
types.NewMsgFinalizeTokenDeposit(
addrsStr[0], addrsStr[0], addrsStr[1], sdk.NewCoin(testDenoms[0], math.NewInt(100)), 2, 1, "l1_test0", []byte(""),
),
types.NewMsgFinalizeTokenDeposit(
addrsStr[0], addrsStr[0], addrsStr[1], sdk.NewCoin(testDenoms[0], math.NewInt(100)), 3, 1, "l1_test0", []byte(""),
),
},
}

_, err = rbd.AnteHandle(sdkCtx, tx, false, func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) { return sdk.Context{}, nil })
require.NoError(t, err)

tx = testTx{
msgs: []sdk.Msg{
types.NewMsgFinalizeTokenDeposit(
addrsStr[0], addrsStr[0], addrsStr[1], sdk.NewCoin(testDenoms[0], math.NewInt(100)), 4, 1, "l1_test0", []byte(""),
),
},
}

_, err = rbd.AnteHandle(sdkCtx, tx, false, func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) { return sdk.Context{}, nil })
require.NoError(t, err)
}
Loading

0 comments on commit 89d9d95

Please sign in to comment.