forked from s1na/rpctestgen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerators.go
1001 lines (958 loc) · 25.4 KB
/
generators.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
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"
"crypto/ecdsa"
"fmt"
"math/big"
"reflect"
"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"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"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"
)
var (
addr common.Address
pk *ecdsa.PrivateKey
)
func init() {
pk, _ = crypto.HexToECDSA("9c647b8b7c4e7c3490668fb6c11473619db80c93704c70893d3813af4090c39c")
addr = crypto.PubkeyToAddress(pk.PublicKey) // 658bdf435d810c91414ec09147daa6db62406379
}
type T struct {
eth *ethclient.Client
geth *gethclient.Client
rpc *rpc.Client
chain *core.BlockChain
}
func NewT(eth *ethclient.Client, geth *gethclient.Client, rpc *rpc.Client, chain *core.BlockChain) *T {
return &T{eth, geth, rpc, 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
Run func(context.Context, *T) error
}
// AllMethods is a slice of all JSON-RPC methods with tests.
var AllMethods = []MethodTests{
EthBlockNumber,
EthGetBlockByNumber,
EthGetProof,
EthChainID,
EthGetBalance,
// EthGetHeaderByNumber,
// EthGetHeaderByHash,
EthGetBlockByHash,
EthGetCode,
EthGetStorage,
EthCall,
EthEstimateGas,
EthCreateAccessList,
EthGetBlockTransactionCountByNumber,
EthGetBlockTransactionCountByHash,
EthGetTransactionByBlockHashAndIndex,
EthGetTransactionByBlockNumberAndIndex,
EthGetTransactionCount,
EthGetTransactionByHash,
EthGetTransactionReceipt,
EthSendRawTransaction,
EthGasPrice,
EthMaxPriorityFeePerGas,
EthSyncing,
EthFeeHistory,
// EthGetUncleByBlockNumberAndIndex,
DebugGetRawHeader,
DebugGetRawBlock,
DebugGetRawReceipts,
DebugGetRawTransaction,
}
// EthBlockNumber stores a list of all tests against the method.
var EthBlockNumber = MethodTests{
"eth_blockNumber",
[]Test{
{
"simple-test",
"retrieves the client's current block number",
func(ctx context.Context, t *T) error {
got, err := t.eth.BlockNumber(ctx)
if err != nil {
return err
} else if want := t.chain.CurrentHeader().Number.Uint64(); 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{
{
"get-chain-id",
"retrieves the client's current chain id",
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
},
},
},
}
// EthGetHeaderByNumber stores a list of all tests against the method.
var EthGetHeaderByNumber = MethodTests{
"eth_getHeaderByNumber",
[]Test{
{
"get-header-by-number",
"gets a header by number",
func(ctx context.Context, t *T) error {
var got *types.Header
err := t.rpc.CallContext(ctx, got, "eth_getHeaderByNumber", "0x1")
if err != nil {
return err
}
want := t.chain.GetHeaderByNumber(1)
if reflect.DeepEqual(got, want) {
return fmt.Errorf("unexpected header (got: %s, want: %s)", got.Hash(), want.Hash())
}
return nil
},
},
},
}
// EthGetHeaderByHash stores a list of all tests against the method.
var EthGetHeaderByHash = MethodTests{
"eth_getHeaderByHash",
[]Test{
{
"get-header-by-hash",
"gets a header by hash",
func(ctx context.Context, t *T) error {
want := t.chain.GetHeaderByNumber(1)
var got *types.Header
err := t.rpc.CallContext(ctx, got, "eth_getHeaderByHash", want.Hash())
if err != nil {
return err
}
if reflect.DeepEqual(got, want) {
return fmt.Errorf("unexpected header (got: %s, want: %s)", got.Hash(), want.Hash())
}
return nil
},
},
},
}
// EthGetCode stores a list of all tests against the method.
var EthGetCode = MethodTests{
"eth_getCode",
[]Test{
{
"get-code",
"gets code for 0xaa",
func(ctx context.Context, t *T) error {
addr := common.Address{0xaa}
var got hexutil.Bytes
err := t.rpc.CallContext(ctx, &got, "eth_getCode", addr, "latest")
if err != nil {
return err
}
state, _ := t.chain.State()
want := state.GetCode(addr)
if !bytes.Equal(got, want) {
return fmt.Errorf("unexpected code (got: %s, want %s)", got, want)
}
return nil
},
},
},
}
// EthGetStorage stores a list of all tests against the method.
var EthGetStorage = MethodTests{
"eth_getStorage",
[]Test{
{
"get-storage",
"gets storage for 0xaa",
func(ctx context.Context, t *T) error {
addr := common.Address{0xaa}
key := common.Hash{0x01}
got, err := t.eth.StorageAt(ctx, addr, key, nil)
if err != nil {
return err
}
state, _ := t.chain.State()
want := state.GetState(addr, key)
if !bytes.Equal(got, want.Bytes()) {
return fmt.Errorf("unexpected storage value (got: %s, want %s)", got, want)
}
return nil
},
},
},
}
// EthGetBlockByHash stores a list of all tests against the method.
var EthGetBlockByHash = MethodTests{
"eth_getBlockByHash",
[]Test{
{
"get-block-by-hash",
"gets block 1",
func(ctx context.Context, t *T) error {
want := t.chain.GetHeaderByNumber(1)
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
},
},
},
}
// EthChainID stores a list of all tests against the method.
var EthGetBalance = MethodTests{
"eth_getBalance",
[]Test{
{
"get-balance",
"retrieves the an account's balance",
func(ctx context.Context, t *T) error {
addr := common.Address{0xaa}
got, err := t.eth.BalanceAt(ctx, addr, nil)
if err != nil {
return err
}
state, _ := t.chain.State()
want := state.GetBalance(addr)
if got.Uint64() != want.Uint64() {
return fmt.Errorf("unexpect balance (got: %d, want: %d)", got, want)
}
return nil
},
},
{
"get-balance-blockhash",
"retrieves the an account's balance at a specific blockhash",
func(ctx context.Context, t *T) error {
var (
block = t.chain.GetBlockByNumber(1)
addr = common.Address{0xaa}
got hexutil.Big
)
if err := t.rpc.CallContext(ctx, &got, "eth_getBalance", addr, block.Hash()); err != nil {
return err
}
state, _ := t.chain.StateAt(block.Root())
want := state.GetBalance(addr)
if got.ToInt().Uint64() != want.Uint64() {
return fmt.Errorf("unexpect balance (got: %d, want: %d)", got.ToInt(), want)
}
return nil
},
},
},
}
// EthGetBlockByNumber stores a list of all tests against the method.
var EthGetBlockByNumber = MethodTests{
"eth_getBlockByNumber",
[]Test{
{
"get-genesis",
"gets block 0",
func(ctx context.Context, t *T) error {
block, err := t.eth.BlockByNumber(ctx, common.Big0)
if err != nil {
return err
}
if n := block.Number().Uint64(); n != 0 {
return fmt.Errorf("expected block 0, got block %d", n)
}
return nil
},
},
{
"get-block-n",
"gets block 2",
func(ctx context.Context, t *T) error {
block, err := t.eth.BlockByNumber(ctx, common.Big2)
if err != nil {
return err
}
if n := block.Number().Uint64(); n != 2 {
return fmt.Errorf("expected block 2, got block %d", n)
}
return nil
},
},
},
}
// EthCall stores a list of all tests against the method.
var EthCall = MethodTests{
"eth_call",
[]Test{
{
"call-simple-transfer",
"simulates a simple transfer",
func(ctx context.Context, t *T) error {
msg := ethereum.CallMsg{From: common.Address{0xaa}, To: &common.Address{0x01}, Gas: 100000}
got, err := t.eth.CallContract(ctx, msg, nil)
if err != nil {
return err
}
if len(got) != 0 {
return fmt.Errorf("unexpected return value (got: %s, want: nil)", hexutil.Bytes(got))
}
return nil
},
},
{
"call-simple-contract",
"simulates a simple contract call with no return",
func(ctx context.Context, t *T) error {
aa := common.Address{0xaa}
msg := ethereum.CallMsg{From: aa, To: &aa}
got, err := t.eth.CallContract(ctx, msg, nil)
if err != nil {
return err
}
if len(got) != 0 {
return fmt.Errorf("unexpected return value (got: %s, want: nil)", hexutil.Bytes(got))
}
return nil
},
},
},
}
// EthEstimateGas stores a list of all tests against the method.
var EthEstimateGas = MethodTests{
"eth_estimateGas",
[]Test{
{
"estimate-simple-transfer",
"estimates a simple transfer",
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
},
},
{
"estimate-simple-contract",
"estimates a simple contract call with no return",
func(ctx context.Context, t *T) error {
aa := common.Address{0xaa}
msg := ethereum.CallMsg{From: aa, To: &aa}
got, err := t.eth.EstimateGas(ctx, msg)
if err != nil {
return err
}
want := params.TxGas + 3
if got != want {
return fmt.Errorf("unexpected return value (got: %d, want: %d)", got, want)
}
return nil
},
},
},
}
// EthEstimateGas stores a list of all tests against the method.
var EthCreateAccessList = MethodTests{
"eth_createAccessList",
[]Test{
{
"create-al-simple-transfer",
"estimates a simple transfer",
func(ctx context.Context, t *T) error {
msg := make(map[string]interface{})
msg["from"] = addr
msg["to"] = common.Address{0x01}
got := make(map[string]interface{})
err := t.rpc.CallContext(ctx, &got, "eth_createAccessList", msg, "latest")
if err != nil {
return err
}
return nil
},
},
{
"create-al-simple-contract",
"estimates a simple contract call with no return",
func(ctx context.Context, t *T) error {
msg := make(map[string]interface{})
msg["from"] = addr
msg["to"] = common.Address{0xaa}
got := make(map[string]interface{})
err := t.rpc.CallContext(ctx, &got, "eth_createAccessList", msg, "latest")
if err != nil {
return err
}
return nil
},
},
{
"create-al-multiple-reads",
"estimates a simple contract call with no return",
func(ctx context.Context, t *T) error {
msg := make(map[string]interface{})
msg["from"] = addr
msg["to"] = common.Address{0xbb}
got := make(map[string]interface{})
err := t.rpc.CallContext(ctx, &got, "eth_createAccessList", msg, "latest")
if err != nil {
return err
}
return nil
},
},
},
}
// EthGetBlockTransactionCountByNumber stores a list of all tests against the method.
var EthGetBlockTransactionCountByNumber = MethodTests{
"eth_getBlockTransactionCountByNumber",
[]Test{
{
"get-genesis",
"gets tx count in block 0",
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
}
want := len(t.chain.GetBlockByNumber(0).Transactions())
if int(got) != want {
return fmt.Errorf("tx counts don't match (got: %d, want: %d)", int(got), want)
}
return nil
},
},
{
"get-block-n",
"gets tx count in block 2",
func(ctx context.Context, t *T) error {
var got hexutil.Uint
err := t.rpc.CallContext(ctx, &got, "eth_getBlockTransactionCountByNumber", hexutil.Uint(2))
if err != nil {
return err
}
want := len(t.chain.GetBlockByNumber(2).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{
{
"get-genesis",
"gets tx count in block 0",
func(ctx context.Context, t *T) error {
block := t.chain.GetBlockByNumber(0)
var got hexutil.Uint
err := t.rpc.CallContext(ctx, &got, "eth_getBlockTransactionCountByHash", block.Hash())
if err != nil {
return err
}
want := len(t.chain.GetBlockByNumber(0).Transactions())
if int(got) != want {
return fmt.Errorf("tx counts don't match (got: %d, want: %d)", int(got), want)
}
return nil
},
},
{
"get-block-n",
"gets tx count in block 2",
func(ctx context.Context, t *T) error {
block := t.chain.GetBlockByNumber(2)
var got hexutil.Uint
err := t.rpc.CallContext(ctx, &got, "eth_getBlockTransactionCountByHash", block.Hash())
if err != nil {
return err
}
want := len(t.chain.GetBlockByNumber(2).Transactions())
if int(got) != want {
return fmt.Errorf("tx counts don't match (got: %d, want: %d)", int(got), want)
}
return nil
},
},
},
}
// EthGetTransactionByBlockHashAndIndex stores a list of all tests against the method.
var EthGetTransactionByBlockHashAndIndex = MethodTests{
"eth_getTransactionByBlockNumberAndIndex",
[]Test{
{
"get-block-n",
"gets tx 0 in block 2",
func(ctx context.Context, t *T) error {
var got types.Transaction
err := t.rpc.CallContext(ctx, &got, "eth_getTransactionByBlockNumberAndIndex", hexutil.Uint(2), hexutil.Uint(0))
if err != nil {
return err
}
want := t.chain.GetBlockByNumber(2).Transactions()[0]
if got.Hash() != want.Hash() {
return fmt.Errorf("tx don't match (got: %d, want: %d)", got.Hash(), want.Hash())
}
return nil
},
},
},
}
// EthGetTransactionByBlockNumberAndIndex stores a list of all tests against the method.
var EthGetTransactionByBlockNumberAndIndex = MethodTests{
"eth_getTransactionByBlockHashAndIndex",
[]Test{
{
"get-block-n",
"gets tx 0 in block 2",
func(ctx context.Context, t *T) error {
block := t.chain.GetBlockByNumber(2)
var got types.Transaction
err := t.rpc.CallContext(ctx, &got, "eth_getTransactionByBlockHashAndIndex", block.Hash(), hexutil.Uint(0))
if err != nil {
return err
}
want := t.chain.GetBlockByNumber(2).Transactions()[0]
if got.Hash() != want.Hash() {
return fmt.Errorf("tx don't match (got: %d, want: %d)", got.Hash(), want.Hash())
}
return nil
},
},
},
}
// EthGetTransactionCount stores a list of all tests against the method.
var EthGetTransactionCount = MethodTests{
"eth_getTransactionCount",
[]Test{
{
"get-account-nonce",
"gets nonce for a certain account",
func(ctx context.Context, t *T) error {
addr := common.Address{0xaa}
got, err := t.eth.NonceAt(ctx, addr, nil)
if err != nil {
return err
}
state, _ := t.chain.State()
want := state.GetNonce(addr)
if got != want {
return fmt.Errorf("unexpected nonce (got: %d, want: %d)", got, want)
}
return nil
},
},
},
}
// EthGetTransactionByHash stores a list of all tests against the method.
// TODO: do legacy, al, and dynamic txs
var EthGetTransactionByHash = MethodTests{
"eth_getTransactionByHash",
[]Test{
{
"get-legacy-tx",
"gets a legacy transaction",
func(ctx context.Context, t *T) error {
want := t.chain.GetBlockByNumber(2).Transactions()[0]
got, _, err := t.eth.TransactionByHash(ctx, want.Hash())
if err != nil {
return err
}
if got.Hash() != want.Hash() {
return fmt.Errorf("tx mismatch (got: %s, want: %s)", got.Hash(), want.Hash())
}
return nil
},
},
},
}
// EthGetTransactionReceipt stores a list of all tests against the method.
// TODO: do legacy, al, and dynamic txs
var EthGetTransactionReceipt = MethodTests{
"eth_getTransactionReceipt",
[]Test{
{
"get-legacy-receipt",
"gets a receipt for a legacy transaction",
func(ctx context.Context, t *T) error {
block := t.chain.GetBlockByNumber(2)
receipt, err := t.eth.TransactionReceipt(ctx, block.Transactions()[0].Hash())
if err != nil {
return err
}
got, _ := receipt.MarshalBinary()
want, _ := t.chain.GetReceiptsByHash(block.Hash())[0].MarshalBinary()
if !bytes.Equal(got, want) {
return fmt.Errorf("receipt mismatch (got: %s, want: %s)", hexutil.Bytes(got), hexutil.Bytes(want))
}
return nil
},
},
},
}
// EthSendRawTransaction stores a list of all tests against the method.
// TODO: do legacy, al, and dynamic txs
var EthSendRawTransaction = MethodTests{
"eth_sendRawTransaction",
[]Test{
{
"send-legacy-transaction",
"sends a raw legacy transaction",
func(ctx context.Context, t *T) error {
genesis := t.chain.Genesis()
state, _ := t.chain.State()
txdata := &types.LegacyTx{
Nonce: state.GetNonce(addr),
To: &common.Address{0xaa},
Value: big.NewInt(10),
Gas: 25000,
GasPrice: new(big.Int).Add(genesis.BaseFee(), big.NewInt(1)),
Data: common.FromHex("5544"),
}
s := types.MakeSigner(t.chain.Config(), t.chain.CurrentHeader().Number)
tx, _ := types.SignNewTx(pk, s, txdata)
if err := t.eth.SendTransaction(ctx, tx); err != nil {
return err
}
return nil
},
},
},
}
// EthGasPrice stores a list of all tests against the method.
var EthGasPrice = MethodTests{
"eth_gasPrice",
[]Test{
{
"get-current-gas-price",
"gets the current gas price in wei",
func(ctx context.Context, t *T) error {
if _, err := t.eth.SuggestGasPrice(ctx); err != nil {
return err
}
return nil
},
},
},
}
// EthMaxPriorityFeePerGas stores a list of all tests against the method.
var EthMaxPriorityFeePerGas = MethodTests{
"eth_maxPriorityFeePerGas",
[]Test{
{
"get-current-tip",
"gets the current maxPriorityFeePerGas in wei",
func(ctx context.Context, t *T) error {
if _, err := t.eth.SuggestGasTipCap(ctx); err != nil {
return err
}
return nil
},
},
},
}
// EthFeeHistory stores a list of all tests against the method.
var EthFeeHistory = MethodTests{
"eth_feeHistory",
[]Test{
{
"fee-history",
"gets fee history information",
func(ctx context.Context, t *T) error {
got, err := t.eth.FeeHistory(ctx, 1, big.NewInt(2), []float64{95, 99})
if err != nil {
return err
}
block := t.chain.GetBlockByNumber(2)
tip, err := block.Transactions()[0].EffectiveGasTip(block.BaseFee())
if err != nil {
return fmt.Errorf("unable to get effective tip: %w", err)
}
if len(got.Reward) != 1 {
return fmt.Errorf("mismatch number of rewards (got: %d, want: 1", len(got.Reward))
}
if got.Reward[0][0].Cmp(tip) != 0 {
return fmt.Errorf("mismatch reward value (got: %d, want: %d)", got.Reward[0][0], tip)
}
return nil
},
},
},
}
// EthSyncing stores a list of all tests against the method.
var EthSyncing = MethodTests{
"eth_syncing",
[]Test{
{
"check-syncing",
"checks client syncing status",
func(ctx context.Context, t *T) error {
_, err := t.eth.SyncProgress(ctx)
if err != nil {
return err
}
return nil
},
},
},
}
// EthGetUncleByBlockNumberAndIndex stores a list of all tests against the method.
var EthGetUncleByBlockNumberAndIndex = MethodTests{
"eth_getUncleByBlockNumberAndIndex",
[]Test{
{
"get-uncle",
"gets uncle header",
func(ctx context.Context, t *T) error {
var got *types.Header
t.rpc.CallContext(ctx, got, "eth_getUncleByBlockNumberAndIndex", hexutil.Uint(2), hexutil.Uint(0))
want := t.chain.GetBlockByNumber(2).Uncles()[0]
if got.Hash() != want.Hash() {
return fmt.Errorf("mismatch uncle hash (got: %s, want: %s", got.Hash(), want.Hash())
}
return nil
},
},
},
}
// EthGetProof stores a list of all tests against the method.
var EthGetProof = MethodTests{
"eth_getProof",
[]Test{
{
"get-account-proof",
"gets proof for a certain account",
func(ctx context.Context, t *T) error {
addr := common.Address{0xaa}
result, err := t.geth.GetProof(ctx, addr, []string{}, big.NewInt(3))
if err != nil {
return err
}
state, _ := t.chain.State()
balance := state.GetBalance(addr)
if result.Balance.Cmp(balance) != 0 {
return fmt.Errorf("unexpected balance (got: %s, want: %s)", result.Balance, balance)
}
return nil
},
},
{
"get-account-proof-blockhash",
"gets proof for a certain account at the specified blockhash",
func(ctx context.Context, t *T) error {
addr := common.Address{0xaa}
type accountResult struct {
Balance *hexutil.Big `json:"balance"`
}
var result accountResult
if err := t.rpc.CallContext(ctx, &result, "eth_getProof", addr, []string{}, t.chain.CurrentHeader().Hash()); err != nil {
return err
}
state, _ := t.chain.State()
balance := state.GetBalance(addr)
if result.Balance.ToInt().Cmp(balance) != 0 {
return fmt.Errorf("unexpected balance (got: %s, want: %s)", result.Balance, balance)
}
return nil
},
},
{
"get-account-proof-with-storage",
"gets proof for a certain account",
func(ctx context.Context, t *T) error {
addr := common.Address{0xaa}
result, err := t.geth.GetProof(ctx, addr, []string{"0x01"}, big.NewInt(3))
if err != nil {
return err
}
state, _ := t.chain.State()
balance := state.GetBalance(addr)
if result.Balance.Cmp(balance) != 0 {
return fmt.Errorf("unexpected balance (got: %s, want: %s)", result.Balance, balance)
}
if len(result.StorageProof) == 0 || len(result.StorageProof[0].Proof) == 0 {
return fmt.Errorf("expected storage proof")
}
return nil
},
},
},
}
var DebugGetRawHeader = MethodTests{
"debug_getRawHeader",
[]Test{
{
"get-genesis",
"gets block 0",
func(ctx context.Context, t *T) error {
var got hexutil.Bytes
if err := t.rpc.CallContext(ctx, &got, "debug_getRawHeader", "0x0"); err != nil {
return err
}
return checkHeaderRLP(t, 0, got)
},
},
{
"get-block-n",
"gets non-zero block",
func(ctx context.Context, t *T) error {
var got hexutil.Bytes
if err := t.rpc.CallContext(ctx, &got, "debug_getRawHeader", "0x3"); err != nil {
return err
}
return checkHeaderRLP(t, 3, got)
},
},
{
"get-invalid-number",
"gets block with invalid number formatting",
func(ctx context.Context, t *T) error {
err := t.rpc.CallContext(ctx, nil, "debug_getRawHeader", "2")
if !strings.HasPrefix(err.Error(), "invalid argument 0") {
return err
}
return nil
},
},
},
}
var DebugGetRawBlock = MethodTests{
"debug_getRawBlock",
[]Test{
{
"get-genesis",
"gets block 0",
func(ctx context.Context, t *T) error {
var got hexutil.Bytes
if err := t.rpc.CallContext(ctx, &got, "debug_getRawBlock", "0x0"); err != nil {
return err
}
return checkBlockRLP(t, 0, got)
},
},
{
"get-block-n",
"gets non-zero block",
func(ctx context.Context, t *T) error {
var got hexutil.Bytes
if err := t.rpc.CallContext(ctx, &got, "debug_getRawBlock", "0x3"); err != nil {
return err
}
return checkBlockRLP(t, 3, got)
},
},
{
"get-invalid-number",
"gets block with invalid number formatting",
func(ctx context.Context, t *T) error {
err := t.rpc.CallContext(ctx, nil, "debug_getRawBlock", "2")
if !strings.HasPrefix(err.Error(), "invalid argument 0") {
return err
}
return nil
},
},
},
}
var DebugGetRawReceipts = MethodTests{
"debug_getRawReceipts",
[]Test{
{
"get-genesis",
"gets receipts for block 0",
func(ctx context.Context, t *T) error {
return t.rpc.CallContext(ctx, nil, "debug_getRawReceipts", "0x0")
},
},
{
"get-block-n",
"gets receipts non-zero block",
func(ctx context.Context, t *T) error {
return t.rpc.CallContext(ctx, nil, "debug_getRawReceipts", "0x3")
},
},
{
"get-invalid-number",
"gets receipts with invalid number formatting",
func(ctx context.Context, t *T) error {
err := t.rpc.CallContext(ctx, nil, "debug_getRawReceipts", "2")
if !strings.HasPrefix(err.Error(), "invalid argument 0") {
return err
}
return nil
},
},
},
}
var DebugGetRawTransaction = MethodTests{
"debug_getRawTransaction",
[]Test{
{
"get-tx",
"gets tx rlp by hash",
func(ctx context.Context, t *T) error {
tx := t.chain.GetBlockByNumber(1).Transactions()[0]
var got hexutil.Bytes
if err := t.rpc.CallContext(ctx, &got, "debug_getRawTransaction", tx.Hash().Hex()); err != nil {
return err
}
want, err := tx.MarshalBinary()
if err != nil {
return err
}
if !bytes.Equal(got, want) {
return fmt.Errorf("mismatching raw tx (got: %s, want: %s)", hexutil.Bytes(got), hexutil.Bytes(want))
}
return nil
},
},
{
"get-invalid-hash",
"gets tx with hash missing 0x prefix",
func(ctx context.Context, t *T) error {
var got hexutil.Bytes
err := t.rpc.CallContext(ctx, &got, "debug_getRawTransaction", "1000000000000000000000000000000000000000000000000000000000000001")
if !strings.HasPrefix(err.Error(), "invalid argument 0") {
return err
}
return nil
},
},
},