request_test.go 4.4 KB

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