odr.go 7.1 KB

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