17
17
package eth
18
18
19
19
import (
20
+ "bytes"
20
21
"math"
21
22
"math/big"
22
23
"math/rand"
23
24
"testing"
25
+ "time"
24
26
25
27
"github.com/ethereum/go-ethereum/common"
26
28
"github.com/ethereum/go-ethereum/consensus"
@@ -37,6 +39,7 @@ import (
37
39
"github.com/ethereum/go-ethereum/p2p"
38
40
"github.com/ethereum/go-ethereum/p2p/enode"
39
41
"github.com/ethereum/go-ethereum/params"
42
+ "github.com/ethereum/go-ethereum/rlp"
40
43
)
41
44
42
45
var (
@@ -142,10 +145,12 @@ func (b *testBackend) RunPeer(peer *Peer, handler Handler) error {
142
145
func (b * testBackend ) PeerInfo (enode.ID ) interface {} { panic ("not implemented" ) }
143
146
144
147
func (b * testBackend ) AcceptTxs () bool {
145
- panic ("data processing tests should be done in the handler package" )
148
+ return true
149
+ //panic("data processing tests should be done in the handler package")
146
150
}
147
151
func (b * testBackend ) Handle (* Peer , Packet ) error {
148
- panic ("data processing tests should be done in the handler package" )
152
+ return nil
153
+ //panic("data processing tests should be done in the handler package")
149
154
}
150
155
151
156
// Tests that block headers can be retrieved from a remote chain based on user queries.
@@ -498,3 +503,76 @@ func testGetBlockReceipts(t *testing.T, protocol uint) {
498
503
t .Errorf ("receipts mismatch: %v" , err )
499
504
}
500
505
}
506
+
507
+ type decoder struct {
508
+ msg []byte
509
+ }
510
+
511
+ func (d decoder ) Decode (val interface {}) error {
512
+ buffer := bytes .NewBuffer (d .msg )
513
+ s := rlp .NewStream (buffer , uint64 (len (d .msg )))
514
+ return s .Decode (val )
515
+ }
516
+
517
+ func (d decoder ) Time () time.Time {
518
+ return time .Now ()
519
+ }
520
+
521
+ func setup () (* testBackend , * testPeer ) {
522
+ // Generate some transactions etc.
523
+ acc1Key , _ := crypto .HexToECDSA ("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a" )
524
+ acc2Key , _ := crypto .HexToECDSA ("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee" )
525
+ acc1Addr := crypto .PubkeyToAddress (acc1Key .PublicKey )
526
+ acc2Addr := crypto .PubkeyToAddress (acc2Key .PublicKey )
527
+ signer := types.HomesteadSigner {}
528
+ gen := func (n int , block * core.BlockGen ) {
529
+ if n % 2 == 0 {
530
+ w := & types.Withdrawal {
531
+ Address : common.Address {0xaa },
532
+ Amount : 42 ,
533
+ }
534
+ block .AddWithdrawal (w )
535
+ }
536
+ switch n {
537
+ case 0 :
538
+ // In block 1, the test bank sends account #1 some ether.
539
+ tx , _ := types .SignTx (types .NewTransaction (block .TxNonce (testAddr ), acc1Addr , big .NewInt (10_000_000_000_000_000 ), params .TxGas , block .BaseFee (), nil ), signer , testKey )
540
+ block .AddTx (tx )
541
+ case 1 :
542
+ // In block 2, the test bank sends some more ether to account #1.
543
+ // acc1Addr passes it on to account #2.
544
+ tx1 , _ := types .SignTx (types .NewTransaction (block .TxNonce (testAddr ), acc1Addr , big .NewInt (1_000_000_000_000_000 ), params .TxGas , block .BaseFee (), nil ), signer , testKey )
545
+ tx2 , _ := types .SignTx (types .NewTransaction (block .TxNonce (acc1Addr ), acc2Addr , big .NewInt (1_000_000_000_000_000 ), params .TxGas , block .BaseFee (), nil ), signer , acc1Key )
546
+ block .AddTx (tx1 )
547
+ block .AddTx (tx2 )
548
+ case 2 :
549
+ // Block 3 is empty but was mined by account #2.
550
+ block .SetCoinbase (acc2Addr )
551
+ block .SetExtra ([]byte ("yeehaw" ))
552
+ }
553
+ }
554
+ backend := newTestBackendWithGenerator (maxBodiesServe + 15 , true , gen )
555
+ peer , _ := newTestPeer ("peer" , ETH68 , backend )
556
+ // Discard all messages
557
+ go func () {
558
+ for {
559
+ msg , err := peer .app .ReadMsg ()
560
+ if err == nil {
561
+ msg .Discard ()
562
+ }
563
+ }
564
+ }()
565
+ return backend , peer
566
+ }
567
+
568
+ func FuzzEthProtocolHandlers (f * testing.F ) {
569
+ handlers := eth68
570
+ backend , peer := setup ()
571
+ f .Fuzz (func (t * testing.T , code byte , msg []byte ) {
572
+ handler := handlers [uint64 (code )% protocolLengths [ETH68 ]]
573
+ if handler == nil {
574
+ return
575
+ }
576
+ handler (backend , decoder {msg : msg }, peer .Peer )
577
+ })
578
+ }
0 commit comments