Skip to content

Commit 01d9194

Browse files
skylenetnonsense
authored andcommitted
network: add own address to KademliaInfo (ethersphere#1775)
1 parent 5ca79dc commit 01d9194

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

network/kademlia.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package network
1818

1919
import (
2020
"bytes"
21+
"encoding/hex"
2122
"fmt"
2223
"math/rand"
2324
"sort"
@@ -95,6 +96,7 @@ type Kademlia struct {
9596
}
9697

9798
type KademliaInfo struct {
99+
Self string `json:"self"`
98100
Depth int `json:"depth"`
99101
TotalConnections int `json:"total_connections"`
100102
TotalKnown int `json:"total_known"`
@@ -140,7 +142,7 @@ func Label(e *entry) string {
140142

141143
// Hex is the hexadecimal serialisation of the entry address
142144
func (e *entry) Hex() string {
143-
return fmt.Sprintf("%x", e.Address())
145+
return hex.EncodeToString(e.Address())
144146
}
145147

146148
// Register enters each address as kademlia peer record into the
@@ -580,6 +582,7 @@ func (k *Kademlia) KademliaInfo() KademliaInfo {
580582
}
581583

582584
func (k *Kademlia) kademliaInfo() (ki KademliaInfo) {
585+
ki.Self = hex.EncodeToString(k.BaseAddr())
583586
ki.Depth = depthForPot(k.conns, k.NeighbourhoodSize, k.base)
584587
ki.TotalConnections = k.conns.Size()
585588
ki.TotalKnown = k.addrs.Size()
@@ -594,7 +597,7 @@ func (k *Kademlia) kademliaInfo() (ki KademliaInfo) {
594597
row := []string{}
595598
f(func(val pot.Val) bool {
596599
e := val.(*Peer)
597-
row = append(row, fmt.Sprintf("%x", e.Address()))
600+
row = append(row, hex.EncodeToString(e.Address()))
598601
return true
599602
})
600603
sort.Strings(row)
@@ -611,7 +614,7 @@ func (k *Kademlia) kademliaInfo() (ki KademliaInfo) {
611614
row := []string{}
612615
f(func(val pot.Val) bool {
613616
e := val.(*entry)
614-
row = append(row, fmt.Sprintf("%x", e.Address()))
617+
row = append(row, hex.EncodeToString(e.Address()))
615618
return true
616619
})
617620
sort.Strings(row)
@@ -657,7 +660,7 @@ func (k *Kademlia) string() string {
657660
rest -= size
658661
f(func(val pot.Val) bool {
659662
e := val.(*Peer)
660-
row = append(row, fmt.Sprintf("%x", e.Address()[:2]))
663+
row = append(row, hex.EncodeToString(e.Address()[:2]))
661664
rowlen++
662665
return rowlen < 4
663666
})

0 commit comments

Comments
 (0)