aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/discv5/ticket.go
blob: 752fdc9b41623f26c99f5790c4d4c7496029a2a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package discv5

import (
    "bytes"
    "encoding/binary"
    "fmt"
    "math"
    "math/rand"
    "sort"
    "time"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/common/mclock"
    "github.com/ethereum/go-ethereum/crypto"
)

const (
    ticketTimeBucketLen = time.Minute
    timeWindow          = 10 // * ticketTimeBucketLen
    wantTicketsInWindow = 10
    collectFrequency    = time.Second * 30
    registerFrequency   = time.Second * 60
    maxCollectDebt      = 10
    maxRegisterDebt     = 5
    keepTicketConst     = time.Minute * 10
    keepTicketExp       = time.Minute * 5
    targetWaitTime      = time.Minute * 10
    topicQueryTimeout   = time.Second * 5
    topicQueryResend    = time.Minute
    // topic radius detection
    maxRadius           = 0xffffffffffffffff
    radiusTC            = time.Minute * 20
    radiusBucketsPerBit = 8
    minSlope            = 1
    minPeakSize         = 40
    maxNoAdjust         = 20
    lookupWidth         = 8
    minRightSum         = 20
    searchForceQuery    = 4
)

// timeBucket represents absolute monotonic time in minutes.
// It is used as the index into the per-topic ticket buckets.
type timeBucket int

type ticket struct {
    topics  []Topic
    regTime []mclock.AbsTime // Per-topic local absolute time when the ticket can be used.

    // The serial number that was issued by the server.
    serial uint32
    // Used by registrar, tracks absolute time when the ticket was created.
    issueTime mclock.AbsTime

    // Fields used only by registrants
    node   *Node  // the registrar node that signed this ticket
    refCnt int    // tracks number of topics that will be registered using this ticket
    pong   []byte // encoded pong packet signed by the registrar
}

// ticketRef refers to a single topic in a ticket.
type ticketRef struct {
    t   *ticket
    idx int // index of the topic in t.topics and t.regTime
}

func (ref ticketRef) topic() Topic {
    return ref.t.topics[ref.idx]
}

func (ref ticketRef) topicRegTime() mclock.AbsTime {
    return ref.t.regTime[ref.idx]
}

func pongToTicket(localTime mclock.AbsTime, topics []Topic, node *Node, p *ingressPacket) (*ticket, error) {
    wps := p.data.(*pong).WaitPeriods
    if len(topics) != len(wps) {
        return nil, fmt.Errorf("bad wait period list: got %d values, want %d", len(topics), len(wps))
    }
    if rlpHash(topics) != p.data.(*pong).TopicHash {
        return nil, fmt.Errorf("bad topic hash")
    }
    t := &ticket{
        issueTime: localTime,
        node:      node,
        topics:    topics,
        pong:      p.rawData,
        regTime:   make([]mclock.AbsTime, len(wps)),
    }
    // Convert wait periods to local absolute time.
    for i, wp := range wps {
        t.regTime[i] = localTime + mclock.AbsTime(time.Second*time.Duration(wp))
    }
    return t, nil
}

func ticketToPong(t *ticket, pong *pong) {
    pong.Expiration = uint64(t.issueTime / mclock.AbsTime(time.Second))
    pong.TopicHash = rlpHash(t.topics)
    pong.TicketSerial = t.serial
    pong.WaitPeriods = make([]uint32, len(t.regTime))
    for i, regTime := range t.regTime {
        pong.WaitPeriods[i] = uint32(time.Duration(regTime-t.issueTime) / time.Second)
    }
}

