diff options
author | Felix Lange <fjl@twurst.com> | 2017-02-24 16:58:04 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2017-02-28 17:20:29 +0800 |
commit | 96ae35e2ac8c360781407d7294081aabdcbb3652 (patch) | |
tree | 378cc7aae99127a4e85a721e5cd319ced87e103a /p2p/discover | |
parent | 35e8308bf76df11c3c1e8d11fac782814583ea5c (diff) | |
download | dexon-96ae35e2ac8c360781407d7294081aabdcbb3652.tar dexon-96ae35e2ac8c360781407d7294081aabdcbb3652.tar.gz dexon-96ae35e2ac8c360781407d7294081aabdcbb3652.tar.bz2 dexon-96ae35e2ac8c360781407d7294081aabdcbb3652.tar.lz dexon-96ae35e2ac8c360781407d7294081aabdcbb3652.tar.xz dexon-96ae35e2ac8c360781407d7294081aabdcbb3652.tar.zst dexon-96ae35e2ac8c360781407d7294081aabdcbb3652.zip |
p2p, p2p/discover, p2p/nat: rework logging using context keys
Diffstat (limited to 'p2p/discover')
-rw-r--r-- | p2p/discover/database.go | 8 | ||||
-rw-r--r-- | p2p/discover/node.go | 5 | ||||
-rw-r--r-- | p2p/discover/ntp.go | 13 | ||||
-rw-r--r-- | p2p/discover/table.go | 14 | ||||
-rw-r--r-- | p2p/discover/udp.go | 46 |
5 files changed, 42 insertions, 44 deletions
diff --git a/p2p/discover/database.go b/p2p/discover/database.go index a8b32d31e..7206a63c6 100644 --- a/p2p/discover/database.go +++ b/p2p/discover/database.go @@ -23,7 +23,6 @@ import ( "bytes" "crypto/rand" "encoding/binary" - "fmt" "os" "sync" "time" @@ -180,12 +179,11 @@ func (db *nodeDB) storeInt64(key []byte, n int64) error { func (db *nodeDB) node(id NodeID) *Node { blob, err := db.lvl.Get(makeKey(id, nodeDBDiscoverRoot), nil) if err != nil { - log.Trace(fmt.Sprintf("failed to retrieve node %v: %v", id, err)) return nil } node := new(Node) if err := rlp.DecodeBytes(blob, node); err != nil { - log.Warn(fmt.Sprintf("failed to decode node RLP: %v", err)) + log.Error("Failed to decode node RLP", "err", err) return nil } node.sha = crypto.Keccak256Hash(node.ID[:]) @@ -233,7 +231,7 @@ func (db *nodeDB) expirer() { select { case <-tick: if err := db.expireNodes(); err != nil { - log.Error(fmt.Sprintf("Failed to expire nodedb items: %v", err)) + log.Error("Failed to expire nodedb items", "err", err) } case <-db.quit: @@ -352,7 +350,7 @@ func nextNode(it iterator.Iterator) *Node { } var n Node if err := rlp.DecodeBytes(it.Value(), &n); err != nil { - log.Warn(fmt.Sprintf("invalid node %x: %v", id, err)) + log.Warn("Failed to decode node RLP", "id", id, "err", err) continue } return &n diff --git a/p2p/discover/node.go b/p2p/discover/node.go index f0262762e..6a7ab814e 100644 --- a/p2p/discover/node.go +++ b/p2p/discover/node.go @@ -221,6 +221,11 @@ func (n NodeID) GoString() string { return fmt.Sprintf("discover.HexID(\"%x\")", n[:]) } +// TerminalString returns a shortened hex string for terminal logging. +func (n NodeID) TerminalString() string { + return hex.EncodeToString(n[:8]) +} + // HexID converts a hex string to a NodeID. // The string may be prefixed with 0x. func HexID(in string) (NodeID, error) { diff --git a/p2p/discover/ntp.go b/p2p/discover/ntp.go index df67e1c5b..1bb52399f 100644 --- a/p2p/discover/ntp.go +++ b/p2p/discover/ntp.go @@ -23,7 +23,6 @@ import ( "fmt" "net" "sort" - "strings" "time" "github.com/ethereum/go-ethereum/log" @@ -50,16 +49,10 @@ func checkClockDrift() { return } if drift < -driftThreshold || drift > driftThreshold { - warning := fmt.Sprintf("System clock seems off by %v, which can prevent network connectivity", drift) - howtofix := fmt.Sprintf("Please enable network time synchronisation in system settings") - separator := strings.Repeat("-", len(warning)) - - log.Warn(fmt.Sprint(separator)) - log.Warn(fmt.Sprint(warning)) - log.Warn(fmt.Sprint(howtofix)) - log.Warn(fmt.Sprint(separator)) + log.Warn(fmt.Sprintf("System clock seems off by %v, which can prevent network connectivity", drift)) + log.Warn("Please enable network time synchronisation in system settings.") } else { - log.Debug(fmt.Sprintf("Sanity NTP check reported %v drift, all ok", drift)) + log.Debug("NTP sanity check done", "drift", drift) } } diff --git a/p2p/discover/table.go b/p2p/discover/table.go index 03392b563..2f5a26c34 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -277,10 +277,10 @@ func (tab *Table) lookup(targetID NodeID, refreshIfEmpty bool) []*Node { // Bump the failure counter to detect and evacuate non-bonded entries fails := tab.db.findFails(n.ID) + 1 tab.db.updateFindFails(n.ID, fails) - log.Trace(fmt.Sprintf("Bumping failures for %x: %d", n.ID[:8], fails)) + log.Trace("Bumping findnode failure counter", "id", n.ID, "failcount", fails) if fails >= maxFindnodeFailures { - log.Trace(fmt.Sprintf("Evacuating node %x: %d findnode failures", n.ID[:8], fails)) + log.Trace("Too many findnode failures, dropping", "id", n.ID, "failcount", fails) tab.delete(n) } } @@ -385,13 +385,11 @@ func (tab *Table) doRefresh(done chan struct{}) { seeds = tab.bondall(append(seeds, tab.nursery...)) if len(seeds) == 0 { - log.Debug(fmt.Sprintf("no seed nodes found")) + log.Debug("No discv4 seed nodes found") } for _, n := range seeds { - log.Debug("", "msg", log.Lazy{Fn: func() string { - age := time.Since(tab.db.lastPong(n.ID)) - return fmt.Sprintf("seed node (age %v): %v", age, n) - }}) + age := log.Lazy{Fn: func() time.Duration { return time.Since(tab.db.lastPong(n.ID)) }} + log.Trace("Found seed node in database", "id", n.ID, "addr", n.addr(), "age", age) } tab.mutex.Lock() tab.stuff(seeds) @@ -470,7 +468,7 @@ func (tab *Table) bond(pinged bool, id NodeID, addr *net.UDPAddr, tcpPort uint16 var result error age := time.Since(tab.db.lastPong(id)) if node == nil || fails > 0 || age > nodeDBNodeExpiration { - log.Trace(fmt.Sprintf("Bonding %x: known=%t, fails=%d age=%v", id[:8], node != nil, fails, age)) + log.Trace("Starting bonding ping/pong", "id", id, "known", node != nil, "failcount", fails, "age", age) tab.bondmu.Lock() w := tab.bonding[id] diff --git a/p2p/discover/udp.go b/p2p/discover/udp.go index eafc3f394..93545e7d5 100644 --- a/p2p/discover/udp.go +++ b/p2p/discover/udp.go @@ -147,6 +147,7 @@ func nodeToRPC(n *Node) rpcNode { type packet interface { handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error + name() string } type conn interface { @@ -223,7 +224,7 @@ func ListenUDP(priv *ecdsa.PrivateKey, laddr string, natm nat.Interface, nodeDBP if err != nil { return nil, err } - log.Info(fmt.Sprint("Listening,", tab.self)) + log.Debug("UDP listener up", "self", tab.self) return tab, nil } @@ -269,7 +270,7 @@ func (t *udp) close() { func (t *udp) ping(toid NodeID, toaddr *net.UDPAddr) error { // TODO: maybe check for ReplyTo field in callback to measure RTT errc := t.pending(toid, pongPacket, func(interface{}) bool { return true }) - t.send(toaddr, pingPacket, ping{ + t.send(toaddr, pingPacket, &ping{ Version: Version, From: t.ourEndpoint, To: makeEndpoint(toaddr, 0), // TODO: maybe use known TCP port from DB @@ -293,14 +294,14 @@ func (t *udp) findnode(toid NodeID, toaddr *net.UDPAddr, target NodeID) ([]*Node nreceived++ n, err := t.nodeFromRPC(toaddr, rn) if err != nil { - log.Trace(fmt.Sprintf("invalid neighbor node (%v) from %v: %v", rn.IP, toaddr, err)) + log.Trace("Invalid neighbor node received", "ip", rn.IP, "addr", toaddr, "err", err) continue } nodes = append(nodes, n) } return nreceived >= bucketSize }) - t.send(toaddr, findnodePacket, findnode{ + t.send(toaddr, findnodePacket, &findnode{ Target: target, Expiration: uint64(time.Now().Add(expiration).Unix()), }) @@ -458,15 +459,13 @@ func init() { } } -func (t *udp) send(toaddr *net.UDPAddr, ptype byte, req interface{}) error { +func (t *udp) send(toaddr *net.UDPAddr, ptype byte, req packet) error { packet, err := encodePacket(t.priv, ptype, req) if err != nil { return err } - log.Trace(fmt.Sprintf(">>> %v %T", toaddr, req)) - if _, err = t.conn.WriteToUDP(packet, toaddr); err != nil { - log.Trace(fmt.Sprint("UDP send failed:", err)) - } + _, err = t.conn.WriteToUDP(packet, toaddr) + log.Trace(">> "+req.name(), "addr", toaddr, "err", err) return err } @@ -475,13 +474,13 @@ func encodePacket(priv *ecdsa.PrivateKey, ptype byte, req interface{}) ([]byte, b.Write(headSpace) b.WriteByte(ptype) if err := rlp.Encode(b, req); err != nil { - log.Error(fmt.Sprint("error encoding packet:", err)) + log.Error("Can't encode discv4 packet", "err", err) return nil, err } packet := b.Bytes() sig, err := crypto.Sign(crypto.Keccak256(packet[headSize:]), priv) if err != nil { - log.Error(fmt.Sprint("could not sign packet:", err)) + log.Error("Can't sign discv4 packet", "err", err) return nil, err } copy(packet[macSize:], sig) @@ -503,11 +502,11 @@ func (t *udp) readLoop() { nbytes, from, err := t.conn.ReadFromUDP(buf) if netutil.IsTemporaryError(err) { // Ignore temporary read errors. - log.Debug(fmt.Sprintf("Temporary read error: %v", err)) + log.Debug("Temporary UDP read error", "err", err) continue } else if err != nil { // Shut down the loop for permament errors. - log.Debug(fmt.Sprintf("Read error: %v", err)) + log.Debug("UDP read error", "err", err) return } t.handlePacket(from, buf[:nbytes]) @@ -517,14 +516,11 @@ func (t *udp) readLoop() { func (t *udp) handlePacket(from *net.UDPAddr, buf []byte) error { packet, fromID, hash, err := decodePacket(buf) if err != nil { - log.Debug(fmt.Sprintf("Bad packet from %v: %v", from, err)) + log.Debug("Bad discv4 packet", "addr", from, "err", err) return err } - status := "ok" - if err = packet.handle(t, from, fromID, hash); err != nil { - status = err.Error() - } - log.Trace(fmt.Sprintf("<<< %v %T: %s", from, packet, status)) + err = packet.handle(t, from, fromID, hash) + log.Trace("<< "+packet.name(), "addr", from, "err", err) return err } @@ -563,7 +559,7 @@ func (req *ping) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) er if expired(req.Expiration) { return errExpired } - t.send(from, pongPacket, pong{ + t.send(from, pongPacket, &pong{ To: makeEndpoint(from, req.From.TCP), ReplyTok: mac, Expiration: uint64(time.Now().Add(expiration).Unix()), @@ -575,6 +571,8 @@ func (req *ping) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) er return nil } +func (req *ping) name() string { return "PING/v4" } + func (req *pong) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error { if expired(req.Expiration) { return errExpired @@ -585,6 +583,8 @@ func (req *pong) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) er return nil } +func (req *pong) name() string { return "PONG/v4" } + func (req *findnode) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error { if expired(req.Expiration) { return errExpired @@ -613,13 +613,15 @@ func (req *findnode) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte } p.Nodes = append(p.Nodes, nodeToRPC(n)) if len(p.Nodes) == maxNeighbors || i == len(closest)-1 { - t.send(from, neighborsPacket, p) + t.send(from, neighborsPacket, &p) p.Nodes = p.Nodes[:0] } } return nil } +func (req *findnode) name() string { return "FINDNODE/v4" } + func (req *neighbors) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byte) error { if expired(req.Expiration) { return errExpired @@ -630,6 +632,8 @@ func (req *neighbors) handle(t *udp, from *net.UDPAddr, fromID NodeID, mac []byt return nil } +func (req *neighbors) name() string { return "NEIGHBORS/v4" } + func expired(ts uint64) bool { return time.Unix(int64(ts), 0).Before(time.Now()) } |