Skip to content

Commit

Permalink
test: my own debug msg
Browse files Browse the repository at this point in the history
  • Loading branch information
krish-nr committed May 15, 2024
1 parent 093a6ce commit 4138aeb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 20 deletions.
53 changes: 36 additions & 17 deletions cmd/bootnode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,51 @@ import (
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/p2p/netutil"
"golang.org/x/exp/slog"
"io"
)

func main() {
var (
listenAddr = flag.String("addr", ":30301", "listen address")
genKey = flag.String("genkey", "", "generate a node key")
writeAddr = flag.Bool("writeaddress", false, "write out the node's public key and quit")
nodeKeyFile = flag.String("nodekey", "", "private key filename")
nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)")
natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|pmp:<IP>|extip:<IP>)")
netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)")
runv5 = flag.Bool("v5", true, "run a v5 topic discovery bootnode")
runv4 = flag.Bool("v4", false, "run a v4 topic discovery bootnode")
verbosity = flag.Int("verbosity", 3, "log verbosity (0-5)")
vmodule = flag.String("vmodule", "", "log verbosity pattern")
listenAddr = flag.String("addr", ":30301", "listen address")
genKey = flag.String("genkey", "", "generate a node key")
writeAddr = flag.Bool("writeaddress", false, "write out the node's public key and quit")
nodeKeyFile = flag.String("nodekey", "", "private key filename")
nodeKeyHex = flag.String("nodekeyhex", "", "private key as hex (for testing)")
natdesc = flag.String("nat", "none", "port mapping mechanism (any|none|upnp|pmp|pmp:<IP>|extip:<IP>)")
netrestrict = flag.String("netrestrict", "", "restrict network communication to the given IP networks (CIDR masks)")
runv5 = flag.Bool("v5", true, "run a v5 topic discovery bootnode")
runv4 = flag.Bool("v4", false, "run a v4 topic discovery bootnode")
verbosity = flag.Int("verbosity", 3, "log verbosity (0-5)")
vmodule = flag.String("vmodule", "", "log verbosity pattern")
network = flag.String("network", "", "testnet/mainnet")
staticP2pNodes = flag.String("staticnodes", "", "static p2p nodes for discovery")
nodeKey *ecdsa.PrivateKey
err error
nodeKey *ecdsa.PrivateKey
err error
)

var staticV4Nodes []v4wire.Node

flag.Parse()

glogger := log.NewGlogHandler(log.NewTerminalHandler(os.Stderr, false))
//set log
var (
handler slog.Handler
glogger *log.GlogHandler
terminalOutput = io.Writer(os.Stderr)
output io.Writer
logOutputFile io.WriteCloser
)

logFile := "/Users/zhaoxueliang/opbnb_project_pr/geth_bootnode.log"
if logOutputFile, err = os.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644); err != nil {
utils.Fatalf("err: %v", err)
}
output = io.MultiWriter(logOutputFile, terminalOutput)

handler = log.LogfmtHandler(output)
glogger = log.NewGlogHandler(handler)