type ticketStore struct {
    // radius detector and target address generator
    // exists for both searched and registered topics
    radius map[Topic]*topicRadius

    // Contains buckets (for each absolute minute) of tickets
    // that can be used in that minute.
    // This is only set if the topic is being registered.
    tickets     map[Topic]topicTickets
    regtopics   []Topic
    nodes       map[*Node]*ticket
    nodeLastReq map[*Node]reqInfo

    lastBucketFetched timeBucket
    nextTicketCached  *ticketRef
    nextTicketReg     mclock.AbsTime

    searchTopicMap        map[Topic]searchTopic
    nextTopicQueryCleanup mclock.AbsTime
    queriesSent           map[*Node]map[common.Hash]sentQuery
}

type searchTopic struct {
    foundChn chan<- *Node
}

type sentQuery struct {
    sent   mclock.AbsTime
    lookup lookupInfo
}

type topicTickets struct {
    buckets             map[timeBucket][]ticketRef
    nextLookup, nextReg mclock.AbsTime
}

func newTicketStore() *ticketStore {
    return &ticketStore{
        radius:         make(map[Topic]*topicRadius),
        tickets:        make(map[Topic]topicTickets),
        nodes:          make(map[*Node]*ticket),
        nodeLastReq:    make(map[*Node]reqInfo),
        searchTopicMap: make(map[Topic]searchTopic),
        queriesSent:    make(map[*Node]map[common.Hash]sentQuery),
    }
}

// addTopic starts tracking a topic. If register is true,
// the local node will register the topic and tickets will be collected.
func (s *ticketStore) addTopic(t Topic, register bool) {
    debugLog(fmt.Sprintf(" addTopic(%v, %v)", t, register))
    if s.radius[t] == nil {
        s.radius[t] = newTopicRadius(t)
    }
    if register && s.tickets[t].buckets == nil {
        s.tickets[t] = topicTickets{buckets: make(map[timeBucket][]ticketRef)}
    }
}

func (s *ticketStore) addSearchTopic(t Topic, foundChn chan<- *Node) {
    s.addTopic(t, false)
    if s.searchTopicMap[t].foundChn == nil {
        s.searchTopicMap[t] = searchTopic{foundChn: foundChn}
    }
}

func (s *ticketStore) removeSearchTopic(t Topic) {
    if st := s.searchTopicMap[t]; st.foundChn != nil {
        delete(s.searchTopicMap, t)
    }
}

// removeRegisterTopic deletes all tickets for the given topic.
func (s *ticketStore) removeRegisterTopic(topic Topic) {
    debugLog(fmt.Sprintf(" removeRegisterTopic(%v)", topic))
    for _, list := range s.tickets[topic].buckets {
        for _, ref := range list {
            ref.t.refCnt--
            if ref.t.refCnt == 0 {
                delete(s.nodes, ref.t.node)
                delete(s.nodeLastReq, ref.t.node)
            }
        }
    }
    delete(s.tickets, topic)
}

func (s *ticketStore) regTopicSet() []Topic {
    topics := make([]Topic, 0, len(s.tickets))
    for topic := range s.tickets {
        topics = append(topics, topic)
    }
    return topics
}

// nextRegisterLookup returns the target of the next lookup for ticket collection.
func (s *ticketStore) nextRegisterLookup() (lookup lookupInfo, delay time.Duration) {
    debugLog("nextRegisterLookup()")
    firstTopic, ok := s.iterRegTopics()
    for topic := firstTopic; ok; {
        debugLog(fmt.Sprintf(" checking topic %v, len(s.tickets[topic]) = %d", topic, len(s.tickets[topic].buckets)))
        if s.tickets[topic].buckets != nil && s.needMoreTickets(topic) {
            next := s.radius[topic].nextTarget(false)
            debugLog(fmt.Sprintf(" %x 1s", next.target[:8]))
            return next, 100 * time.Millisecond
        }
        topic, ok = s.iterRegTopics()
        if topic == firstTopic {
            break // We have checked all topics.
        }
    }
    debugLog(" null, 40s")
    return lookupInfo{}, 40 * time.Second
}

