request_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package les
  17. import (
  18. "context"
  19. "testing"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/ethdb"
  25. "github.com/ethereum/go-ethereum/light"
  26. )
  27. var testBankSecureTrieKey = secAddr(testBankAddress)
  28. func secAddr(addr common.Address) []byte {
  29. return crypto.Keccak256(addr[:])
  30. }
  31. type accessTestFn func(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest
  32. func TestBlockAccessLes1(t *testing.T) { testAccess(t, 1, tfBlockAccess) }
  33. func tfBlockAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
  34. return &light.BlockRequest{Hash: bhash, Number: number}
  35. }
  36. func TestReceiptsAccessLes1(t *testing.T) { testAccess(t, 1, tfReceiptsAccess) }
  37. func tfReceiptsAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
  38. return &light.ReceiptsRequest{Hash: bhash, Number: number}
  39. }
  40. func TestTrieEntryAccessLes1(t *testing.T) { testAccess(t, 1, tfTrieEntryAccess) }
  41. func tfTrieEntryAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
  42. return &light.TrieRequest{Id: light.StateTrieID(core.GetHeader(db, bhash, core.GetBlockNumber(db, bhash))), Key: testBankSecureTrieKey}
  43. }
  44. func TestCodeAccessLes1(t *testing.T) { testAccess(t, 1, tfCodeAccess) }
  45. func tfCodeAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
  46. header := core.GetHeader(db, bhash, core.GetBlockNumber(db, bhash))
  47. if header.Number.Uint64() < testContractDeployed {
  48. return nil
  49. }
  50. sti := light.StateTrieID(header)
  51. ci := light.StorageTrieID(sti, crypto.Keccak256Hash(testContractAddr[:]), common.Hash{})
  52. return &light.CodeRequest{Id: ci, Hash: crypto.Keccak256Hash(testContractCodeDeployed)}
  53. }
  54. func testAccess(t *testing.T, protocol int, fn accessTestFn) {
  55. // Assemble the test environment
  56. peers := newPeerSet()
  57. dist := newRequestDistributor(peers, make(chan struct{}))
  58. rm := newRetrieveManager(peers, dist, nil)
  59. db, _ := ethdb.NewMemDatabase()
  60. ldb, _ := ethdb.NewMemDatabase()
  61. odr := NewLesOdr(ldb, rm)
  62. pm := newTestProtocolManagerMust(t, false, 4, testChainGen, nil, nil, db)
  63. lpm := newTestProtocolManagerMust(t, true, 0, nil, peers, odr, ldb)
  64. _, err1, lpeer, err2 := newTestPeerPair("peer", protocol, pm, lpm)
  65. select {
  66. case <-time.After(time.Millisecond * 100):
  67. case err := <-err1:
  68. t.Fatalf("peer 1 handshake error: %v", err)
  69. case err := <-err2:
  70. t.Fatalf("peer 1 handshake error: %v", err)
  71. }
  72. lpm.synchronise(lpeer)
  73. test := func(expFail uint64) {
  74. for i := uint64(0); i <= pm.blockchain.CurrentHeader().Number.Uint64(); i++ {
  75. bhash := core.GetCanonicalHash(db, i)
  76. if req := fn(ldb, bhash, i); req != nil {
  77. ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
  78. defer cancel()
  79. err := odr.Retrieve(ctx, req)
  80. got := err == nil
  81. exp := i < expFail
  82. if exp && !got {
  83. t.Errorf("object retrieval failed")
  84. }
  85. if !exp && got {
  86. t.Errorf("unexpected object retrieval success")
  87. }
  88. }
  89. }
  90. }
  91. // temporarily remove peer to test odr fails
  92. peers.Unregister(lpeer.id)
  93. time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed
  94. // expect retrievals to fail (except genesis block) without a les peer
  95. test(0)
  96. peers.Register(lpeer)
  97. time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed
  98. lpeer.lock.Lock()
  99. lpeer.hasBlock = func(common.Hash, uint64) bool { return true }
  100. lpeer.lock.Unlock()
  101. // expect all retrievals to pass
  102. test(5)
  103. }