Skip to content

Commit

Permalink
Add EthDepositTx proto
Browse files Browse the repository at this point in the history
Previously, we used `repeated bytes` for the type of the deposit transactions in
`MsgApplyL1Txs`. Unfortunately, this led to arbitrary byte strings unmarshaling
into the `MsgApplyL1Txs` type. By encapsulating each deposit tx in a separate
`EthDepositTx` type, we avoid this issue.
  • Loading branch information
joshklop committed Nov 28, 2024
1 parent 07d7585 commit 61d9968
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 86 deletions.
18 changes: 10 additions & 8 deletions adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ func countDepositTransactions(ethTxs []hexutil.Bytes) (int, error) {
return numDepositTxs, nil
}

func packDepositTxsToCosmosTx(depositTxs []hexutil.Bytes, _ string) (*rolluptypes.MsgApplyL1Txs, error) { //nolint:unparam
depositTxsBytes := make([][]byte, 0, len(depositTxs))
for _, depositTx := range depositTxs {
depositTxsBytes = append(depositTxsBytes, depositTx)
func packDepositTxsToCosmosTx(ethDepositTxs []hexutil.Bytes, _ string) (*rolluptypes.MsgApplyL1Txs, error) { //nolint:unparam
depositTxs := make([]*rolluptypes.EthDepositTx, 0, len(ethDepositTxs))
for _, ethDepositTx := range ethDepositTxs {
depositTxs = append(depositTxs, &rolluptypes.EthDepositTx{
Tx: ethDepositTx,
})
}
return &rolluptypes.MsgApplyL1Txs{
TxBytes: depositTxsBytes,
Txs: depositTxs,
}, nil
}

Expand Down Expand Up @@ -110,14 +112,14 @@ func GetDepositTxs(txsBytes [][]byte) (ethtypes.Transactions, error) {
if err := msg.Unmarshal(txsBytes[0]); err != nil {
return nil, fmt.Errorf("unmarshal MsgL1Txs msg: %v", err)
}
ethTxsBytes := msg.GetTxBytes()
ethTxsBytes := msg.GetTxs()
if len(ethTxsBytes) == 0 {
return nil, errL1AttributesNotFound
}
txs := make(ethtypes.Transactions, 0, len(ethTxsBytes)+len(txsBytes)-1)
for _, txBytes := range ethTxsBytes {
for _, userDepositTx := range ethTxsBytes {
var tx ethtypes.Transaction
if err := tx.UnmarshalBinary(txBytes); err != nil {
if err := tx.UnmarshalBinary(userDepositTx.Tx); err != nil {
return nil, fmt.Errorf("unmarshal binary: %v", err)
}
if !tx.IsDepositTx() {
Expand Down
11 changes: 8 additions & 3 deletions proto/rollup/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ service Msg {
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);
}

// DepositTx is a eth deposit tx.
message EthDepositTx {
// tx is the marshaled Ethereum Deposit tx.
bytes tx = 1;
}

// MsgApplyL1Txs defines the message for applying all L1 system and user deposit txs.
message MsgApplyL1Txs {
// Array of bytes where each bytes is a eth.Transaction.MarshalBinary tx.
// The first tx must be the L1 system deposit tx, and the rest are user txs if present.
repeated bytes tx_bytes = 1;
// txs are all of the system and user deposit txs.
repeated EthDepositTx txs = 1;
}

// MsgApplyL1TxsResponse defines the Msg/ApplyL1Txs response type.
Expand Down
4 changes: 2 additions & 2 deletions x/rollup/keeper/deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ func (k *Keeper) processL1AttributesTx(ctx sdk.Context, txBytes []byte) (*types.
// and returns associated events.
func (k *Keeper) processL1UserDepositTxs(
ctx sdk.Context, //nolint:gocritic // hugeParam
txs [][]byte,
txs []*types.EthDepositTx,
l1blockInfo *types.L1BlockInfo,
) (sdk.Events, error) {
mintEvents := sdk.Events{}

// skip the first tx - it is the L1 attributes tx
for i := 1; i < len(txs); i++ {
txBytes := txs[i]
txBytes := txs[i].Tx
var tx ethtypes.Transaction
if err := tx.UnmarshalBinary(txBytes); err != nil {
return nil, types.WrapError(types.ErrInvalidL1Txs, "failed to unmarshal user deposit transaction", "index", i, "err", err)
Expand Down
6 changes: 3 additions & 3 deletions x/rollup/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ var _ types.MsgServer = &Keeper{}
func (k *Keeper) ApplyL1Txs(goCtx context.Context, msg *types.MsgApplyL1Txs) (*types.MsgApplyL1TxsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

ctx.Logger().Debug("Processing L1 txs", "txCount", len(msg.TxBytes))
ctx.Logger().Debug("Processing L1 txs", "txCount", len(msg.Txs))

// process L1 attributes tx and get L1 block info
l1blockInfo, err := k.processL1AttributesTx(ctx, msg.TxBytes[0])
l1blockInfo, err := k.processL1AttributesTx(ctx, msg.Txs[0].Tx)
if err != nil {
return nil, types.WrapError(types.ErrProcessL1SystemDepositTx, "err: %v", err)
}
Expand All @@ -34,7 +34,7 @@ func (k *Keeper) ApplyL1Txs(goCtx context.Context, msg *types.MsgApplyL1Txs) (*t
ctx.Logger().Debug("Save L1 block info", "l1blockInfo", string(lo.Must(l1blockInfo.Marshal())))

// process L1 user deposit txs
mintEvents, err := k.processL1UserDepositTxs(ctx, msg.TxBytes, l1blockInfo)
mintEvents, err := k.processL1UserDepositTxs(ctx, msg.Txs, l1blockInfo)
if err != nil {
return nil, types.WrapError(types.ErrProcessL1UserDepositTxs, "err: %v", err)
}
Expand Down
9 changes: 8 additions & 1 deletion x/rollup/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,15 @@ func (s *KeeperTestSuite) TestApplyL1Txs() {
}
s.mockMintETH()

depositTxs := make([]*types.EthDepositTx, 0)
for _, txBytes := range test.txBytes {
depositTxs = append(depositTxs, &types.EthDepositTx{
Tx: txBytes,
})
}

resp, err := s.rollupKeeper.ApplyL1Txs(s.ctx, &types.MsgApplyL1Txs{
TxBytes: test.txBytes,
Txs: depositTxs,
})

if test.shouldError {
Expand Down
24 changes: 22 additions & 2 deletions x/rollup/tests/integration/rollup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,33 @@ func TestRollup(t *testing.T) {

// send an invalid MsgApplyL1Txs and assert error
_, err = integrationApp.RunMsg(&rolluptypes.MsgApplyL1Txs{
TxBytes: [][]byte{l1AttributesTxBz, l1AttributesTxBz},
Txs: []*rolluptypes.EthDepositTx{
{
Tx: l1AttributesTxBz,
},
{
Tx: l1AttributesTxBz,
},
},
})
require.Error(t, err)

// send a successful MsgApplyL1Txs and mint ETH to user
_, err = integrationApp.RunMsg(&rolluptypes.MsgApplyL1Txs{
TxBytes: [][]byte{l1AttributesTxBz, ethDepositTxBz, ethBridgeDepositTxBz, erc20DepositTxBz},
Txs: []*rolluptypes.EthDepositTx{
{
Tx: l1AttributesTxBz,
},
{
Tx: ethDepositTxBz,
},
{
Tx: ethBridgeDepositTxBz,
},
{
Tx: erc20DepositTxBz,
},
},
})
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion x/rollup/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
var _ sdktypes.Msg = (*MsgApplyL1Txs)(nil)

func (m *MsgApplyL1Txs) ValidateBasic() error {
if m.TxBytes == nil || len(m.TxBytes) < 1 {
if m.Txs == nil || len(m.Txs) < 1 {
return WrapError(ErrInvalidL1Txs, "must have at least one L1 Info Deposit tx")
}
return nil
Expand Down
Loading

0 comments on commit 61d9968

Please sign in to comment.