func (s *ticketStore) nextSearchLookup(topic Topic) lookupInfo {
    tr := s.radius[topic]
    target := tr.nextTarget(tr.radiusLookupCnt >= searchForceQuery)
    if target.radiusLookup {
        tr.radiusLookupCnt++
    } else {
        tr.radiusLookupCnt = 0
    }
    return target
}

// iterRegTopics returns topics to register in arbitrary order.
// The second return value is false if there are no topics.
func (s *ticketStore) iterRegTopics() (Topic, bool) {
    debugLog("iterRegTopics()")
    if len(s.regtopics) == 0 {
        if len(s.tickets) == 0 {
            debugLog(" false")
            return "", false
        }
        // Refill register list.
        for t := range s.tickets {
            s.regtopics = append(s.regtopics, t)
        }
    }
    topic := s.regtopics[len(s.regtopics)-1]
    s.regtopics = s.regtopics[:len(s.regtopics)-1]
    debugLog(" " + string(topic) + " true")
    return topic, true
}

func (s *ticketStore) needMoreTickets(t Topic) bool {
    return s.tickets[t].nextLookup < mclock.Now()
}

// ticketsInWindow returns the tickets of a given topic in the registration window.
func (s *ticketStore) ticketsInWindow(t Topic) []ticketRef {
    ltBucket := s.lastBucketFetched
    var res []ticketRef
    tickets := s.tickets[t].buckets
    for g := ltBucket; g < ltBucket+timeWindow; g++ {
        res = append(res, tickets[g]...)
    }
    debugLog(fmt.Sprintf("ticketsInWindow(%v) = %v", t, len(res)))
    return res
}

func (s *ticketStore) removeExcessTickets(t Topic) {
    tickets := s.ticketsInWindow(t)
    if len(tickets) <= wantTicketsInWindow {
        return
    }
    sort.Sort(ticketRefByWaitTime(tickets))
    for _, r := range tickets[wantTicketsInWindow:] {
        s.removeTicketRef(r)
    }
}

type ticketRefByWaitTime []ticketRef

// Len is the number of elements in the collection.
func (s ticketRefByWaitTime) Len() int {
    return len(s)
}

func (r ticketRef) waitTime() mclock.AbsTime {
    return r.t.regTime[r.idx] - r.t.issueTime
}

// Less reports whether the element with
// index i should sort before the element with index j.
func (s ticketRefByWaitTime) Less(i, j int) bool {
    return s[i].waitTime() < s[j].waitTime()
}

// Swap swaps the elements with indexes i and j.
func (s ticketRefByWaitTime) Swap(i, j int) {
    s[i], s[j] = s[j], s[i]
}

func (s *ticketStore) addTicketRef(r ticketRef) {
    topic := r.t.topics[r.idx]
    t := s.tickets[topic]
    if t.buckets == nil {
        return
    }
    bucket := timeBucket(r.t.regTime[r.idx] / mclock.AbsTime(ticketTimeBucketLen))
    t.buckets[bucket] = append(t.buckets[bucket], r)
    r.t.refCnt++

    min := mclock.Now() - mclock.AbsTime(collectFrequency)*maxCollectDebt
    if t.nextLookup < min {
        t.nextLookup = min
    }
    t.nextLookup += mclock.AbsTime(collectFrequency)
    s.tickets[topic] = t

    //s.removeExcessTickets(topic)
}

func (s *ticketStore) nextFilteredTicket() (t *ticketRef, wait time.Duration) {
    now := mclock.Now()
    for {
        t, wait = s.nextRegisterableTicket()
        if t == nil {
            return
        }
        regTime := now + mclock.AbsTime(wait)
        topic := t.t.topics[t.idx]
        if regTime >= s.tickets[topic].nextReg {
            return
        }
        s.removeTicketRef(*t)
    }
}

