github.com/ethereum-optimism/optimism@v1.7.2/op-node/p2p/notifications.go (about) 1 package p2p 2 3 import ( 4 "github.com/libp2p/go-libp2p/core/network" 5 ma "github.com/multiformats/go-multiaddr" 6 7 "github.com/ethereum-optimism/optimism/op-node/metrics" 8 9 "github.com/ethereum/go-ethereum/log" 10 ) 11 12 type NotificationsMetricer interface { 13 IncPeerCount() 14 DecPeerCount() 15 IncStreamCount() 16 DecStreamCount() 17 } 18 19 type notifications struct { 20 log log.Logger 21 m NotificationsMetricer 22 } 23 24 func (notif *notifications) Listen(n network.Network, a ma.Multiaddr) { 25 notif.log.Info("started listening network address", "addr", a) 26 } 27 func (notif *notifications) ListenClose(n network.Network, a ma.Multiaddr) { 28 notif.log.Info("stopped listening network address", "addr", a) 29 } 30 func (notif *notifications) Connected(n network.Network, v network.Conn) { 31 notif.m.IncPeerCount() 32 notif.log.Info("connected to peer", "peer", v.RemotePeer(), "addr", v.RemoteMultiaddr()) 33 } 34 func (notif *notifications) Disconnected(n network.Network, v network.Conn) { 35 notif.m.DecPeerCount() 36 notif.log.Info("disconnected from peer", "peer", v.RemotePeer(), "addr", v.RemoteMultiaddr()) 37 } 38 func (notif *notifications) OpenedStream(n network.Network, v network.Stream) { 39 notif.m.IncStreamCount() 40 c := v.Conn() 41 notif.log.Trace("opened stream", "protocol", v.Protocol(), "peer", c.RemotePeer(), "addr", c.RemoteMultiaddr()) 42 } 43 func (notif *notifications) ClosedStream(n network.Network, v network.Stream) { 44 notif.m.DecStreamCount() 45 c := v.Conn() 46 notif.log.Trace("opened stream", "protocol", v.Protocol(), "peer", c.RemotePeer(), "addr", c.RemoteMultiaddr()) 47 } 48 49 func NewNetworkNotifier(log log.Logger, m metrics.Metricer) network.Notifiee { 50 if m == nil { 51 m = metrics.NoopMetrics 52 } 53 return ¬ifications{log: log, m: m} 54 }