odr.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. "sort"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common/mclock"
  22. "github.com/ethereum/go-ethereum/core"
  23. "github.com/ethereum/go-ethereum/ethdb"
  24. "github.com/ethereum/go-ethereum/light"
  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. peers *serverPeerSet
  32. retriever *retrieveManager
  33. stop chan struct{}
  34. }
  35. func NewLesOdr(db ethdb.Database, config *light.IndexerConfig, peers *serverPeerSet, retriever *retrieveManager) *LesOdr {
  36. return &LesOdr{
  37. db: db,
  38. indexerConfig: config,
  39. peers: peers,
  40. retriever: retriever,
  41. stop: make(chan struct{}),
  42. }
  43. }
  44. // Stop cancels all pending retrievals
  45. func (odr *LesOdr) Stop() {
  46. close(odr.stop)
  47. }
  48. // Database returns the backing database
  49. func (odr *LesOdr) Database() ethdb.Database {
  50. return odr.db
  51. }
  52. // SetIndexers adds the necessary chain indexers to the ODR backend
  53. func (odr *LesOdr) SetIndexers(chtIndexer, bloomTrieIndexer, bloomIndexer *core.ChainIndexer) {
  54. odr.chtIndexer = chtIndexer
  55. odr.bloomTrieIndexer = bloomTrieIndexer
  56. odr.bloomIndexer = bloomIndexer
  57. }
  58. // ChtIndexer returns the CHT chain indexer
  59. func (odr *LesOdr) ChtIndexer() *core.ChainIndexer {
  60. return odr.chtIndexer
  61. }
  62. // BloomTrieIndexer returns the bloom trie chain indexer
  63. func (odr *LesOdr) BloomTrieIndexer() *core.ChainIndexer {
  64. return odr.bloomTrieIndexer
  65. }
  66. // BloomIndexer returns the bloombits chain indexer
  67. func (odr *LesOdr) BloomIndexer() *core.ChainIndexer {
  68. return odr.bloomIndexer
  69. }
  70. // IndexerConfig returns the indexer config.
  71. func (odr *LesOdr) IndexerConfig() *light.IndexerConfig {
  72. return odr.indexerConfig
  73. }
  74. const (
  75. MsgBlockHeaders = iota
  76. MsgBlockBodies
  77. MsgCode
  78. MsgReceipts
  79. MsgProofsV2
  80. MsgHelperTrieProofs
  81. MsgTxStatus
  82. )
  83. // Msg encodes a LES message that delivers reply data for a request
  84. type Msg struct {
  85. MsgType int
  86. ReqID uint64
  87. Obj interface{}
  88. }
  89. // peerByTxHistory is a heap.Interface implementation which can sort
  90. // the peerset by transaction history.
  91. type peerByTxHistory []*serverPeer
  92. func (h peerByTxHistory) Len() int { return len(h) }
  93. func (h peerByTxHistory) Less(i, j int) bool {
  94. if h[i].txHistory == txIndexUnlimited {
  95. return false
  96. }
  97. if h[j].txHistory == txIndexUnlimited {
  98. return true
  99. }
  100. return h[i].txHistory < h[j].txHistory
  101. }
  102. func (h peerByTxHistory) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
  103. const (
  104. maxTxStatusRetry = 3 // The maximum retrys will be made for tx status request.
  105. maxTxStatusCandidates = 5 // The maximum les servers the tx status requests will be sent to.
  106. )
  107. // RetrieveTxStatus retrieves the transaction status from the LES network.
  108. // There is no guarantee in the LES protocol that the mined transaction will
  109. // be retrieved back for sure because of different reasons(the transaction
  110. // is unindexed, the malicous server doesn't reply it deliberately, etc).
  111. // Therefore, unretrieved transactions(UNKNOWN) will receive a certain number
  112. // of retries, thus giving a weak guarantee.
  113. func (odr *LesOdr) RetrieveTxStatus(ctx context.Context, req *light.TxStatusRequest) error {
  114. // Sort according to the transaction history supported by the peer and
  115. // select the peers with longest history.
  116. var (
  117. retries int
  118. peers []*serverPeer
  119. missing = len(req.Hashes)
  120. result = make([]light.TxStatus, len(req.Hashes))
  121. canSend = make(map[string]bool)
  122. )
  123. for _, peer := range odr.peers.allPeers() {
  124. if peer.txHistory == txIndexDisabled {
  125. continue
  126. }
  127. peers = append(peers, peer)
  128. }
  129. sort.Sort(sort.Reverse(peerByTxHistory(peers)))
  130. for i := 0; i < maxTxStatusCandidates && i < len(peers); i++ {
  131. canSend[peers[i].id] = true
  132. }
  133. // Send out the request and assemble the result.
  134. for {
  135. if retries >= maxTxStatusRetry || len(canSend) == 0 {
  136. break
  137. }
  138. var (
  139. // Deep copy the request, so that the partial result won't be mixed.
  140. req = &TxStatusRequest{Hashes: req.Hashes}
  141. id = genReqID()
  142. distreq = &distReq{
  143. getCost: func(dp distPeer) uint64 { return req.GetCost(dp.(*serverPeer)) },
  144. canSend: func(dp distPeer) bool { return canSend[dp.(*serverPeer).id] },
  145. request: func(dp distPeer) func() {
  146. p := dp.(*serverPeer)
  147. p.fcServer.QueuedRequest(id, req.GetCost(p))
  148. delete(canSend, p.id)
  149. return func() { req.Request(id, p) }
  150. },
  151. }
  152. )
  153. if err := odr.retriever.retrieve(ctx, id, distreq, func(p distPeer, msg *Msg) error { return req.Validate(odr.db, msg) }, odr.stop); err != nil {
  154. return err
  155. }
  156. // Collect the response and assemble them to the final result.
  157. // All the response is not verifiable, so always pick the first
  158. // one we get.
  159. for index, status := range req.Status {
  160. if result[index].Status != core.TxStatusUnknown {
  161. continue
  162. }
  163. if status.Status == core.TxStatusUnknown {
  164. continue
  165. }
  166. result[index], missing = status, missing-1
  167. }
  168. // Abort the procedure if all the status are retrieved
  169. if missing == 0 {
  170. break
  171. }
  172. retries += 1
  173. }
  174. req.Status = result
  175. return nil
  176. }
  177. // Retrieve tries to fetch an object from the LES network. It's a common API
  178. // for most of the LES requests except for the TxStatusRequest which needs
  179. // the additional retry mechanism.
  180. // If the network retrieval was successful, it stores the object in local db.
  181. func (odr *LesOdr) Retrieve(ctx context.Context, req light.OdrRequest) (err error) {
  182. lreq := LesRequest(req)
  183. reqID := genReqID()
  184. rq := &distReq{
  185. getCost: func(dp distPeer) uint64 {
  186. return lreq.GetCost(dp.(*serverPeer))
  187. },
  188. canSend: func(dp distPeer) bool {
  189. p := dp.(*serverPeer)
  190. if !p.onlyAnnounce {
  191. return lreq.CanSend(p)
  192. }
  193. return false
  194. },
  195. request: func(dp distPeer) func() {
  196. p := dp.(*serverPeer)
  197. cost := lreq.GetCost(p)
  198. p.fcServer.QueuedRequest(reqID, cost)
  199. return func() { lreq.Request(reqID, p) }
  200. },
  201. }
  202. defer func(sent mclock.AbsTime) {
  203. if err != nil {
  204. return
  205. }
  206. requestRTT.Update(time.Duration(mclock.Now() - sent))
  207. }(mclock.Now())
  208. if err := odr.retriever.retrieve(ctx, reqID, rq, func(p distPeer, msg *Msg) error { return lreq.Validate(odr.db, msg) }, odr.stop); err != nil {
  209. return err
  210. }
  211. req.StoreResult(odr.db)
  212. return nil
  213. }