slogVerbosity := log.FromLegacyLevel(*verbosity)
glogger.Verbosity(slogVerbosity)
glogger.Vmodule(*vmodule)
Expand Down Expand Up @@ -168,9 +187,9 @@ func main() {
// Start discovery services.
if *runv4 {
cfg := discover.Config{
PrivateKey: nodeKey,
NetRestrict: restrictList,
Unhandled: unhandled,
PrivateKey: nodeKey,
NetRestrict: restrictList,
Unhandled: unhandled,
StaticV4Nodes: staticV4Nodes,
}
_, err := discover.ListenV4(conn, ln, cfg)
Expand Down
4 changes: 4 additions & 0 deletions p2p/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,10 @@ loop:
slots := d.freeDialSlots()
slots -= d.startStaticDials(slots)
if slots > 0 {
log.Info("ZXL: nodesCh yes!")
nodesCh = d.nodesIn
} else {
log.Info("ZXL: nodesCh no!")
nodesCh = nil
}
d.rearmHistoryTimer()
Expand All @@ -244,6 +246,7 @@ loop:
if err := d.checkDial(node); err != nil {
d.log.Trace("Discarding dial candidate", "id", node.ID(), "ip", node.IP(), "reason", err)
} else {
log.Info("ZXL: start dial from nodesCh", "node", node.IP())
d.startDial(newDialTask(node, dynDialedConn))
}

Expand Down Expand Up @@ -401,6 +404,7 @@ func (d *dialScheduler) startStaticDials(n int) (started int) {
for started = 0; started < n && len(d.staticPool) > 0; started++ {
idx := d.rand.Intn(len(d.staticPool))
task := d.staticPool[idx]
log.Info("ZXL: static dial")
d.startDial(task)
d.removeFromStaticPool(idx)
}
Expand Down
16 changes: 15 additions & 1 deletion p2p/discover/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,14 @@ loop:
case <-refresh.C:
tab.seedRand()
if refreshDone == nil {
log.Info("ZXL do refresh")
refreshDone = make(chan struct{})
go tab.doRefresh(refreshDone)
}
case req := <-tab.refreshReq:
waiting = append(waiting, req)
if refreshDone == nil {
log.Info("ZXL do refresh")
refreshDone = make(chan struct{})
go tab.doRefresh(refreshDone)
}
Expand All @@ -279,11 +281,13 @@ loop:
refresh.Reset(tab.nextRefreshTime())
case <-revalidate.C:
revalidateDone = make(chan struct{})
log.Info("ZXL do revalidate")
go tab.doRevalidate(revalidateDone)
case <-revalidateDone:
revalidate.Reset(tab.nextRevalidateTime())
revalidateDone = nil
case <-copyNodes.C:
log.Info("ZXL do copyNodes")
go tab.copyLiveNodes()
case <-tab.closeReq:
break loop
Expand Down Expand Up @@ -350,6 +354,7 @@ func (tab *Table) doRevalidate(done chan<- struct{}) {
return
}

log.Info("ping from do revalidate")
// Ping the selected node and wait for a pong.
remoteSeq, err := tab.net.ping(unwrapNode(last))

Expand All @@ -372,6 +377,8 @@ func (tab *Table) doRevalidate(done chan<- struct{}) {
tab.log.Debug("Revalidated node", "b", bi, "id", last.ID(), "checks", last.livenessChecks)
tab.bumpInBucket(b, last)
return
} else {
log.Error("ZXL tab.net.ping", "err", err)
}
// No reply received, pick a replacement or delete the node if there aren't
// any replacements.
Expand Down Expand Up @@ -515,20 +522,24 @@ func (tab *Table) getAllNodes() []enode.Node {
// appendLiveNodes adds nodes at the given distance to the result slice.
func (tab *Table) appendLiveNodes(dist uint, result []*enode.Node) []*enode.Node {
if dist > 256 {
log.Info("ZXL: return nil", "dist", dist)
return result
}
if dist == 0 {
log.Info("ZXL: dist 0", "dist", dist)
return append(result, tab.self())
}

tab.mutex.Lock()
defer tab.mutex.Unlock()
for _, n := range tab.bucketAtDistance(int(dist)).entries {
for i, n := range tab.bucketAtDistance(int(dist)).entries {
log.Info("ZXL bucketAtDistance", "index", i, "nodesIp", n.IP().String(), "tcpport", n.TCP(), "udoport", n.UDP(), "checkcount", n.livenessChecks)
if n.livenessChecks >= 1 {
node := n.Node // avoid handing out pointer to struct field
result = append(result, &node)
}
}
log.Info("ZXL appendLiveNodes result", "livenodes num", len(result))
return result
}

Expand Down Expand Up @@ -579,6 +590,7 @@ func (tab *Table) addSeenNode(n *node) {
b := tab.bucket(n.ID())
if contains(b.entries, n.ID()) {
// Already in bucket, don't add.
log.Info("ZXL: Already in bucket", "nodeid", n.ID(), "nodeip", n.IP().String())
return
}
if len(b.entries) >= bucketSize {
Expand All @@ -596,6 +608,8 @@ func (tab *Table) addSeenNode(n *node) {
b.replacements = deleteNode(b.replacements, n)
n.addedAt = time.Now()

log.Info("ZXL: add node in bucket", "nodeid", n.ID(), "nodeip", n.IP().String())

if tab.nodeAddedHook != nil {
tab.nodeAddedHook(b, n)
}
Expand Down
7 changes: 5 additions & 2 deletions p2p/discover/v5_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func (t *UDPv5) ping(n *enode.Node) (uint64, error) {

// RequestENR requests n's record.
func (t *UDPv5) RequestENR(n *enode.Node) (*enode.Node, error) {
log.Info("ZXL", "findnode from requestENR")
log.Info("ZXL: findnode from requestENR")
nodes, err := t.findnode(n, []uint{0})
if err != nil {
return nil, err
Expand Down Expand Up @@ -502,6 +502,7 @@ func (t *UDPv5) initCall(c *callV5, responseType byte, packet v5wire.Packet) {
}
}

// ZXL 看下啥意思
// callDone tells dispatch that the active call is done.
func (t *UDPv5) callDone(c *callV5) {
// This needs a loop because further responses may be incoming until the
Expand Down Expand Up @@ -877,7 +878,8 @@ func (t *UDPv5) collectTableNodes(rip net.IP, distances []uint, limit int) []*en
for _, n := range t.tab.appendLiveNodes(dist, bn[:0]) {
// Apply some pre-checks to avoid sending invalid nodes.
// Note liveness is checked by appendLiveNodes.
if netutil.CheckRelayIP(rip, n.IP()) != nil {
if err := netutil.CheckRelayIP(rip, n.IP()); err != nil {
log.Error("ZXL", "err", err)
continue
}
nodes = append(nodes, n)
Expand All @@ -892,6 +894,7 @@ func (t *UDPv5) collectTableNodes(rip net.IP, distances []uint, limit int) []*en
// packNodes creates NODES response packets for the given node list.
func packNodes(reqid []byte, nodes []*enode.Node) []*v5wire.Nodes {
if len(nodes) == 0 {
log.Info("ZXL packNodes num 0")
return []*v5wire.Nodes{{ReqID: reqid, RespCount: 1}}
}

Expand Down

0 comments on commit 4138aeb

Please sign in to comment.