@@ -19,10 +19,7 @@ package les
19
19
import (
20
20
"bytes"
21
21
"context"
22
- "crypto/rand"
23
- "fmt"
24
22
"math/big"
25
- "reflect"
26
23
"testing"
27
24
"time"
28
25
@@ -32,7 +29,6 @@ import (
32
29
"github.com/celo-org/celo-blockchain/core"
33
30
"github.com/celo-org/celo-blockchain/core/rawdb"
34
31
"github.com/celo-org/celo-blockchain/core/state"
35
- "github.com/celo-org/celo-blockchain/p2p"
36
32
37
33
"github.com/celo-org/celo-blockchain/core/types"
38
34
"github.com/celo-org/celo-blockchain/core/vm"
@@ -260,200 +256,3 @@ func testOdr(t *testing.T, protocol int, expFail uint64, checkCached bool, fn od
260
256
test (5 )
261
257
}
262
258
}
263
-
264
- func TestGetTxStatusFromUnindexedPeersLES5 (t * testing.T ) { testGetTxStatusFromUnindexedPeers (t , lpv5 ) }
265
-
266
- func testGetTxStatusFromUnindexedPeers (t * testing.T , protocol int ) {
267
- var (
268
- blocks = 8
269
- netconfig = testnetConfig {
270
- blocks : blocks ,
271
- syncMode : downloader .LightSync ,
272
- protocol : protocol ,
273
- nopruning : true ,
274
- }
275
- )
276
- server , client , tearDown := newClientServerEnv (t , netconfig )
277
- defer tearDown ()
278
-
279
- // Iterate the chain, create the tx indexes locally
280
- var (
281
- testHash common.Hash
282
- testStatus light.TxStatus
283
-
284
- txs = make (map [common.Hash ]* types.Transaction ) // Transaction objects set
285
- blockNumbers = make (map [common.Hash ]uint64 ) // Transaction hash to block number mappings
286
- blockHashes = make (map [common.Hash ]common.Hash ) // Transaction hash to block hash mappings
287
- intraIndex = make (map [common.Hash ]uint64 ) // Transaction intra-index in block
288
- )
289
- for number := uint64 (1 ); number < server .backend .Blockchain ().CurrentBlock ().NumberU64 (); number ++ {
290
- block := server .backend .Blockchain ().GetBlockByNumber (number )
291
- if block == nil {
292
- t .Fatalf ("Failed to retrieve block %d" , number )
293
- }
294
- for index , tx := range block .Transactions () {
295
- txs [tx .Hash ()] = tx
296
- blockNumbers [tx .Hash ()] = number
297
- blockHashes [tx .Hash ()] = block .Hash ()
298
- intraIndex [tx .Hash ()] = uint64 (index )
299
-
300
- if testHash == (common.Hash {}) {
301
- testHash = tx .Hash ()
302
- testStatus = light.TxStatus {
303
- Status : core .TxStatusIncluded ,
304
- Lookup : & rawdb.LegacyTxLookupEntry {
305
- BlockHash : block .Hash (),
306
- BlockIndex : block .NumberU64 (),
307
- Index : uint64 (index ),
308
- },
309
- }
310
- }
311
- }
312
- }
313
- // serveMsg processes incoming GetTxStatusMsg and sends the response back.
314
- serveMsg := func (peer * testPeer , txLookup uint64 ) error {
315
- var (
316
- msg p2p.Msg
317
- err error
318
- )
319
- loop:
320
- for {
321
- msg , err = peer .app .ReadMsg ()
322
- if err != nil {
323
- return err
324
- }
325
- switch msg .Code {
326
- case GetEtherbaseMsg :
327
- continue
328
- default :
329
- break loop
330
- }
331
- }
332
- if msg .Code != GetTxStatusMsg {
333
- return fmt .Errorf ("message code mismatch: got %d, expected %d" , msg .Code , GetTxStatusMsg )
334
- }
335
- var r GetTxStatusPacket
336
- if err := msg .Decode (& r ); err != nil {
337
- return err
338
- }
339
- stats := make ([]light.TxStatus , len (r .Hashes ))
340
- for i , hash := range r .Hashes {
341
- number , exist := blockNumbers [hash ]
342
- if ! exist {
343
- continue // Filter out unknown transactions
344
- }
345
- min := uint64 (blocks ) - txLookup
346
- if txLookup != txIndexUnlimited && (txLookup == txIndexDisabled || number < min ) {
347
- continue // Filter out unindexed transactions
348
- }
349
- stats [i ].Status = core .TxStatusIncluded
350
- stats [i ].Lookup = & rawdb.LegacyTxLookupEntry {
351
- BlockHash : blockHashes [hash ],
352
- BlockIndex : number ,
353
- Index : intraIndex [hash ],
354
- }
355
- }
356
- data , _ := rlp .EncodeToBytes (stats )
357
- reply := & reply {peer .app , TxStatusMsg , r .ReqID , data }
358
- if err = reply .send (testBufLimit ); err != nil {
359
- return err
360
- }
361
- return nil
362
- }
363
-
364
- var testspecs = []struct {
365
- peers int
366
- txLookups []uint64
367
- txs []common.Hash
368
- results []light.TxStatus
369
- }{
370
- // Retrieve mined transaction from the empty peerset
371
- {
372
- peers : 0 ,
373
- txLookups : []uint64 {},
374
- txs : []common.Hash {testHash },
375
- results : []light.TxStatus {{}},
376
- },
377
- // Retrieve unknown transaction from the full peers
378
- {
379
- peers : 3 ,
380
- txLookups : []uint64 {txIndexUnlimited , txIndexUnlimited , txIndexUnlimited },
381
- txs : []common.Hash {randomHash ()},
382
- results : []light.TxStatus {{}},
383
- },
384
- // Retrieve mined transaction from the full peers
385
- {
386
- peers : 3 ,
387
- txLookups : []uint64 {txIndexUnlimited , txIndexUnlimited , txIndexUnlimited },
388
- txs : []common.Hash {testHash },
389
- results : []light.TxStatus {testStatus },
390
- },
391
- // Retrieve mixed transactions from the full peers
392
- {
393
- peers : 3 ,
394
- txLookups : []uint64 {txIndexUnlimited , txIndexUnlimited , txIndexUnlimited },
395
- txs : []common.Hash {randomHash (), testHash },
396
- results : []light.TxStatus {{}, testStatus },
397
- },
398
- // Retrieve mixed transactions from unindexed peer(but the target is still available)
399
- {
400
- peers : 3 ,
401
- txLookups : []uint64 {uint64 (blocks ) - testStatus .Lookup .BlockIndex , uint64 (blocks ) - testStatus .Lookup .BlockIndex - 1 , uint64 (blocks ) - testStatus .Lookup .BlockIndex - 2 },
402
- txs : []common.Hash {randomHash (), testHash },
403
- results : []light.TxStatus {{}, testStatus },
404
- },
405
- // Retrieve mixed transactions from unindexed peer(but the target is not available)
406
- {
407
- peers : 3 ,
408
- txLookups : []uint64 {uint64 (blocks ) - testStatus .Lookup .BlockIndex - 1 , uint64 (blocks ) - testStatus .Lookup .BlockIndex - 1 , uint64 (blocks ) - testStatus .Lookup .BlockIndex - 2 },
409
- txs : []common.Hash {randomHash (), testHash },
410
- results : []light.TxStatus {{}, {}},
411
- },
412
- }
413
- for _ , testspec := range testspecs {
414
- // Create a bunch of server peers with different tx history
415
- var (
416
- serverPeers []* testPeer
417
- closeFns []func ()
418
- )
419
- for i := 0 ; i < testspec .peers ; i ++ {
420
- peer , closePeer , _ := client .newRawPeer (t , fmt .Sprintf ("server-%d" , i ), protocol , testspec .txLookups [i ])
421
- serverPeers = append (serverPeers , peer )
422
- closeFns = append (closeFns , closePeer )
423
-
424
- // Create a one-time routine for serving message
425
- go func (i int , peer * testPeer , lookup uint64 ) {
426
- serveMsg (peer , lookup )
427
- }(i , peer , testspec .txLookups [i ])
428
- }
429
-
430
- // Send out the GetTxStatus requests, compare the result with
431
- // expected value.
432
- r := & light.TxStatusRequest {Hashes : testspec .txs }
433
- ctx , cancel := context .WithTimeout (context .Background (), time .Second )
434
- defer cancel ()
435
-
436
- err := client .handler .backend .odr .RetrieveTxStatus (ctx , r )
437
- if err != nil {
438
- t .Errorf ("Failed to retrieve tx status %v" , err )
439
- } else {
440
- if ! reflect .DeepEqual (testspec .results , r .Status ) {
441
- t .Errorf ("Result mismatch, diff" )
442
- }
443
- }
444
-
445
- // Close all connected peers and start the next round
446
- for _ , closeFn := range closeFns {
447
- closeFn ()
448
- }
449
- }
450
- }
451
-
452
- // randomHash generates a random blob of data and returns it as a hash.
453
- func randomHash () common.Hash {
454
- var hash common.Hash
455
- if n , err := rand .Read (hash [:]); n != common .HashLength || err != nil {
456
- panic (err )
457
- }
458
- return hash
459
- }
0 commit comments