odr.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. )
  25. // LesOdr implements light.OdrBackend
  26. type LesOdr struct {
  27. db ethdb.Database
  28. indexerConfig *light.IndexerConfig
  29. chtIndexer, bloomTrieIndexer, bloomIndexer *core.ChainIndexer
  30. retriever *retrieveManager
  31. stop chan struct{}
  32. }
  33. func NewLesOdr(db ethdb.Database, config *light.IndexerConfig, retriever *retrieveManager) *LesOdr {
  34. return &LesOdr{
  35. db: db,
  36. indexerConfig: config,
  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. // SetIndexers adds the necessary chain indexers to the ODR backend
  50. func (odr *LesOdr) SetIndexers(chtIndexer, bloomTrieIndexer, bloomIndexer *core.ChainIndexer) {
  51. odr.chtIndexer = chtIndexer
  52. odr.bloomTrieIndexer = bloomTrieIndexer
  53. odr.bloomIndexer = bloomIndexer
  54. }
  55. // ChtIndexer returns the CHT chain indexer
  56. func (odr *LesOdr) ChtIndexer() *core.ChainIndexer {
  57. return odr.chtIndexer
  58. }
  59. // BloomTrieIndexer returns the bloom trie chain indexer
  60. func (odr *LesOdr) BloomTrieIndexer() *core.ChainIndexer {
  61. return odr.bloomTrieIndexer
  62. }
  63. // BloomIndexer returns the bloombits chain indexer
  64. func (odr *LesOdr) BloomIndexer() *core.ChainIndexer {
  65. return odr.bloomIndexer
  66. }
  67. // IndexerConfig returns the indexer config.
  68. func (odr *LesOdr) IndexerConfig() *light.IndexerConfig {
  69. return odr.indexerConfig
  70. }
  71. const (
  72. MsgBlockHeaders = iota
  73. MsgBlockBodies
  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. defer func(sent mclock.AbsTime) {
  110. if err != nil {
  111. return
  112. }
  113. requestRTT.Update(time.Duration(mclock.Now() - sent))
  114. }(mclock.Now())
  115. if err := odr.retriever.retrieve(ctx, reqID, rq, func(p distPeer, msg *Msg) error { return lreq.Validate(odr.db, msg) }, odr.stop); err != nil {
  116. return err
  117. }
  118. req.StoreResult(odr.db)
  119. return nil
  120. }