2021-10-06 15:34:39 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2021-10-16 21:50:49 +00:00
|
|
|
"context"
|
2021-10-06 15:34:39 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2021-10-16 21:50:49 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/metrics"
|
2021-10-06 15:34:39 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/filter"
|
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/lightpush"
|
2021-10-08 13:50:56 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/relay"
|
2021-10-06 15:34:39 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/store"
|
2021-10-16 21:50:49 +00:00
|
|
|
"go.opencensus.io/stats"
|
2022-01-18 18:17:06 +00:00
|
|
|
"go.uber.org/zap"
|
2021-10-06 15:34:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// A map of peer IDs to supported protocols
|
|
|
|
type PeerStats map[peer.ID][]string
|
|
|
|
|
|
|
|
type ConnStatus struct {
|
|
|
|
IsOnline bool
|
|
|
|
HasHistory bool
|
|
|
|
Peers PeerStats
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConnectionNotifier struct {
|
|
|
|
h host.Host
|
2021-10-16 21:50:49 +00:00
|
|
|
ctx context.Context
|
2022-01-18 18:17:06 +00:00
|
|
|
log *zap.SugaredLogger
|
2021-10-06 15:34:39 +00:00
|
|
|
DisconnectChan chan peer.ID
|
|
|
|
quit chan struct{}
|
|
|
|
}
|
|
|
|
|
2022-01-18 18:17:06 +00:00
|
|
|
func NewConnectionNotifier(ctx context.Context, h host.Host, log *zap.SugaredLogger) ConnectionNotifier {
|
2021-10-06 15:34:39 +00:00
|
|
|
return ConnectionNotifier{
|
|
|
|
h: h,
|
2021-10-16 21:50:49 +00:00
|
|
|
ctx: ctx,
|
2021-10-06 15:34:39 +00:00
|
|
|
DisconnectChan: make(chan peer.ID, 100),
|
|
|
|
quit: make(chan struct{}),
|
2022-01-18 18:17:06 +00:00
|
|
|
log: log,
|
2021-10-06 15:34:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c ConnectionNotifier) Listen(n network.Network, m ma.Multiaddr) {
|
|
|
|
// called when network starts listening on an addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c ConnectionNotifier) ListenClose(n network.Network, m ma.Multiaddr) {
|
|
|
|
// called when network stops listening on an addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c ConnectionNotifier) Connected(n network.Network, cc network.Conn) {
|
|
|
|
// called when a connection opened
|
2022-01-18 18:17:06 +00:00
|
|
|
c.log.Info(fmt.Sprintf("Peer %s connected", cc.RemotePeer()))
|
2021-10-16 21:50:49 +00:00
|
|
|
stats.Record(c.ctx, metrics.Peers.M(1))
|
2021-10-06 15:34:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c ConnectionNotifier) Disconnected(n network.Network, cc network.Conn) {
|
|
|
|
// called when a connection closed
|
2022-01-18 18:17:06 +00:00
|
|
|
c.log.Info(fmt.Sprintf("Peer %s disconnected", cc.RemotePeer()))
|
2021-10-16 21:50:49 +00:00
|
|
|
stats.Record(c.ctx, metrics.Peers.M(-1))
|
2021-10-06 15:34:39 +00:00
|
|
|
c.DisconnectChan <- cc.RemotePeer()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c ConnectionNotifier) OpenedStream(n network.Network, s network.Stream) {
|
|
|
|
// called when a stream opened
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c ConnectionNotifier) ClosedStream(n network.Network, s network.Stream) {
|
|
|
|
// called when a stream closed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c ConnectionNotifier) Close() {
|
|
|
|
close(c.quit)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WakuNode) sendConnStatus() {
|
|
|
|
isOnline, hasHistory := w.Status()
|
|
|
|
if w.connStatusChan != nil {
|
2021-11-10 13:36:51 +00:00
|
|
|
connStatus := ConnStatus{IsOnline: isOnline, HasHistory: hasHistory, Peers: w.PeerStats()}
|
2021-10-06 15:34:39 +00:00
|
|
|
w.connStatusChan <- connStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WakuNode) connectednessListener() {
|
2021-11-23 15:03:12 +00:00
|
|
|
defer w.wg.Done()
|
|
|
|
|
2021-10-06 15:34:39 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-w.quit:
|
|
|
|
return
|
2022-02-24 13:40:18 +00:00
|
|
|
case <-w.protocolEventSub.Out():
|
|
|
|
case <-w.identificationEventSub.Out():
|
2021-10-06 19:25:41 +00:00
|
|
|
case <-w.connectionNotif.DisconnectChan:
|
2021-10-06 15:34:39 +00:00
|
|
|
}
|
|
|
|
w.sendConnStatus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *WakuNode) Status() (isOnline bool, hasHistory bool) {
|
|
|
|
hasRelay := false
|
|
|
|
hasLightPush := false
|
|
|
|
hasStore := false
|
|
|
|
hasFilter := false
|
|
|
|
|
|
|
|
for _, peer := range w.host.Network().Peers() {
|
|
|
|
protocols, err := w.host.Peerstore().GetProtocols(peer)
|
|
|
|
if err != nil {
|
2022-01-18 18:17:06 +00:00
|
|
|
w.log.Warn(fmt.Errorf("could not read peer %s protocols", peer))
|
2021-10-06 15:34:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, protocol := range protocols {
|
2021-10-08 13:50:56 +00:00
|
|
|
if !hasRelay && protocol == string(relay.WakuRelayID_v200) {
|
2021-10-06 15:34:39 +00:00
|
|
|
hasRelay = true
|
|
|
|
}
|
|
|
|
if !hasLightPush && protocol == string(lightpush.LightPushID_v20beta1) {
|
|
|
|
hasLightPush = true
|
|
|
|
}
|
2022-02-23 15:06:47 +00:00
|
|
|
if !hasStore && protocol == string(store.StoreID_v20beta4) {
|
2021-10-06 15:34:39 +00:00
|
|
|
hasStore = true
|
|
|
|
}
|
|
|
|
if !hasFilter && protocol == string(filter.FilterID_v20beta1) {
|
|
|
|
hasFilter = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasStore {
|
|
|
|
hasHistory = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasRelay || hasLightPush && (hasStore || hasFilter) {
|
|
|
|
isOnline = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|