request_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/rawdb"
  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 TestBlockAccessLes2(t *testing.T) { testAccess(t, 2, 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 TestReceiptsAccessLes2(t *testing.T) { testAccess(t, 2, 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 TestTrieEntryAccessLes2(t *testing.T) { testAccess(t, 2, tfTrieEntryAccess) }
  41. func tfTrieEntryAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
  42. if number := rawdb.ReadHeaderNumber(db, bhash); number != nil {
  43. return &light.TrieRequest{Id: light.StateTrieID(rawdb.ReadHeader(db, bhash, *number)), Key: testBankSecureTrieKey}
  44. }
  45. return nil
  46. }
  47. func TestCodeAccessLes2(t *testing.T) { testAccess(t, 2, tfCodeAccess) }
  48. func tfCodeAccess(db ethdb.Database, bhash common.Hash, num uint64) light.OdrRequest {
  49. number := rawdb.ReadHeaderNumber(db, bhash)
  50. if number != nil {
  51. return nil
  52. }
  53. header := rawdb.ReadHeader(db, bhash, *number)
  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. func testAccess(t *testing.T, protocol int, fn accessTestFn) {
  62. // Assemble the test environment
  63. server, client, tearDown := newClientServerEnv(t, 4, protocol, nil, true)
  64. defer tearDown()
  65. client.pm.synchronise(client.rPeer)
  66. test := func(expFail uint64) {
  67. for i := uint64(0); i <= server.pm.blockchain.CurrentHeader().Number.Uint64(); i++ {
  68. bhash := rawdb.ReadCanonicalHash(server.db, i)
  69. if req := fn(client.db, bhash, i); req != nil {
  70. ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
  71. defer cancel()
  72. err := client.pm.odr.Retrieve(ctx, req)
  73. got := err == nil
  74. exp := i < expFail
  75. if exp && !got {
  76. t.Errorf("object retrieval failed")
  77. }
  78. if !exp && got {
  79. t.Errorf("unexpected object retrieval success")
  80. }
  81. }
  82. }
  83. }
  84. // temporarily remove peer to test odr fails
  85. client.peers.Unregister(client.rPeer.id)
  86. time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed
  87. // expect retrievals to fail (except genesis block) without a les peer
  88. test(0)
  89. client.peers.Register(client.rPeer)
  90. time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed
  91. client.rPeer.lock.Lock()
  92. client.rPeer.hasBlock = func(common.Hash, uint64, bool) bool { return true }
  93. client.rPeer.lock.Unlock()
  94. // expect all retrievals to pass
  95. test(5)
  96. }