odr.go 3.7 KB

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