/
deposit_log.go
357 lines (303 loc) · 10.4 KB
/
deposit_log.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package derive
import (
"encoding/binary"
"fmt"
"math/big"
"github.com/holiman/uint256"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
var (
DepositEventABI = "TransactionDeposited(address,address,uint256,bytes)"
DepositEventABIHash = crypto.Keccak256Hash([]byte(DepositEventABI))
DepositEventVersion0 = common.Hash{}
DepositEventVersion1 = common.Hash{31: 0x01}
)
// UnmarshalDepositLogEvent decodes an EVM log entry emitted by the deposit contract into typed deposit data.
//
// parse log data for:
//
// event TransactionDeposited(
// address indexed from,
// address indexed to,
// uint256 indexed version,
// bytes opaqueData
// );
//
// Additionally, the event log-index and
func UnmarshalDepositLogEvent(ev *types.Log) (*types.DepositTx, error) {
if len(ev.Topics) != 4 {
return nil, fmt.Errorf("expected 4 event topics (event identity, indexed from, indexed to, indexed version), got %d", len(ev.Topics))
}
if ev.Topics[0] != DepositEventABIHash {
return nil, fmt.Errorf("invalid deposit event selector: %s, expected %s", ev.Topics[0], DepositEventABIHash)
}
if len(ev.Data) < 64 {
return nil, fmt.Errorf("incomplate opaqueData slice header (%d bytes): %x", len(ev.Data), ev.Data)
}
if len(ev.Data)%32 != 0 {
return nil, fmt.Errorf("expected log data to be multiple of 32 bytes: got %d bytes", len(ev.Data))
}
// indexed 0
from := common.BytesToAddress(ev.Topics[1][12:])
// indexed 1
to := common.BytesToAddress(ev.Topics[2][12:])
// indexed 2
version := ev.Topics[3]
// unindexed data
// Solidity serializes the event's Data field as follows:
// abi.encode(abi.encodPacked(uint256 mint, uint256 value, uint64 gasLimit, uint8 isCreation, bytes data))
// Thus the first 32 bytes of the Data will give us the offset of the opaqueData,
// which should always be 0x20.
var opaqueContentOffset uint256.Int
opaqueContentOffset.SetBytes(ev.Data[0:32])
if !opaqueContentOffset.IsUint64() || opaqueContentOffset.Uint64() != 32 {
return nil, fmt.Errorf("invalid opaqueData slice header offset: %d", opaqueContentOffset.Uint64())
}
// The next 32 bytes indicate the length of the opaqueData content.
var opaqueContentLength uint256.Int
opaqueContentLength.SetBytes(ev.Data[32:64])
// Make sure the length is an uint64, it's not larger than the remaining data, and the log is using minimal padding (i.e. can't add 32 bytes without exceeding data)
if !opaqueContentLength.IsUint64() || opaqueContentLength.Uint64() > uint64(len(ev.Data)-64) || opaqueContentLength.Uint64()+32 <= uint64(len(ev.Data)-64) {
return nil, fmt.Errorf("invalid opaqueData slice header length: %d", opaqueContentLength.Uint64())
}
// The remaining data is the opaqueData which is tightly packed
// and then padded to 32 bytes by the EVM.
opaqueData := ev.Data[64 : 64+opaqueContentLength.Uint64()]
var dep types.DepositTx
source := UserDepositSource{
L1BlockHash: ev.BlockHash,
LogIndex: uint64(ev.Index),
}
dep.SourceHash = source.SourceHash()
dep.From = from
dep.IsSystemTransaction = false
var err error
switch version {
case DepositEventVersion0:
err = unmarshalDepositVersion0(&dep, to, opaqueData)
case DepositEventVersion1:
err = unmarshalDepositVersion1(&dep, to, opaqueData)
default:
return nil, fmt.Errorf("invalid deposit version, got %s", version)
}
if err != nil {
return nil, fmt.Errorf("failed to decode deposit (version %s): %w", version, err)
}
return &dep, nil
}
func unmarshalDepositVersion0(dep *types.DepositTx, to common.Address, opaqueData []byte) error {
if len(opaqueData) < 32+32+32+8+1 {
return fmt.Errorf("unexpected opaqueData length: %d", len(opaqueData))
}
offset := uint64(0)
// uint256 mint
dep.Mint = new(big.Int).SetBytes(opaqueData[offset : offset+32])
// 0 mint is represented as nil to skip minting code
if dep.Mint.Cmp(new(big.Int)) == 0 {
dep.Mint = nil
}
offset += 32
// uint256 value
dep.Value = new(big.Int).SetBytes(opaqueData[offset : offset+32])
offset += 32
//uint256 value for ethValue
dep.EthValue = new(big.Int).SetBytes(opaqueData[offset : offset+32])
// 0 mint is represented as nil to skip minting code
if dep.EthValue.Cmp(new(big.Int)) == 0 {
dep.EthValue = nil
}
offset += 32
// uint64 gas
gas := new(big.Int).SetBytes(opaqueData[offset : offset+8])
if !gas.IsUint64() {
return fmt.Errorf("bad gas value: %x", opaqueData[offset:offset+8])
}
dep.Gas = gas.Uint64()
offset += 8
// uint8 isCreation
// isCreation: If the boolean byte is 1 then dep.To will stay nil,
// and it will create a contract using L2 account nonce to determine the created address.
if opaqueData[offset] == 0 {
dep.To = &to
}
offset += 1
// The remainder of the opaqueData is the transaction data (without length prefix).
// The data may be padded to a multiple of 32 bytes
txDataLen := uint64(len(opaqueData)) - offset
// remaining bytes fill the data
dep.Data = opaqueData[offset : offset+txDataLen]
return nil
}
func unmarshalDepositVersion1(dep *types.DepositTx, to common.Address, opaqueData []byte) error {
if len(opaqueData) < 32+32+32+32+8+1 {
return fmt.Errorf("unexpected opaqueData length: %d", len(opaqueData))
}
offset := uint64(0)
// uint256 mint
dep.Mint = new(big.Int).SetBytes(opaqueData[offset : offset+32])
// 0 mint is represented as nil to skip minting code
if dep.Mint.Cmp(new(big.Int)) == 0 {
dep.Mint = nil
}
offset += 32
// uint256 value
dep.Value = new(big.Int).SetBytes(opaqueData[offset : offset+32])
offset += 32
//uint256 value for ethValue
dep.EthValue = new(big.Int).SetBytes(opaqueData[offset : offset+32])
// 0 mint is represented as nil to skip minting code
if dep.EthValue.Cmp(new(big.Int)) == 0 {
dep.EthValue = nil
}
offset += 32
//uint256 value for ethTxValue
dep.EthTxValue = new(big.Int).SetBytes(opaqueData[offset : offset+32])
// 0 mint is represented as nil to skip minting code
if dep.EthTxValue.Cmp(new(big.Int)) == 0 {
dep.EthTxValue = nil
}
offset += 32
// uint64 gas
gas := new(big.Int).SetBytes(opaqueData[offset : offset+8])
if !gas.IsUint64() {
return fmt.Errorf("bad gas value: %x", opaqueData[offset:offset+8])
}
dep.Gas = gas.Uint64()
offset += 8
// uint8 isCreation
// isCreation: If the boolean byte is 1 then dep.To will stay nil,
// and it will create a contract using L2 account nonce to determine the created address.
if opaqueData[offset] == 0 {
dep.To = &to
}
offset += 1
// The remainder of the opaqueData is the transaction data (without length prefix).
// The data may be padded to a multiple of 32 bytes
txDataLen := uint64(len(opaqueData)) - offset
// remaining bytes fill the data
dep.Data = opaqueData[offset : offset+txDataLen]
return nil
}
// MarshalDepositLogEventV0 returns an EVM log entry that encodes a TransactionDeposited event from the deposit contract.
// This is the reverse of the deposit transaction derivation.
func MarshalDepositLogEventV0(depositContractAddr common.Address, deposit *types.DepositTx) (*types.Log, error) {
toBytes := common.Hash{}
if deposit.To != nil {
toBytes = deposit.To.Hash()
}
topics := []common.Hash{
DepositEventABIHash,
deposit.From.Hash(),
toBytes,
DepositEventVersion0,
}
data := make([]byte, 64, 64+3*32)
// opaqueData slice content offset: value will always be 0x20.
binary.BigEndian.PutUint64(data[32-8:32], 32)
opaqueData, err := marshalDepositVersion0(deposit)
if err != nil {
return &types.Log{}, err
}
// opaqueData slice length
binary.BigEndian.PutUint64(data[64-8:64], uint64(len(opaqueData)))
// opaqueData slice content
data = append(data, opaqueData...)
// pad to multiple of 32
if len(data)%32 != 0 {
data = append(data, make([]byte, 32-(len(data)%32))...)
}
return &types.Log{
Address: depositContractAddr,
Topics: topics,
Data: data,
Removed: false,
// ignored (zeroed):
BlockNumber: 0,
TxHash: common.Hash{},
TxIndex: 0,
BlockHash: common.Hash{},
Index: 0,
}, nil
}
func marshalDepositVersion0(deposit *types.DepositTx) ([]byte, error) {
opaqueData := make([]byte, 32+32+32+8+1, 32+32+32+8+1+len(deposit.Data))
offset := 0
// uint256 mint
if deposit.Mint != nil {
if deposit.Mint.BitLen() > 256 {
return nil, fmt.Errorf("mint value exceeds 256 bits: %d", deposit.Mint)
}
deposit.Mint.FillBytes(opaqueData[offset : offset+32])
}
offset += 32
// uint256 value
if deposit.Value.BitLen() > 256 {
return nil, fmt.Errorf("value value exceeds 256 bits: %d", deposit.Value)
}
deposit.Value.FillBytes(opaqueData[offset : offset+32])
offset += 32
// uint256 value
if deposit.EthValue != nil {
if deposit.EthValue.BitLen() > 256 {
return nil, fmt.Errorf("ethValue value exceeds 256 bits: %d", deposit.EthValue)
}
deposit.EthValue.FillBytes(opaqueData[offset : offset+32])
}
offset += 32
// uint64 gas
binary.BigEndian.PutUint64(opaqueData[offset:offset+8], deposit.Gas)
offset += 8
// uint8 isCreation
if deposit.To == nil { // isCreation
opaqueData[offset] = 1
}
// Deposit data then fills the remaining event data
opaqueData = append(opaqueData, deposit.Data...)
return opaqueData, nil
}
func marshalDepositVersion1(deposit *types.DepositTx) ([]byte, error) {
opaqueData := make([]byte, 32+32+32+32+8+1, 32+32+32+32+8+1+len(deposit.Data))
offset := 0
// uint256 mint
if deposit.Mint != nil {
if deposit.Mint.BitLen() > 256 {
return nil, fmt.Errorf("mint value exceeds 256 bits: %d", deposit.Mint)
}
deposit.Mint.FillBytes(opaqueData[offset : offset+32])
}
offset += 32
// uint256 value
if deposit.Value.BitLen() > 256 {
return nil, fmt.Errorf("value value exceeds 256 bits: %d", deposit.Value)
}
deposit.Value.FillBytes(opaqueData[offset : offset+32])
offset += 32
// uint256 value
if deposit.EthValue != nil {
if deposit.EthValue.BitLen() > 256 {
return nil, fmt.Errorf("ethValue value exceeds 256 bits: %d", deposit.EthValue)
}
deposit.EthValue.FillBytes(opaqueData[offset : offset+32])
}
offset += 32
// uint256 value
if deposit.EthTxValue != nil {
if deposit.EthTxValue.BitLen() > 256 {
return nil, fmt.Errorf("ethValue value exceeds 256 bits: %d", deposit.EthTxValue)
}
deposit.EthTxValue.FillBytes(opaqueData[offset : offset+32])
}
offset += 32
// uint64 gas
binary.BigEndian.PutUint64(opaqueData[offset:offset+8], deposit.Gas)
offset += 8
// uint8 isCreation
if deposit.To == nil { // isCreation
opaqueData[offset] = 1
}
// Deposit data then fills the remaining event data
opaqueData = append(opaqueData, deposit.Data...)
return opaqueData, nil
}