github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/les/request_test.go (about) 1 package les 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/quickchainproject/quickchain/common" 9 "github.com/quickchainproject/quickchain/core" 10 "github.com/quickchainproject/quickchain/crypto" 11 "github.com/quickchainproject/quickchain/qct" 12 "github.com/quickchainproject/quickchain/qctdb" 13 "github.com/quickchainproject/quickchain/light" 14 ) 15 16 var testBankSecureTrieKey = secAddr(testBankAddress) 17 18 func secAddr(addr common.Address) []byte { 19 return crypto.Keccak256(addr[:]) 20 } 21 22 type accessTestFn func(db qctdb.Database, bhash common.Hash, number uint64) light.OdrRequest 23 24 func TestBlockAccessLes1(t *testing.T) { testAccess(t, 1, tfBlockAccess) } 25 26 func TestBlockAccessLes2(t *testing.T) { testAccess(t, 2, tfBlockAccess) } 27 28 func tfBlockAccess(db qctdb.Database, bhash common.Hash, number uint64) light.OdrRequest { 29 return &light.BlockRequest{Hash: bhash, Number: number} 30 } 31 32 func TestReceiptsAccessLes1(t *testing.T) { testAccess(t, 1, tfReceiptsAccess) } 33 34 func TestReceiptsAccessLes2(t *testing.T) { testAccess(t, 2, tfReceiptsAccess) } 35 36 func tfReceiptsAccess(db qctdb.Database, bhash common.Hash, number uint64) light.OdrRequest { 37 return &light.ReceiptsRequest{Hash: bhash, Number: number} 38 } 39 40 func TestTrieEntryAccessLes1(t *testing.T) { testAccess(t, 1, tfTrieEntryAccess) } 41 42 func TestTrieEntryAccessLes2(t *testing.T) { testAccess(t, 2, tfTrieEntryAccess) } 43 44 func tfTrieEntryAccess(db qctdb.Database, bhash common.Hash, number uint64) light.OdrRequest { 45 return &light.TrieRequest{Id: light.StateTrieID(core.GetHeader(db, bhash, core.GetBlockNumber(db, bhash))), Key: testBankSecureTrieKey} 46 } 47 48 func TestCodeAccessLes1(t *testing.T) { testAccess(t, 1, tfCodeAccess) } 49 50 func TestCodeAccessLes2(t *testing.T) { testAccess(t, 2, tfCodeAccess) } 51 52 func tfCodeAccess(db qctdb.Database, bhash common.Hash, number uint64) light.OdrRequest { 53 header := core.GetHeader(db, bhash, core.GetBlockNumber(db, bhash)) 54 if header.Number.Uint64() < testContractDeployed { 55 return nil 56 } 57 sti := light.StateTrieID(header) 58 ci := light.StorageTrieID(sti, crypto.Keccak256Hash(testContractAddr[:]), common.Hash{}) 59 return &light.CodeRequest{Id: ci, Hash: crypto.Keccak256Hash(testContractCodeDeployed)} 60 } 61 62 func testAccess(t *testing.T, protocol int, fn accessTestFn) { 63 // Assemble the test environment 64 peers := newPeerSet() 65 dist := newRequestDistributor(peers, make(chan struct{})) 66 rm := newRetrieveManager(peers, dist, nil) 67 db, _ := qctdb.NewMemDatabase() 68 ldb, _ := qctdb.NewMemDatabase() 69 odr := NewLesOdr(ldb, light.NewChtIndexer(db, true), light.NewBloomTrieIndexer(db, true), let.NewBloomIndexer(db, light.BloomTrieFrequency), rm) 70 71 pm := newTestProtocolManagerMust(t, false, 4, testChainGen, nil, nil, db) 72 lpm := newTestProtocolManagerMust(t, true, 0, nil, peers, odr, ldb) 73 _, err1, lpeer, err2 := newTestPeerPair("peer", protocol, pm, lpm) 74 select { 75 case <-time.After(time.Millisecond * 100): 76 case err := <-err1: 77 t.Fatalf("peer 1 handshake error: %v", err) 78 case err := <-err2: 79 t.Fatalf("peer 1 handshake error: %v", err) 80 } 81 82 lpm.synchronise(lpeer) 83 84 test := func(expFail uint64) { 85 for i := uint64(0); i <= pm.blockchain.CurrentHeader().Number.Uint64(); i++ { 86 bhash := core.GetCanonicalHash(db, i) 87 if req := fn(ldb, bhash, i); req != nil { 88 ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) 89 defer cancel() 90 91 err := odr.Retrieve(ctx, req) 92 got := err == nil 93 exp := i < expFail 94 if exp && !got { 95 t.Errorf("object retrieval failed") 96 } 97 if !exp && got { 98 t.Errorf("unexpected object retrieval success") 99 } 100 } 101 } 102 } 103 104 // temporarily remove peer to test odr fails 105 peers.Unregister(lpeer.id) 106 time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed 107 // expect retrievals to fail (except genesis block) without a les peer 108 test(0) 109 110 peers.Register(lpeer) 111 time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed 112 lpeer.lock.Lock() 113 lpeer.hasBlock = func(common.Hash, uint64) bool { return true } 114 lpeer.lock.Unlock() 115 // expect all retrievals to pass 116 test(5) 117 }