-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds unit test coverage to assert behavior for the x/rollup messages and the related helper functions involved in the deposit and withdrawal flow.
- Loading branch information
1 parent
291f66b
commit 1315787
Showing
5 changed files
with
280 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
storetypes "cosmossdk.io/store/types" | ||
"github.com/cosmos/cosmos-sdk/runtime" | ||
"github.com/cosmos/cosmos-sdk/testutil" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" | ||
"github.com/golang/mock/gomock" | ||
"github.com/polymerdao/monomer/x/rollup/keeper" | ||
rolluptestutil "github.com/polymerdao/monomer/x/rollup/testutil" | ||
"github.com/polymerdao/monomer/x/rollup/types" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type KeeperTestSuite struct { | ||
suite.Suite | ||
ctx context.Context | ||
rollupKeeper *keeper.Keeper | ||
bankKeeper *rolluptestutil.MockBankKeeper | ||
rollupStore storetypes.KVStore | ||
} | ||
|
||
func TestKeeperTestSuite(t *testing.T) { | ||
suite.Run(t, new(KeeperTestSuite)) | ||
} | ||
|
||
func (s *KeeperTestSuite) SetupSubTest() { | ||
storeKey := storetypes.NewKVStoreKey(types.StoreKey) | ||
s.ctx = testutil.DefaultContextWithDB( | ||
s.T(), | ||
storeKey, | ||
storetypes.NewTransientStoreKey("transient_test")).Ctx | ||
s.bankKeeper = rolluptestutil.NewMockBankKeeper(gomock.NewController(s.T())) | ||
s.rollupKeeper = keeper.NewKeeper( | ||
moduletestutil.MakeTestEncodingConfig().Codec, | ||
runtime.NewKVStoreService(storeKey), | ||
s.bankKeeper, | ||
) | ||
s.rollupStore = sdk.UnwrapSDKContext(s.ctx).KVStore(storeKey) | ||
} | ||
|
||
func (s *KeeperTestSuite) mockBurnETH() { | ||
s.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), types.ModuleName, gomock.Any()).Return(nil).AnyTimes() | ||
s.bankKeeper.EXPECT().BurnCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(nil).AnyTimes() | ||
} | ||
|
||
func (s *KeeperTestSuite) mockMintETH() { | ||
s.bankKeeper.EXPECT().MintCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(nil).AnyTimes() | ||
s.bankKeeper.EXPECT().SendCoinsFromModuleToAccount(gomock.Any(), types.ModuleName, gomock.Any(), gomock.Any()).Return(nil).AnyTimes() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"cosmossdk.io/math" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/ethereum-optimism/optimism/op-node/rollup/derive" | ||
"github.com/ethereum-optimism/optimism/op-service/eth" | ||
gethtypes "github.com/ethereum/go-ethereum/core/types" | ||
"github.com/golang/mock/gomock" | ||
"github.com/polymerdao/monomer/testutils" | ||
"github.com/polymerdao/monomer/x/rollup/keeper" | ||
"github.com/polymerdao/monomer/x/rollup/types" | ||
) | ||
|
||
func (s *KeeperTestSuite) TestApplyL1Txs() { | ||
l1AttributesTx, depositTx, cosmosEthTx := testutils.GenerateEthTxs(s.T()) | ||
// The only constraint for a contract creation tx is that it must be a non-system DepositTx with no To field | ||
contractCreationTx := gethtypes.NewTx(&gethtypes.DepositTx{}) | ||
|
||
l1AttributesTxBz, err := l1AttributesTx.MarshalBinary() | ||
s.Require().NoError(err) | ||
depositTxBz, err := depositTx.MarshalBinary() | ||
s.Require().NoError(err) | ||
cosmosEthTxBz, err := cosmosEthTx.MarshalBinary() | ||
s.Require().NoError(err) | ||
contractCreationTxBz, err := contractCreationTx.MarshalBinary() | ||
s.Require().NoError(err) | ||
invalidTxBz := []byte("invalid tx bytes") | ||
|
||
tests := map[string]struct { | ||
txBytes [][]byte | ||
setupMocks func() | ||
shouldError bool | ||
}{ | ||
"successful message with single user deposit tx": { | ||
txBytes: [][]byte{l1AttributesTxBz, depositTxBz}, | ||
shouldError: false, | ||
}, | ||
"successful message with multiple user deposit txs": { | ||
txBytes: [][]byte{l1AttributesTxBz, depositTxBz, depositTxBz}, | ||
shouldError: false, | ||
}, | ||
"invalid l1 attributes tx bytes": { | ||
txBytes: [][]byte{invalidTxBz, depositTxBz}, | ||
shouldError: true, | ||
}, | ||
"non-deposit tx passed in as l1 attributes tx": { | ||
txBytes: [][]byte{cosmosEthTxBz, depositTxBz}, | ||
shouldError: true, | ||
}, | ||
"user deposit tx passed in as l1 attributes tx": { | ||
txBytes: [][]byte{depositTxBz, depositTxBz}, | ||
shouldError: true, | ||
}, | ||
"invalid user deposit tx bytes": { | ||
txBytes: [][]byte{l1AttributesTxBz, invalidTxBz}, | ||
shouldError: true, | ||
}, | ||
"non-deposit tx passed in as user deposit tx": { | ||
txBytes: [][]byte{l1AttributesTxBz, cosmosEthTxBz}, | ||
shouldError: true, | ||
}, | ||
"l1 attributes tx passed in as user deposit tx": { | ||
txBytes: [][]byte{l1AttributesTxBz, l1AttributesTxBz}, | ||
shouldError: true, | ||
}, | ||
"contract creation tx passed in as user deposit tx": { | ||
txBytes: [][]byte{l1AttributesTxBz, contractCreationTxBz}, | ||
shouldError: true, | ||
}, | ||
"one valid l1 user deposit tx and an invalid tx passed in as user deposit txs": { | ||
txBytes: [][]byte{l1AttributesTxBz, depositTxBz, invalidTxBz}, | ||
shouldError: true, | ||
}, | ||
"bank keeper mint coins failure": { | ||
txBytes: [][]byte{l1AttributesTxBz, depositTxBz}, | ||
setupMocks: func() { | ||
s.bankKeeper.EXPECT().MintCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(sdkerrors.ErrUnauthorized) | ||
}, | ||
shouldError: true, | ||
}, | ||
"bank keeper send coins failure": { | ||
txBytes: [][]byte{l1AttributesTxBz, depositTxBz}, | ||
setupMocks: func() { | ||
s.bankKeeper.EXPECT().SendCoinsFromModuleToAccount(gomock.Any(), types.ModuleName, gomock.Any(), gomock.Any()).Return(sdkerrors.ErrUnknownRequest) | ||
}, | ||
shouldError: true, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
s.Run(name, func() { | ||
if test.setupMocks != nil { | ||
test.setupMocks() | ||
} | ||
s.mockMintETH() | ||
|
||
var resp *types.MsgApplyL1TxsResponse | ||
resp, err = keeper.NewMsgServerImpl(s.rollupKeeper).ApplyL1Txs(s.ctx, &types.MsgApplyL1Txs{ | ||
TxBytes: test.txBytes, | ||
}) | ||
|
||
if test.shouldError { | ||
s.Require().Error(err) | ||
s.Require().Nil(resp) | ||
} else { | ||
s.Require().NoError(err) | ||
s.Require().NotNil(resp) | ||
|
||
// TODO: Verify that the expected event types are emitted | ||
|
||
// Verify that the l1 block info and l1 block history are saved to the store | ||
expectedBlockInfo := eth.BlockToInfo(testutils.GenerateL1Block()) | ||
l1BlockInfoBz := s.rollupStore.Get([]byte(types.KeyL1BlockInfo)) | ||
historicalL1BlockInfoBz := s.rollupStore.Get(expectedBlockInfo.Hash().Bytes()) | ||
s.Require().NotNil(l1BlockInfoBz) | ||
s.Require().NotNil(historicalL1BlockInfoBz) | ||
s.Require().Equal(l1BlockInfoBz, historicalL1BlockInfoBz) | ||
|
||
var l1BlockInfo *derive.L1BlockInfo | ||
err = json.Unmarshal(l1BlockInfoBz, &l1BlockInfo) | ||
s.Require().NoError(err) | ||
s.Require().Equal(expectedBlockInfo.NumberU64(), l1BlockInfo.Number) | ||
s.Require().Equal(expectedBlockInfo.BaseFee(), l1BlockInfo.BaseFee) | ||
s.Require().Equal(expectedBlockInfo.Time(), l1BlockInfo.Time) | ||
s.Require().Equal(expectedBlockInfo.Hash(), l1BlockInfo.BlockHash) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func (s *KeeperTestSuite) TestInitiateWithdrawal() { | ||
sender := sdk.AccAddress("addr").String() | ||
l1Target := "0x12345abcde" | ||
withdrawalAmount := math.NewInt(1000000) | ||
|
||
tests := map[string]struct { | ||
sender string | ||
setupMocks func() | ||
shouldError bool | ||
}{ | ||
"successful message": { | ||
sender: sender, | ||
shouldError: false, | ||
}, | ||
"invalid sender addr": { | ||
sender: "invalid", | ||
shouldError: true, | ||
}, | ||
"bank keeper insufficient funds failure": { | ||
setupMocks: func() { | ||
s.bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), gomock.Any(), types.ModuleName, gomock.Any()).Return(types.ErrBurnETH) | ||
}, | ||
sender: sender, | ||
shouldError: true, | ||
}, | ||
"bank keeper burn coins failure": { | ||
setupMocks: func() { | ||
s.bankKeeper.EXPECT().BurnCoins(gomock.Any(), types.ModuleName, gomock.Any()).Return(sdkerrors.ErrUnknownRequest) | ||
}, | ||
sender: sender, | ||
shouldError: true, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
s.Run(name, func() { | ||
if test.setupMocks != nil { | ||
test.setupMocks() | ||
} | ||
s.mockBurnETH() | ||
|
||
resp, err := keeper.NewMsgServerImpl(s.rollupKeeper).InitiateWithdrawal(s.ctx, &types.MsgInitiateWithdrawal{ | ||
Sender: test.sender, | ||
Target: l1Target, | ||
Value: withdrawalAmount, | ||
}) | ||
|
||
if test.shouldError { | ||
s.Require().Error(err) | ||
s.Require().Nil(resp) | ||
} else { | ||
s.Require().NoError(err) | ||
s.Require().NotNil(resp) | ||
|
||
// Verify that the expected event types are emitted | ||
expectedEventTypes := []string{ | ||
sdk.EventTypeMessage, | ||
types.EventTypeWithdrawalInitiated, | ||
types.EventTypeBurnETH, | ||
} | ||
for i, event := range sdk.UnwrapSDKContext(s.ctx).EventManager().Events() { | ||
s.Require().Equal(expectedEventTypes[i], event.Type) | ||
} | ||
} | ||
}) | ||
} | ||
} |