Skip to content

Commit

Permalink
Simplify adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
joshklop committed Dec 17, 2024
1 parent 780a117 commit 0123ea5
Showing 1 changed file with 61 additions and 88 deletions.
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):]

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

0 comments on commit 0123ea5

Please sign in to comment.