-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathgenerators.go
More file actions
6196 lines (6125 loc) · 207 KB
/
generators.go
File metadata and controls
6196 lines (6125 loc) · 207 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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package testgen
import (
"bytes"
"context"
"errors"
"fmt"
"math/big"
"slices"
"strings"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/ethclient/gethclient"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/holiman/uint256"
"golang.org/x/exp/maps"
)
var (
emitContract = common.HexToAddress("0x7dcd17433742f4c0ca53122ab541d0ba67fc27df")
nonAccount = common.HexToAddress("0xc1cadaffffffffffffffffffffffffffffffffff")
)
type T struct {
eth *ethclient.Client
geth *gethclient.Client
rpc *rpc.Client
chain *Chain
}
func NewT(client *rpc.Client, chain *Chain) *T {
eth := ethclient.NewClient(client)
geth := gethclient.New(client)
return &T{eth, geth, client, chain}
}
// MethodTests is a collection of tests for a certain JSON-RPC method.
type MethodTests struct {
Name string
Tests []Test
}
// Test is a wrapper for a function that performs an interaction with the
// client.
type Test struct {
Name string
About string
// If SpecOnly is true, the client response doesn't have to match exactly and is
// checked for spec validity only.
SpecOnly bool
Run func(context.Context, *T) error
}
// AllMethods is a slice of all JSON-RPC methods with tests.
var AllMethods = []MethodTests{
EthBlockNumber,
EthGetBlockByNumber,
EthGetBlockByHash,
EthGetProof,
EthChainID,
EthGetBalance,
EthGetCode,
EthGetStorage,
EthCall,
EthSimulateV1,
EthEstimateGas,
EthCreateAccessList,
EthGetBlockTransactionCountByNumber,
EthGetBlockTransactionCountByHash,
EthGetTransactionByBlockHashAndIndex,
EthGetTransactionByBlockNumberAndIndex,
EthGetTransactionCount,
EthGetTransactionByHash,
EthGetTransactionReceipt,
EthGetBlockReceipts,
EthSendRawTransaction,
EthSyncing,
EthFeeHistory,
EthGetLogs,
DebugGetRawHeader,
DebugGetRawBlock,
DebugGetRawReceipts,
DebugGetRawTransaction,
EthBlobBaseFee,
NetVersion,
// -- gas price tests are disabled because of non-determinism
// EthGasPrice,
// EthMaxPriorityFeePerGas,
// -- uncle APIs are not required anymore after the merge
// EthGetUncleByBlockNumberAndIndex,
}
// EthBlockNumber stores a list of all tests against the method.
var EthBlockNumber = MethodTests{
"eth_blockNumber",
[]Test{
{
Name: "simple-test",
About: "retrieves the client's current block number",
Run: func(ctx context.Context, t *T) error {
got, err := t.eth.BlockNumber(ctx)
if err != nil {
return err
} else if want := t.chain.Head().NumberU64(); got != want {
return fmt.Errorf("unexpect current block number (got: %d, want: %d)", got, want)
}
return nil
},
},
},
}
// EthChainID stores a list of all tests against the method.
var EthChainID = MethodTests{
"eth_chainId",
[]Test{
{
Name: "get-chain-id",
About: "retrieves the client's current chain id",
Run: func(ctx context.Context, t *T) error {
got, err := t.eth.ChainID(ctx)
if err != nil {
return err
} else if want := t.chain.Config().ChainID.Uint64(); got.Uint64() != want {
return fmt.Errorf("unexpect chain id (got: %d, want: %d)", got, want)
}
return nil
},
},
},
}
// EthGetCode stores a list of all tests against the method.
var EthGetCode = MethodTests{
"eth_getCode",
[]Test{
{
Name: "get-code",
About: "requests code of an existing contract",
Run: func(ctx context.Context, t *T) error {
var got hexutil.Bytes
err := t.rpc.CallContext(ctx, &got, "eth_getCode", emitContract, "latest")
if err != nil {
return err
}
want := t.chain.state[emitContract].Code
if !bytes.Equal(got, want) {
return fmt.Errorf("unexpected code (got: %s, want %s)", got, want)
}
return nil
},
},
{
Name: "get-code-eip7702-delegation",
About: `requests code of an account that has an EIP-7702 delegation. the server is expected to return
the delegation designator.`,
Run: func(ctx context.Context, t *T) error {
account := t.chain.txinfo.EIP7702.Account
var got hexutil.Bytes
err := t.rpc.CallContext(ctx, &got, "eth_getCode", account, "latest")
if err != nil {
return err
}
want := t.chain.state[account].Code
if !bytes.Equal(got, want) {
return fmt.Errorf("unexpected code (got: %s, want %s)", got, want)
}
return nil
},
},
{
Name: "get-code-unknown-account",
About: "requests code of a non-existent account",
Run: func(ctx context.Context, t *T) error {
var got hexutil.Bytes
err := t.rpc.CallContext(ctx, &got, "eth_getCode", nonAccount, "latest")
if err != nil {
return err
}
if len(got) > 0 {
return fmt.Errorf("account %v has non-empty code", nonAccount)
}
return nil
},
},
},
}
// EthGetStorage stores a list of all tests against the method.
var EthGetStorage = MethodTests{
"eth_getStorageAt",
[]Test{
{
Name: "get-storage",
About: "gets storage of a contract",
Run: func(ctx context.Context, t *T) error {
addr := emitContract
key := common.Hash{}
got, err := t.eth.StorageAt(ctx, addr, key, nil)
if err != nil {
return err
}
want := t.chain.Storage(addr, key)
if !bytes.Equal(got, want) {
return fmt.Errorf("unexpected storage value (got: %s, want %s)", got, want)
}
// Check for any non-zero byte in the value.
// If it's all-zero, the slot doesn't really exist, indicating a problem with the test itself.
nz := slices.ContainsFunc(got, func(b byte) bool { return b != 0 })
if !nz {
return fmt.Errorf("requested storage slot is zero")
}
return nil
},
},
{
Name: "get-storage-unknown-account",
About: "gets storage of a non-existent account",
Run: func(ctx context.Context, t *T) error {
key := common.Hash{1}
got, err := t.eth.StorageAt(ctx, nonAccount, key, nil)
if err != nil {
return err
}
nz := slices.ContainsFunc(got, func(b byte) bool { return b != 0 })
if nz {
return fmt.Errorf("storage is non-empty")
}
return nil
},
},
{
Name: "get-storage-invalid-key-too-large",
About: "requests an invalid storage key",
Run: func(ctx context.Context, t *T) error {
err := t.rpc.CallContext(ctx, nil, "eth_getStorageAt", "0xaa00000000000000000000000000000000000000", "0x00000000000000000000000000000000000000000000000000000000000000000", "latest")
if err == nil {
return fmt.Errorf("expected error")
}
return nil
},
},
{
Name: "get-storage-invalid-key",
About: "requests an invalid storage key",
Run: func(ctx context.Context, t *T) error {
err := t.rpc.CallContext(ctx, nil, "eth_getStorageAt", "0xaa00000000000000000000000000000000000000", "0xasdf", "latest")
if err == nil {
return fmt.Errorf("expected error")
}
return nil
},
},
},
}
// EthGetBlockByHash stores a list of all tests against the method.
var EthGetBlockByHash = MethodTests{
"eth_getBlockByHash",
[]Test{
{
Name: "get-block-by-hash",
About: "gets block 1",
Run: func(ctx context.Context, t *T) error {
want := t.chain.GetBlock(1).Header()
got, err := t.eth.BlockByHash(ctx, want.Hash())
if err != nil {
return err
}
if got.Hash() != want.Hash() {
return fmt.Errorf("unexpected block (got: %s, want: %s)", got.Hash(), want.Hash())
}
return nil
},
},
{
Name: "get-block-by-empty-hash",
About: "gets block empty hash",
Run: func(ctx context.Context, t *T) error {
_, err := t.eth.BlockByHash(ctx, common.Hash{})
if !errors.Is(err, ethereum.NotFound) {
return errors.New("expected not found error")
}
return nil
},
},
{
Name: "get-block-by-notfound-hash",
About: "gets block not found hash",
Run: func(ctx context.Context, t *T) error {
_, err := t.eth.BlockByHash(ctx, common.HexToHash("deadbeef"))
if !errors.Is(err, ethereum.NotFound) {
return errors.New("expected not found error")
}
return nil
},
},
},
}
// EthChainID stores a list of all tests against the method.
var EthGetBalance = MethodTests{
"eth_getBalance",
[]Test{
{
Name: "get-balance",
About: "retrieves the an account balance",
Run: func(ctx context.Context, t *T) error {
addr := emitContract
got, err := t.eth.BalanceAt(ctx, addr, nil)
if err != nil {
return err
}
want := t.chain.Balance(addr)
if got.Cmp(want) != 0 {
return fmt.Errorf("unexpect balance (got: %d, want: %d)", got, want)
}
return nil
},
},
{
Name: "get-balance-unknown-account",
About: "requests the balance of a non-existent account",
Run: func(ctx context.Context, t *T) error {
got, err := t.eth.BalanceAt(ctx, nonAccount, nil)
if err != nil {
return err
}
if got.Sign() > 0 {
return fmt.Errorf("account %v has non-zero balance", nonAccount)
}
return nil
},
},
{
Name: "get-balance-blockhash",
About: "retrieves the an account's balance at a specific blockhash",
Run: func(ctx context.Context, t *T) error {
var (
block = t.chain.GetBlock(int(t.chain.Head().NumberU64()) - 10)
addr = emitContract
got hexutil.Big
)
if err := t.rpc.CallContext(ctx, &got, "eth_getBalance", addr, block.Hash()); err != nil {
return err
}
// We can't really check the result here because there is no state, but the
// balance shouldn't be zero.
if got.ToInt().Sign() <= 0 {
return errors.New("invalid historical balance, should be > zero")
}
return nil
},
},
},
}
// EthGetBlockByNumber stores a list of all tests against the method.
var EthGetBlockByNumber = MethodTests{
"eth_getBlockByNumber",
[]Test{
{
Name: "get-genesis",
About: "gets block number zero",
Run: func(ctx context.Context, t *T) error {
block, err := t.eth.BlockByNumber(ctx, big.NewInt(0))
if err != nil {
return err
}
if n := block.Number().Uint64(); n != 0 {
return fmt.Errorf("expected block 0, got block %d", n)
}
return nil
},
},
{
Name: "get-latest",
About: "gets the block with tag \"latest\"",
Run: func(ctx context.Context, t *T) error {
block, err := t.eth.BlockByNumber(ctx, nil)
if err != nil {
return err
}
head := t.chain.Head().NumberU64()
if n := block.Number().Uint64(); n != head {
return fmt.Errorf("expected block %d, got block %d", head, n)
}
return nil
},
},
{
Name: "get-safe",
About: "get the block with tag \"safe\"",
Run: func(ctx context.Context, t *T) error {
block, err := t.eth.BlockByNumber(ctx, big.NewInt(int64(rpc.SafeBlockNumber)))
if err != nil {
return err
}
head := t.chain.Head().NumberU64()
if n := block.Number().Uint64(); n != head {
return fmt.Errorf("expected block %d, got block %d", head, n)
}
return nil
},
},
{
Name: "get-finalized",
About: "get the block with tag \"finalized\"",
Run: func(ctx context.Context, t *T) error {
block, err := t.eth.BlockByNumber(ctx, big.NewInt(int64(rpc.FinalizedBlockNumber)))
if err != nil {
return err
}
head := t.chain.Head().NumberU64()
if n := block.Number().Uint64(); n != head {
return fmt.Errorf("expected block %d, got block %d", head, n)
}
return nil
},
},
{
Name: "get-block-london-fork",
About: "requests a block at the London fork",
Run: func(ctx context.Context, t *T) error {
hdr, err := t.eth.HeaderByNumber(ctx, t.chain.config.LondonBlock)
if err != nil {
return err
}
if hdr.BaseFee == nil {
return fmt.Errorf("missing basefee in block")
}
return nil
},
},
{
Name: "get-block-merge-fork",
About: "requests a block at the merge (Paris) fork",
Run: func(ctx context.Context, t *T) error {
hdr, err := t.eth.HeaderByNumber(ctx, t.chain.config.MergeNetsplitBlock)
if err != nil {
return err
}
if hdr.Difficulty.Sign() > 0 {
return fmt.Errorf("block difficulty > 0")
}
return nil
},
},
{
Name: "get-block-shanghai-fork",
About: "requests a block at the Shanghai fork",
Run: func(ctx context.Context, t *T) error {
blocknum := t.chain.BlockAtTime(*t.chain.config.ShanghaiTime).Number()
hdr, err := t.eth.HeaderByNumber(ctx, blocknum)
if err != nil {
return err
}
if hdr.WithdrawalsHash == nil {
return fmt.Errorf("block has no withdrawalsHash")
}
return nil
},
},
{
Name: "get-block-cancun-fork",
About: "requests a block at the Cancun fork",
Run: func(ctx context.Context, t *T) error {
blocknum := t.chain.BlockAtTime(*t.chain.config.CancunTime).Number()
b, err := t.eth.HeaderByNumber(ctx, blocknum)
if err != nil {
return err
}
if b.BlobGasUsed == nil {
return fmt.Errorf("block has no blobGasUsed")
}
return nil
},
},
{
Name: "get-block-prague-fork",
About: "requests a block at the Prague fork",
Run: func(ctx context.Context, t *T) error {
blocknum := t.chain.txinfo.EIP7002.Block
hdr, err := t.eth.HeaderByNumber(ctx, big.NewInt(int64(blocknum)))
if err != nil {
return err
}
if hdr.RequestsHash == nil || *hdr.RequestsHash == types.EmptyRequestsHash {
return fmt.Errorf("block hash empty or missing requestsHash")
}
return nil
},
},
{
Name: "get-block-notfound",
About: "requests a block number that does not exist",
Run: func(ctx context.Context, t *T) error {
_, err := t.eth.BlockByNumber(ctx, big.NewInt(1000))
if !errors.Is(err, ethereum.NotFound) {
return errors.New("get a non-existent block should return null")
}
return nil
},
},
},
}
// EthCall stores a list of all tests against the method.
var EthCall = MethodTests{
"eth_call",
[]Test{
{
Name: "call-contract",
About: "performs a basic contract call with default settings",
Run: func(ctx context.Context, t *T) error {
msg := ethereum.CallMsg{
To: &t.chain.txinfo.CallMeContract.Addr,
// This is the expected input that makes the call pass.
// See https://github.com/ethereum/hive/blob/master/cmd/hivechain/contracts/callme.eas
Data: []byte{0xff, 0x01},
}
result, err := t.eth.CallContract(ctx, msg, nil)
if err != nil {
return err
}
want := []byte{0xff, 0xee}
if !bytes.Equal(result, want) {
return fmt.Errorf("unexpected return value (got: %#x, want: %#x)", result, want)
}
return nil
},
},
{
Name: "call-callenv",
About: `Performs a call to the callenv contract, which echoes the EVM transaction environment.
See https://github.com/ethereum/hive/tree/master/cmd/hivechain/contracts/callenv.eas for the output structure.`,
Run: func(ctx context.Context, t *T) error {
msg := ethereum.CallMsg{
To: &t.chain.txinfo.CallEnvContract.Addr,
}
result, err := t.eth.CallContract(ctx, msg, nil)
if err != nil {
return err
}
if len(result) == 0 {
return fmt.Errorf("empty call result")
}
return nil
},
},
{
Name: "call-callenv-options-eip1559",
About: `Performs a call to the callenv contract, which echoes the EVM transaction environment.
This call uses EIP1559 transaction options.
See https://github.com/ethereum/hive/tree/master/cmd/hivechain/contracts/callenv.eas for the output structure.`,
Run: func(ctx context.Context, t *T) error {
sender, _ := t.chain.GetSender(1)
basefee := t.chain.Head().BaseFee()
basefee.Add(basefee, big.NewInt(1))
msg := ethereum.CallMsg{
From: sender,
To: &t.chain.txinfo.CallEnvContract.Addr,
Gas: 60000,
GasFeeCap: basefee,
GasTipCap: big.NewInt(11),
Value: big.NewInt(23),
Data: []byte{0x33, 0x34, 0x35},
}
result, err := t.eth.CallContract(ctx, msg, nil)
if err != nil {
return err
}
if len(result) == 0 {
return fmt.Errorf("empty call result")
}
return nil
},
},
{
Name: "call-eip7702-delegation",
About: `Performs a call to an account that has an EIP-7702 code delegation.`,
Run: func(ctx context.Context, t *T) error {
msg := ethereum.CallMsg{
To: &t.chain.txinfo.EIP7702.Account,
Gas: 100000,
}
result, err := t.eth.CallContract(ctx, msg, nil)
if err != nil {
return err
}
if len(result) == 0 {
return fmt.Errorf("empty call result")
}
expectedOutput := slices.Concat(
make([]byte, 12),
t.chain.txinfo.EIP7702.Account[:],
[]byte("invoked"),
make([]byte, 25),
)
if !bytes.Equal(result, expectedOutput) {
return fmt.Errorf("wrong return value: %x", result)
}
return nil
},
},
{
Name: "call-revert-abi-panic",
About: "calls a contract that reverts with an ABI-encoded Panic(uint) value",
Run: func(ctx context.Context, t *T) error {
msg := ethereum.CallMsg{
To: &t.chain.txinfo.CallRevertContract.Addr,
Gas: 100000,
Data: []byte{0}, // triggers panic(uint) revert
}
got, err := t.eth.CallContract(ctx, msg, nil)
if len(got) != 0 {
return fmt.Errorf("unexpected return value (got: %s, want: nil)", hexutil.Bytes(got))
}
if err == nil {
return fmt.Errorf("expected error for reverting call")
}
return nil
},
},
{
Name: "call-revert-abi-error",
About: "calls a contract that reverts with an ABI-encoded Error(string) value",
Run: func(ctx context.Context, t *T) error {
msg := ethereum.CallMsg{
To: &t.chain.txinfo.CallRevertContract.Addr,
Gas: 100000,
Data: []byte{1}, // triggers error(string) revert
}
got, err := t.eth.CallContract(ctx, msg, nil)
if len(got) != 0 {
return fmt.Errorf("unexpected return value (got: %s, want: nil)", hexutil.Bytes(got))
}
if err == nil {
return fmt.Errorf("expected error for reverting call")
}
return nil
},
},
},
}
// EthEstimateGas stores a list of all tests against the method.
var EthEstimateGas = MethodTests{
"eth_estimateGas",
[]Test{
{
Name: "estimate-simple-transfer",
About: "estimates a simple transfer",
Run: func(ctx context.Context, t *T) error {
msg := ethereum.CallMsg{From: common.Address{0xaa}, To: &common.Address{0x01}}
got, err := t.eth.EstimateGas(ctx, msg)
if err != nil {
return err
}
if got != params.TxGas {
return fmt.Errorf("unexpected return value (got: %d, want: %d)", got, params.TxGas)
}
return nil
},
},
{
Name: "estimate-successful-call",
About: "estimates a successful contract call",
SpecOnly: true, // EVM gas estimation is not required to be identical across clients
Run: func(ctx context.Context, t *T) error {
caller := common.Address{1, 2, 3}
callme := t.chain.txinfo.CallMeContract.Addr
msg := ethereum.CallMsg{
From: caller,
To: &callme,
// This is the expected input that makes the call pass.
// See https://github.com/ethereum/hive/blob/master/cmd/hivechain/contracts/callme.eas
Data: []byte{0xff, 0x01},
}
got, err := t.eth.EstimateGas(ctx, msg)
if err != nil {
return err
}
want := uint64(21270)
if got != want {
return fmt.Errorf("unexpected return value (got: %d, want: %d)", got, want)
}
return nil
},
},
{
Name: "estimate-failed-call",
About: "estimates a contract call that reverts",
SpecOnly: true, // EVM gas estimation is not required to be identical across clients
Run: func(ctx context.Context, t *T) error {
caller := common.Address{1, 2, 3}
callme := t.chain.txinfo.CallMeContract.Addr
msg := ethereum.CallMsg{
From: caller,
To: &callme,
Data: []byte{0xff, 0x03, 0x04, 0x05},
}
if _, err := t.eth.EstimateGas(ctx, msg); err == nil {
return fmt.Errorf("expected error for failed contract call")
}
return nil
},
},
{
Name: "estimate-call-abi-error",
About: "estimates a contract call that reverts using Solidity Error(string) data",
SpecOnly: true, // EVM gas estimation is not required to be identical across clients
Run: func(ctx context.Context, t *T) error {
caller := common.Address{1, 2, 3}
contract := t.chain.txinfo.CallRevertContract.Addr
msg := ethereum.CallMsg{
From: caller,
To: &contract,
Data: []byte{1}, // triggers error(string) revert
}
if _, err := t.eth.EstimateGas(ctx, msg); err == nil {
return fmt.Errorf("expected error for failed contract call")
}
return nil
},
},
{
Name: "estimate-with-eip7702",
About: "checks that including an EIP-7720 authorization in the message increases gas",
SpecOnly: true,
Run: func(ctx context.Context, t *T) error {
sender, nonce := t.chain.GetSender(0)
to := common.Address{0x01}
baseMsg := map[string]any{
"from": sender,
"to": to,
"value": hexutil.Uint64(1),
"nonce": hexutil.Uint64(nonce),
}
withAuth := map[string]any{
"type": "0x4",
"from": sender,
"to": to,
"value": hexutil.Uint64(1),
"nonce": hexutil.Uint64(nonce),
"authorizationList": []map[string]any{
{
"chainId": "0x1",
"address": "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"nonce": "0x0",
"yParity": "0x0",
"r": "0x1111111111111111111111111111111111111111111111111111111111111111",
"s": "0x2222222222222222222222222222222222222222222222222222222222222222",
},
},
}
var baseGas, authGas hexutil.Uint64
if err := t.rpc.CallContext(ctx, &baseGas, "eth_estimateGas", baseMsg); err != nil {
return fmt.Errorf("base estimation failed: %v", err)
}
if err := t.rpc.CallContext(ctx, &authGas, "eth_estimateGas", withAuth); err != nil {
return fmt.Errorf("with auth estimation failed: %v", err)
}
if authGas <= baseGas {
return fmt.Errorf("expected higher gas with auth (got: %d, base: %d)", authGas, baseGas)
}
return nil
},
},
{
Name: "estimate-with-eip4844",
About: "checks gas estimation for blob transactions (EIP-4844)",
SpecOnly: true,
Run: func(ctx context.Context, t *T) error {
sender, nonce := t.chain.GetSender(0)
to := common.Address{0x01}
msg := map[string]any{
"type": "0x3",
"from": sender,
"to": to,
"value": hexutil.Uint64(1),
"nonce": hexutil.Uint64(nonce),
"maxFeePerBlobGas": "0x5",
"blobVersionedHashes": []string{
"0x0100000000000000000000000000000000000000000000000000000000000000",
},
}
var gas hexutil.Uint64
if err := t.rpc.CallContext(ctx, &gas, "eth_estimateGas", msg); err != nil {
return fmt.Errorf("estimation failed: %v", err)
}
if gas < 21000 {
return fmt.Errorf("expected blob tx to require more than base gas, got %d", gas)
}
return nil
},
},
},
}
// EthEstimateGas stores a list of all tests against the method.
var EthCreateAccessList = MethodTests{
"eth_createAccessList",
[]Test{
{
Name: "create-al-value-transfer",
About: "estimates a simple transfer",
Run: func(ctx context.Context, t *T) error {
sender, nonce := t.chain.GetSender(0)
msg := map[string]any{
"from": sender,
"to": common.Address{0x01},
"value": hexutil.Uint64(10),
"nonce": hexutil.Uint64(nonce),
}
result := make(map[string]any)
err := t.rpc.CallContext(ctx, &result, "eth_createAccessList", msg, "latest")
if err != nil {
return err
}
return nil
},
},
{
Name: "create-al-contract",
About: "creates an access list for a contract invocation that accesses storage",
SpecOnly: true,
Run: func(ctx context.Context, t *T) error {
gasprice := t.chain.Head().BaseFee()
sender, nonce := t.chain.GetSender(0)
msg := map[string]any{
"from": sender,
"to": emitContract,
"nonce": hexutil.Uint64(nonce),
"gas": hexutil.Uint64(60000),
"gasPrice": (*hexutil.Big)(gasprice),
"input": "0x010203040506",
}
var result struct {
AccessList types.AccessList
}
err := t.rpc.CallContext(ctx, &result, "eth_createAccessList", msg, "latest")
if err != nil {
return err
}
if len(result.AccessList) == 0 {
return fmt.Errorf("empty access list")
}
if result.AccessList[0].Address != emitContract {
return fmt.Errorf("wrong address in access list entry")
}
if len(result.AccessList[0].StorageKeys) == 0 {
return fmt.Errorf("no storage keys in access list entry")
}
return nil
},
},
{
Name: "create-al-contract-eip1559",
About: `Creates an access list for a contract invocation that accesses storage.
This invocation uses EIP-1559 fields to specify the gas price.`,
SpecOnly: true,
Run: func(ctx context.Context, t *T) error {
gasprice := t.chain.Head().BaseFee()
sender, nonce := t.chain.GetSender(0)
msg := map[string]any{
"from": sender,
"to": emitContract,
"nonce": hexutil.Uint64(nonce),
"gas": hexutil.Uint64(60000),
"maxFeePerGas": (*hexutil.Big)(gasprice),
"maxPriorityFeePerGas": (*hexutil.Big)(big.NewInt(3)),
"input": "0x010203040506",
}
var result struct {
AccessList types.AccessList
}
err := t.rpc.CallContext(ctx, &result, "eth_createAccessList", msg, "latest")
if err != nil {
return err
}
if len(result.AccessList) == 0 {
return fmt.Errorf("empty access list")
}
if result.AccessList[0].Address != emitContract {
return fmt.Errorf("wrong address in access list entry")
}
if len(result.AccessList[0].StorageKeys) == 0 {
return fmt.Errorf("no storage keys in access list entry")
}
return nil
},
},
{
Name: "create-al-abi-revert",
About: `Creates an access list for a contract invocation that reverts.
The server should return the accessed slots regardless of failure, and should report the failure
in the "error" field.`,
SpecOnly: true,
Run: func(ctx context.Context, t *T) error {
msg := map[string]any{
"to": t.chain.txinfo.CallRevertContract.Addr,
"gas": hexutil.Uint64(100000),
"input": "0x01", // triggers error(string) revert
}
var result struct {
AccessList types.AccessList
Error string
}
err := t.rpc.CallContext(ctx, &result, "eth_createAccessList", msg, "latest")
if err != nil {
return fmt.Errorf("reverting call returned JSON-RPC error")
}
if len(result.AccessList) == 0 {
return fmt.Errorf("empty access list")
}
if len(result.Error) == 0 {
return fmt.Errorf("EVM revert error not signaled in response")
}
return nil
},
},
},
}
// EthGetBlockTransactionCountByNumber stores a list of all tests against the method.
var EthGetBlockTransactionCountByNumber = MethodTests{
"eth_getBlockTransactionCountByNumber",
[]Test{
{
Name: "get-genesis",
About: "gets tx count in block 0",
Run: func(ctx context.Context, t *T) error {
var got hexutil.Uint
err := t.rpc.CallContext(ctx, &got, "eth_getBlockTransactionCountByNumber", hexutil.Uint(0))
if err != nil {
return err
}
if int(got) != 0 {
return fmt.Errorf("tx counts don't match (got: %d, want: %d)", int(got), 0)
}
return nil
},
},
{
Name: "get-block-n",
About: "gets tx count in a non-empty block",
Run: func(ctx context.Context, t *T) error {
block := t.chain.BlockWithTransactions("", nil)
var got hexutil.Uint
err := t.rpc.CallContext(ctx, &got, "eth_getBlockTransactionCountByNumber", hexutil.Uint64(block.NumberU64()))
if err != nil {
return err
}
want := len(block.Transactions())
if int(got) != want {
return fmt.Errorf("tx counts don't match (got: %d, want: %d)", int(got), want)
}
return nil
},
},
},
}
// EthGetBlockTransactionCountByHash stores a list of all tests against the method.
var EthGetBlockTransactionCountByHash = MethodTests{
"eth_getBlockTransactionCountByHash",
[]Test{
{
Name: "get-genesis",
About: "gets tx count in block 0",
Run: func(ctx context.Context, t *T) error {
block := t.chain.GetBlock(0)
var got hexutil.Uint
err := t.rpc.CallContext(ctx, &got, "eth_getBlockTransactionCountByHash", block.Hash())
if err != nil {
return err
}
if int(got) != 0 {
return fmt.Errorf("tx counts don't match (got: %d, want: %d)", int(got), 0)
}
return nil
},
},
{
Name: "get-block-n",
About: "gets tx count in a non-empty block",
Run: func(ctx context.Context, t *T) error {
block := t.chain.BlockWithTransactions("any", nil)