func (s *ticketStore) ticketRegistered(t ticketRef) {
    now := mclock.Now()

    topic := t.t.topics[t.idx]
    tt := s.tickets[topic]
    min := now - mclock.AbsTime(registerFrequency)*maxRegisterDebt
    if min > tt.nextReg {
        tt.nextReg = min
    }
    tt.nextReg += mclock.AbsTime(registerFrequency)
    s.tickets[topic] = tt

    s.removeTicketRef(t)
}

// nextRegisterableTicket returns the next ticket that can be used
// to register.
//
// If the returned wait time <= zero the ticket can be used. For a positive
// wait time, the caller should requery the next ticket later.
//
// A ticket can be returned more than once with <= zero wait time in case
// the ticket contains multiple topics.
func (s *ticketStore) nextRegisterableTicket() (t *ticketRef, wait time.Duration) {
    defer func() {
        if t == nil {
            debugLog(" nil")
        } else {
            debugLog(fmt.Sprintf(" node = %x sn = %v wait = %v", t.t.node.ID[:8], t.t.serial, wait))
        }
    }()

    debugLog("nextRegisterableTicket()")
    now := mclock.Now()
    if s.nextTicketCached != nil {
        return s.nextTicketCached, time.Duration(s.nextTicketCached.topicRegTime() - now)
    }

    for bucket := s.lastBucketFetched; ; bucket++ {
        var (
            empty      = true    // true if there are no tickets
            nextTicket ticketRef // uninitialized if this bucket is empty
        )
        for _, tickets := range s.tickets {
            //s.removeExcessTickets(topic)
            if len(tickets.buckets) != 0 {
                empty = false
                if list := tickets.buckets[bucket]; list != nil {
                    for _, ref := range list {
                        //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)))
                        if nextTicket.t == nil || ref.topicRegTime() < nextTicket.topicRegTime() {
                            nextTicket = ref
                        }
                    }
                }
            }
        }
        if empty {
            return nil, 0
        }
        if nextTicket.t != nil {
            wait = time.Duration(nextTicket.topicRegTime() - now)
            s.nextTicketCached = &nextTicket
            return &nextTicket, wait
        }
        s.lastBucketFetched = bucket
    }
}

// removeTicket removes a ticket from the ticket store
func (s *ticketStore) removeTicketRef(ref ticketRef) {
    debugLog(fmt.Sprintf("removeTicketRef(node = %x sn = %v)", ref.t.node.ID[:8], ref.t.serial))
    topic := ref.topic()
    tickets := s.tickets[topic].buckets
    if tickets == nil {
        return
    }
    bucket := timeBucket(ref.t.regTime[ref.idx] / mclock.AbsTime(ticketTimeBucketLen))
    list := tickets[bucket]
    idx := -1
    for i, bt := range list {
        if bt.t == ref.t {
            idx = i
            break
        }
    }
    if idx == -1 {
        panic(nil)
    }
    list = append(list[:idx], list[idx+1:]...)
    if len(list) != 0 {
        tickets[bucket] = list
    } else {
        delete(tickets, bucket)
    }
    ref.t.refCnt--
    if ref.t.refCnt == 0 {
        delete(s.nodes, ref.t.node)
        delete(s.nodeLastReq, ref.t.node)
    }

    // Make nextRegisterableTicket return the next available ticket.
    s.nextTicketCached = nil
}

type lookupInfo struct {
    target       common.Hash
    topic        Topic
    radiusLookup bool
}

type reqInfo struct {
    pingHash []byte
    lookup   lookupInfo
    time     mclock.AbsTime
}

// returns -1 if not found
func (t *ticket) findIdx(topic Topic) int {
    for i, tt := range t.topics {
        if tt == topic {
            return i
        }
    }
    return -1
}

