aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet/service/dht/routingtable.go
diff options
context:
space:
mode:
authorBernd Fix <brf@hoi-polloi.org>2022-08-18 14:58:34 +0200
committerBernd Fix <brf@hoi-polloi.org>2022-08-18 14:58:34 +0200
commit5b03f7567ad8242cc87924c3920dfc04420365ef (patch)
treed2e27aca792e1fe8b185e37e571b77c7b7d6a249 /src/gnunet/service/dht/routingtable.go
parentb62071330cd7e0445e89660d07b7aed098f80285 (diff)
downloadgnunet-go-5b03f7567ad8242cc87924c3920dfc04420365ef.tar.gz
gnunet-go-5b03f7567ad8242cc87924c3920dfc04420365ef.zip
Prepared for integration test.v0.1.32
Diffstat (limited to 'src/gnunet/service/dht/routingtable.go')
-rw-r--r--src/gnunet/service/dht/routingtable.go58
1 files changed, 27 insertions, 31 deletions
diff --git a/src/gnunet/service/dht/routingtable.go b/src/gnunet/service/dht/routingtable.go
index 20bf5fc..21c8823 100644
--- a/src/gnunet/service/dht/routingtable.go
+++ b/src/gnunet/service/dht/routingtable.go
@@ -66,7 +66,7 @@ func NewPeerAddress(peer *util.PeerID) *PeerAddress {
66func NewQueryAddress(key *crypto.HashCode) *PeerAddress { 66func NewQueryAddress(key *crypto.HashCode) *PeerAddress {
67 return &PeerAddress{ 67 return &PeerAddress{
68 Peer: nil, 68 Peer: nil,
69 Key: crypto.NewHashCode(key.Bits), 69 Key: crypto.NewHashCode(key.Data),
70 lastSeen: util.AbsoluteTimeNow(), 70 lastSeen: util.AbsoluteTimeNow(),
71 lastUsed: util.AbsoluteTimeNow(), 71 lastUsed: util.AbsoluteTimeNow(),
72 } 72 }
@@ -74,18 +74,18 @@ func NewQueryAddress(key *crypto.HashCode) *PeerAddress {
74 74
75// String returns a human-readble representation of an address. 75// String returns a human-readble representation of an address.
76func (addr *PeerAddress) String() string { 76func (addr *PeerAddress) String() string {
77 return hex.EncodeToString(addr.Key.Bits) 77 return hex.EncodeToString(addr.Key.Data)
78} 78}
79 79
80// Equals returns true if two peer addresses are the same. 80// Equal returns true if two peer addresses are the same.
81func (addr *PeerAddress) Equals(p *PeerAddress) bool { 81func (addr *PeerAddress) Equal(p *PeerAddress) bool {
82 return bytes.Equal(addr.Key.Bits, p.Key.Bits) 82 return bytes.Equal(addr.Key.Data, p.Key.Data)
83} 83}
84 84
85// Distance between two addresses: returns a distance value and a 85// Distance between two addresses: returns a distance value and a
86// bucket index (smaller index = less distant). 86// bucket index (smaller index = less distant).
87func (addr *PeerAddress) Distance(p *PeerAddress) (*math.Int, int) { 87func (addr *PeerAddress) Distance(p *PeerAddress) (*math.Int, int) {
88 r := util.Distance(addr.Key.Bits, p.Key.Bits) 88 r := util.Distance(addr.Key.Data, p.Key.Data)
89 return r, 512 - r.BitLen() 89 return r, 512 - r.BitLen()
90} 90}
91 91
@@ -136,24 +136,22 @@ func NewRoutingTable(ref *PeerAddress, cfg *config.RoutingConfig) *RoutingTable
136// Returns true if the entry was added, false otherwise. 136// Returns true if the entry was added, false otherwise.
137func (rt *RoutingTable) Add(p *PeerAddress, label string) bool { 137func (rt *RoutingTable) Add(p *PeerAddress, label string) bool {
138 k := p.String() 138 k := p.String()
139 logger.Printf(logger.DBG, "[%s] Add(%s)", label, k)
140 139
141 // check if peer is already known 140 // check if peer is already known
142 if px, ok := rt.list.Get(k, 0); ok { 141 if px, ok := rt.list.Get(k, 0); ok {
143 logger.Printf(logger.DBG, "[%s] --> already known", label)
144 px.lastSeen = util.AbsoluteTimeNow() 142 px.lastSeen = util.AbsoluteTimeNow()
145 return false 143 return false
146 } 144 }
147 // compute distance (bucket index) and insert address. 145 // compute distance (bucket index) and insert address.
148 _, idx := p.Distance(rt.ref) 146 _, idx := p.Distance(rt.ref)
149 if rt.buckets[idx].Add(p) { 147 if rt.buckets[idx].Add(p) {
150 logger.Printf(logger.DBG, "[%s] --> entry added", label)
151 p.lastUsed = util.AbsoluteTimeNow() 148 p.lastUsed = util.AbsoluteTimeNow()
152 rt.list.Put(k, p, 0) 149 rt.list.Put(k, p, 0)
150 logger.Printf(logger.INFO, "[%s] %s added to routing table",
151 label, p.Peer.Short())
153 return true 152 return true
154 } 153 }
155 // Full bucket: we did not add the address to the routing table. 154 // Full bucket: we did not add the address to the routing table.
156 logger.Printf(logger.DBG, "[%s] --> bucket[%d] full -- discarded", label, idx)
157 return false 155 return false
158} 156}
159 157
@@ -178,41 +176,39 @@ func (rt *RoutingTable) Check(p *PeerAddress) int {
178 176
179// Remove peer address from routing table. 177// Remove peer address from routing table.
180// Returns true if the entry was removed, false otherwise. 178// Returns true if the entry was removed, false otherwise.
181func (rt *RoutingTable) Remove(p *PeerAddress, pid int) bool { 179func (rt *RoutingTable) Remove(p *PeerAddress, label string, pid int) bool {
182 k := p.String()
183 logger.Printf(logger.DBG, "[RT] Remove(%s)", k)
184
185 // compute distance (bucket index) and remove entry from bucket 180 // compute distance (bucket index) and remove entry from bucket
186 rc := false 181 rc := false
187 _, idx := p.Distance(rt.ref) 182 _, idx := p.Distance(rt.ref)
188 if rt.buckets[idx].Remove(p) { 183 if rt.buckets[idx].Remove(p) {
189 logger.Println(logger.DBG, "[RT] --> entry removed from bucket and internal lists") 184 logger.Printf(logger.DBG, "[%s] %s removed from RT (bucket and internal lists)", label, p.Peer.Short())
190 rc = true 185 rc = true
191 } else { 186 } else {
192 // remove from internal list 187 // remove from internal list
193 logger.Println(logger.DBG, "[RT] --> entry removed from internal lists only") 188 logger.Printf(logger.DBG, "[%s] %s removed from RT (internal lists only)", label, p.Peer.Short())
194 } 189 }
195 rt.list.Delete(k, 0) 190 rt.list.Delete(p.String(), 0)
196 // delete from HELLO cache 191 // delete from HELLO cache
197 rt.helloCache.Delete(p.Peer.String(), pid) 192 rt.helloCache.Delete(p.Peer.String(), pid)
198 return rc 193 return rc
199} 194}
200 195
201// Contains checks if a peer is available in the routing table 196// Contains checks if a peer is available in the routing table
202func (rt *RoutingTable) Contains(p *PeerAddress) bool { 197func (rt *RoutingTable) Contains(p *PeerAddress, label string) bool {
203 k := p.String() 198 k := p.String()
204 logger.Printf(logger.DBG, "[RT] Contains(%s)?", k)
205 199
206 // check for peer in internal list 200 // check for peer in internal list
207 px, ok := rt.list.Get(k, 0) 201 px, ok := rt.list.Get(k, 0)
208 if !ok { 202 if !ok {
209 logger.Println(logger.DBG, "[RT] --> NOT found in current list:") 203 logger.Printf(logger.WARN, "[%s] %s NOT found in RT", label, p.Peer.Short())
204 var list []string
210 _ = rt.list.ProcessRange(func(key string, val *PeerAddress, _ int) error { 205 _ = rt.list.ProcessRange(func(key string, val *PeerAddress, _ int) error {
211 logger.Printf(logger.DBG, "[RT] * %s", val) 206 list = append(list, val.Peer.Short())
212 return nil 207 return nil
213 }, true) 208 }, true)
209 logger.Printf(logger.DBG, "[%s] RT=%v", list)
214 } else { 210 } else {
215 logger.Println(logger.DBG, "[RT] --> found in current list") 211 //logger.Println(logger.DBG, "[RT] --> found in current list")
216 px.lastSeen = util.AbsoluteTimeNow() 212 px.lastSeen = util.AbsoluteTimeNow()
217 } 213 }
218 return ok 214 return ok
@@ -310,7 +306,7 @@ func (rt *RoutingTable) IsClosestPeer(p, k *PeerAddress, pf *blocks.PeerFilter,
310 return d1.Cmp(d0) < 0 306 return d1.Cmp(d0) < 0
311 } 307 }
312 // check if p is closest peer 308 // check if p is closest peer
313 return n.Equals(p) 309 return n.Equal(p)
314} 310}
315 311
316// ComputeOutDegree computes the number of neighbors that a message should be forwarded to. 312// ComputeOutDegree computes the number of neighbors that a message should be forwarded to.
@@ -340,23 +336,23 @@ func (rt *RoutingTable) ComputeOutDegree(repl, hop uint16) int {
340func (rt *RoutingTable) heartbeat(ctx context.Context) { 336func (rt *RoutingTable) heartbeat(ctx context.Context) {
341 337
342 // check for dead or expired peers 338 // check for dead or expired peers
343 logger.Println(logger.DBG, "[dht] RT heartbeat...") 339 logger.Println(logger.DBG, "[dht-rt-hb] RT heartbeat...")
344 timeout := util.NewRelativeTime(time.Duration(rt.cfg.PeerTTL) * time.Second) 340 timeout := util.NewRelativeTime(time.Duration(rt.cfg.PeerTTL) * time.Second)
345 if err := rt.list.ProcessRange(func(k string, p *PeerAddress, pid int) error { 341 if err := rt.list.ProcessRange(func(k string, p *PeerAddress, pid int) error {
346 // check if we can/need to drop a peer 342 // check if we can/need to drop a peer
347 drop := timeout.Compare(p.lastSeen.Elapsed()) < 0 343 drop := timeout.Compare(p.lastSeen.Elapsed()) < 0
348 if drop || timeout.Compare(p.lastUsed.Elapsed()) < 0 { 344 if drop || timeout.Compare(p.lastUsed.Elapsed()) < 0 {
349 logger.Printf(logger.DBG, "[RT] removing %v: %v, %v", p, p.lastSeen.Elapsed(), p.lastUsed.Elapsed()) 345 logger.Printf(logger.DBG, "[dht-rt-hb] removing %v: %v, %v", p, p.lastSeen.Elapsed(), p.lastUsed.Elapsed())
350 rt.Remove(p, pid) 346 rt.Remove(p, "dht-rt-hb", pid)
351 } 347 }
352 return nil 348 return nil
353 }, false); err != nil { 349 }, false); err != nil {
354 logger.Println(logger.ERROR, "[dht] RT heartbeat failed: "+err.Error()) 350 logger.Println(logger.ERROR, "[dht-rt-hb] RT heartbeat failed: "+err.Error())
355 } 351 }
356 352
357 // drop expired entries from the HELLO cache 353 // drop expired entries from the HELLO cache
358 _ = rt.helloCache.ProcessRange(func(key string, val *blocks.HelloBlock, pid int) error { 354 _ = rt.helloCache.ProcessRange(func(key string, val *blocks.HelloBlock, pid int) error {
359 if val.Expires.Expired() { 355 if val.Expire_.Expired() {
360 rt.helloCache.Delete(key, pid) 356 rt.helloCache.Delete(key, pid)
361 } 357 }
362 return nil 358 return nil
@@ -369,7 +365,7 @@ func (rt *RoutingTable) heartbeat(ctx context.Context) {
369//---------------------------------------------------------------------- 365//----------------------------------------------------------------------
370 366
371// LookupHello returns blocks from the HELLO cache for given query. 367// LookupHello returns blocks from the HELLO cache for given query.
372func (rt *RoutingTable) LookupHello(addr *PeerAddress, rf blocks.ResultFilter, approx bool) (results []*store.DHTResult) { 368func (rt *RoutingTable) LookupHello(addr *PeerAddress, rf blocks.ResultFilter, approx bool, label string) (results []*store.DHTResult) {
373 // iterate over cached HELLOs to find matches; 369 // iterate over cached HELLOs to find matches;
374 // approximate search is limited by distance (max. diff for bucket index is 16) 370 // approximate search is limited by distance (max. diff for bucket index is 16)
375 _ = rt.helloCache.ProcessRange(func(key string, hb *blocks.HelloBlock, _ int) error { 371 _ = rt.helloCache.ProcessRange(func(key string, hb *blocks.HelloBlock, _ int) error {
@@ -390,7 +386,7 @@ func (rt *RoutingTable) LookupHello(addr *PeerAddress, rf blocks.ResultFilter, a
390 results = append(results, result) 386 results = append(results, result)
391 } 387 }
392 } else { 388 } else {
393 logger.Printf(logger.DBG, "[RT] GET-HELLO: cache block is filtered") 389 logger.Printf(logger.DBG, "[%s] LookupHello: cached HELLO block is filtered")
394 } 390 }
395 return nil 391 return nil
396 }, true) 392 }, true)
@@ -479,7 +475,7 @@ func (b *Bucket) Remove(p *PeerAddress) bool {
479 defer b.Unlock() 475 defer b.Unlock()
480 476
481 for i, pe := range b.list { 477 for i, pe := range b.list {
482 if pe.Equals(p) { 478 if pe.Equal(p) {
483 // found entry: remove it 479 // found entry: remove it
484 b.list = append(b.list[:i], b.list[i+1:]...) 480 b.list = append(b.list[:i], b.list[i+1:]...)
485 return true 481 return true