-
Notifications
You must be signed in to change notification settings - Fork 20
/
adapters.go
172 lines (153 loc) · 5.27 KB
/
adapters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package monomer
import (
"errors"
"fmt"
"time"
bfttypes "github.com/cometbft/cometbft/types"
"github.com/ethereum-optimism/optimism/op-node/rollup"
"github.com/ethereum-optimism/optimism/op-node/rollup/derive"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
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) {
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 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) {
var ethL1AttributesTx ethtypes.Transaction
if err := ethL1AttributesTx.UnmarshalBinary(ethDepositTxs[0]); err != nil {
return nil, fmt.Errorf("unmarshal binary: %v", err)
}
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)
}
l1Attributes := &rolluptypes.MsgSetL1Attributes{
L1BlockInfo: &rolluptypes.L1BlockInfo{
Number: l1BlockInfo.Number,
Time: l1BlockInfo.Time,
BlockHash: l1BlockInfo.BlockHash[:],
SequenceNumber: l1BlockInfo.SequenceNumber,
BatcherAddr: l1BlockInfo.BatcherAddr[:],
L1FeeOverhead: l1BlockInfo.L1FeeOverhead[:],
L1FeeScalar: l1BlockInfo.L1FeeScalar[:],
BaseFeeScalar: l1BlockInfo.BaseFeeScalar,
BlobBaseFeeScalar: l1BlockInfo.BlobBaseFeeScalar,
},
EthTx: ethDepositTxs[0],
}
if l1Attributes.L1BlockInfo.BaseFee != nil {
l1Attributes.L1BlockInfo.BaseFee = l1BlockInfo.BaseFee.Bytes()
}
if l1Attributes.L1BlockInfo.BlobBaseFee != nil {
l1Attributes.L1BlockInfo.BlobBaseFee = l1BlockInfo.BlobBaseFee.Bytes()
}
depositTxs := make([]*rolluptypes.MsgApplyUserDeposit, 0)
if len(ethDepositTxs) > 1 {
for _, ethDepositTx := range ethDepositTxs[1:] {
depositTxs = append(depositTxs, &rolluptypes.MsgApplyUserDeposit{
Tx: ethDepositTx,
})
}
}
return &rolluptypes.DepositsTx{
L1Attributes: l1Attributes,
UserDeposits: depositTxs,
}, nil
}
func convertToCosmosNonDepositTxs(nonDepositTxs []hexutil.Bytes) (bfttypes.Txs, error) {
// Unpack Cosmos txs from ethTxs.
cosmosTxs := make(bfttypes.Txs, 0, len(nonDepositTxs))
for _, cosmosTx := range nonDepositTxs {
var tx ethtypes.Transaction
if err := tx.UnmarshalBinary(cosmosTx); err != nil {
return nil, fmt.Errorf("unmarshal binary tx: %v", err)
}
cosmosTxs = append(cosmosTxs, tx.Data())
}
return cosmosTxs, nil
}
func AdaptCosmosTxsToEthTxs(cosmosTxs bfttypes.Txs) (ethtypes.Transactions, error) {
if cosmosTxs.Len() == 0 {
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
}
func GetDepositTxs(cosmosDepositTx []byte) (ethtypes.Transactions, error) {
msg := new(rolluptypes.DepositsTx)
if err := msg.Unmarshal(cosmosDepositTx); err != nil {
return nil, fmt.Errorf("unmarshal MsgL1Txs msg: %v", err)
}
var ethL1AttributesTx ethtypes.Transaction
if err := ethL1AttributesTx.UnmarshalBinary(msg.L1Attributes.EthTx); err != nil {
return nil, fmt.Errorf("unmarshal binary l1 attributes tx: %v", err)
}
txs := ethtypes.Transactions{ðL1AttributesTx}
ethTxsBytes := msg.GetUserDeposits()
for _, userDepositTx := range ethTxsBytes {
var tx ethtypes.Transaction
if err := tx.UnmarshalBinary(userDepositTx.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")
}
txs = append(txs, &tx)
}
return txs, nil
}
func AdaptNonDepositCosmosTxToEthTx(cosmosTx bfttypes.Tx) *ethtypes.Transaction {
return ethtypes.NewTx(ðtypes.DynamicFeeTx{
// TODO maybe fill in other fields?
Data: cosmosTx,
})
}