p2p/simulations: Add server id to logs

To support debugging in-memory network simulations when
multiple peers are logging
This commit is contained in:
zelig 2017-09-18 13:51:46 +02:00 committed by Anton Evangelatov
parent f79f35dc1a
commit 72535b00b1
6 changed files with 51 additions and 28 deletions

View file

@ -135,6 +135,9 @@ type Config struct {
// *WARNING* Only set this if the node is running in a trusted network, exposing // *WARNING* Only set this if the node is running in a trusted network, exposing
// private APIs to untrusted users is a major security risk. // private APIs to untrusted users is a major security risk.
WSExposeAll bool `toml:",omitempty"` WSExposeAll bool `toml:",omitempty"`
// Logger is a custom logger to use with the p2p.Server.
Logger log.Logger
} }
// IPCEndpoint resolves an IPC endpoint based on a configured value, taking into // IPCEndpoint resolves an IPC endpoint based on a configured value, taking into

View file

@ -69,6 +69,8 @@ type Node struct {
stop chan struct{} // Channel to wait for termination notifications stop chan struct{} // Channel to wait for termination notifications
lock sync.RWMutex lock sync.RWMutex
log log.Logger
} }
// New creates a new P2P node, ready for protocol registration. // New creates a new P2P node, ready for protocol registration.
@ -101,6 +103,9 @@ func New(conf *Config) (*Node, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if conf.Logger == nil {
conf.Logger = log.New()
}
// Note: any interaction with Config that would create/touch files // Note: any interaction with Config that would create/touch files
// in the data directory or instance directory is delayed until Start. // in the data directory or instance directory is delayed until Start.
return &Node{ return &Node{
@ -112,6 +117,7 @@ func New(conf *Config) (*Node, error) {
httpEndpoint: conf.HTTPEndpoint(), httpEndpoint: conf.HTTPEndpoint(),
wsEndpoint: conf.WSEndpoint(), wsEndpoint: conf.WSEndpoint(),
eventmux: new(event.TypeMux), eventmux: new(event.TypeMux),
log: conf.Logger,
}, nil }, nil
} }
@ -146,6 +152,7 @@ func (n *Node) Start() error {
n.serverConfig = n.config.P2P n.serverConfig = n.config.P2P
n.serverConfig.PrivateKey = n.config.NodeKey() n.serverConfig.PrivateKey = n.config.NodeKey()
n.serverConfig.Name = n.config.NodeName() n.serverConfig.Name = n.config.NodeName()
n.serverConfig.Logger = n.log
if n.serverConfig.StaticNodes == nil { if n.serverConfig.StaticNodes == nil {
n.serverConfig.StaticNodes = n.config.StaticNodes() n.serverConfig.StaticNodes = n.config.StaticNodes()
} }
@ -156,7 +163,7 @@ func (n *Node) Start() error {
n.serverConfig.NodeDatabase = n.config.NodeDB() n.serverConfig.NodeDatabase = n.config.NodeDB()
} }
running := &p2p.Server{Config: n.serverConfig} running := &p2p.Server{Config: n.serverConfig}
log.Info("Starting peer-to-peer node", "instance", n.serverConfig.Name) n.log.Info("Starting peer-to-peer node", "instance", n.serverConfig.Name)
// Otherwise copy and specialize the P2P configuration // Otherwise copy and specialize the P2P configuration
services := make(map[reflect.Type]Service) services := make(map[reflect.Type]Service)
@ -280,7 +287,7 @@ func (n *Node) startInProc(apis []rpc.API) error {
if err := handler.RegisterName(api.Namespace, api.Service); err != nil { if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
return err return err
} }
log.Debug(fmt.Sprintf("InProc registered %T under '%s'", api.Service, api.Namespace)) n.log.Debug(fmt.Sprintf("InProc registered %T under '%s'", api.Service, api.Namespace))
} }
n.inprocHandler = handler n.inprocHandler = handler
return nil return nil
@ -306,7 +313,7 @@ func (n *Node) startIPC(apis []rpc.API) error {
if err := handler.RegisterName(api.Namespace, api.Service); err != nil { if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
return err return err
} }
log.Debug(fmt.Sprintf("IPC registered %T under '%s'", api.Service, api.Namespace)) n.log.Debug(fmt.Sprintf("IPC registered %T under '%s'", api.Service, api.Namespace))
} }
// All APIs registered, start the IPC listener // All APIs registered, start the IPC listener
var ( var (
@ -317,7 +324,7 @@ func (n *Node) startIPC(apis []rpc.API) error {
return err return err
} }
go func() { go func() {
log.Info(fmt.Sprintf("IPC endpoint opened: %s", n.ipcEndpoint)) n.log.Info(fmt.Sprintf("IPC endpoint opened: %s", n.ipcEndpoint))
for { for {
conn, err := listener.Accept() conn, err := listener.Accept()
@ -330,7 +337,7 @@ func (n *Node) startIPC(apis []rpc.API) error {
return return
} }
// Not closed, just some error; report and continue // Not closed, just some error; report and continue
log.Error(fmt.Sprintf("IPC accept failed: %v", err)) n.log.Error(fmt.Sprintf("IPC accept failed: %v", err))
continue continue
} }
go handler.ServeCodec(rpc.NewJSONCodec(conn), rpc.OptionMethodInvocation|rpc.OptionSubscriptions) go handler.ServeCodec(rpc.NewJSONCodec(conn), rpc.OptionMethodInvocation|rpc.OptionSubscriptions)
@ -349,7 +356,7 @@ func (n *Node) stopIPC() {
n.ipcListener.Close() n.ipcListener.Close()
n.ipcListener = nil n.ipcListener = nil
log.Info(fmt.Sprintf("IPC endpoint closed: %s", n.ipcEndpoint)) n.log.Info(fmt.Sprintf("IPC endpoint closed: %s", n.ipcEndpoint))
} }
if n.ipcHandler != nil { if n.ipcHandler != nil {
n.ipcHandler.Stop() n.ipcHandler.Stop()
@ -375,7 +382,7 @@ func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors
if err := handler.RegisterName(api.Namespace, api.Service); err != nil { if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
return err return err
} }
log.Debug(fmt.Sprintf("HTTP registered %T under '%s'", api.Service, api.Namespace)) n.log.Debug(fmt.Sprintf("HTTP registered %T under '%s'", api.Service, api.Namespace))
} }
} }
// All APIs registered, start the HTTP listener // All APIs registered, start the HTTP listener
@ -387,7 +394,7 @@ func (n *Node) startHTTP(endpoint string, apis []rpc.API, modules []string, cors
return err return err
} }
go rpc.NewHTTPServer(cors, handler).Serve(listener) go rpc.NewHTTPServer(cors, handler).Serve(listener)
log.Info(fmt.Sprintf("HTTP endpoint opened: http://%s", endpoint)) n.log.Info(fmt.Sprintf("HTTP endpoint opened: http://%s", endpoint))
// All listeners booted successfully // All listeners booted successfully
n.httpEndpoint = endpoint n.httpEndpoint = endpoint
@ -403,7 +410,7 @@ func (n *Node) stopHTTP() {
n.httpListener.Close() n.httpListener.Close()
n.httpListener = nil n.httpListener = nil
log.Info(fmt.Sprintf("HTTP endpoint closed: http://%s", n.httpEndpoint)) n.log.Info(fmt.Sprintf("HTTP endpoint closed: http://%s", n.httpEndpoint))
} }
if n.httpHandler != nil { if n.httpHandler != nil {
n.httpHandler.Stop() n.httpHandler.Stop()
@ -429,7 +436,7 @@ func (n *Node) startWS(endpoint string, apis []rpc.API, modules []string, wsOrig
if err := handler.RegisterName(api.Namespace, api.Service); err != nil { if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
return err return err
} }
log.Debug(fmt.Sprintf("WebSocket registered %T under '%s'", api.Service, api.Namespace)) n.log.Debug(fmt.Sprintf("WebSocket registered %T under '%s'", api.Service, api.Namespace))
} }
} }
// All APIs registered, start the HTTP listener // All APIs registered, start the HTTP listener
@ -441,7 +448,7 @@ func (n *Node) startWS(endpoint string, apis []rpc.API, modules []string, wsOrig
return err return err
} }
go rpc.NewWSServer(wsOrigins, handler).Serve(listener) go rpc.NewWSServer(wsOrigins, handler).Serve(listener)
log.Info(fmt.Sprintf("WebSocket endpoint opened: ws://%s", listener.Addr())) n.log.Info(fmt.Sprintf("WebSocket endpoint opened: ws://%s", listener.Addr()))
// All listeners booted successfully // All listeners booted successfully
n.wsEndpoint = endpoint n.wsEndpoint = endpoint
@ -457,7 +464,7 @@ func (n *Node) stopWS() {
n.wsListener.Close() n.wsListener.Close()
n.wsListener = nil n.wsListener = nil
log.Info(fmt.Sprintf("WebSocket endpoint closed: ws://%s", n.wsEndpoint)) n.log.Info(fmt.Sprintf("WebSocket endpoint closed: ws://%s", n.wsEndpoint))
} }
if n.wsHandler != nil { if n.wsHandler != nil {
n.wsHandler.Stop() n.wsHandler.Stop()
@ -496,7 +503,7 @@ func (n *Node) Stop() error {
// Release instance directory lock. // Release instance directory lock.
if n.instanceDirLock != nil { if n.instanceDirLock != nil {
if err := n.instanceDirLock.Release(); err != nil { if err := n.instanceDirLock.Release(); err != nil {
log.Error("Can't release datadir lock", "err", err) n.log.Error("Can't release datadir lock", "err", err)
} }
n.instanceDirLock = nil n.instanceDirLock = nil
} }

View file

@ -139,6 +139,9 @@ type Config struct {
// If EnableMsgEvents is set then the server will emit PeerEvents // If EnableMsgEvents is set then the server will emit PeerEvents
// whenever a message is sent to or received from a peer // whenever a message is sent to or received from a peer
EnableMsgEvents bool EnableMsgEvents bool
// Logger is a custom logger to use with the p2p.Server.
Logger log.Logger
} }
// Server manages all peer connections. // Server manages all peer connections.
@ -172,6 +175,7 @@ type Server struct {
delpeer chan peerDrop delpeer chan peerDrop
loopWG sync.WaitGroup // loop, listenLoop loopWG sync.WaitGroup // loop, listenLoop
peerFeed event.Feed peerFeed event.Feed
log log.Logger
} }
type peerOpFunc func(map[discover.NodeID]*Peer) type peerOpFunc func(map[discover.NodeID]*Peer)
@ -359,7 +363,11 @@ func (srv *Server) Start() (err error) {
return errors.New("server already running") return errors.New("server already running")
} }
srv.running = true srv.running = true
log.Info("Starting P2P networking") srv.log = srv.Config.Logger
if srv.log == nil {
srv.log = log.New()
}
srv.log.Info("Starting P2P networking")
// static fields // static fields
if srv.PrivateKey == nil { if srv.PrivateKey == nil {
@ -421,7 +429,7 @@ func (srv *Server) Start() (err error) {
} }
} }
if srv.NoDial && srv.ListenAddr == "" { if srv.NoDial && srv.ListenAddr == "" {
log.Warn("P2P server will be useless, neither dialing nor listening") srv.log.Warn("P2P server will be useless, neither dialing nor listening")
} }
srv.loopWG.Add(1) srv.loopWG.Add(1)
@ -489,7 +497,7 @@ func (srv *Server) run(dialstate dialer) {
i := 0 i := 0
for ; len(runningTasks) < maxActiveDialTasks && i < len(ts); i++ { for ; len(runningTasks) < maxActiveDialTasks && i < len(ts); i++ {
t := ts[i] t := ts[i]
log.Trace("New dial task", "task", t) srv.log.Trace("New dial task", "task", t)
go func() { t.Do(srv); taskdone <- t }() go func() { t.Do(srv); taskdone <- t }()
runningTasks = append(runningTasks, t) runningTasks = append(runningTasks, t)
} }
@ -517,13 +525,13 @@ running:
// This channel is used by AddPeer to add to the // This channel is used by AddPeer to add to the
// ephemeral static peer list. Add it to the dialer, // ephemeral static peer list. Add it to the dialer,
// it will keep the node connected. // it will keep the node connected.
log.Debug("Adding static node", "node", n) srv.log.Debug("Adding static node", "node", n)
dialstate.addStatic(n) dialstate.addStatic(n)
case n := <-srv.removestatic: case n := <-srv.removestatic:
// This channel is used by RemovePeer to send a // This channel is used by RemovePeer to send a
// disconnect request to a peer and begin the // disconnect request to a peer and begin the
// stop keeping the node connected // stop keeping the node connected
log.Debug("Removing static node", "node", n) srv.log.Debug("Removing static node", "node", n)
dialstate.removeStatic(n) dialstate.removeStatic(n)
if p, ok := peers[n.ID]; ok { if p, ok := peers[n.ID]; ok {
p.Disconnect(DiscRequested) p.Disconnect(DiscRequested)
@ -536,7 +544,7 @@ running:
// A task got done. Tell dialstate about it so it // A task got done. Tell dialstate about it so it
// can update its state and remove it from the active // can update its state and remove it from the active
// tasks list. // tasks list.
log.Trace("Dial task done", "task", t) srv.log.Trace("Dial task done", "task", t)
dialstate.taskDone(t, time.Now()) dialstate.taskDone(t, time.Now())
delTask(t) delTask(t)
case c := <-srv.posthandshake: case c := <-srv.posthandshake:
@ -565,7 +573,7 @@ running:
p.events = &srv.peerFeed p.events = &srv.peerFeed
} }
name := truncateName(c.name) name := truncateName(c.name)
log.Debug("Adding p2p peer", "id", c.id, "name", name, "addr", c.fd.RemoteAddr(), "peers", len(peers)+1) srv.log.Debug("Adding p2p peer", "name", name, "addr", c.fd.RemoteAddr(), "peers", len(peers)+1)
peers[c.id] = p peers[c.id] = p
go srv.runPeer(p) go srv.runPeer(p)
} }
@ -585,7 +593,7 @@ running:
} }
} }
log.Trace("P2P networking is spinning down") srv.log.Trace("P2P networking is spinning down")
// Terminate discovery. If there is a running lookup it will terminate soon. // Terminate discovery. If there is a running lookup it will terminate soon.
if srv.ntab != nil { if srv.ntab != nil {
@ -639,7 +647,7 @@ type tempError interface {
// inbound connections. // inbound connections.
func (srv *Server) listenLoop() { func (srv *Server) listenLoop() {
defer srv.loopWG.Done() defer srv.loopWG.Done()
log.Info("RLPx listener up", "self", srv.makeSelf(srv.listener, srv.ntab)) srv.log.Info("RLPx listener up", "self", srv.makeSelf(srv.listener, srv.ntab))
// This channel acts as a semaphore limiting // This channel acts as a semaphore limiting
// active inbound connections that are lingering pre-handshake. // active inbound connections that are lingering pre-handshake.
@ -664,10 +672,10 @@ func (srv *Server) listenLoop() {
for { for {
fd, err = srv.listener.Accept() fd, err = srv.listener.Accept()
if tempErr, ok := err.(tempError); ok && tempErr.Temporary() { if tempErr, ok := err.(tempError); ok && tempErr.Temporary() {
log.Debug("Temporary read error", "err", err) srv.log.Debug("Temporary read error", "err", err)
continue continue
} else if err != nil { } else if err != nil {
log.Debug("Read error", "err", err) srv.log.Debug("Read error", "err", err)
return return
} }
break break
@ -676,7 +684,7 @@ func (srv *Server) listenLoop() {
// Reject connections that do not match NetRestrict. // Reject connections that do not match NetRestrict.
if srv.NetRestrict != nil { if srv.NetRestrict != nil {
if tcp, ok := fd.RemoteAddr().(*net.TCPAddr); ok && !srv.NetRestrict.Contains(tcp.IP) { if tcp, ok := fd.RemoteAddr().(*net.TCPAddr); ok && !srv.NetRestrict.Contains(tcp.IP) {
log.Debug("Rejected conn (not whitelisted in NetRestrict)", "addr", fd.RemoteAddr()) srv.log.Debug("Rejected conn (not whitelisted in NetRestrict)", "addr", fd.RemoteAddr())
fd.Close() fd.Close()
slots <- struct{}{} slots <- struct{}{}
continue continue
@ -684,7 +692,7 @@ func (srv *Server) listenLoop() {
} }
fd = newMeteredConn(fd, true) fd = newMeteredConn(fd, true)
log.Trace("Accepted connection", "addr", fd.RemoteAddr()) srv.log.Trace("Accepted connection", "addr", fd.RemoteAddr())
// Spawn the handler. It will give the slot back when the connection // Spawn the handler. It will give the slot back when the connection
// has been established. // has been established.
@ -711,7 +719,7 @@ func (srv *Server) SetupConn(fd net.Conn, flags connFlag, dialDest *discover.Nod
// Run the encryption handshake. // Run the encryption handshake.
var err error var err error
if c.id, err = c.doEncHandshake(srv.PrivateKey, dialDest); err != nil { if c.id, err = c.doEncHandshake(srv.PrivateKey, dialDest); err != nil {
log.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err) srv.log.Trace("Failed RLPx handshake", "addr", c.fd.RemoteAddr(), "conn", c.flags, "err", err)
c.close(err) c.close(err)
return return
} }

View file

@ -28,6 +28,7 @@ import (
"strings" "strings"
"github.com/docker/docker/pkg/reexec" "github.com/docker/docker/pkg/reexec"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/discover"
) )
@ -94,6 +95,7 @@ func (d *DockerAdapter) NewNode(config *NodeConfig) (Node, error) {
conf.Stack.P2P.NoDiscovery = true conf.Stack.P2P.NoDiscovery = true
conf.Stack.P2P.NAT = nil conf.Stack.P2P.NAT = nil
conf.Stack.NoUSB = true conf.Stack.NoUSB = true
conf.Stack.Logger = log.New("node.id", config.ID.String())
node := &DockerNode{ node := &DockerNode{
ExecNode: ExecNode{ ExecNode: ExecNode{

View file

@ -104,6 +104,7 @@ func (e *ExecAdapter) NewNode(config *NodeConfig) (Node, error) {
conf.Stack.P2P.NoDiscovery = true conf.Stack.P2P.NoDiscovery = true
conf.Stack.P2P.NAT = nil conf.Stack.P2P.NAT = nil
conf.Stack.NoUSB = true conf.Stack.NoUSB = true
conf.Stack.Logger = log.New("node.id", config.ID.String())
// listen on a random localhost port (we'll get the actual port after // listen on a random localhost port (we'll get the actual port after
// starting the node through the RPC admin.nodeInfo method) // starting the node through the RPC admin.nodeInfo method)

View file

@ -24,6 +24,7 @@ import (
"sync" "sync"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/discover"
@ -82,7 +83,8 @@ func (s *SimAdapter) NewNode(config *NodeConfig) (Node, error) {
Dialer: s, Dialer: s,
EnableMsgEvents: true, EnableMsgEvents: true,
}, },
NoUSB: true, NoUSB: true,
Logger: log.New("node.id", id.String()),
}) })
if err != nil { if err != nil {
return nil, err return nil, err