func (s *ticketStore) registerLookupDone(lookup lookupInfo, nodes []*Node, ping func(n *Node) []byte) {
    now := mclock.Now()
    for i, n := range nodes {
        if i == 0 || (binary.BigEndian.Uint64(n.sha[:8])^binary.BigEndian.Uint64(lookup.target[:8])) < s.radius[lookup.topic].minRadius {
            if lookup.radiusLookup {
                if lastReq, ok := s.nodeLastReq[n]; !ok || time.Duration(now-lastReq.time) > radiusTC {
                    s.nodeLastReq[n] = reqInfo{pingHash: ping(n), lookup: lookup, time: now}
                }
            } else {
                if s.nodes[n] == nil {
                    s.nodeLastReq[n] = reqInfo{pingHash: ping(n), lookup: lookup, time: now}
                }
            }
        }
    }
}

func (s *ticketStore) searchLookupDone(lookup lookupInfo, nodes []*Node, ping func(n *Node) []byte, query func(n *Node, topic Topic) []byte) {
    now := mclock.Now()
    for i, n := range nodes {
        if i == 0 || (binary.BigEndian.Uint64(n.sha[:8])^binary.BigEndian.Uint64(lookup.target[:8])) < s.radius[lookup.topic].minRadius {
            if lookup.radiusLookup {
                if lastReq, ok := s.nodeLastReq[n]; !ok || time.Duration(now-lastReq.time) > radiusTC {
                    s.nodeLastReq[n] = reqInfo{pingHash: ping(n), lookup: lookup, time: now}
                }
            } // else {
            if s.canQueryTopic(n, lookup.topic) {
                hash := query(n, lookup.topic)
                if hash != nil {
                    s.addTopicQuery(common.BytesToHash(hash), n, lookup)
                }
            }
            //}
        }
    }
}

func (s *ticketStore) adjustWithTicket(now mclock.AbsTime, targetHash common.Hash, t *ticket) {
    for i, topic := range t.topics {
        if tt, ok := s.radius[topic]; ok {
            tt.adjustWithTicket(now, targetHash, ticketRef{t, i})
        }
    }
}

func (s *ticketStore) addTicket(localTime mclock.AbsTime, pingHash []byte, t *ticket) {
    debugLog(fmt.Sprintf("add(node = %x sn = %v)", t.node.ID[:8], t.serial))

    lastReq, ok := s.nodeLastReq[t.node]
    if !(ok && bytes.Equal(pingHash, lastReq.pingHash)) {
        return
    }
    s.adjustWithTicket(localTime, lastReq.lookup.target, t)

    if lastReq.lookup.radiusLookup || s.nodes[t.node] != nil {
        return
    }

    topic := lastReq.lookup.topic
    topicIdx := t.findIdx(topic)
    if topicIdx == -1 {
        return
    }

    bucket := timeBucket(localTime / mclock.AbsTime(ticketTimeBucketLen))
    if s.lastBucketFetched == 0 || bucket < s.lastBucketFetched {
        s.lastBucketFetched = bucket
    }

    if _, ok := s.tickets[topic]; ok {
        wait := t.regTime[topicIdx] - localTime
        rnd := rand.ExpFloat64()
        if rnd > 10 {
            rnd = 10
        }
        if float64(wait) < float64(keepTicketConst)+float64(keepTicketExp)*rnd {
            // use the ticket to register this topic
            //fmt.Println("addTicket", t.node.ID[:8], t.node.addr().String(), t.serial, t.pong)
            s.addTicketRef(ticketRef{t, topicIdx})
        }
    }

    if t.refCnt > 0 {
        s.nextTicketCached = nil
        s.nodes[t.node] = t
    }
}

func (s *ticketStore) getNodeTicket(node *Node) *ticket {
    if s.nodes[node] == nil {
        debugLog(fmt.Sprintf("getNodeTicket(%x) sn = nil", node.ID[:8]))
    } else {
        debugLog(fmt.Sprintf("getNodeTicket(%x) sn = %v", node.ID[:8], s.nodes[node].serial))
    }
    return s.nodes[node]
}

