odr.go 3.9 KB

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