-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgossipSubController.go
234 lines (193 loc) · 6.42 KB
/
gossipSubController.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"context"
"fmt"
"io/ioutil"
"strings"
// "os"
"encoding/json"
"log"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p-core/peer"
pb "github.com/FlavScheidt/gossipGoSnt/proto"
// "google.golang.org/grpc"
)
const BufSize = 128
// Topic represents a subscription to a single PubSub topic. Messages
// can be published to the topic with validator.Publish, and received
// messages are pushed to the Messages channel.
type Topic struct {
// Messages is a channel of messages received from other peers in the chat room
Messages chan *Message
ctx context.Context
ps *pubsub.PubSub
topic *pubsub.Topic
sub *pubsub.Subscription
// validatorID peer.ID
self peer.ID
// validatorKey string
// ip string
name string
}
type Message struct {
Message []byte
Validator_Key string
Hash string
SenderID string
SenderName string
}
//Extracted from gossipsub-hardening
// type PubsubNode struct {
// cfg NodeConfig
// ctx context.Context
// // shutdown func()
// // runenv *runtime.RunEnv
// h host.Host
// ps *pubsub.PubSub
// lk sync.RWMutex
// // topics map[string]*topicState
// pubwg sync.WaitGroup
// }
type NodeConfig struct {
// topics to join when node starts
// Topics []TopicConfig
// whether we're a publisher or a lurker
// Publisher bool
// pubsub event tracer
Tracer pubsub.EventTracer
// Test instance identifier
// Seq int64
// How long to wait after connecting to bootstrap peers before publishing
// Warmup time.Duration
// How long to wait for cooldown
// Cooldown time.Duration
// Gossipsub heartbeat params
// Heartbeat HeartbeatParams
// whether to flood the network when publishing our own messages.
// Ignored unless hardening_api build tag is present.
// FloodPublishing bool
// Params for peer scoring function. Ignored unless hardening_api build tag is present.
// PeerScoreParams ScoreParams
OverlayParams OverlayParams
// Params for inspecting the scoring values.
// PeerScoreInspect InspectParams
// Size of the pubsub validation queue.
// ValidateQueueSize int
// Size of the pubsub outbound queue.
// OutboundQueueSize int
// Heartbeat tics for opportunistic grafting
// OpportunisticGraftTicks int
}
func Subscribe(ctx context.Context, ps *pubsub.PubSub, gRPCclient pb.GossipMessageClient, selfID peer.ID, peerTopic peerInfo) (*Topic, error) {
// join the pubsub topic
topic, err := ps.Join(topicName(peerTopic.name))
if err != nil {
return nil, err
}
// and subscribe to it
sub, err := topic.Subscribe()
if err != nil {
return nil, err
}
cr := &Topic{
ctx: ctx,
ps: ps,
topic: topic,
sub: sub,
self: selfID,
// validatorID: peerTopic.id,
// validatorKey: peer.
// ip: peer.ip,
name: peerTopic.name,
Messages: make(chan *Message, BufSize),
}
// start reading messages from the subscription in a loop
go cr.readLoop(gRPCclient)
return cr, nil
}
// Subscribe to the topic only for publishing
//Doenst really subscribes
func SubscribeWithoutReceiving(ctx context.Context, ps *pubsub.PubSub, gRPCclient pb.GossipMessageClient, selfID peer.ID, peerTopic peerInfo) (*Topic, error) {
// join the pubsub topic
topic, err := ps.Join(topicName(peerTopic.name))
if err != nil {
return nil, err
}
// and subscribe to it
sub, err := topic.Subscribe()
if err != nil {
return nil, err
}
cr := &Topic{
ctx: ctx,
ps: ps,
topic: topic,
sub: sub,
self: selfID,
// validatorID: peerTopic.id,
// validatorKey: peer.
// ip: peer.ip,
name: peerTopic.name,
Messages: make(chan *Message, BufSize),
}
// start reading messages from the subscription in a loop
// go cr.readLoop(gRPCclient)
return cr, nil
}
// Publish sends a message to the pubsub topic.
func (cr *Topic) Publish(message []byte, validatorKey string, hash string) error {
m := Message{
Message: message,
Validator_Key: validatorKey,
Hash: hash,
SenderID: cr.self.Pretty(),
SenderName: cr.name,
}
msgBytes, err := json.Marshal(m)
if err != nil {
return err
}
return cr.topic.Publish(cr.ctx, msgBytes)
}
func (cr *Topic) ListPeers() []peer.ID {
return cr.ps.ListPeers(topicName(cr.name))
}
// readLoop pulls messages from the pubsub topic and pushes them onto the Messages channel.
func (cr *Topic) readLoop(gRPCclient pb.GossipMessageClient) {
nodeName, err := ioutil.ReadFile("/etc/hostname")
if err != nil {
log.Fatal(err)
}
node := strings.TrimSpace(fmt.Sprintf("%s",nodeName))
for {
msg, err := cr.sub.Next(cr.ctx)
if err != nil {
close(cr.Messages)
return
}
// only forward messages delivered by others
if msg.ReceivedFrom == cr.self {
continue
}
cm := new(Message)
err = json.Unmarshal(msg.Data, cm)
if err != nil {
continue
}
// send valid messages onto the Messages channel
cr.Messages <- cm
m := <-cr.Messages
// Log format is "time | node name| handler | received/sent | orign/destination | data"
log.Printf("| %s | GossipSub | Recieved | GossipSub | %v | %v | %v| %v | %v \n", node, cr.name, msg.ReceivedFrom, m.SenderName, m.Hash, m.Validator_Key)
//Send to rippled
_, err = gRPCclient.ToRippled(cr.ctx, &pb.Gossip{Message: m.Message, Validator_Key: m.Validator_Key, Hash: m.Hash})
if err != nil {
log.Fatalf("%s Error when calling ToRippled: %s", node, err)
}
// Log format is "time | node name | handler | received/sent | orign/destination | data"
log.Printf(" | %s | gRPC-Client | Sent | Rippled | %v | %v \n", node, m.Hash, m.Validator_Key)
}
}
func topicName(peerName string) string {
return "validator:" + peerName
}