func (s *ticketStore) canQueryTopic(node *Node, topic Topic) bool {
    qq := s.queriesSent[node]
    if qq != nil {
        now := mclock.Now()
        for _, sq := range qq {
            if sq.lookup.topic == topic && sq.sent > now-mclock.AbsTime(topicQueryResend) {
                return false
            }
        }
    }
    return true
}

func (s *ticketStore) addTopicQuery(hash common.Hash, node *Node, lookup lookupInfo) {
    now := mclock.Now()
    qq := s.queriesSent[node]
    if qq == nil {
        qq = make(map[common.Hash]sentQuery)
        s.queriesSent[node] = qq
    }
    qq[hash] = sentQuery{sent: now, lookup: lookup}
    s.cleanupTopicQueries(now)
}

func (s *ticketStore) cleanupTopicQueries(now mclock.AbsTime) {
    if s.nextTopicQueryCleanup > now {
        return
    }
    exp := now - mclock.AbsTime(topicQueryResend)
    for n, qq := range s.queriesSent {
        for h, q := range qq {
            if q.sent < exp {
                delete(qq, h)
            }
        }
        if len(qq) == 0 {
            delete(s.queriesSent, n)
        }
    }
    s.nextTopicQueryCleanup = now + mclock.AbsTime(topicQueryTimeout)
}

func (s *ticketStore) gotTopicNodes(from *Node, hash common.Hash, nodes []rpcNode) (timeout bool) {
    now := mclock.Now()
    //fmt.Println("got", from.addr().String(), hash, len(nodes))
    qq := s.queriesSent[from]
    if qq == nil {
        return true
    }
    q, ok := qq[hash]
    if !ok || now > q.sent+mclock.AbsTime(topicQueryTimeout) {
        return true
    }
    inside := float64(0)
    if len(nodes) > 0 {
        inside = 1
    }
    s.radius[q.lookup.topic].adjust(now, q.lookup.target, from.sha, inside)
    chn := s.searchTopicMap[q.lookup.topic].foundChn
    if chn == nil {
        //fmt.Println("no channel")
        return false
    }
    for _, node := range nodes {
        ip := node.IP
        if ip.IsUnspecified() || ip.IsLoopback() {
            ip = from.IP
        }
        n := NewNode(node.ID, ip, node.UDP-1, node.TCP-1) // subtract one from port while discv5 is running in test mode on UDPport+1
        select {
        case chn <- n:
        default:
            return false
        }
    }
    return false
}

type topicRadius struct {
    topic             Topic
    topicHashPrefix   uint64
    radius, minRadius uint64
    buckets           []topicRadiusBucket
    converged         bool
    radiusLookupCnt   int
}

type topicRadiusEvent int

const (
    trOutside topicRadiusEvent = iota
    trInside
    trNoAdjust
    trCount
)

type topicRadiusBucket struct {
    weights    [trCount]float64
    lastTime   mclock.AbsTime
    value      float64
    lookupSent map[common.Hash]mclock.AbsTime
}

func (b *topicRadiusBucket) update(now mclock.AbsTime) {
    if now == b.lastTime {
        return
    }
    exp := math.Exp(-float64(now-b.lastTime) / float64(radiusTC))
    for i, w := range b.weights {
        b.weights[i] = w * exp
    }
    b.lastTime = now

    for target, tm := range b.lookupSent {
        if now-tm > mclock.AbsTime(respTimeout) {
            b.weights[trNoAdjust] += 1
            delete(b.lookupSent, target)
        }
    }
}

func (b *topicRadiusBucket) adjust(now mclock.AbsTime, inside float64) {
    b.update(now)
    if inside <= 0 {
        b.weights[trOutside] += 1
    } else {
        if inside >= 1 {
            b.weights[trInside] += 1
        } else {
            b.weights[trInside] += inside
            b.weights[trOutside] += 1 - inside
        }
    }
}

