Skip to content

Commit

Permalink
Add DepositsTx transaction type
Browse files Browse the repository at this point in the history
In preparation for splitting the MsgApplyL1Txs type into multiple messages,
we add a Cosmos SDK transaction type that will wrap all deposits.
  • Loading branch information
joshklop committed Nov 28, 2024
1 parent 61d9968 commit 86afcd7
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 85 deletions.
12 changes: 7 additions & 5 deletions adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,17 @@ func countDepositTransactions(ethTxs []hexutil.Bytes) (int, error) {
return numDepositTxs, nil
}

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

Expand Down Expand Up @@ -108,11 +110,11 @@ func AdaptCosmosTxsToEthTxs(cosmosTxs bfttypes.Txs) (ethtypes.Transactions, erro
}

func GetDepositTxs(txsBytes [][]byte) (ethtypes.Transactions, error) {
msg := new(rolluptypes.MsgApplyL1Txs)
msg := new(rolluptypes.DepositsTx)
if err := msg.Unmarshal(txsBytes[0]); err != nil {
return nil, fmt.Errorf("unmarshal MsgL1Txs msg: %v", err)
}
ethTxsBytes := msg.GetTxs()
ethTxsBytes := msg.GetDeposits().Txs
if len(ethTxsBytes) == 0 {
return nil, errL1AttributesNotFound
}
Expand Down
5 changes: 5 additions & 0 deletions proto/rollup/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ service Msg {
rpc UpdateParams(MsgUpdateParams) returns (MsgUpdateParamsResponse);
}

// DepositsTx is the Cosmos SDK transaction type that wraps OP Stack deposit transactions.
message DepositsTx {
MsgApplyL1Txs deposits = 1;
}

// DepositTx is a eth deposit tx.
message EthDepositTx {
// tx is the marshaled Ethereum Deposit tx.
Expand Down
35 changes: 5 additions & 30 deletions x/rollup/tx/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,22 @@ import (
"fmt"

sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/gogoproto/proto"
protov1 "github.com/golang/protobuf/proto" //nolint:staticcheck
"github.com/polymerdao/monomer/x/rollup/tx/internal"
"github.com/polymerdao/monomer/x/rollup/types"
"google.golang.org/protobuf/reflect/protoreflect"
)

type Deposit struct {
Msg *types.MsgApplyL1Txs
}

var _ sdktypes.Tx = (*Deposit)(nil)

func (d *Deposit) GetMsgs() []proto.Message {
return []proto.Message{d.Msg}
}

func (d *Deposit) GetMsgsV2() ([]protoreflect.ProtoMessage, error) {
return []protoreflect.ProtoMessage{protov1.MessageV2(d.Msg)}, nil
}

// TODO split deposits into three types:
// 1. L1Info
// 2. Mint
// 3. ForceInclude
// also add an sdktypes.PreBlocker to check that these are in the propoer order.

func DepositAnteHandler(ctx sdktypes.Context, tx sdktypes.Tx, simulate bool) (sdktypes.Context, error) { //nolint:gocritic // hugeparam
if _, ok := tx.(*Deposit); ok {
if _, ok := tx.(*types.DepositsTx); ok {
ctx = ctx.WithGasMeter(internal.NewFreeInfiniteGasMeter())
}
return ctx, nil
}

// DepositDecoder is an sdktypes.TxDecoder.
func DepositDecoder(txBytes []byte) (sdktypes.Tx, error) {
depositMsg := new(types.MsgApplyL1Txs)
if err := proto.Unmarshal(txBytes, depositMsg); err != nil {
return nil, fmt.Errorf("unmarshal proto: %v", err)
depositsTx := new(types.DepositsTx)
if err := depositsTx.Unmarshal(txBytes); err != nil {
return nil, fmt.Errorf("unmarshal deposits tx: %v", err)
}
return &Deposit{
Msg: depositMsg,
}, nil
return depositsTx, nil
}
3 changes: 2 additions & 1 deletion x/rollup/tx/helpers/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
rollupkeeper "github.com/polymerdao/monomer/x/rollup/keeper"
rolluptx "github.com/polymerdao/monomer/x/rollup/tx"
rolluptypes "github.com/polymerdao/monomer/x/rollup/types"
)

type AnteHandler struct {
Expand Down Expand Up @@ -35,7 +36,7 @@ func (a *AnteHandler) AnteHandle(
simulate bool,
) (sdktypes.Context, error) {
switch tx.(type) {
case *rolluptx.Deposit:
case *rolluptypes.DepositsTx:
newCtx, err := rolluptx.DepositAnteHandler(ctx, tx, simulate)
if err != nil {
return newCtx, fmt.Errorf("deposit ante handle: %v", err)
Expand Down
23 changes: 23 additions & 0 deletions x/rollup/types/deposit_tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package types

import (
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/gogoproto/proto"
protov1 "github.com/golang/protobuf/proto" //nolint:staticcheck
"google.golang.org/protobuf/reflect/protoreflect"
)

var _ sdktypes.Tx = (*DepositsTx)(nil)

func (d *DepositsTx) GetMsgs() []proto.Message {
return []proto.Message{d.Deposits}
}

func (d *DepositsTx) GetMsgsV2() ([]protoreflect.ProtoMessage, error) {
msgsV1 := d.GetMsgs()
msgs := make([]protoreflect.ProtoMessage, 0, len(msgsV1))
for _, msgV1 := range msgsV1 {
msgs = append(msgs, protov1.MessageV2(msgV1))
}
return msgs, nil
}
Loading

0 comments on commit 86afcd7

Please sign in to comment.