@@ -18,9 +18,11 @@ package eth
18
18
19
19
import (
20
20
"bytes"
21
+ "crypto/sha256"
21
22
"math"
22
23
"math/big"
23
24
"math/rand"
25
+ "os"
24
26
"testing"
25
27
"time"
26
28
@@ -30,15 +32,18 @@ import (
30
32
"github.com/ethereum/go-ethereum/core"
31
33
"github.com/ethereum/go-ethereum/core/rawdb"
32
34
"github.com/ethereum/go-ethereum/core/txpool"
35
+ "github.com/ethereum/go-ethereum/core/txpool/blobpool"
33
36
"github.com/ethereum/go-ethereum/core/txpool/legacypool"
34
37
"github.com/ethereum/go-ethereum/core/types"
35
38
"github.com/ethereum/go-ethereum/core/vm"
36
39
"github.com/ethereum/go-ethereum/crypto"
40
+ "github.com/ethereum/go-ethereum/crypto/kzg4844"
37
41
"github.com/ethereum/go-ethereum/ethdb"
38
42
"github.com/ethereum/go-ethereum/p2p"
39
43
"github.com/ethereum/go-ethereum/p2p/enode"
40
44
"github.com/ethereum/go-ethereum/params"
41
45
"github.com/ethereum/go-ethereum/rlp"
46
+ "github.com/holiman/uint256"
42
47
)
43
48
44
49
var (
@@ -62,12 +67,12 @@ type testBackend struct {
62
67
63
68
// newTestBackend creates an empty chain and wraps it into a mock backend.
64
69
func newTestBackend (blocks int ) * testBackend {
65
- return newTestBackendWithGenerator (blocks , false , nil )
70
+ return newTestBackendWithGenerator (blocks , false , false , nil )
66
71
}
67
72
68
73
// newTestBackendWithGenerator creates a chain with a number of explicitly defined blocks and
69
74
// wraps it into a mock backend.
70
- func newTestBackendWithGenerator (blocks int , shanghai bool , generator func (int , * core.BlockGen )) * testBackend {
75
+ func newTestBackendWithGenerator (blocks int , shanghai bool , cancun bool , generator func (int , * core.BlockGen )) * testBackend {
71
76
var (
72
77
// Create a database pre-initialize with a genesis block
73
78
db = rawdb .NewMemoryDatabase ()
@@ -99,9 +104,21 @@ func newTestBackendWithGenerator(blocks int, shanghai bool, generator func(int,
99
104
}
100
105
}
101
106
107
+ if cancun {
108
+ config .CancunTime = u64 (0 )
109
+ config .BlobScheduleConfig = & params.BlobScheduleConfig {
110
+ Cancun : & params.BlobConfig {
111
+ Target : 3 ,
112
+ Max : 6 ,
113
+ UpdateFraction : params .DefaultCancunBlobConfig .UpdateFraction ,
114
+ },
115
+ }
116
+ }
117
+
102
118
gspec := & core.Genesis {
103
- Config : config ,
104
- Alloc : types.GenesisAlloc {testAddr : {Balance : big .NewInt (100_000_000_000_000_000 )}},
119
+ Config : config ,
120
+ Alloc : types.GenesisAlloc {testAddr : {Balance : big .NewInt (100_000_000_000_000_000 )}},
121
+ Difficulty : common .Big0 ,
105
122
}
106
123
chain , _ := core .NewBlockChain (db , nil , gspec , nil , engine , vm.Config {}, nil )
107
124
@@ -115,8 +132,12 @@ func newTestBackendWithGenerator(blocks int, shanghai bool, generator func(int,
115
132
txconfig := legacypool .DefaultConfig
116
133
txconfig .Journal = "" // Don't litter the disk with test journals
117
134
118
- pool := legacypool .New (txconfig , chain )
119
- txpool , _ := txpool .New (txconfig .PriceLimit , chain , []txpool.SubPool {pool })
135
+ storage , _ := os .MkdirTemp ("" , "blobpool-" )
136
+ defer os .RemoveAll (storage )
137
+
138
+ blobPool := blobpool .New (blobpool.Config {Datadir : storage }, chain )
139
+ legacyPool := legacypool .New (txconfig , chain )
140
+ txpool , _ := txpool .New (txconfig .PriceLimit , chain , []txpool.SubPool {legacyPool , blobPool })
120
141
121
142
return & testBackend {
122
143
db : db ,
@@ -351,7 +372,7 @@ func testGetBlockBodies(t *testing.T, protocol uint) {
351
372
}
352
373
}
353
374
354
- backend := newTestBackendWithGenerator (maxBodiesServe + 15 , true , gen )
375
+ backend := newTestBackendWithGenerator (maxBodiesServe + 15 , true , false , gen )
355
376
defer backend .close ()
356
377
357
378
peer , _ := newTestPeer ("peer" , protocol , backend )
@@ -471,7 +492,7 @@ func testGetBlockReceipts(t *testing.T, protocol uint) {
471
492
}
472
493
}
473
494
// Assemble the test environment
474
- backend := newTestBackendWithGenerator (4 , false , generator )
495
+ backend := newTestBackendWithGenerator (4 , false , false , generator )
475
496
defer backend .close ()
476
497
477
498
peer , _ := newTestPeer ("peer" , protocol , backend )
@@ -548,7 +569,7 @@ func setup() (*testBackend, *testPeer) {
548
569
block .SetExtra ([]byte ("yeehaw" ))
549
570
}
550
571
}
551
- backend := newTestBackendWithGenerator (maxBodiesServe + 15 , true , gen )
572
+ backend := newTestBackendWithGenerator (maxBodiesServe + 15 , true , false , gen )
552
573
peer , _ := newTestPeer ("peer" , ETH68 , backend )
553
574
// Discard all messages
554
575
go func () {
@@ -573,3 +594,80 @@ func FuzzEthProtocolHandlers(f *testing.F) {
573
594
handler (backend , decoder {msg : msg }, peer .Peer )
574
595
})
575
596
}
597
+
598
+ func TestGetPooledTransaction (t * testing.T ) {
599
+ t .Run ("blobTx" , func (t * testing.T ) {
600
+ testGetPooledTransaction (t , true )
601
+ })
602
+ t .Run ("legacyTx" , func (t * testing.T ) {
603
+ testGetPooledTransaction (t , false )
604
+ })
605
+ }
606
+
607
+ func testGetPooledTransaction (t * testing.T , blobTx bool ) {
608
+ var (
609
+ emptyBlob = kzg4844.Blob {}
610
+ emptyBlobs = []kzg4844.Blob {emptyBlob }
611
+ emptyBlobCommit , _ = kzg4844 .BlobToCommitment (& emptyBlob )
612
+ emptyBlobProof , _ = kzg4844 .ComputeBlobProof (& emptyBlob , emptyBlobCommit )
613
+ emptyBlobHash = kzg4844 .CalcBlobHashV1 (sha256 .New (), & emptyBlobCommit )
614
+ )
615
+ backend := newTestBackendWithGenerator (0 , true , true , nil )
616
+ defer backend .close ()
617
+
618
+ peer , _ := newTestPeer ("peer" , ETH68 , backend )
619
+ defer peer .close ()
620
+
621
+ var (
622
+ tx * types.Transaction
623
+ err error
624
+ signer = types .NewCancunSigner (params .TestChainConfig .ChainID )
625
+ )
626
+ if blobTx {
627
+ tx , err = types .SignNewTx (testKey , signer , & types.BlobTx {
628
+ ChainID : uint256 .MustFromBig (params .TestChainConfig .ChainID ),
629
+ Nonce : 0 ,
630
+ GasTipCap : uint256 .NewInt (20_000_000_000 ),
631
+ GasFeeCap : uint256 .NewInt (21_000_000_000 ),
632
+ Gas : 21000 ,
633
+ To : testAddr ,
634
+ BlobHashes : []common.Hash {emptyBlobHash },
635
+ BlobFeeCap : uint256 .MustFromBig (common .Big1 ),
636
+ Sidecar : & types.BlobTxSidecar {
637
+ Blobs : emptyBlobs ,
638
+ Commitments : []kzg4844.Commitment {emptyBlobCommit },
639
+ Proofs : []kzg4844.Proof {emptyBlobProof },
640
+ },
641
+ })
642
+ if err != nil {
643
+ t .Fatal (err )
644
+ }
645
+ } else {
646
+ tx , err = types .SignTx (
647
+ types .NewTransaction (0 , testAddr , big .NewInt (10_000 ), params .TxGas , big .NewInt (1_000_000_000 ), nil ),
648
+ signer ,
649
+ testKey ,
650
+ )
651
+ if err != nil {
652
+ t .Fatal (err )
653
+ }
654
+ }
655
+ errs := backend .txpool .Add ([]* types.Transaction {tx }, true )
656
+ for _ , err := range errs {
657
+ if err != nil {
658
+ t .Fatal (err )
659
+ }
660
+ }
661
+
662
+ // Send the hash request and verify the response
663
+ p2p .Send (peer .app , GetPooledTransactionsMsg , GetPooledTransactionsPacket {
664
+ RequestId : 123 ,
665
+ GetPooledTransactionsRequest : []common.Hash {tx .Hash ()},
666
+ })
667
+ if err := p2p .ExpectMsg (peer .app , PooledTransactionsMsg , PooledTransactionsPacket {
668
+ RequestId : 123 ,
669
+ PooledTransactionsResponse : []* types.Transaction {tx },
670
+ }); err != nil {
671
+ t .Errorf ("pooled transaction mismatch: %v" , err )
672
+ }
673
+ }
0 commit comments