Skip to content

Commit

Permalink
debug: add more logs
Browse files Browse the repository at this point in the history
  • Loading branch information
krish-nr committed Jan 12, 2024
1 parent ca13956 commit 22fa6fd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
37 changes: 34 additions & 3 deletions p2p/discover/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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)

}
}

Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions p2p/discover/v4_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -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(),
Expand Down
12 changes: 12 additions & 0 deletions p2p/enr/enr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 22fa6fd

Please sign in to comment.