request_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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(bankAddr)
  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 TestBlockAccessLes3(t *testing.T) { testAccess(t, 3, tfBlockAccess) }
  34. func tfBlockAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
  35. return &light.BlockRequest{Hash: bhash, Number: number}
  36. }
  37. func TestReceiptsAccessLes2(t *testing.T) { testAccess(t, 2, tfReceiptsAccess) }
  38. func TestReceiptsAccessLes3(t *testing.T) { testAccess(t, 3, tfReceiptsAccess) }
  39. func tfReceiptsAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
  40. return &light.ReceiptsRequest{Hash: bhash, Number: number}
  41. }
  42. func TestTrieEntryAccessLes2(t *testing.T) { testAccess(t, 2, tfTrieEntryAccess) }
  43. func TestTrieEntryAccessLes3(t *testing.T) { testAccess(t, 3, tfTrieEntryAccess) }
  44. func tfTrieEntryAccess(db ethdb.Database, bhash common.Hash, number uint64) light.OdrRequest {
  45. if number := rawdb.ReadHeaderNumber(db, bhash); number != nil {
  46. return &light.TrieRequest{Id: light.StateTrieID(rawdb.ReadHeader(db, bhash, *number)), Key: testBankSecureTrieKey}
  47. }
  48. return nil
  49. }
  50. func TestCodeAccessLes2(t *testing.T) { testAccess(t, 2, tfCodeAccess) }
  51. func TestCodeAccessLes3(t *testing.T) { testAccess(t, 3, tfCodeAccess) }
  52. func tfCodeAccess(db ethdb.Database, bhash common.Hash, num uint64) light.OdrRequest {
  53. number := rawdb.ReadHeaderNumber(db, bhash)
  54. if number != nil {
  55. return nil
  56. }
  57. header := rawdb.ReadHeader(db, bhash, *number)
  58. if header.Number.Uint64() < testContractDeployed {
  59. return nil
  60. }
  61. sti := light.StateTrieID(header)
  62. ci := light.StorageTrieID(sti, crypto.Keccak256Hash(testContractAddr[:]), common.Hash{})
  63. return &light.CodeRequest{Id: ci, Hash: crypto.Keccak256Hash(testContractCodeDeployed)}
  64. }
  65. func testAccess(t *testing.T, protocol int, fn accessTestFn) {
  66. // Assemble the test environment
  67. server, client, tearDown := newClientServerEnv(t, 4, protocol, nil, nil, 0, false, true)
  68. defer tearDown()
  69. client.handler.synchronise(client.peer.speer)
  70. // Ensure the client has synced all necessary data.
  71. clientHead := client.handler.backend.blockchain.CurrentHeader()
  72. if clientHead.Number.Uint64() != 4 {
  73. t.Fatalf("Failed to sync the chain with server, head: %v", clientHead.Number.Uint64())
  74. }
  75. test := func(expFail uint64) {
  76. for i := uint64(0); i <= server.handler.blockchain.CurrentHeader().Number.Uint64(); i++ {
  77. bhash := rawdb.ReadCanonicalHash(server.db, i)
  78. if req := fn(client.db, bhash, i); req != nil {
  79. ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
  80. err := client.handler.backend.odr.Retrieve(ctx, req)
  81. cancel()
  82. got := err == nil
  83. exp := i < expFail
  84. if exp && !got {
  85. t.Errorf("object retrieval failed")
  86. }
  87. if !exp && got {
  88. t.Errorf("unexpected object retrieval success")
  89. }
  90. }
  91. }
  92. }
  93. test(5)
  94. }