@@ -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,15 @@ 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 (
76
+ blocks int , shanghai bool , cancun bool ,
77
+ generator func (int , * core.BlockGen ),
78
+ ) * testBackend {
71
79
var (
72
80
// Create a database pre-initialize with a genesis block
73
81
db = rawdb .NewMemoryDatabase ()
@@ -99,9 +107,21 @@ func newTestBackendWithGenerator(blocks int, shanghai bool, generator func(int,
99
107
}
100
108
}
101
109
110
+ if cancun {
111
+ config .CancunTime = u64 (0 )
112
+ config .BlobScheduleConfig = & params.BlobScheduleConfig {
113
+ Cancun : & params.BlobConfig {
114
+ Target : 3 ,
115
+ Max : 6 ,
116
+ UpdateFraction : params .DefaultCancunBlobConfig .UpdateFraction ,
117
+ },
118
+ }
119
+ }
120
+
102
121
gspec := & core.Genesis {
103
- Config : config ,
104
- Alloc : types.GenesisAlloc {testAddr : {Balance : big .NewInt (100_000_000_000_000_000 )}},
122
+ Config : config ,
123
+ Alloc : types.GenesisAlloc {testAddr : {Balance : big .NewInt (100_000_000_000_000_000 )}},
124
+ Difficulty : common .Big0 ,
105
125
}
106
126
chain , _ := core .NewBlockChain (db , nil , gspec , nil , engine , vm.Config {}, nil )
107
127
@@ -115,8 +135,12 @@ func newTestBackendWithGenerator(blocks int, shanghai bool, generator func(int,
115
135
txconfig := legacypool .DefaultConfig
116
136
txconfig .Journal = "" // Don't litter the disk with test journals
117
137
118
- pool := legacypool .New (txconfig , chain )
119
- txpool , _ := txpool .New (txconfig .PriceLimit , chain , []txpool.SubPool {pool })
138
+ storage , _ := os .MkdirTemp ("" , "blobpool-" )
139
+ defer os .RemoveAll (storage )
140
+
141
+ blobPool := blobpool .New (blobpool.Config {Datadir : storage }, chain )
142
+ legacyPool := legacypool .New (txconfig , chain )
143
+ txpool , _ := txpool .New (txconfig .PriceLimit , chain , []txpool.SubPool {legacyPool , blobPool })
120
144
121
145
return & testBackend {
122
146
db : db ,
@@ -351,7 +375,7 @@ func testGetBlockBodies(t *testing.T, protocol uint) {
351
375
}
352
376
}
353
377
354
- backend := newTestBackendWithGenerator (maxBodiesServe + 15 , true , gen )
378
+ backend := newTestBackendWithGenerator (maxBodiesServe + 15 , true , false , gen )
355
379
defer backend .close ()
356
380
357
381
peer , _ := newTestPeer ("peer" , protocol , backend )
@@ -471,7 +495,7 @@ func testGetBlockReceipts(t *testing.T, protocol uint) {
471
495
}
472
496
}
473
497
// Assemble the test environment
474
- backend := newTestBackendWithGenerator (4 , false , generator )
498
+ backend := newTestBackendWithGenerator (4 , false , false , generator )
475
499
defer backend .close ()
476
500
477
501
peer , _ := newTestPeer ("peer" , protocol , backend )
@@ -548,7 +572,7 @@ func setup() (*testBackend, *testPeer) {
548
572
block .SetExtra ([]byte ("yeehaw" ))
549
573
}
550
574
}
551
- backend := newTestBackendWithGenerator (maxBodiesServe + 15 , true , gen )
575
+ backend := newTestBackendWithGenerator (maxBodiesServe + 15 , true , false , gen )
552
576
peer , _ := newTestPeer ("peer" , ETH68 , backend )
553
577
// Discard all messages
554
578
go func () {
@@ -573,3 +597,81 @@ func FuzzEthProtocolHandlers(f *testing.F) {
573
597
handler (backend , decoder {msg : msg }, peer .Peer )
574
598
})
575
599
}
600
+
601
+ func TestGetPooledTransaction (t * testing.T ) {
602
+ t .Run ("blobTx" , func (t * testing.T ) {
603
+ testGetPooledTransaction (t , true )
604
+ })
605
+ t .Run ("legacyTx" , func (t * testing.T ) {
606
+ testGetPooledTransaction (t , false )
607
+ })
608
+ }
609
+
610
+ func testGetPooledTransaction (t * testing.T , blobTx bool ) {
611
+ var (
612
+ emptyBlob = kzg4844.Blob {}
613
+ emptyBlobs = []kzg4844.Blob {emptyBlob }
614
+ emptyBlobCommit , _ = kzg4844 .BlobToCommitment (& emptyBlob )
615
+ emptyBlobProof , _ = kzg4844 .ComputeBlobProof (& emptyBlob , emptyBlobCommit )
616
+ emptyBlobHash = kzg4844 .CalcBlobHashV1 (sha256 .New (), & emptyBlobCommit )
617
+ )
618
+ backend := newTestBackendWithGenerator (0 , true , true , nil )
619
+ defer backend .close ()
620
+
621
+ peer , _ := newTestPeer ("peer" , ETH68 , backend )
622
+ defer peer .close ()
623
+
624
+ signer := types .NewCancunSigner (params .TestChainConfig .ChainID )
625
+ var (
626
+ tx * types.Transaction
627
+ err error
628
+ )
629
+
630
+ if blobTx {
631
+ tx , err = types .SignNewTx (testKey , signer , & types.BlobTx {
632
+ ChainID : uint256 .MustFromBig (params .TestChainConfig .ChainID ),
633
+ Nonce : 0 ,
634
+ GasTipCap : uint256 .NewInt (20_000_000_000 ),
635
+ GasFeeCap : uint256 .NewInt (21_000_000_000 ),
636
+ Gas : 21000 ,
637
+ To : testAddr ,
638
+ BlobHashes : []common.Hash {emptyBlobHash },
639
+ BlobFeeCap : uint256 .MustFromBig (common .Big1 ),
640
+ Sidecar : & types.BlobTxSidecar {
641
+ Blobs : emptyBlobs ,
642
+ Commitments : []kzg4844.Commitment {emptyBlobCommit },
643
+ Proofs : []kzg4844.Proof {emptyBlobProof },
644
+ },
645
+ })
646
+ if err != nil {
647
+ t .Fatal (err )
648
+ }
649
+ } else {
650
+ tx , err = types .SignTx (
651
+ types .NewTransaction (0 , testAddr , big .NewInt (10_000 ), params .TxGas , big .NewInt (1_000_000_000 ), nil ),
652
+ signer ,
653
+ testKey ,
654
+ )
655
+ if err != nil {
656
+ t .Fatal (err )
657
+ }
658
+ }
659
+ errs := backend .txpool .Add ([]* types.Transaction {tx }, true )
660
+ for _ , err := range errs {
661
+ if err != nil {
662
+ t .Fatal (err )
663
+ }
664
+ }
665
+
666
+ // Send the hash request and verify the response
667
+ p2p .Send (peer .app , GetPooledTransactionsMsg , GetPooledTransactionsPacket {
668
+ RequestId : 123 ,
669
+ GetPooledTransactionsRequest : []common.Hash {tx .Hash ()},
670
+ })
671
+ if err := p2p .ExpectMsg (peer .app , PooledTransactionsMsg , PooledTransactionsPacket {
672
+ RequestId : 123 ,
673
+ PooledTransactionsResponse : []* types.Transaction {tx },
674
+ }); err != nil {
675
+ t .Errorf ("pooled transaction mismatch: %v" , err )
676
+ }
677
+ }
0 commit comments