From 3426eab8189537c419838fda4720d7419ac74126 Mon Sep 17 00:00:00 2001 From: Rafael Matias Date: Wed, 18 Sep 2019 13:36:44 +0200 Subject: [PATCH 1/2] network: add own address to KademliaInfo --- network/kademlia.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/network/kademlia.go b/network/kademlia.go index 6af58d6a74..28b9bdeb9f 100644 --- a/network/kademlia.go +++ b/network/kademlia.go @@ -95,6 +95,7 @@ type Kademlia struct { } type KademliaInfo struct { + Self string `json:"self"` Depth int `json:"depth"` TotalConnections int `json:"total_connections"` TotalKnown int `json:"total_known"` @@ -580,6 +581,7 @@ func (k *Kademlia) KademliaInfo() KademliaInfo { } func (k *Kademlia) kademliaInfo() (ki KademliaInfo) { + ki.Self = fmt.Sprintf("%x", k.BaseAddr()) ki.Depth = depthForPot(k.conns, k.NeighbourhoodSize, k.base) ki.TotalConnections = k.conns.Size() ki.TotalKnown = k.addrs.Size() From 43c0337b36f856da12d42588b2db2aac83171cf9 Mon Sep 17 00:00:00 2001 From: Rafael Matias Date: Wed, 18 Sep 2019 14:27:27 +0200 Subject: [PATCH 2/2] network: use hex.EncodeToString --- network/kademlia.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/network/kademlia.go b/network/kademlia.go index 28b9bdeb9f..f767137ba5 100644 --- a/network/kademlia.go +++ b/network/kademlia.go @@ -18,6 +18,7 @@ package network import ( "bytes" + "encoding/hex" "fmt" "math/rand" "sort" @@ -141,7 +142,7 @@ func Label(e *entry) string { // Hex is the hexadecimal serialisation of the entry address func (e *entry) Hex() string { - return fmt.Sprintf("%x", e.Address()) + return hex.EncodeToString(e.Address()) } // Register enters each address as kademlia peer record into the @@ -581,7 +582,7 @@ func (k *Kademlia) KademliaInfo() KademliaInfo { } func (k *Kademlia) kademliaInfo() (ki KademliaInfo) { - ki.Self = fmt.Sprintf("%x", k.BaseAddr()) + ki.Self = hex.EncodeToString(k.BaseAddr()) ki.Depth = depthForPot(k.conns, k.NeighbourhoodSize, k.base) ki.TotalConnections = k.conns.Size() ki.TotalKnown = k.addrs.Size() @@ -596,7 +597,7 @@ func (k *Kademlia) kademliaInfo() (ki KademliaInfo) { row := []string{} f(func(val pot.Val) bool { e := val.(*Peer) - row = append(row, fmt.Sprintf("%x", e.Address())) + row = append(row, hex.EncodeToString(e.Address())) return true }) sort.Strings(row) @@ -613,7 +614,7 @@ func (k *Kademlia) kademliaInfo() (ki KademliaInfo) { row := []string{} f(func(val pot.Val) bool { e := val.(*entry) - row = append(row, fmt.Sprintf("%x", e.Address())) + row = append(row, hex.EncodeToString(e.Address())) return true }) sort.Strings(row) @@ -659,7 +660,7 @@ func (k *Kademlia) string() string { rest -= size f(func(val pot.Val) bool { e := val.(*Peer) - row = append(row, fmt.Sprintf("%x", e.Address()[:2])) + row = append(row, hex.EncodeToString(e.Address()[:2])) rowlen++ return rowlen < 4 })