request_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/eth"
  25. "github.com/ethereum/go-ethereum/ethdb"
  26. "github.com/ethereum/go-ethereum/light"
  27. )
  28. var testBankSecureTrieKey = secAddr(testBankAddress)
  29. func secAddr(addr common.Address) []byte {
  30. return crypto.Keccak256(addr[:])
  31. }
  32. type accessTestFn func(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest
  33. func TestBlockAccessLes1(t *testing.T) { testAccess(t, 1, tfBlockAccess) }
  34. func TestBlockAccessLes2(t *testing.T) { testAccess(t, 2, tfBlockAccess) }
  35. func tfBlockAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
  36. return &light.BlockRequest{Hash: bhash, Number: number}
  37. }
  38. func TestReceiptsAccessLes1(t *testing.T) { testAccess(t, 1, tfReceiptsAccess) }
  39. func TestReceiptsAccessLes2(t *testing.T) { testAccess(t, 2, tfReceiptsAccess) }
  40. func tfReceiptsAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
  41. return &light.ReceiptsRequest{Hash: bhash, Number: number}
  42. }
  43. func TestTrieEntryAccessLes1(t *testing.T) { testAccess(t, 1, tfTrieEntryAccess) }
  44. func TestTrieEntryAccessLes2(t *testing.T) { testAccess(t, 2, tfTrieEntryAccess) }
  45. func tfTrieEntryAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
  46. if number := rawdb.ReadHeaderNumber(db, bhash); number != nil {
  47. return &light.TrieRequest{Id: light.StateTrieID(rawdb.ReadHeader(db, bhash, *number)), Key: testBankSecureTrieKey}
  48. }
  49. return nil
  50. }
  51. func TestCodeAccessLes1(t *testing.T) { testAccess(t, 1, tfCodeAccess) }
  52. func TestCodeAccessLes2(t *testing.T) { testAccess(t, 2, tfCodeAccess) }
  53. func tfCodeAccess(db ethdb.Database, bhash common.Hash, num uint64) light.OdrRequest {
  54. number := rawdb.ReadHeaderNumber(db, bhash)
  55. if number != nil {
  56. return nil
  57. }
  58. header := rawdb.ReadHeader(db, bhash, *number)
  59. if header.Number.Uint64() < testContractDeployed {
  60. return nil
  61. }
  62. sti := light.StateTrieID(header)
  63. ci := light.StorageTrieID(sti, crypto.Keccak256Hash(testContractAddr[:]), common.Hash{})
  64. return &light.CodeRequest{Id: ci, Hash: crypto.Keccak256Hash(testContractCodeDeployed)}
  65. }
  66. func testAccess(t *testing.T, protocol int, fn accessTestFn) {
  67. // Assemble the test environment
  68. peers := newPeerSet()
  69. dist := newRequestDistributor(peers, make(chan struct{}))
  70. rm := newRetrieveManager(peers, dist, nil)
  71. db := ethdb.NewMemDatabase()
  72. ldb := ethdb.NewMemDatabase()
  73. odr := NewLesOdr(ldb, light.NewChtIndexer(db, true), light.NewBloomTrieIndexer(db, true), eth.NewBloomIndexer(db, light.BloomTrieFrequency), rm)
  74. pm := newTestProtocolManagerMust(t, false, 4, testChainGen, nil, nil, db)
  75. lpm := newTestProtocolManagerMust(t, true, 0, nil, peers, odr, ldb)
  76. _, err1, lpeer, err2 := newTestPeerPair("peer", protocol, pm, lpm)
  77. select {
  78. case <-time.After(time.Millisecond * 100):
  79. case err := <-err1:
  80. t.Fatalf("peer 1 handshake error: %v", err)
  81. case err := <-err2:
  82. t.Fatalf("peer 1 handshake error: %v", err)
  83. }
  84. lpm.synchronise(lpeer)
  85. test := func(expFail uint64) {
  86. for i := uint64(0); i <= pm.blockchain.CurrentHeader().Number.Uint64(); i++ {
  87. bhash := rawdb.ReadCanonicalHash(db, i)
  88. if req := fn(ldb, bhash, i); req != nil {
  89. ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
  90. defer cancel()
  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. // temporarily remove peer to test odr fails
  104. peers.Unregister(lpeer.id)
  105. time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed
  106. // expect retrievals to fail (except genesis block) without a les peer
  107. test(0)
  108. peers.Register(lpeer)
  109. time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed
  110. lpeer.lock.Lock()
  111. lpeer.hasBlock = func(common.Hash, uint64) bool { return true }
  112. lpeer.lock.Unlock()
  113. // expect all retrievals to pass
  114. test(5)
  115. }