-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathserialization_test.go
More file actions
577 lines (549 loc) · 15.4 KB
/
serialization_test.go
File metadata and controls
577 lines (549 loc) · 15.4 KB
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
package transaction
import (
"math/big"
"strings"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/tempoxyz/tempo-go/pkg/signer"
)
func TestSerialize(t *testing.T) {
tests := []struct {
name string
tx *Tx
opts *SerializeOptions
want string
wantErr bool
}{
{
name: "minimal transaction",
tx: &Tx{
ChainID: big.NewInt(42424),
MaxPriorityFeePerGas: big.NewInt(1000000),
MaxFeePerGas: big.NewInt(2000000),
Gas: 21000,
Calls: []Call{
{
To: addrPtr(common.HexToAddress("0x1234567890123456789012345678901234567890")),
Value: big.NewInt(1000000000000000000), // 1 ETH
Data: []byte{},
},
},
AccessList: AccessList{},
NonceKey: big.NewInt(0),
Nonce: 1,
ValidBefore: 0,
ValidAfter: 0,
FeeToken: common.HexToAddress("0x20c0000000000000000000000000000000000001"),
Signature: nil,
FeePayerSignature: nil,
},
opts: nil,
// Should start with 0x76
want: "0x76",
},
{
name: "transaction with fee payer format",
tx: &Tx{
ChainID: big.NewInt(42424),
MaxPriorityFeePerGas: big.NewInt(1000000),
MaxFeePerGas: big.NewInt(2000000),
Gas: 21000,
Calls: []Call{
{
To: addrPtr(common.HexToAddress("0x1234567890123456789012345678901234567890")),
Value: big.NewInt(1000000),
Data: []byte{},
},
},
AccessList: AccessList{},
NonceKey: big.NewInt(0),
Nonce: 1,
FeeToken: common.HexToAddress("0x20c0000000000000000000000000000000000001"),
Signature: nil,
FeePayerSignature: nil,
},
opts: &SerializeOptions{
Format: FormatFeePayer,
Sender: common.HexToAddress("0xabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd"),
},
// Should start with 0x78
want: "0x78",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Serialize(tt.tx, tt.opts)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.True(t, strings.HasPrefix(got, tt.want), "Serialize() = %v, want prefix %v", got[:4], tt.want)
assert.True(t, strings.HasPrefix(got, "0x"), "Serialize() result doesn't start with 0x: %v", got[:4])
})
}
}
func TestSerializeForSigning(t *testing.T) {
tx := &Tx{
ChainID: big.NewInt(42424),
MaxPriorityFeePerGas: big.NewInt(1000000),
MaxFeePerGas: big.NewInt(2000000),
Gas: 21000,
Calls: []Call{
{
To: addrPtr(common.HexToAddress("0x1234567890123456789012345678901234567890")),
Value: big.NewInt(1000000),
Data: []byte{},
},
},
AccessList: AccessList{},
NonceKey: big.NewInt(0),
Nonce: 1,
FeeToken: common.HexToAddress("0x20c0000000000000000000000000000000000001"),
Signature: signer.NewSignatureEnvelope(big.NewInt(123), big.NewInt(456), 0),
FeePayerSignature: signer.NewSignature(big.NewInt(789), big.NewInt(101), 1),
}
got, err := SerializeForSigning(tx)
assert.NoError(t, err)
assert.True(t, strings.HasPrefix(got, "0x76"), "SerializeForSigning() = %v, want prefix 0x76", got[:4])
deserialized, err := Deserialize(got)
assert.NoError(t, err)
assert.Nil(t, deserialized.Signature, "SerializeForSigning() should not include sender signature")
assert.Nil(t, deserialized.FeePayerSignature, "SerializeForSigning() should not include fee payer signature")
}
func TestSerializeForFeePayerSigning(t *testing.T) {
sender := common.HexToAddress("0xabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd")
tx := &Tx{
ChainID: big.NewInt(42424),
MaxPriorityFeePerGas: big.NewInt(1000000),
MaxFeePerGas: big.NewInt(2000000),
Gas: 21000,
Calls: []Call{
{
To: addrPtr(common.HexToAddress("0x1234567890123456789012345678901234567890")),
Value: big.NewInt(1000000),
Data: []byte{},
},
},
AccessList: AccessList{},
NonceKey: big.NewInt(0),
Nonce: 1,
FeeToken: common.HexToAddress("0x20c0000000000000000000000000000000000001"),
Signature: signer.NewSignatureEnvelope(big.NewInt(123), big.NewInt(456), 0),
FeePayerSignature: signer.NewSignature(big.NewInt(789), big.NewInt(101), 1),
}
got, err := SerializeForFeePayerSigning(tx, sender)
assert.NoError(t, err)
assert.True(t, strings.HasPrefix(got, "0x78"), "SerializeForFeePayerSigning() = %v, want prefix 0x78", got[:4])
}
func TestDeserialize(t *testing.T) {
tests := []struct {
name string
input string
want *Tx
wantErr bool
}{
{
name: "invalid prefix",
input: "0x02f86b0180...",
wantErr: true,
},
{
name: "missing 0x76 prefix",
input: "0x01f86b0180...",
wantErr: true,
},
{
name: "empty data",
input: "",
wantErr: true,
},
{
name: "malformed hex",
input: "0x76zzz",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Deserialize(tt.input)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.NotNil(t, got)
}
})
}
}
func TestDeserializeCalls(t *testing.T) {
tests := []struct {
name string
input []interface{}
want []Call
wantErr bool
}{
{
name: "empty calls",
input: []interface{}{},
want: []Call{},
},
{
name: "single call with all fields",
input: []interface{}{
[]interface{}{
common.HexToAddress("0x1234567890123456789012345678901234567890").Bytes(),
big.NewInt(1000000).Bytes(),
[]byte{0xde, 0xad, 0xbe, 0xef},
},
},
want: []Call{
{
To: addrPtr(common.HexToAddress("0x1234567890123456789012345678901234567890")),
Value: big.NewInt(1000000),
Data: []byte{0xde, 0xad, 0xbe, 0xef},
},
},
},
{
name: "multiple calls",
input: []interface{}{
[]interface{}{
common.HexToAddress("0x1111111111111111111111111111111111111111").Bytes(),
big.NewInt(100).Bytes(),
[]byte{},
},
[]interface{}{
common.HexToAddress("0x2222222222222222222222222222222222222222").Bytes(),
big.NewInt(200).Bytes(),
[]byte{0xaa, 0xbb},
},
},
want: []Call{
{
To: addrPtr(common.HexToAddress("0x1111111111111111111111111111111111111111")),
Value: big.NewInt(100),
Data: []byte{},
},
{
To: addrPtr(common.HexToAddress("0x2222222222222222222222222222222222222222")),
Value: big.NewInt(200),
Data: []byte{0xaa, 0xbb},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := decodeCalls(tt.input)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Len(t, got, len(tt.want))
assert.True(t, cmp.Equal(tt.want, got, cmpOpts...), "decodeCalls() mismatch: %s", cmp.Diff(tt.want, got, cmpOpts...))
})
}
}
func TestDecodeAccessList(t *testing.T) {
tests := []struct {
name string
input []interface{}
want AccessList
wantErr bool
}{
{
name: "empty access list",
input: []interface{}{},
want: AccessList{},
},
{
name: "single entry with storage keys",
input: []interface{}{
[]interface{}{
common.HexToAddress("0x1234567890123456789012345678901234567890").Bytes(),
[]interface{}{
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000001").Bytes(),
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000002").Bytes(),
},
},
},
want: AccessList{
{
Address: common.HexToAddress("0x1234567890123456789012345678901234567890"),
StorageKeys: []common.Hash{
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000001"),
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000002"),
},
},
},
},
{
name: "entry with no storage keys",
input: []interface{}{
[]interface{}{
common.HexToAddress("0x1234567890123456789012345678901234567890").Bytes(),
[]interface{}{},
},
},
want: AccessList{
{
Address: common.HexToAddress("0x1234567890123456789012345678901234567890"),
StorageKeys: []common.Hash{},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := decodeAccessList(tt.input)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Len(t, got, len(tt.want))
for i := range got {
assert.Equal(t, tt.want[i].Address, got[i].Address, "decodeAccessList() entry %d address mismatch", i)
assert.Len(t, got[i].StorageKeys, len(tt.want[i].StorageKeys), "decodeAccessList() entry %d storage keys length mismatch", i)
}
})
}
}
func TestDecodeSignature(t *testing.T) {
tests := []struct {
name string
input []interface{}
want *signer.Signature
wantErr bool
}{
{
name: "valid signature",
input: []interface{}{
[]byte{0},
big.NewInt(12345).Bytes(),
big.NewInt(67890).Bytes(),
},
want: signer.NewSignature(big.NewInt(12345), big.NewInt(67890), 0),
},
{
name: "signature with yParity = 1",
input: []interface{}{
[]byte{1},
big.NewInt(12345).Bytes(),
big.NewInt(67890).Bytes(),
},
want: signer.NewSignature(big.NewInt(12345), big.NewInt(67890), 1),
},
{
name: "invalid - wrong length",
input: []interface{}{
[]byte{0},
big.NewInt(12345).Bytes(),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := decodeSignature(tt.input)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.want.YParity, got.YParity)
assert.Equal(t, 0, got.R.Cmp(tt.want.R))
assert.Equal(t, 0, got.S.Cmp(tt.want.S))
})
}
}
func TestRoundtrip(t *testing.T) {
tests := []struct {
name string
tx *Tx
}{
{
name: "minimal transaction",
tx: &Tx{
ChainID: big.NewInt(42424),
MaxPriorityFeePerGas: big.NewInt(1000000),
MaxFeePerGas: big.NewInt(2000000),
Gas: 21000,
Calls: []Call{
{
To: addrPtr(common.HexToAddress("0x1234567890123456789012345678901234567890")),
Value: big.NewInt(1000000000000000000),
Data: []byte{},
},
},
AccessList: AccessList{},
NonceKey: big.NewInt(0),
Nonce: 1,
ValidBefore: 0,
ValidAfter: 0,
FeeToken: common.HexToAddress("0x20c0000000000000000000000000000000000001"),
Signature: nil,
FeePayerSignature: nil,
},
},
{
name: "transaction with all fields",
tx: &Tx{
ChainID: big.NewInt(42424),
MaxPriorityFeePerGas: big.NewInt(1000000),
MaxFeePerGas: big.NewInt(2000000),
Gas: 21000,
Calls: []Call{
{
To: addrPtr(common.HexToAddress("0x1234567890123456789012345678901234567890")),
Value: big.NewInt(1000000000000000000),
Data: []byte{0xde, 0xad, 0xbe, 0xef},
},
},
AccessList: AccessList{
{
Address: common.HexToAddress("0xabcd1234abcd1234abcd1234abcd1234abcd1234"),
StorageKeys: []common.Hash{
common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000001"),
},
},
},
NonceKey: big.NewInt(123),
Nonce: 456,
ValidBefore: 1700000000,
ValidAfter: 1600000000,
FeeToken: common.HexToAddress("0x20c0000000000000000000000000000000000001"),
Signature: signer.NewSignatureEnvelope(
hexToBigInt("0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"),
hexToBigInt("0xfedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321"),
0,
),
FeePayerSignature: nil,
},
},
{
name: "transaction with multiple calls",
tx: &Tx{
ChainID: big.NewInt(42424),
MaxPriorityFeePerGas: big.NewInt(1000000),
MaxFeePerGas: big.NewInt(2000000),
Gas: 100000,
Calls: []Call{
{
To: addrPtr(common.HexToAddress("0x1111111111111111111111111111111111111111")),
Value: big.NewInt(1000),
Data: []byte{},
},
{
To: addrPtr(common.HexToAddress("0x2222222222222222222222222222222222222222")),
Value: big.NewInt(2000),
Data: []byte{0xaa, 0xbb},
},
{
To: addrPtr(common.HexToAddress("0x3333333333333333333333333333333333333333")),
Value: big.NewInt(3000),
Data: []byte{0xcc, 0xdd, 0xee},
},
},
AccessList: AccessList{},
NonceKey: big.NewInt(0),
Nonce: 1,
ValidBefore: 0,
ValidAfter: 0,
FeeToken: common.HexToAddress("0x20c0000000000000000000000000000000000001"),
Signature: nil,
FeePayerSignature: nil,
},
},
{
name: "transaction with dual signatures",
tx: &Tx{
ChainID: big.NewInt(42424),
MaxPriorityFeePerGas: big.NewInt(1000000),
MaxFeePerGas: big.NewInt(2000000),
Gas: 21000,
Calls: []Call{
{
To: addrPtr(common.HexToAddress("0x1234567890123456789012345678901234567890")),
Value: big.NewInt(1000000),
Data: []byte{},
},
},
AccessList: AccessList{},
NonceKey: big.NewInt(0),
Nonce: 1,
ValidBefore: 0,
ValidAfter: 0,
FeeToken: common.HexToAddress("0x20c0000000000000000000000000000000000001"),
Signature: signer.NewSignatureEnvelope(
big.NewInt(12345),
big.NewInt(67890),
0,
),
FeePayerSignature: signer.NewSignature(
big.NewInt(99999),
big.NewInt(88888),
1,
),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
serialized, err := Serialize(tt.tx, nil)
assert.NoError(t, err)
deserialized, err := Deserialize(serialized)
assert.NoError(t, err)
assert.True(t, cmp.Equal(tt.tx, deserialized, cmpOpts...), "Roundtrip failed: %s", cmp.Diff(tt.tx, deserialized, cmpOpts...))
// == roundtrip again ==
serialized2, err := Serialize(deserialized, nil)
assert.NoError(t, err)
assert.Equal(t, serialized, serialized2)
})
}
}
// TestRoundtripWithOptions tests roundtrip with different serialization options.
func TestRoundtripWithOptions(t *testing.T) {
tx := &Tx{
ChainID: big.NewInt(42424),
MaxPriorityFeePerGas: big.NewInt(1000000),
MaxFeePerGas: big.NewInt(2000000),
Gas: 21000,
Calls: []Call{
{
To: addrPtr(common.HexToAddress("0x1234567890123456789012345678901234567890")),
Value: big.NewInt(1000000),
Data: []byte{},
},
},
AccessList: AccessList{},
NonceKey: big.NewInt(0),
Nonce: 1,
ValidBefore: 0,
ValidAfter: 0,
FeeToken: common.HexToAddress("0x20c0000000000000000000000000000000000001"),
Signature: signer.NewSignatureEnvelope(
big.NewInt(12345),
big.NewInt(67890),
0,
),
FeePayerSignature: nil,
}
t.Run("normal format", func(t *testing.T) {
serialized, err := Serialize(tx, &SerializeOptions{Format: FormatNormal})
assert.NoError(t, err)
deserialized, err := Deserialize(serialized)
assert.NoError(t, err)
assert.True(t, cmp.Equal(tx, deserialized, cmpOpts...), "Roundtrip with normal format failed: %s", cmp.Diff(tx, deserialized, cmpOpts...))
})
}
// Helper functions
func hexToBigInt(s string) *big.Int {
n := new(big.Int)
n.SetString(s[2:], 16) // Remove 0x prefix
return n
}