@@ -37,6 +37,7 @@ import (
37
37
"github.com/ethereum/go-ethereum/crypto"
38
38
"github.com/ethereum/go-ethereum/ethdb"
39
39
"github.com/ethereum/go-ethereum/event"
40
+ "github.com/ethereum/go-ethereum/internal/ethapi"
40
41
"github.com/ethereum/go-ethereum/params"
41
42
"github.com/ethereum/go-ethereum/rpc"
42
43
)
@@ -52,11 +53,12 @@ type testBackend struct {
52
53
}
53
54
54
55
func (b * testBackend ) ChainConfig () * params.ChainConfig {
55
- panic ( "implement me" )
56
+ return params . TestChainConfig
56
57
}
57
58
58
59
func (b * testBackend ) CurrentHeader () * types.Header {
59
- panic ("implement me" )
60
+ hdr , _ := b .HeaderByNumber (context .TODO (), rpc .LatestBlockNumber )
61
+ return hdr
60
62
}
61
63
62
64
func (b * testBackend ) ChainDb () ethdb.Database {
@@ -256,10 +258,10 @@ func TestPendingTxFilter(t *testing.T) {
256
258
types .NewTransaction (4 , common .HexToAddress ("0xb794f5ea0ba39494ce83a213fffba74279579268" ), new (big.Int ), 0 , new (big.Int ), nil ),
257
259
}
258
260
259
- txs []* types. Transaction
261
+ hashes []common. Hash
260
262
)
261
263
262
- fid0 := api .NewPendingTransactionFilter ()
264
+ fid0 := api .NewPendingTransactionFilter (nil )
263
265
264
266
time .Sleep (1 * time .Second )
265
267
backend .txFeed .Send (core.NewTxsEvent {Txs : transactions })
@@ -271,7 +273,64 @@ func TestPendingTxFilter(t *testing.T) {
271
273
t .Fatalf ("Unable to retrieve logs: %v" , err )
272
274
}
273
275
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 )
275
334
txs = append (txs , tx ... )
276
335
if len (txs ) >= len (transactions ) {
277
336
break
@@ -289,8 +348,8 @@ func TestPendingTxFilter(t *testing.T) {
289
348
return
290
349
}
291
350
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 )
294
353
}
295
354
}
296
355
}
@@ -854,15 +913,15 @@ func TestPendingTxFilterDeadlock(t *testing.T) {
854
913
// timeout either in 100ms or 200ms
855
914
fids := make ([]rpc.ID , 20 )
856
915
for i := 0 ; i < len (fids ); i ++ {
857
- fid := api .NewPendingTransactionFilter ()
916
+ fid := api .NewPendingTransactionFilter (nil )
858
917
fids [i ] = fid
859
918
// Wait for at least one tx to arrive in filter
860
919
for {
861
- txs , err := api .GetFilterChanges (fid )
920
+ hashes , err := api .GetFilterChanges (fid )
862
921
if err != nil {
863
922
t .Fatalf ("Filter should exist: %v\n " , err )
864
923
}
865
- if len (txs .([]* types. Transaction )) > 0 {
924
+ if len (hashes .([]common. Hash )) > 0 {
866
925
break
867
926
}
868
927
runtime .Gosched ()
0 commit comments