ticket.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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 discv5
  17. import (
  18. "bytes"
  19. "encoding/binary"
  20. "fmt"
  21. "math"
  22. "math/rand"
  23. "time"
  24. "github.com/ethereum/go-ethereum/common"
  25. "github.com/ethereum/go-ethereum/common/mclock"
  26. "github.com/ethereum/go-ethereum/crypto"
  27. "github.com/ethereum/go-ethereum/log"
  28. )
  29. const (
  30. ticketTimeBucketLen = time.Minute
  31. collectFrequency = time.Second * 30
  32. registerFrequency = time.Second * 60
  33. maxCollectDebt = 10
  34. maxRegisterDebt = 5
  35. keepTicketConst = time.Minute * 10
  36. keepTicketExp = time.Minute * 5
  37. targetWaitTime = time.Minute * 10
  38. topicQueryTimeout = time.Second * 5
  39. topicQueryResend = time.Minute
  40. // topic radius detection
  41. maxRadius = 0xffffffffffffffff
  42. radiusTC = time.Minute * 20
  43. radiusBucketsPerBit = 8
  44. minSlope = 1
  45. minPeakSize = 40
  46. maxNoAdjust = 20
  47. lookupWidth = 8
  48. minRightSum = 20
  49. searchForceQuery = 4
  50. )
  51. // timeBucket represents absolute monotonic time in minutes.
  52. // It is used as the index into the per-topic ticket buckets.
  53. type timeBucket int
  54. type ticket struct {
  55. topics []Topic
  56. regTime []mclock.AbsTime // Per-topic local absolute time when the ticket can be used.
  57. // The serial number that was issued by the server.
  58. serial uint32
  59. // Used by registrar, tracks absolute time when the ticket was created.
  60. issueTime mclock.AbsTime
  61. // Fields used only by registrants
  62. node *Node // the registrar node that signed this ticket
  63. refCnt int // tracks number of topics that will be registered using this ticket
  64. pong []byte // encoded pong packet signed by the registrar
  65. }
  66. // ticketRef refers to a single topic in a ticket.
  67. type ticketRef struct {
  68. t *ticket
  69. idx int // index of the topic in t.topics and t.regTime
  70. }
  71. func (ref ticketRef) topic() Topic {
  72. return ref.t.topics[ref.idx]
  73. }
  74. func (ref ticketRef) topicRegTime() mclock.AbsTime {
  75. return ref.t.regTime[ref.idx]
  76. }
  77. func pongToTicket(localTime mclock.AbsTime, topics []Topic, node *Node, p *ingressPacket) (*ticket, error) {
  78. wps := p.data.(*pong).WaitPeriods
  79. if len(topics) != len(wps) {
  80. return nil, fmt.Errorf("bad wait period list: got %d values, want %d", len(topics), len(wps))
  81. }
  82. if rlpHash(topics) != p.data.(*pong).TopicHash {
  83. return nil, fmt.Errorf("bad topic hash")
  84. }
  85. t := &ticket{
  86. issueTime: localTime,
  87. node: node,
  88. topics: topics,
  89. pong: p.rawData,
  90. regTime: make([]mclock.AbsTime, len(wps)),
  91. }
  92. // Convert wait periods to local absolute time.
  93. for i, wp := range wps {
  94. t.regTime[i] = localTime + mclock.AbsTime(time.Second*time.Duration(wp))
  95. }
  96. return t, nil
  97. }
  98. func ticketToPong(t *ticket, pong *pong) {
  99. pong.Expiration = uint64(t.issueTime / mclock.AbsTime(time.Second))
  100. pong.TopicHash = rlpHash(t.topics)
  101. pong.TicketSerial = t.serial
  102. pong.WaitPeriods = make([]uint32, len(t.regTime))
  103. for i, regTime := range t.regTime {
  104. pong.WaitPeriods[i] = uint32(time.Duration(regTime-t.issueTime) / time.Second)
  105. }
  106. }
  107. type ticketStore struct {
  108. // radius detector and target address generator
  109. // exists for both searched and registered topics
  110. radius map[Topic]*topicRadius
  111. // Contains buckets (for each absolute minute) of tickets
  112. // that can be used in that minute.
  113. // This is only set if the topic is being registered.
  114. tickets map[Topic]*topicTickets
  115. regQueue []Topic // Topic registration queue for round robin attempts
  116. regSet map[Topic]struct{} // Topic registration queue contents for fast filling
  117. nodes map[*Node]*ticket
  118. nodeLastReq map[*Node]reqInfo
  119. lastBucketFetched timeBucket
  120. nextTicketCached *ticketRef
  121. searchTopicMap map[Topic]searchTopic
  122. nextTopicQueryCleanup mclock.AbsTime
  123. queriesSent map[*Node]map[common.Hash]sentQuery
  124. }
  125. type searchTopic struct {
  126. foundChn chan<- *Node
  127. }
  128. type sentQuery struct {
  129. sent mclock.AbsTime
  130. lookup lookupInfo
  131. }
  132. type topicTickets struct {
  133. buckets map[timeBucket][]ticketRef
  134. nextLookup mclock.AbsTime
  135. nextReg mclock.AbsTime
  136. }
  137. func newTicketStore() *ticketStore {
  138. return &ticketStore{
  139. radius: make(map[Topic]*topicRadius),
  140. tickets: make(map[Topic]*topicTickets),
  141. regSet: make(map[Topic]struct{}),
  142. nodes: make(map[*Node]*ticket),
  143. nodeLastReq: make(map[*Node]reqInfo),
  144. searchTopicMap: make(map[Topic]searchTopic),
  145. queriesSent: make(map[*Node]map[common.Hash]sentQuery),
  146. }
  147. }
  148. // addTopic starts tracking a topic. If register is true,
  149. // the local node will register the topic and tickets will be collected.
  150. func (s *ticketStore) addTopic(topic Topic, register bool) {
  151. log.Trace("Adding discovery topic", "topic", topic, "register", register)
  152. if s.radius[topic] == nil {
  153. s.radius[topic] = newTopicRadius(topic)
  154. }
  155. if register && s.tickets[topic] == nil {
  156. s.tickets[topic] = &topicTickets{buckets: make(map[timeBucket][]ticketRef)}
  157. }
  158. }
  159. func (s *ticketStore) addSearchTopic(t Topic, foundChn chan<- *Node) {
  160. s.addTopic(t, false)
  161. if s.searchTopicMap[t].foundChn == nil {
  162. s.searchTopicMap[t] = searchTopic{foundChn: foundChn}
  163. }
  164. }
  165. func (s *ticketStore) removeSearchTopic(t Topic) {
  166. if st := s.searchTopicMap[t]; st.foundChn != nil {
  167. delete(s.searchTopicMap, t)
  168. }
  169. }
  170. // removeRegisterTopic deletes all tickets for the given topic.
  171. func (s *ticketStore) removeRegisterTopic(topic Topic) {
  172. log.Trace("Removing discovery topic", "topic", topic)
  173. if s.tickets[topic] == nil {
  174. log.Warn("Removing non-existent discovery topic", "topic", topic)
  175. return
  176. }
  177. for _, list := range s.tickets[topic].buckets {
  178. for _, ref := range list {
  179. ref.t.refCnt--
  180. if ref.t.refCnt == 0 {
  181. delete(s.nodes, ref.t.node)
  182. delete(s.nodeLastReq, ref.t.node)
  183. }
  184. }
  185. }
  186. delete(s.tickets, topic)
  187. }
  188. func (s *ticketStore) regTopicSet() []Topic {
  189. topics := make([]Topic, 0, len(s.tickets))
  190. for topic := range s.tickets {
  191. topics = append(topics, topic)
  192. }
  193. return topics
  194. }
  195. // nextRegisterLookup returns the target of the next lookup for ticket collection.
  196. func (s *ticketStore) nextRegisterLookup() (lookupInfo, time.Duration) {
  197. // Queue up any new topics (or discarded ones), preserving iteration order
  198. for topic := range s.tickets {
  199. if _, ok := s.regSet[topic]; !ok {
  200. s.regQueue = append(s.regQueue, topic)
  201. s.regSet[topic] = struct{}{}
  202. }
  203. }
  204. // Iterate over the set of all topics and look up the next suitable one
  205. for len(s.regQueue) > 0 {
  206. // Fetch the next topic from the queue, and ensure it still exists
  207. topic := s.regQueue[0]
  208. s.regQueue = s.regQueue[1:]
  209. delete(s.regSet, topic)
  210. if s.tickets[topic] == nil {
  211. continue
  212. }
  213. // If the topic needs more tickets, return it
  214. if s.tickets[topic].nextLookup < mclock.Now() {
  215. next, delay := s.radius[topic].nextTarget(false), 100*time.Millisecond
  216. log.Trace("Found discovery topic to register", "topic", topic, "target", next.target, "delay", delay)
  217. return next, delay
  218. }
  219. }
  220. // No registration topics found or all exhausted, sleep
  221. delay := 40 * time.Second
  222. log.Trace("No topic found to register", "delay", delay)
  223. return lookupInfo{}, delay
  224. }
  225. func (s *ticketStore) nextSearchLookup(topic Topic) lookupInfo {
  226. tr := s.radius[topic]
  227. target := tr.nextTarget(tr.radiusLookupCnt >= searchForceQuery)
  228. if target.radiusLookup {
  229. tr.radiusLookupCnt++
  230. } else {
  231. tr.radiusLookupCnt = 0
  232. }
  233. return target
  234. }
  235. func (s *ticketStore) addTicketRef(r ticketRef) {
  236. topic := r.t.topics[r.idx]
  237. tickets := s.tickets[topic]
  238. if tickets == nil {
  239. log.Warn("Adding ticket to non-existent topic", "topic", topic)
  240. return
  241. }
  242. bucket := timeBucket(r.t.regTime[r.idx] / mclock.AbsTime(ticketTimeBucketLen))
  243. tickets.buckets[bucket] = append(tickets.buckets[bucket], r)
  244. r.t.refCnt++
  245. min := mclock.Now() - mclock.AbsTime(collectFrequency)*maxCollectDebt
  246. if tickets.nextLookup < min {
  247. tickets.nextLookup = min
  248. }
  249. tickets.nextLookup += mclock.AbsTime(collectFrequency)
  250. //s.removeExcessTickets(topic)
  251. }
  252. func (s *ticketStore) nextFilteredTicket() (*ticketRef, time.Duration) {
  253. now := mclock.Now()
  254. for {
  255. ticket, wait := s.nextRegisterableTicket()
  256. if ticket == nil {
  257. return ticket, wait
  258. }
  259. log.Trace("Found discovery ticket to register", "node", ticket.t.node, "serial", ticket.t.serial, "wait", wait)
  260. regTime := now + mclock.AbsTime(wait)
  261. topic := ticket.t.topics[ticket.idx]
  262. if s.tickets[topic] != nil && regTime >= s.tickets[topic].nextReg {
  263. return ticket, wait
  264. }
  265. s.removeTicketRef(*ticket)
  266. }
  267. }
  268. func (s *ticketStore) ticketRegistered(ref ticketRef) {
  269. now := mclock.Now()
  270. topic := ref.t.topics[ref.idx]
  271. tickets := s.tickets[topic]
  272. min := now - mclock.AbsTime(registerFrequency)*maxRegisterDebt
  273. if min > tickets.nextReg {
  274. tickets.nextReg = min
  275. }
  276. tickets.nextReg += mclock.AbsTime(registerFrequency)
  277. s.tickets[topic] = tickets
  278. s.removeTicketRef(ref)
  279. }
  280. // nextRegisterableTicket returns the next ticket that can be used
  281. // to register.
  282. //
  283. // If the returned wait time <= zero the ticket can be used. For a positive
  284. // wait time, the caller should requery the next ticket later.
  285. //
  286. // A ticket can be returned more than once with <= zero wait time in case
  287. // the ticket contains multiple topics.
  288. func (s *ticketStore) nextRegisterableTicket() (*ticketRef, time.Duration) {
  289. now := mclock.Now()
  290. if s.nextTicketCached != nil {
  291. return s.nextTicketCached, time.Duration(s.nextTicketCached.topicRegTime() - now)
  292. }
  293. for bucket := s.lastBucketFetched; ; bucket++ {
  294. var (
  295. empty = true // true if there are no tickets
  296. nextTicket ticketRef // uninitialized if this bucket is empty
  297. )
  298. for _, tickets := range s.tickets {
  299. //s.removeExcessTickets(topic)
  300. if len(tickets.buckets) != 0 {
  301. empty = false
  302. list := tickets.buckets[bucket]
  303. for _, ref := range list {
  304. //debugLog(fmt.Sprintf(" nrt bucket = %d node = %x sn = %v wait = %v", bucket, ref.t.node.ID[:8], ref.t.serial, time.Duration(ref.topicRegTime()-now)))
  305. if nextTicket.t == nil || ref.topicRegTime() < nextTicket.topicRegTime() {
  306. nextTicket = ref
  307. }
  308. }
  309. }
  310. }
  311. if empty {
  312. return nil, 0
  313. }
  314. if nextTicket.t != nil {
  315. s.nextTicketCached = &nextTicket
  316. return &nextTicket, time.Duration(nextTicket.topicRegTime() - now)
  317. }
  318. s.lastBucketFetched = bucket
  319. }
  320. }
  321. // removeTicket removes a ticket from the ticket store
  322. func (s *ticketStore) removeTicketRef(ref ticketRef) {
  323. log.Trace("Removing discovery ticket reference", "node", ref.t.node.ID, "serial", ref.t.serial)
  324. // Make nextRegisterableTicket return the next available ticket.
  325. s.nextTicketCached = nil
  326. topic := ref.topic()
  327. tickets := s.tickets[topic]
  328. if tickets == nil {
  329. log.Trace("Removing tickets from unknown topic", "topic", topic)
  330. return
  331. }
  332. bucket := timeBucket(ref.t.regTime[ref.idx] / mclock.AbsTime(ticketTimeBucketLen))
  333. list := tickets.buckets[bucket]
  334. idx := -1
  335. for i, bt := range list {
  336. if bt.t == ref.t {
  337. idx = i
  338. break
  339. }
  340. }
  341. if idx == -1 {
  342. panic(nil)
  343. }
  344. list = append(list[:idx], list[idx+1:]...)
  345. if len(list) != 0 {
  346. tickets.buckets[bucket] = list
  347. } else {
  348. delete(tickets.buckets, bucket)
  349. }
  350. ref.t.refCnt--
  351. if ref.t.refCnt == 0 {
  352. delete(s.nodes, ref.t.node)
  353. delete(s.nodeLastReq, ref.t.node)
  354. }
  355. }
  356. type lookupInfo struct {
  357. target common.Hash
  358. topic Topic
  359. radiusLookup bool
  360. }
  361. type reqInfo struct {
  362. pingHash []byte
  363. lookup lookupInfo
  364. time mclock.AbsTime
  365. }
  366. // returns -1 if not found
  367. func (t *ticket) findIdx(topic Topic) int {
  368. for i, tt := range t.topics {
  369. if tt == topic {
  370. return i
  371. }
  372. }
  373. return -1
  374. }
  375. func (s *ticketStore) registerLookupDone(lookup lookupInfo, nodes []*Node, ping func(n *Node) []byte) {
  376. now := mclock.Now()
  377. for i, n := range nodes {
  378. if i == 0 || (binary.BigEndian.Uint64(n.sha[:8])^binary.BigEndian.Uint64(lookup.target[:8])) < s.radius[lookup.topic].minRadius {
  379. if lookup.radiusLookup {
  380. if lastReq, ok := s.nodeLastReq[n]; !ok || time.Duration(now-lastReq.time) > radiusTC {
  381. s.nodeLastReq[n] = reqInfo{pingHash: ping(n), lookup: lookup, time: now}
  382. }
  383. } else {
  384. if s.nodes[n] == nil {
  385. s.nodeLastReq[n] = reqInfo{pingHash: ping(n), lookup: lookup, time: now}
  386. }
  387. }
  388. }
  389. }
  390. }
  391. func (s *ticketStore) searchLookupDone(lookup lookupInfo, nodes []*Node, query func(n *Node, topic Topic) []byte) {
  392. now := mclock.Now()
  393. for i, n := range nodes {
  394. if i == 0 || (binary.BigEndian.Uint64(n.sha[:8])^binary.BigEndian.Uint64(lookup.target[:8])) < s.radius[lookup.topic].minRadius {
  395. if lookup.radiusLookup {
  396. if lastReq, ok := s.nodeLastReq[n]; !ok || time.Duration(now-lastReq.time) > radiusTC {
  397. s.nodeLastReq[n] = reqInfo{pingHash: nil, lookup: lookup, time: now}
  398. }
  399. } // else {
  400. if s.canQueryTopic(n, lookup.topic) {
  401. hash := query(n, lookup.topic)
  402. if hash != nil {
  403. s.addTopicQuery(common.BytesToHash(hash), n, lookup)
  404. }
  405. }
  406. //}
  407. }
  408. }
  409. }
  410. func (s *ticketStore) adjustWithTicket(now mclock.AbsTime, targetHash common.Hash, t *ticket) {
  411. for i, topic := range t.topics {
  412. if tt, ok := s.radius[topic]; ok {
  413. tt.adjustWithTicket(now, targetHash, ticketRef{t, i})
  414. }
  415. }
  416. }
  417. func (s *ticketStore) addTicket(localTime mclock.AbsTime, pingHash []byte, ticket *ticket) {
  418. log.Trace("Adding discovery ticket", "node", ticket.node.ID, "serial", ticket.serial)
  419. lastReq, ok := s.nodeLastReq[ticket.node]
  420. if !(ok && bytes.Equal(pingHash, lastReq.pingHash)) {
  421. return
  422. }
  423. s.adjustWithTicket(localTime, lastReq.lookup.target, ticket)
  424. if lastReq.lookup.radiusLookup || s.nodes[ticket.node] != nil {
  425. return
  426. }
  427. topic := lastReq.lookup.topic
  428. topicIdx := ticket.findIdx(topic)
  429. if topicIdx == -1 {
  430. return
  431. }
  432. bucket := timeBucket(localTime / mclock.AbsTime(ticketTimeBucketLen))
  433. if s.lastBucketFetched == 0 || bucket < s.lastBucketFetched {
  434. s.lastBucketFetched = bucket
  435. }
  436. if _, ok := s.tickets[topic]; ok {
  437. wait := ticket.regTime[topicIdx] - localTime
  438. rnd := rand.ExpFloat64()
  439. if rnd > 10 {
  440. rnd = 10
  441. }
  442. if float64(wait) < float64(keepTicketConst)+float64(keepTicketExp)*rnd {
  443. // use the ticket to register this topic
  444. //fmt.Println("addTicket", ticket.node.ID[:8], ticket.node.addr().String(), ticket.serial, ticket.pong)
  445. s.addTicketRef(ticketRef{ticket, topicIdx})
  446. }
  447. }
  448. if ticket.refCnt > 0 {
  449. s.nextTicketCached = nil
  450. s.nodes[ticket.node] = ticket
  451. }
  452. }
  453. func (s *ticketStore) canQueryTopic(node *Node, topic Topic) bool {
  454. qq := s.queriesSent[node]
  455. if qq != nil {
  456. now := mclock.Now()
  457. for _, sq := range qq {
  458. if sq.lookup.topic == topic && sq.sent > now-mclock.AbsTime(topicQueryResend) {
  459. return false
  460. }
  461. }
  462. }
  463. return true
  464. }
  465. func (s *ticketStore) addTopicQuery(hash common.Hash, node *Node, lookup lookupInfo) {
  466. now := mclock.Now()
  467. qq := s.queriesSent[node]
  468. if qq == nil {
  469. qq = make(map[common.Hash]sentQuery)
  470. s.queriesSent[node] = qq
  471. }
  472. qq[hash] = sentQuery{sent: now, lookup: lookup}
  473. s.cleanupTopicQueries(now)
  474. }
  475. func (s *ticketStore) cleanupTopicQueries(now mclock.AbsTime) {
  476. if s.nextTopicQueryCleanup > now {
  477. return
  478. }
  479. exp := now - mclock.AbsTime(topicQueryResend)
  480. for n, qq := range s.queriesSent {
  481. for h, q := range qq {
  482. if q.sent < exp {
  483. delete(qq, h)
  484. }
  485. }
  486. if len(qq) == 0 {
  487. delete(s.queriesSent, n)
  488. }
  489. }
  490. s.nextTopicQueryCleanup = now + mclock.AbsTime(topicQueryTimeout)
  491. }
  492. func (s *ticketStore) gotTopicNodes(from *Node, hash common.Hash, nodes []rpcNode) (timeout bool) {
  493. now := mclock.Now()
  494. //fmt.Println("got", from.addr().String(), hash, len(nodes))
  495. qq := s.queriesSent[from]
  496. if qq == nil {
  497. return true
  498. }
  499. q, ok := qq[hash]
  500. if !ok || now > q.sent+mclock.AbsTime(topicQueryTimeout) {
  501. return true
  502. }
  503. inside := float64(0)
  504. if len(nodes) > 0 {
  505. inside = 1
  506. }
  507. s.radius[q.lookup.topic].adjust(now, q.lookup.target, from.sha, inside)
  508. chn := s.searchTopicMap[q.lookup.topic].foundChn
  509. if chn == nil {
  510. //fmt.Println("no channel")
  511. return false
  512. }
  513. for _, node := range nodes {
  514. ip := node.IP
  515. if ip.IsUnspecified() || ip.IsLoopback() {
  516. ip = from.IP
  517. }
  518. n := NewNode(node.ID, ip, node.UDP, node.TCP)
  519. select {
  520. case chn <- n:
  521. default:
  522. return false
  523. }
  524. }
  525. return false
  526. }
  527. type topicRadius struct {
  528. topic Topic
  529. topicHashPrefix uint64
  530. radius, minRadius uint64
  531. buckets []topicRadiusBucket
  532. converged bool
  533. radiusLookupCnt int
  534. }
  535. type topicRadiusEvent int
  536. const (
  537. trOutside topicRadiusEvent = iota
  538. trInside
  539. trNoAdjust
  540. trCount
  541. )
  542. type topicRadiusBucket struct {
  543. weights [trCount]float64
  544. lastTime mclock.AbsTime
  545. value float64
  546. lookupSent map[common.Hash]mclock.AbsTime
  547. }
  548. func (b *topicRadiusBucket) update(now mclock.AbsTime) {
  549. if now == b.lastTime {
  550. return
  551. }
  552. exp := math.Exp(-float64(now-b.lastTime) / float64(radiusTC))
  553. for i, w := range b.weights {
  554. b.weights[i] = w * exp
  555. }
  556. b.lastTime = now
  557. for target, tm := range b.lookupSent {
  558. if now-tm > mclock.AbsTime(respTimeout) {
  559. b.weights[trNoAdjust] += 1
  560. delete(b.lookupSent, target)
  561. }
  562. }
  563. }
  564. func (b *topicRadiusBucket) adjust(now mclock.AbsTime, inside float64) {
  565. b.update(now)
  566. if inside <= 0 {
  567. b.weights[trOutside] += 1
  568. } else {
  569. if inside >= 1 {
  570. b.weights[trInside] += 1
  571. } else {
  572. b.weights[trInside] += inside
  573. b.weights[trOutside] += 1 - inside
  574. }
  575. }
  576. }
  577. func newTopicRadius(t Topic) *topicRadius {
  578. topicHash := crypto.Keccak256Hash([]byte(t))
  579. topicHashPrefix := binary.BigEndian.Uint64(topicHash[0:8])
  580. return &topicRadius{
  581. topic: t,
  582. topicHashPrefix: topicHashPrefix,
  583. radius: maxRadius,
  584. minRadius: maxRadius,
  585. }
  586. }
  587. func (r *topicRadius) getBucketIdx(addrHash common.Hash) int {
  588. prefix := binary.BigEndian.Uint64(addrHash[0:8])
  589. var log2 float64
  590. if prefix != r.topicHashPrefix {
  591. log2 = math.Log2(float64(prefix ^ r.topicHashPrefix))
  592. }
  593. bucket := int((64 - log2) * radiusBucketsPerBit)
  594. max := 64*radiusBucketsPerBit - 1
  595. if bucket > max {
  596. return max
  597. }
  598. if bucket < 0 {
  599. return 0
  600. }
  601. return bucket
  602. }
  603. func (r *topicRadius) targetForBucket(bucket int) common.Hash {
  604. min := math.Pow(2, 64-float64(bucket+1)/radiusBucketsPerBit)
  605. max := math.Pow(2, 64-float64(bucket)/radiusBucketsPerBit)
  606. a := uint64(min)
  607. b := randUint64n(uint64(max - min))
  608. xor := a + b
  609. if xor < a {
  610. xor = ^uint64(0)
  611. }
  612. prefix := r.topicHashPrefix ^ xor
  613. var target common.Hash
  614. binary.BigEndian.PutUint64(target[0:8], prefix)
  615. globalRandRead(target[8:])
  616. return target
  617. }
  618. // package rand provides a Read function in Go 1.6 and later, but
  619. // we can't use it yet because we still support Go 1.5.
  620. func globalRandRead(b []byte) {
  621. pos := 0
  622. val := 0
  623. for n := 0; n < len(b); n++ {
  624. if pos == 0 {
  625. val = rand.Int()
  626. pos = 7
  627. }
  628. b[n] = byte(val)
  629. val >>= 8
  630. pos--
  631. }
  632. }
  633. func (r *topicRadius) chooseLookupBucket(a, b int) int {
  634. if a < 0 {
  635. a = 0
  636. }
  637. if a > b {
  638. return -1
  639. }
  640. c := 0
  641. for i := a; i <= b; i++ {
  642. if i >= len(r.buckets) || r.buckets[i].weights[trNoAdjust] < maxNoAdjust {
  643. c++
  644. }
  645. }
  646. if c == 0 {
  647. return -1
  648. }
  649. rnd := randUint(uint32(c))
  650. for i := a; i <= b; i++ {
  651. if i >= len(r.buckets) || r.buckets[i].weights[trNoAdjust] < maxNoAdjust {
  652. if rnd == 0 {
  653. return i
  654. }
  655. rnd--
  656. }
  657. }
  658. panic(nil) // should never happen
  659. }
  660. func (r *topicRadius) needMoreLookups(a, b int, maxValue float64) bool {
  661. var max float64
  662. if a < 0 {
  663. a = 0
  664. }
  665. if b >= len(r.buckets) {
  666. b = len(r.buckets) - 1
  667. if r.buckets[b].value > max {
  668. max = r.buckets[b].value
  669. }
  670. }
  671. if b >= a {
  672. for i := a; i <= b; i++ {
  673. if r.buckets[i].value > max {
  674. max = r.buckets[i].value
  675. }
  676. }
  677. }
  678. return maxValue-max < minPeakSize
  679. }
  680. func (r *topicRadius) recalcRadius() (radius uint64, radiusLookup int) {
  681. maxBucket := 0
  682. maxValue := float64(0)
  683. now := mclock.Now()
  684. v := float64(0)
  685. for i := range r.buckets {
  686. r.buckets[i].update(now)
  687. v += r.buckets[i].weights[trOutside] - r.buckets[i].weights[trInside]
  688. r.buckets[i].value = v
  689. //fmt.Printf("%v %v | ", v, r.buckets[i].weights[trNoAdjust])
  690. }
  691. //fmt.Println()
  692. slopeCross := -1
  693. for i, b := range r.buckets {
  694. v := b.value
  695. if v < float64(i)*minSlope {
  696. slopeCross = i
  697. break
  698. }
  699. if v > maxValue {
  700. maxValue = v
  701. maxBucket = i + 1
  702. }
  703. }
  704. minRadBucket := len(r.buckets)
  705. sum := float64(0)
  706. for minRadBucket > 0 && sum < minRightSum {
  707. minRadBucket--
  708. b := r.buckets[minRadBucket]
  709. sum += b.weights[trInside] + b.weights[trOutside]
  710. }
  711. r.minRadius = uint64(math.Pow(2, 64-float64(minRadBucket)/radiusBucketsPerBit))
  712. lookupLeft := -1
  713. if r.needMoreLookups(0, maxBucket-lookupWidth-1, maxValue) {
  714. lookupLeft = r.chooseLookupBucket(maxBucket-lookupWidth, maxBucket-1)
  715. }
  716. lookupRight := -1
  717. if slopeCross != maxBucket && (minRadBucket <= maxBucket || r.needMoreLookups(maxBucket+lookupWidth, len(r.buckets)-1, maxValue)) {
  718. for len(r.buckets) <= maxBucket+lookupWidth {
  719. r.buckets = append(r.buckets, topicRadiusBucket{lookupSent: make(map[common.Hash]mclock.AbsTime)})
  720. }
  721. lookupRight = r.chooseLookupBucket(maxBucket, maxBucket+lookupWidth-1)
  722. }
  723. if lookupLeft == -1 {
  724. radiusLookup = lookupRight
  725. } else {
  726. if lookupRight == -1 {
  727. radiusLookup = lookupLeft
  728. } else {
  729. if randUint(2) == 0 {
  730. radiusLookup = lookupLeft
  731. } else {
  732. radiusLookup = lookupRight
  733. }
  734. }
  735. }
  736. //fmt.Println("mb", maxBucket, "sc", slopeCross, "mrb", minRadBucket, "ll", lookupLeft, "lr", lookupRight, "mv", maxValue)
  737. if radiusLookup == -1 {
  738. // no more radius lookups needed at the moment, return a radius
  739. r.converged = true
  740. rad := maxBucket
  741. if minRadBucket < rad {
  742. rad = minRadBucket
  743. }
  744. radius = ^uint64(0)
  745. if rad > 0 {
  746. radius = uint64(math.Pow(2, 64-float64(rad)/radiusBucketsPerBit))
  747. }
  748. r.radius = radius
  749. }
  750. return
  751. }
  752. func (r *topicRadius) nextTarget(forceRegular bool) lookupInfo {
  753. if !forceRegular {
  754. _, radiusLookup := r.recalcRadius()
  755. if radiusLookup != -1 {
  756. target := r.targetForBucket(radiusLookup)
  757. r.buckets[radiusLookup].lookupSent[target] = mclock.Now()
  758. return lookupInfo{target: target, topic: r.topic, radiusLookup: true}
  759. }
  760. }
  761. radExt := r.radius / 2
  762. if radExt > maxRadius-r.radius {
  763. radExt = maxRadius - r.radius
  764. }
  765. rnd := randUint64n(r.radius) + randUint64n(2*radExt)
  766. if rnd > radExt {
  767. rnd -= radExt
  768. } else {
  769. rnd = radExt - rnd
  770. }
  771. prefix := r.topicHashPrefix ^ rnd
  772. var target common.Hash
  773. binary.BigEndian.PutUint64(target[0:8], prefix)
  774. globalRandRead(target[8:])
  775. return lookupInfo{target: target, topic: r.topic, radiusLookup: false}
  776. }
  777. func (r *topicRadius) adjustWithTicket(now mclock.AbsTime, targetHash common.Hash, t ticketRef) {
  778. wait := t.t.regTime[t.idx] - t.t.issueTime
  779. inside := float64(wait)/float64(targetWaitTime) - 0.5
  780. if inside > 1 {
  781. inside = 1
  782. }
  783. if inside < 0 {
  784. inside = 0
  785. }
  786. r.adjust(now, targetHash, t.t.node.sha, inside)
  787. }
  788. func (r *topicRadius) adjust(now mclock.AbsTime, targetHash, addrHash common.Hash, inside float64) {
  789. bucket := r.getBucketIdx(addrHash)
  790. //fmt.Println("adjust", bucket, len(r.buckets), inside)
  791. if bucket >= len(r.buckets) {
  792. return
  793. }
  794. r.buckets[bucket].adjust(now, inside)
  795. delete(r.buckets[bucket].lookupSent, targetHash)
  796. }