diff --git a/p2p/discover/table.go b/p2p/discover/table.go index 74f0fa8dc7..f9e4747d69 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -317,12 +317,14 @@ func (tab *Table) loadSeedNodes() { func (tab *Table) doRevalidate(done chan<- struct{}) { defer func() { done <- struct{}{} }() + log.Info("do revalidate") last, bi := tab.nodeToRevalidate() if last == nil { // No non-empty bucket found. return } + log.Info("do revalidate send ping") // Ping the selected node and wait for a pong. remoteSeq, err := tab.net.ping(unwrapNode(last)) @@ -411,10 +413,14 @@ func (tab *Table) findnodeByID(target enode.ID, nresults int, preferLive bool) * liveNodes := &nodesByDistance{target: target} for _, b := range &tab.buckets { for _, n := range b.entries { + // 使用xorDistance函数计算节点n与目标ID的距离 + distance := xorDistance(target, n.ID()) nodes.push(n, nresults) if preferLive && n.livenessChecks > 0 { liveNodes.push(n, nresults) } + log.Info("ZXL Distance", "Node ID", n.ID(), "Target ID", target, "XOR Distance", distance) + } } @@ -424,6 +430,13 @@ func (tab *Table) findnodeByID(target enode.ID, nresults int, preferLive bool) * return nodes } +func xorDistance(a, b enode.ID) (distance enode.ID) { + for i := range a { + distance[i] = a[i] ^ b[i] + } + return distance +} + // get all nodes ids func (tab *Table) getAllNodeIDs() []enode.ID { tab.mutex.Lock() @@ -438,6 +451,20 @@ func (tab *Table) getAllNodeIDs() []enode.ID { return ids } +// get all nodes ids +func (tab *Table) getAllNodes() []enode.Node { + tab.mutex.Lock() + defer tab.mutex.Unlock() + + var nodes []enode.Node + for _, b := range tab.buckets { + for _, n := range b.entries { + nodes = append(nodes, n.Node) + } + } + return nodes +} + // len returns the number of nodes in the table. func (tab *Table) len() (n int) { tab.mutex.Lock() @@ -522,10 +549,14 @@ func (tab *Table) addVerifiedNode(n *node) { if n.ID() == tab.self().ID() { return } - nodeIDs := tab.getAllNodeIDs() + nodes := tab.getAllNodes() - for i, id := range nodeIDs { - log.Info("ZXL: current tab content", "index", i, "nodeId", id.String()) + for i, nodeIndex := range nodes { + log.Info("ZXL: current tab content", "index", i, "nodeId", nodeIndex.ID(), "nodeIP", nodeIndex.IP().String()) + for j, pair := range nodeIndex.Record().GetPairs() { + log.Info("ZXL: current node content", "node index", i, "pair index", j, "pairKey", pair.GetPairKey(), "pairValue", pair.GetPairValue()) + + } } tab.mutex.Lock() diff --git a/p2p/discover/v4_udp.go b/p2p/discover/v4_udp.go index 5d6f2b20e7..f02efe9cb2 100644 --- a/p2p/discover/v4_udp.go +++ b/p2p/discover/v4_udp.go @@ -209,12 +209,14 @@ func (t *UDPv4) ourEndpoint() v4wire.Endpoint { // Ping sends a ping message to the given node. func (t *UDPv4) Ping(n *enode.Node) error { + log.Info("send ping from ping-ping") _, err := t.ping(n) return err } // ping sends a ping message to the given node and waits for a reply. func (t *UDPv4) ping(n *enode.Node) (seq uint64, err error) { + log.Info("ZXL: sendPing from ping") rm := t.sendPing(n.ID(), &net.UDPAddr{IP: n.IP(), Port: n.UDP()}, nil) if err = <-rm.errc; err == nil { seq = rm.reply.(*v4wire.Pong).ENRSeq @@ -225,6 +227,7 @@ func (t *UDPv4) ping(n *enode.Node) (seq uint64, err error) { // sendPing sends a ping message to the given node and invokes the callback // when the reply arrives. // ZXL 在这里发生的变化 +// 只要给Bootndoe发过就会更替 func (t *UDPv4) sendPing(toid enode.ID, toaddr *net.UDPAddr, callback func()) *replyMatcher { req := t.makePing(toaddr) log.Warn("ZXL SendPing", "toID", toid, "fromIP", req.From.IP.String(), "fromPortTCP", req.From.TCP, "fromPortUDP", req.From.UDP) @@ -576,6 +579,7 @@ func (t *UDPv4) checkBond(id enode.ID, ip net.IP) bool { func (t *UDPv4) ensureBond(toid enode.ID, toaddr *net.UDPAddr) { tooOld := time.Since(t.db.LastPingReceived(toid, toaddr.IP)) > bondExpiration if tooOld || t.db.FindFails(toid, toaddr.IP) > maxFindnodeFailures { + log.Info("ZXL: sendPing from ensureBond") rm := t.sendPing(toid, toaddr, nil) <-rm.errc // Wait for them to ping back and process our pong. @@ -678,6 +682,7 @@ func (t *UDPv4) handlePing(h *packetHandlerV4, from *net.UDPAddr, fromID enode.I //ZXL n := wrapNode(enode.NewV4(h.senderKey, from.IP, int(req.From.TCP), from.Port)) if time.Since(t.db.LastPongReceived(n.ID(), from.IP)) > bondExpiration { + log.Info("ZXL: sendPing from bondExpiration") t.sendPing(fromID, from, func() { t.tab.addVerifiedNode(n) }) @@ -792,6 +797,11 @@ func (t *UDPv4) verifyENRRequest(h *packetHandlerV4, from *net.UDPAddr, fromID e } func (t *UDPv4) handleENRRequest(h *packetHandlerV4, from *net.UDPAddr, fromID enode.ID, mac []byte) { + log.Info("handle ENR reqeust", "from", from, "fromID", fromID) + record := t.localNode.Node().Record() + for i, p := range record.GetPairs() { + log.Info("ENR pairs", "index", i, "key", p.GetPairKey(), "value", p.GetPairValue()) + } t.send(from, fromID, &v4wire.ENRResponse{ ReplyTok: mac, Record: *t.localNode.Node().Record(), diff --git a/p2p/enr/enr.go b/p2p/enr/enr.go index 2b093b2f1a..697e942639 100644 --- a/p2p/enr/enr.go +++ b/p2p/enr/enr.go @@ -90,12 +90,24 @@ type Record struct { pairs []pair // sorted list of all key/value pairs } +func (r *Record) GetPairs() []pair { + return r.pairs +} + // pair is a key/value pair in a record. type pair struct { k string v rlp.RawValue } +func (r *pair) GetPairKey() string { + return r.k +} + +func (r *pair) GetPairValue() rlp.RawValue { + return r.v +} + // Size returns the encoded size of the record. func (r *Record) Size() uint64 { if r.raw != nil {