notifications.go 1.66 KB
Newer Older
1 2 3
package p2p

import (
4
	"github.com/libp2p/go-libp2p/core/network"
5
	ma "github.com/multiformats/go-multiaddr"
6

7 8
	"github.com/ethereum-optimism/optimism/op-node/metrics"

9
	"github.com/ethereum/go-ethereum/log"
10 11
)

Matthew Slipper's avatar
Matthew Slipper committed
12 13 14 15 16 17
type NotificationsMetricer interface {
	IncPeerCount()
	DecPeerCount()
	IncStreamCount()
	DecStreamCount()
}
18 19 20

type notifications struct {
	log log.Logger
Matthew Slipper's avatar
Matthew Slipper committed
21
	m   NotificationsMetricer
22 23 24 25 26 27 28 29 30
}

func (notif *notifications) Listen(n network.Network, a ma.Multiaddr) {
	notif.log.Info("started listening network address", "addr", a)
}
func (notif *notifications) ListenClose(n network.Network, a ma.Multiaddr) {
	notif.log.Info("stopped listening network address", "addr", a)
}
func (notif *notifications) Connected(n network.Network, v network.Conn) {
31
	notif.m.IncPeerCount()
32 33 34
	notif.log.Info("connected to peer", "peer", v.RemotePeer(), "addr", v.RemoteMultiaddr())
}
func (notif *notifications) Disconnected(n network.Network, v network.Conn) {
35
	notif.m.DecPeerCount()
36 37 38
	notif.log.Info("disconnected from peer", "peer", v.RemotePeer(), "addr", v.RemoteMultiaddr())
}
func (notif *notifications) OpenedStream(n network.Network, v network.Stream) {
39
	notif.m.IncStreamCount()
40 41 42 43
	c := v.Conn()
	notif.log.Trace("opened stream", "protocol", v.Protocol(), "peer", c.RemotePeer(), "addr", c.RemoteMultiaddr())
}
func (notif *notifications) ClosedStream(n network.Network, v network.Stream) {
44
	notif.m.DecStreamCount()
45 46 47 48
	c := v.Conn()
	notif.log.Trace("opened stream", "protocol", v.Protocol(), "peer", c.RemotePeer(), "addr", c.RemoteMultiaddr())
}

49 50 51 52 53
func NewNetworkNotifier(log log.Logger, m metrics.Metricer) network.Notifiee {
	if m == nil {
		m = metrics.NoopMetrics
	}
	return &notifications{log: log, m: m}
54
}