func newTopicRadius(t Topic) *topicRadius {
    topicHash := crypto.Keccak256Hash([]byte(t))
    topicHashPrefix := binary.BigEndian.Uint64(topicHash[0:8])

    return &topicRadius{
        topic:           t,
        topicHashPrefix: topicHashPrefix,
        radius:          maxRadius,
        minRadius:       maxRadius,
    }
}

func (r *topicRadius) getBucketIdx(addrHash common.Hash) int {
    prefix := binary.BigEndian.Uint64(addrHash[0:8])
    var log2 float64
    if prefix != r.topicHashPrefix {
        log2 = math.Log2(float64(prefix ^ r.topicHashPrefix))
    }
    bucket := int((64 - log2) * radiusBucketsPerBit)
    max := 64*radiusBucketsPerBit - 1
    if bucket > max {
        return max
    }
    if bucket < 0 {
        return 0
    }
    return bucket
}

func (r *topicRadius) targetForBucket(bucket int) common.Hash {
    min := math.Pow(2, 64-float64(bucket+1)/radiusBucketsPerBit)
    max := math.Pow(2, 64-float64(bucket)/radiusBucketsPerBit)
    a := uint64(min)
    b := randUint64n(uint64(max - min))
    xor := a + b
    if xor < a {
        xor = ^uint64(0)
    }
    prefix := r.topicHashPrefix ^ xor
    var target common.Hash
    binary.BigEndian.PutUint64(target[0:8], prefix)
    globalRandRead(target[8:])
    return target
}

// package rand provides a Read function in Go 1.6 and later, but
// we can't use it yet because we still support Go 1.5.
func globalRandRead(b []byte) {
    pos := 0
    val := 0
    for n := 0; n < len(b); n++ {
        if pos == 0 {
            val = rand.Int()
            pos = 7
        }
        b[n] = byte(val)
        val >>= 8
        pos--
    }
}

func (r *topicRadius) isInRadius(addrHash common.Hash) bool {
    nodePrefix := binary.BigEndian.Uint64(addrHash[0:8])
    dist := nodePrefix ^ r.topicHashPrefix
    return dist < r.radius
}

func (r *topicRadius) chooseLookupBucket(a, b int) int {
    if a < 0 {
        a = 0
    }
    if a > b {
        return -1
    }
    c := 0
    for i := a; i <= b; i++ {
        if i >= len(r.buckets) || r.buckets[i].weights[trNoAdjust] < maxNoAdjust {
            c++
        }
    }
    if c == 0 {
        return -1
    }
    rnd := randUint(uint32(c))
    for i := a; i <= b; i++ {
        if i >= len(r.buckets) || r.buckets[i].weights[trNoAdjust] < maxNoAdjust {
            if rnd == 0 {
                return i
            }
            rnd--
        }
    }
    panic(nil) // should never happen
}

func (r *topicRadius) needMoreLookups(a, b int, maxValue float64) bool {
    var max float64
    if a < 0 {
        a = 0
    }
    if b >= len(r.buckets) {
        b = len(r.buckets) - 1
        if r.buckets[b].value > max {
            max = r.buckets[b].value
        }
    }
    if b >= a {
        for i := a; i <= b; i++ {
            if r.buckets[i].value > max {
                max = r.buckets[i].value
            }
        }
    }
    return maxValue-max < minPeakSize
}

