Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify adapters #321

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 61 additions & 88 deletions adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,61 +13,21 @@ import (
rolluptypes "github.com/polymerdao/monomer/x/rollup/types"
)

var errL1AttributesNotFound = errors.New("L1 attributes tx not found")

// AdaptPayloadTxsToCosmosTxs assumes the deposit transactions come first.
func AdaptPayloadTxsToCosmosTxs(ethTxs []hexutil.Bytes) (bfttypes.Txs, error) {
// L1 Attributes transaction.
if len(ethTxs) == 0 {
return bfttypes.Txs{}, nil
}

numDepositTxs, err := countDepositTransactions(ethTxs)
if err != nil {
return nil, fmt.Errorf("count deposit transactions: %v", err)
}

depositTx, err := packDepositTxsToCosmosTx(ethTxs[:numDepositTxs], "")
if err != nil {
return nil, fmt.Errorf("pack deposit txs: %v", err)
}
depositTxBytes, err := depositTx.Marshal()
if err != nil {
return nil, fmt.Errorf("marshal tx: %v", err)
}

cosmosNonDepositTxs, err := convertToCosmosNonDepositTxs(ethTxs[numDepositTxs:])
if err != nil {
return nil, fmt.Errorf("convert to cosmos txs: %v", err)
return nil, errors.New("l1 attributes transaction not found")
}

return append(bfttypes.Txs{depositTxBytes}, cosmosNonDepositTxs...), nil
}

func countDepositTransactions(ethTxs []hexutil.Bytes) (int, error) {
var numDepositTxs int
for _, txBytes := range ethTxs {
var tx ethtypes.Transaction
if err := tx.UnmarshalBinary(txBytes); err != nil {
return 0, fmt.Errorf("unmarshal binary: %v", err)
}
if tx.IsDepositTx() {
numDepositTxs++
} else {
break // Assume deposit transactions must come first.
}
}
if numDepositTxs == 0 {
return 0, errL1AttributesNotFound
}

return numDepositTxs, nil
}

