@@ -37,6 +37,7 @@ import (
3737 "github.com/ethereum/go-ethereum/crypto"
3838 "github.com/ethereum/go-ethereum/ethdb"
3939 "github.com/ethereum/go-ethereum/event"
40+ "github.com/ethereum/go-ethereum/internal/ethapi"
4041 "github.com/ethereum/go-ethereum/params"
4142 "github.com/ethereum/go-ethereum/rpc"
4243)
@@ -52,11 +53,12 @@ type testBackend struct {
5253}
5354
5455func (b * testBackend ) ChainConfig () * params.ChainConfig {
55- panic ( "implement me" )
56+ return params . TestChainConfig
5657}
5758
5859func (b * testBackend ) CurrentHeader () * types.Header {
59- panic ("implement me" )
60+ hdr , _ := b .HeaderByNumber (context .TODO (), rpc .LatestBlockNumber )
61+ return hdr
6062}
6163
6264func (b * testBackend ) ChainDb () ethdb.Database {
@@ -256,10 +258,10 @@ func TestPendingTxFilter(t *testing.T) {
256258 types .NewTransaction (4 , common .HexToAddress ("0xb794f5ea0ba39494ce83a213fffba74279579268" ), new (big.Int ), 0 , new (big.Int ), nil ),
257259 }
258260
259- txs []* types. Transaction
261+ hashes []common. Hash
260262 )
261263
262- fid0 := api .NewPendingTransactionFilter ()
264+ fid0 := api .NewPendingTransactionFilter (nil )
263265
264266 time .Sleep (1 * time .Second )
265267 backend .txFeed .Send (core.NewTxsEvent {Txs : transactions })
@@ -271,7 +273,64 @@ func TestPendingTxFilter(t *testing.T) {
271273 t .Fatalf ("Unable to retrieve logs: %v" , err )
272274 }
273275
274- tx := results .([]* types.Transaction )
276+ h := results .([]common.Hash )
277+ hashes = append (hashes , h ... )
278+ if len (hashes ) >= len (transactions ) {
279+ break
280+ }
281+ // check timeout
282+ if time .Now ().After (timeout ) {
283+ break
284+ }
285+
286+ time .Sleep (100 * time .Millisecond )
287+ }
288+
289+ if len (hashes ) != len (transactions ) {
290+ t .Errorf ("invalid number of transactions, want %d transactions(s), got %d" , len (transactions ), len (hashes ))
291+ return
292+ }
293+ for i := range hashes {
294+ if hashes [i ] != transactions [i ].Hash () {
295+ t .Errorf ("hashes[%d] invalid, want %x, got %x" , i , transactions [i ].Hash (), hashes [i ])
296+ }
297+ }
298+ }
299+
300+ // TestPendingTxFilterFullTx tests whether pending tx filters retrieve all pending transactions that are posted to the event mux.
301+ func TestPendingTxFilterFullTx (t * testing.T ) {
302+ t .Parallel ()
303+
304+ var (
305+ db = rawdb .NewMemoryDatabase ()
306+ backend , sys = newTestFilterSystem (t , db , Config {})
307+ api = NewFilterAPI (sys , false )
308+
309+ transactions = []* types.Transaction {
310+ types .NewTransaction (0 , common .HexToAddress ("0xb794f5ea0ba39494ce83a213fffba74279579268" ), new (big.Int ), 0 , new (big.Int ), nil ),
311+ types .NewTransaction (1 , common .HexToAddress ("0xb794f5ea0ba39494ce83a213fffba74279579268" ), new (big.Int ), 0 , new (big.Int ), nil ),
312+ types .NewTransaction (2 , common .HexToAddress ("0xb794f5ea0ba39494ce83a213fffba74279579268" ), new (big.Int ), 0 , new (big.Int ), nil ),
313+ types .NewTransaction (3 , common .HexToAddress ("0xb794f5ea0ba39494ce83a213fffba74279579268" ), new (big.Int ), 0 , new (big.Int ), nil ),
314+ types .NewTransaction (4 , common .HexToAddress ("0xb794f5ea0ba39494ce83a213fffba74279579268" ), new (big.Int ), 0 , new (big.Int ), nil ),
315+ }
316+
317+ txs []* ethapi.RPCTransaction
318+ )
319+
320+ fullTx := true
321+ fid0 := api .NewPendingTransactionFilter (& fullTx )
322+
323+ time .Sleep (1 * time .Second )
324+ backend .txFeed .Send (core.NewTxsEvent {Txs : transactions })
325+
326+ timeout := time .Now ().Add (1 * time .Second )
327+ for {
328+ results , err := api .GetFilterChanges (fid0 )
329+ if err != nil {
330+ t .Fatalf ("Unable to retrieve logs: %v" , err )
331+ }
332+
333+ tx := results .([]* ethapi.RPCTransaction )
275334 txs = append (txs , tx ... )
276335 if len (txs ) >= len (transactions ) {
277336 break
@@ -289,8 +348,8 @@ func TestPendingTxFilter(t *testing.T) {
289348 return
290349 }
291350 for i := range txs {
292- if txs [i ].Hash () != transactions [i ].Hash () {
293- t .Errorf ("hashes[%d] invalid, want %x, got %x" , i , transactions [i ].Hash (), txs [i ].Hash () )
351+ if txs [i ].Hash != transactions [i ].Hash () {
352+ t .Errorf ("hashes[%d] invalid, want %x, got %x" , i , transactions [i ].Hash (), txs [i ].Hash )
294353 }
295354 }
296355}
@@ -854,15 +913,15 @@ func TestPendingTxFilterDeadlock(t *testing.T) {
854913 // timeout either in 100ms or 200ms
855914 fids := make ([]rpc.ID , 20 )
856915 for i := 0 ; i < len (fids ); i ++ {
857- fid := api .NewPendingTransactionFilter ()
916+ fid := api .NewPendingTransactionFilter (nil )
858917 fids [i ] = fid
859918 // Wait for at least one tx to arrive in filter
860919 for {
861- txs , err := api .GetFilterChanges (fid )
920+ hashes , err := api .GetFilterChanges (fid )
862921 if err != nil {
863922 t .Fatalf ("Filter should exist: %v\n " , err )
864923 }
865- if len (txs .([]* types. Transaction )) > 0 {
924+ if len (hashes .([]common. Hash )) > 0 {
866925 break
867926 }
868927 runtime .Gosched ()
0 commit comments