sync.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // Copyright 2019 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 dnsdisc
  17. import (
  18. "context"
  19. "math/rand"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common/mclock"
  22. "github.com/ethereum/go-ethereum/p2p/enode"
  23. )
  24. // This is the number of consecutive leaf requests that may fail before
  25. // we consider re-resolving the tree root.
  26. const rootRecheckFailCount = 5
  27. // clientTree is a full tree being synced.
  28. type clientTree struct {
  29. c *Client
  30. loc *linkEntry // link to this tree
  31. lastRootCheck mclock.AbsTime // last revalidation of root
  32. leafFailCount int
  33. rootFailCount int
  34. root *rootEntry
  35. enrs *subtreeSync
  36. links *subtreeSync
  37. lc *linkCache // tracks all links between all trees
  38. curLinks map[string]struct{} // links contained in this tree
  39. linkGCRoot string // root on which last link GC has run
  40. }
  41. func newClientTree(c *Client, lc *linkCache, loc *linkEntry) *clientTree {
  42. return &clientTree{c: c, lc: lc, loc: loc}
  43. }
  44. // syncAll retrieves all entries of the tree.
  45. func (ct *clientTree) syncAll(dest map[string]entry) error {
  46. if err := ct.updateRoot(context.Background()); err != nil {
  47. return err
  48. }
  49. if err := ct.links.resolveAll(dest); err != nil {
  50. return err
  51. }
  52. if err := ct.enrs.resolveAll(dest); err != nil {
  53. return err
  54. }
  55. return nil
  56. }
  57. // syncRandom retrieves a single entry of the tree. The Node return value
  58. // is non-nil if the entry was a node.
  59. func (ct *clientTree) syncRandom(ctx context.Context) (n *enode.Node, err error) {
  60. if ct.rootUpdateDue() {
  61. if err := ct.updateRoot(ctx); err != nil {
  62. return nil, err
  63. }
  64. }
  65. // Update fail counter for leaf request errors.
  66. defer func() {
  67. if err != nil {
  68. ct.leafFailCount++
  69. }
  70. }()
  71. // Link tree sync has priority, run it to completion before syncing ENRs.
  72. if !ct.links.done() {
  73. err := ct.syncNextLink(ctx)
  74. return nil, err
  75. }
  76. ct.gcLinks()
  77. // Sync next random entry in ENR tree. Once every node has been visited, we simply
  78. // start over. This is fine because entries are cached internally by the client LRU
  79. // also by DNS resolvers.
  80. if ct.enrs.done() {
  81. ct.enrs = newSubtreeSync(ct.c, ct.loc, ct.root.eroot, false)
  82. }
  83. return ct.syncNextRandomENR(ctx)
  84. }
  85. // canSyncRandom checks if any meaningful action can be performed by syncRandom.
  86. func (ct *clientTree) canSyncRandom() bool {
  87. // Note: the check for non-zero leaf count is very important here.
  88. // If we're done syncing all nodes, and no leaves were found, the tree
  89. // is empty and we can't use it for sync.
  90. return ct.rootUpdateDue() || !ct.links.done() || !ct.enrs.done() || ct.enrs.leaves != 0
  91. }
  92. // gcLinks removes outdated links from the global link cache. GC runs once
  93. // when the link sync finishes.
  94. func (ct *clientTree) gcLinks() {
  95. if !ct.links.done() || ct.root.lroot == ct.linkGCRoot {
  96. return
  97. }
  98. ct.lc.resetLinks(ct.loc.str, ct.curLinks)
  99. ct.linkGCRoot = ct.root.lroot
  100. }
  101. func (ct *clientTree) syncNextLink(ctx context.Context) error {
  102. hash := ct.links.missing[0]
  103. e, err := ct.links.resolveNext(ctx, hash)
  104. if err != nil {
  105. return err
  106. }
  107. ct.links.missing = ct.links.missing[1:]
  108. if dest, ok := e.(*linkEntry); ok {
  109. ct.lc.addLink(ct.loc.str, dest.str)
  110. ct.curLinks[dest.str] = struct{}{}
  111. }
  112. return nil
  113. }
  114. func (ct *clientTree) syncNextRandomENR(ctx context.Context) (*enode.Node, error) {
  115. index := rand.Intn(len(ct.enrs.missing))
  116. hash := ct.enrs.missing[index]
  117. e, err := ct.enrs.resolveNext(ctx, hash)
  118. if err != nil {
  119. return nil, err
  120. }
  121. ct.enrs.missing = removeHash(ct.enrs.missing, index)
  122. if ee, ok := e.(*enrEntry); ok {
  123. return ee.node, nil
  124. }
  125. return nil, nil
  126. }
  127. func (ct *clientTree) String() string {
  128. return ct.loc.String()
  129. }
  130. // removeHash removes the element at index from h.
  131. func removeHash(h []string, index int) []string {
  132. if len(h) == 1 {
  133. return nil
  134. }
  135. last := len(h) - 1
  136. if index < last {
  137. h[index] = h[last]
  138. h[last] = ""
  139. }
  140. return h[:last]
  141. }
  142. // updateRoot ensures that the given tree has an up-to-date root.
  143. func (ct *clientTree) updateRoot(ctx context.Context) error {
  144. if !ct.slowdownRootUpdate(ctx) {
  145. return ctx.Err()
  146. }
  147. ct.lastRootCheck = ct.c.clock.Now()
  148. ctx, cancel := context.WithTimeout(ctx, ct.c.cfg.Timeout)
  149. defer cancel()
  150. root, err := ct.c.resolveRoot(ctx, ct.loc)
  151. if err != nil {
  152. ct.rootFailCount++
  153. return err
  154. }
  155. ct.root = &root
  156. ct.rootFailCount = 0
  157. ct.leafFailCount = 0
  158. // Invalidate subtrees if changed.
  159. if ct.links == nil || root.lroot != ct.links.root {
  160. ct.links = newSubtreeSync(ct.c, ct.loc, root.lroot, true)
  161. ct.curLinks = make(map[string]struct{})
  162. }
  163. if ct.enrs == nil || root.eroot != ct.enrs.root {
  164. ct.enrs = newSubtreeSync(ct.c, ct.loc, root.eroot, false)
  165. }
  166. return nil
  167. }
  168. // rootUpdateDue returns true when a root update is needed.
  169. func (ct *clientTree) rootUpdateDue() bool {
  170. tooManyFailures := ct.leafFailCount > rootRecheckFailCount
  171. scheduledCheck := ct.c.clock.Now() >= ct.nextScheduledRootCheck()
  172. return ct.root == nil || tooManyFailures || scheduledCheck
  173. }
  174. func (ct *clientTree) nextScheduledRootCheck() mclock.AbsTime {
  175. return ct.lastRootCheck.Add(ct.c.cfg.RecheckInterval)
  176. }
  177. // slowdownRootUpdate applies a delay to root resolution if is tried
  178. // too frequently. This avoids busy polling when the client is offline.
  179. // Returns true if the timeout passed, false if sync was canceled.
  180. func (ct *clientTree) slowdownRootUpdate(ctx context.Context) bool {
  181. var delay time.Duration
  182. switch {
  183. case ct.rootFailCount > 20:
  184. delay = 10 * time.Second
  185. case ct.rootFailCount > 5:
  186. delay = 5 * time.Second
  187. default:
  188. return true
  189. }
  190. timeout := ct.c.clock.NewTimer(delay)
  191. defer timeout.Stop()
  192. select {
  193. case <-timeout.C():
  194. return true
  195. case <-ctx.Done():
  196. return false
  197. }
  198. }
  199. // subtreeSync is the sync of an ENR or link subtree.
  200. type subtreeSync struct {
  201. c *Client
  202. loc *linkEntry
  203. root string
  204. missing []string // missing tree node hashes
  205. link bool // true if this sync is for the link tree
  206. leaves int // counter of synced leaves
  207. }
  208. func newSubtreeSync(c *Client, loc *linkEntry, root string, link bool) *subtreeSync {
  209. return &subtreeSync{c, loc, root, []string{root}, link, 0}
  210. }
  211. func (ts *subtreeSync) done() bool {
  212. return len(ts.missing) == 0
  213. }
  214. func (ts *subtreeSync) resolveAll(dest map[string]entry) error {
  215. for !ts.done() {
  216. hash := ts.missing[0]
  217. ctx, cancel := context.WithTimeout(context.Background(), ts.c.cfg.Timeout)
  218. e, err := ts.resolveNext(ctx, hash)
  219. cancel()
  220. if err != nil {
  221. return err
  222. }
  223. dest[hash] = e
  224. ts.missing = ts.missing[1:]
  225. }
  226. return nil
  227. }
  228. func (ts *subtreeSync) resolveNext(ctx context.Context, hash string) (entry, error) {
  229. e, err := ts.c.resolveEntry(ctx, ts.loc.domain, hash)
  230. if err != nil {
  231. return nil, err
  232. }
  233. switch e := e.(type) {
  234. case *enrEntry:
  235. if ts.link {
  236. return nil, errENRInLinkTree
  237. }
  238. ts.leaves++
  239. case *linkEntry:
  240. if !ts.link {
  241. return nil, errLinkInENRTree
  242. }
  243. ts.leaves++
  244. case *branchEntry:
  245. ts.missing = append(ts.missing, e.children...)
  246. }
  247. return e, nil
  248. }
  249. // linkCache tracks links between trees.
  250. type linkCache struct {
  251. backrefs map[string]map[string]struct{}
  252. changed bool
  253. }
  254. func (lc *linkCache) isReferenced(r string) bool {
  255. return len(lc.backrefs[r]) != 0
  256. }
  257. func (lc *linkCache) addLink(from, to string) {
  258. if _, ok := lc.backrefs[to][from]; ok {
  259. return
  260. }
  261. if lc.backrefs == nil {
  262. lc.backrefs = make(map[string]map[string]struct{})
  263. }
  264. if _, ok := lc.backrefs[to]; !ok {
  265. lc.backrefs[to] = make(map[string]struct{})
  266. }
  267. lc.backrefs[to][from] = struct{}{}
  268. lc.changed = true
  269. }
  270. // resetLinks clears all links of the given tree.
  271. func (lc *linkCache) resetLinks(from string, keep map[string]struct{}) {
  272. stk := []string{from}
  273. for len(stk) > 0 {
  274. item := stk[len(stk)-1]
  275. stk = stk[:len(stk)-1]
  276. for r, refs := range lc.backrefs {
  277. if _, ok := keep[r]; ok {
  278. continue
  279. }
  280. if _, ok := refs[item]; !ok {
  281. continue
  282. }
  283. lc.changed = true
  284. delete(refs, item)
  285. if len(refs) == 0 {
  286. delete(lc.backrefs, r)
  287. stk = append(stk, r)
  288. }
  289. }
  290. }
  291. }