func packDepositTxsToCosmosTx(ethDepositTxs []hexutil.Bytes, _ string) (*rolluptypes.DepositsTx, error) {
l1AttributesTxBytes := ethTxs[0]
ethTxs = ethTxs[1:]
var ethL1AttributesTx ethtypes.Transaction
if err := ethL1AttributesTx.UnmarshalBinary(ethDepositTxs[0]); err != nil {
if err := ethL1AttributesTx.UnmarshalBinary(l1AttributesTxBytes); err != nil {
return nil, fmt.Errorf("unmarshal binary: %v", err)
}
if ethL1AttributesTx.Type() != ethtypes.DepositTxType {
return nil, errors.New("first transaction is not a deposit transaction")
}
l1BlockInfo, err := derive.L1BlockInfoFromBytes(&rollup.Config{}, uint64(time.Now().Unix()), ethL1AttributesTx.Data())
if err != nil {
return nil, fmt.Errorf("l1 block info from bytes: %v", err)
Expand All @@ -84,37 +44,51 @@ func packDepositTxsToCosmosTx(ethDepositTxs []hexutil.Bytes, _ string) (*rollupt
BaseFeeScalar: l1BlockInfo.BaseFeeScalar,
BlobBaseFeeScalar: l1BlockInfo.BlobBaseFeeScalar,
},
EthTx: ethDepositTxs[0],
EthTx: l1AttributesTxBytes,
}
if l1Attributes.L1BlockInfo.BaseFee != nil {
if l1BlockInfo.BaseFee != nil {
l1Attributes.L1BlockInfo.BaseFee = l1BlockInfo.BaseFee.Bytes()
}
if l1Attributes.L1BlockInfo.BlobBaseFee != nil {
if l1BlockInfo.BlobBaseFee != nil {
l1Attributes.L1BlockInfo.BlobBaseFee = l1BlockInfo.BlobBaseFee.Bytes()
}

// User deposit transactions.
depositTxs := make([]*rolluptypes.MsgApplyUserDeposit, 0)
if len(ethDepositTxs) > 1 {
for _, ethDepositTx := range ethDepositTxs[1:] {
depositTxs = append(depositTxs, &rolluptypes.MsgApplyUserDeposit{
Tx: ethDepositTx,
})
for _, ethDepositTxBytes := range ethTxs {
var ethDepositTx ethtypes.Transaction
if err := ethDepositTx.UnmarshalBinary(ethDepositTxBytes); err != nil {
return nil, fmt.Errorf("unmarshal binary eth deposit tx bytes: %v", err)
}
if !ethDepositTx.IsDepositTx() {
break // We have reached the end of the deposit txs.
}
depositTxs = append(depositTxs, &rolluptypes.MsgApplyUserDeposit{
Tx: ethDepositTxBytes,
})
}
return &rolluptypes.DepositsTx{
ethTxs = ethTxs[len(depositTxs):]
Comment on lines +58 to +70
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Ensure all deposit transactions are processed regardless of order

The loop from lines 58 to 69 processes deposit transactions until it encounters a non-deposit transaction, then breaks. If there are deposit transactions after non-deposit transactions in ethTxs, they will not be processed, potentially leading to missed deposits.

Consider processing all transactions and separating deposit and non-deposit transactions regardless of order. Here's how you might modify the code:

depositTxs := []*rolluptypes.MsgApplyUserDeposit{}
nonDepositTxs := []hexutil.Bytes{}

for _, ethTxBytes := range ethTxs {
	var ethTx ethtypes.Transaction
	if err := ethTx.UnmarshalBinary(ethTxBytes); err != nil {
		return nil, fmt.Errorf("unmarshal binary eth tx bytes: %v", err)
	}
	if ethTx.IsDepositTx() {
		depositTxs = append(depositTxs, &rolluptypes.MsgApplyUserDeposit{
			Tx: ethTxBytes,
		})
	} else {
		nonDepositTxs = append(nonDepositTxs, ethTxBytes)
	}
}

ethTxs = nonDepositTxs

This ensures that all deposit transactions are processed regardless of their position in ethTxs.


cosmosTxs := make(bfttypes.Txs, 0, 1+len(ethTxs))

depositTxBytes, err := (&rolluptypes.DepositsTx{
L1Attributes: l1Attributes,
UserDeposits: depositTxs,
}, nil
}
}).Marshal()
if err != nil {
return nil, fmt.Errorf("marshal tx: %v", err)
}
cosmosTxs = append(cosmosTxs, depositTxBytes)

func convertToCosmosNonDepositTxs(nonDepositTxs []hexutil.Bytes) (bfttypes.Txs, error) {
// Unpack Cosmos txs from ethTxs.
cosmosTxs := make(bfttypes.Txs, 0, len(nonDepositTxs))
for _, cosmosTx := range nonDepositTxs {
// Non-deposit transactions.
for _, cosmosTx := range ethTxs {
var tx ethtypes.Transaction
if err := tx.UnmarshalBinary(cosmosTx); err != nil {
return nil, fmt.Errorf("unmarshal binary tx: %v", err)
}
if tx.IsDepositTx() {
return nil, errors.New("found a deposit tx after a non-deposit tx")
}
cosmosTxs = append(cosmosTxs, tx.Data())
}

Expand All @@ -126,42 +100,41 @@ func AdaptCosmosTxsToEthTxs(cosmosTxs bfttypes.Txs) (ethtypes.Transactions, erro
return ethtypes.Transactions{}, nil
}
txsBytes := cosmosTxs.ToSliceOfBytes()
ethTxs, err := GetDepositTxs(txsBytes[0])
if err != nil {
return nil, fmt.Errorf("get deposit txs: %v", err)
}
if len(txsBytes) > 1 {
for _, txBytes := range txsBytes[1:] {
ethTxs = append(ethTxs, AdaptNonDepositCosmosTxToEthTx(txBytes))
}
}

return ethTxs, nil
}
cosmosDepositTxBytes := txsBytes[0]
txsBytes = txsBytes[1:]

func GetDepositTxs(cosmosDepositTx []byte) (ethtypes.Transactions, error) {
msg := new(rolluptypes.DepositsTx)
if err := msg.Unmarshal(cosmosDepositTx); err != nil {
cosmosDepositTx := new(rolluptypes.DepositsTx)
if err := cosmosDepositTx.Unmarshal(cosmosDepositTxBytes); err != nil {
return nil, fmt.Errorf("unmarshal MsgL1Txs msg: %v", err)
}

// L1 Attributes.
var ethL1AttributesTx ethtypes.Transaction
if err := ethL1AttributesTx.UnmarshalBinary(msg.L1Attributes.EthTx); err != nil {
if err := ethL1AttributesTx.UnmarshalBinary(cosmosDepositTx.L1Attributes.EthTx); err != nil {
return nil, fmt.Errorf("unmarshal binary l1 attributes tx: %v", err)
}
txs := ethtypes.Transactions{&ethL1AttributesTx}

ethTxsBytes := msg.GetUserDeposits()
for _, userDepositTx := range ethTxsBytes {
var tx ethtypes.Transaction
if err := tx.UnmarshalBinary(userDepositTx.Tx); err != nil {
ethTxs := ethtypes.Transactions{&ethL1AttributesTx}

// User Deposits.
cosmosUserDepositTxs := cosmosDepositTx.GetUserDeposits()
for _, cosmosUserDepositTx := range cosmosUserDepositTxs {
var ethUserDepositTx ethtypes.Transaction
if err := ethUserDepositTx.UnmarshalBinary(cosmosUserDepositTx.Tx); err != nil {
return nil, fmt.Errorf("unmarshal binary user deposit tx: %v", err)
}
if !tx.IsDepositTx() {
return nil, errors.New("MsgL1Tx contains non-deposit tx")
if !ethUserDepositTx.IsDepositTx() {
return nil, errors.New("cosmos deposit tx contains non-deposit tx")
}
txs = append(txs, &tx)
ethTxs = append(ethTxs, &ethUserDepositTx)
}

// Non-deposit transactions.
for _, txBytes := range txsBytes {
ethTxs = append(ethTxs, AdaptNonDepositCosmosTxToEthTx(txBytes))
}
return txs, nil

return ethTxs, nil
}

func AdaptNonDepositCosmosTxToEthTx(cosmosTx bfttypes.Tx) *ethtypes.Transaction {
Expand Down
Loading