func (r *topicRadius) recalcRadius() (radius uint64, radiusLookup int) {
    maxBucket := 0
    maxValue := float64(0)
    now := mclock.Now()
    v := float64(0)
    for i, _ := range r.buckets {
        r.buckets[i].update(now)
        v += r.buckets[i].weights[trOutside] - r.buckets[i].weights[trInside]
        r.buckets[i].value = v
        //fmt.Printf("%v %v | ", v, r.buckets[i].weights[trNoAdjust])
    }
    //fmt.Println()
    slopeCross := -1
    for i, b := range r.buckets {
        v := b.value
        if v < float64(i)*minSlope {
            slopeCross = i
            break
        }
        if v > maxValue {
            maxValue = v
            maxBucket = i + 1
        }
    }

    minRadBucket := len(r.buckets)
    sum := float64(0)
    for minRadBucket > 0 && sum < minRightSum {
        minRadBucket--
        b := r.buckets[minRadBucket]
        sum += b.weights[trInside] + b.weights[trOutside]
    }
    r.minRadius = uint64(math.Pow(2, 64-float64(minRadBucket)/radiusBucketsPerBit))

    lookupLeft := -1
    if r.needMoreLookups(0, maxBucket-lookupWidth-1, maxValue) {
        lookupLeft = r.chooseLookupBucket(maxBucket-lookupWidth, maxBucket-1)
    }
    lookupRight := -1
    if slopeCross != maxBucket && (minRadBucket <= maxBucket || r.needMoreLookups(maxBucket+lookupWidth, len(r.buckets)-1, maxValue)) {
        for len(r.buckets) <= maxBucket+lookupWidth {
            r.buckets = append(r.buckets, topicRadiusBucket{lookupSent: make(map[common.Hash]mclock.AbsTime)})
        }
        lookupRight = r.chooseLookupBucket(maxBucket, maxBucket+lookupWidth-1)
    }
    if lookupLeft == -1 {
        radiusLookup = lookupRight
    } else {
        if lookupRight == -1 {
            radiusLookup = lookupLeft
        } else {
            if randUint(2) == 0 {
                radiusLookup = lookupLeft
            } else {
                radiusLookup = lookupRight
            }
        }
    }

    //fmt.Println("mb", maxBucket, "sc", slopeCross, "mrb", minRadBucket, "ll", lookupLeft, "lr", lookupRight, "mv", maxValue)

    if radiusLookup == -1 {
        // no more radius lookups needed at the moment, return a radius
        r.converged = true
        rad := maxBucket
        if minRadBucket < rad {
            rad = minRadBucket
        }
        radius = ^uint64(0)
        if rad > 0 {
            radius = uint64(math.Pow(2, 64-float64(rad)/radiusBucketsPerBit))
        }
        r.radius = radius
    }

    return
}

func (r *topicRadius) nextTarget(forceRegular bool) lookupInfo {
    if !forceRegular {
        _, radiusLookup := r.recalcRadius()
        if radiusLookup != -1 {
            target := r.targetForBucket(radiusLookup)
            r.buckets[radiusLookup].lookupSent[target] = mclock.Now()
            return lookupInfo{target: target, topic: r.topic, radiusLookup: true}
        }
    }

    radExt := r.radius / 2
    if radExt > maxRadius-r.radius {
        radExt = maxRadius - r.radius
    }
    rnd := randUint64n(r.radius) + randUint64n(2*radExt)
    if rnd > radExt {
        rnd -= radExt
    } else {
        rnd = radExt - rnd
    }

    prefix := r.topicHashPrefix ^ rnd
    var target common.Hash
    binary.BigEndian.PutUint64(target[0:8], prefix)
    globalRandRead(target[8:])
    return lookupInfo{target: target, topic: r.topic, radiusLookup: false}
}

func (r *topicRadius) adjustWithTicket(now mclock.AbsTime, targetHash common.Hash, t ticketRef) {
    wait := t.t.regTime[t.idx] - t.t.issueTime
    inside := float64(wait)/float64(targetWaitTime) - 0.5
    if inside > 1 {
        inside = 1
    }
    if inside < 0 {
        inside = 0
    }
    r.adjust(now, targetHash, t.t.node.sha, inside)
}

func (r *topicRadius) adjust(now mclock.AbsTime, targetHash, addrHash common.Hash, inside float64) {
    bucket := r.getBucketIdx(addrHash)
    //fmt.Println("adjust", bucket, len(r.buckets), inside)
    if bucket >= len(r.buckets) {
        return
    }
    r.buckets[bucket].adjust(now, inside)
    delete(r.buckets[bucket].lookupSent, targetHash)
}