-
Notifications
You must be signed in to change notification settings - Fork 0
/
gossip.go
112 lines (94 loc) · 2.71 KB
/
gossip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package p2p
import (
"context"
"go.uber.org/multierr"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/gridfoundation/furyint/log"
)
// GossipMessage represents message gossiped via P2P network (e.g. transaction, Block etc).
type GossipMessage struct {
Data []byte
From peer.ID
}
// GossiperOption sets optional parameters of Gossiper.
type GossiperOption func(*Gossiper) error
// WithValidator options registers topic validator for Gossiper.
func WithValidator(validator GossipValidator) GossiperOption {
return func(g *Gossiper) error {
return g.ps.RegisterTopicValidator(g.topic.String(), wrapValidator(g, validator))
}
}
// Gossiper is an abstraction of P2P publish subscribe mechanism.
type Gossiper struct {
ownID peer.ID
ps *pubsub.PubSub
topic *pubsub.Topic
sub *pubsub.Subscription
logger log.Logger
}
// NewGossiper creates new, ready to use instance of Gossiper.
//
// Returned Gossiper object can be used for sending (Publishing) and receiving messages in topic identified by topicStr.
func NewGossiper(host host.Host, ps *pubsub.PubSub, topicStr string, logger log.Logger, options ...GossiperOption) (*Gossiper, error) {
topic, err := ps.Join(topicStr)
if err != nil {
return nil, err
}
subscription, err := topic.Subscribe()
if err != nil {
return nil, err
}
g := &Gossiper{
ownID: host.ID(),
ps: ps,
topic: topic,
sub: subscription,
logger: logger,
}
for _, option := range options {
err := option(g)
if err != nil {
return nil, err
}
}
return g, nil
}
// Close is used to disconnect from topic and free resources used by Gossiper.
func (g *Gossiper) Close() error {
err := g.ps.UnregisterTopicValidator(g.topic.String())
g.sub.Cancel()
return multierr.Combine(
err,
g.topic.Close(),
)
}
// Publish publishes data to gossip topic.
func (g *Gossiper) Publish(ctx context.Context, data []byte) error {
return g.topic.Publish(ctx, data)
}
// ProcessMessages waits for messages published in the topic and execute handler.
func (g *Gossiper) ProcessMessages(ctx context.Context) {
for {
_, err := g.sub.Next(ctx)
if err != nil {
g.logger.Error("failed to read message", "error", err)
return
}
// Logic is handled in validator
}
}
func wrapValidator(gossiper *Gossiper, validator GossipValidator) pubsub.Validator {
return func(_ context.Context, _ peer.ID, msg *pubsub.Message) bool {
// Make sure we don't process our own messages.
// In this case we'll want to return true but not to actually handle the message.
if msg.GetFrom() == gossiper.ownID {
return true
}
return validator(&GossipMessage{
Data: msg.Data,
From: msg.GetFrom(),
})
}
}