odr.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "github.com/ethereum/go-ethereum/core"
  20. "github.com/ethereum/go-ethereum/ethdb"
  21. "github.com/ethereum/go-ethereum/light"
  22. "github.com/ethereum/go-ethereum/log"
  23. )
  24. // LesOdr implements light.OdrBackend
  25. type LesOdr struct {
  26. db ethdb.Database
  27. chtIndexer, bloomTrieIndexer, bloomIndexer *core.ChainIndexer
  28. retriever *retrieveManager
  29. stop chan struct{}
  30. }
  31. func NewLesOdr(db ethdb.Database, chtIndexer, bloomTrieIndexer, bloomIndexer *core.ChainIndexer, retriever *retrieveManager) *LesOdr {
  32. return &LesOdr{
  33. db: db,
  34. chtIndexer: chtIndexer,
  35. bloomTrieIndexer: bloomTrieIndexer,
  36. bloomIndexer: bloomIndexer,
  37. retriever: retriever,
  38. stop: make(chan struct{}),
  39. }
  40. }
  41. // Stop cancels all pending retrievals
  42. func (odr *LesOdr) Stop() {
  43. close(odr.stop)
  44. }
  45. // Database returns the backing database
  46. func (odr *LesOdr) Database() ethdb.Database {
  47. return odr.db
  48. }
  49. // ChtIndexer returns the CHT chain indexer
  50. func (odr *LesOdr) ChtIndexer() *core.ChainIndexer {
  51. return odr.chtIndexer
  52. }
  53. // BloomTrieIndexer returns the bloom trie chain indexer
  54. func (odr *LesOdr) BloomTrieIndexer() *core.ChainIndexer {
  55. return odr.bloomTrieIndexer
  56. }
  57. // BloomIndexer returns the bloombits chain indexer
  58. func (odr *LesOdr) BloomIndexer() *core.ChainIndexer {
  59. return odr.bloomIndexer
  60. }
  61. const (
  62. MsgBlockBodies = iota
  63. MsgCode
  64. MsgReceipts
  65. MsgProofsV1
  66. MsgProofsV2
  67. MsgHeaderProofs
  68. MsgHelperTrieProofs
  69. )
  70. // Msg encodes a LES message that delivers reply data for a request
  71. type Msg struct {
  72. MsgType int
  73. ReqID uint64
  74. Obj interface{}
  75. }
  76. // Retrieve tries to fetch an object from the LES network.
  77. // If the network retrieval was successful, it stores the object in local db.
  78. func (odr *LesOdr) Retrieve(ctx context.Context, req light.OdrRequest) (err error) {
  79. lreq := LesRequest(req)
  80. reqID := genReqID()
  81. rq := &distReq{
  82. getCost: func(dp distPeer) uint64 {
  83. return lreq.GetCost(dp.(*peer))
  84. },
  85. canSend: func(dp distPeer) bool {
  86. p := dp.(*peer)
  87. return lreq.CanSend(p)
  88. },
  89. request: func(dp distPeer) func() {
  90. p := dp.(*peer)
  91. cost := lreq.GetCost(p)
  92. p.fcServer.QueueRequest(reqID, cost)
  93. return func() { lreq.Request(reqID, p) }
  94. },
  95. }
  96. if err = odr.retriever.retrieve(ctx, reqID, rq, func(p distPeer, msg *Msg) error { return lreq.Validate(odr.db, msg) }, odr.stop); err == nil {
  97. // retrieved from network, store in db
  98. req.StoreResult(odr.db)
  99. } else {
  100. log.Debug("Failed to retrieve data from network", "err", err)
  101. }
  102. return
  103. }