Skip to content

Commit f678080

Browse files
committed
Add Peers method.
This commit adds a `Peers` method to `Node` as a passthrough to the store's list of peers. This is useful when someone is just using the default in-memory store. Signed-off-by: David Bond <[email protected]>
1 parent d831d31 commit f678080

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ A peer joins the network using an initial TCP connection to an existing peer. Wh
1414
information about itself, including its public key and an advertised address used for further UDP/TCP communication. If
1515
accepted into the network, the peer is given the current state of all known peers from the peer it joined.
1616

17-
Peers do not share their private keys and the ability to join the network is handled at the TCP layer using TLS.
17+
Peers do not share their private keys and the ability to join the network can be handled at the TCP layer using TLS. By
18+
default, whisper does not enable TLS for TCP-based requests and must be configured to do so.
1819

1920
### Sharing State
2021

whisper.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ func (n *Node) Address() string {
184184
return n.address
185185
}
186186

187+
// Peers returns all peers within the gossip network known at the time it is called.
188+
func (n *Node) Peers(ctx context.Context) ([]peer.Peer, error) {
189+
return n.store.ListPeers(ctx)
190+
}
191+
187192
// SetMetadata updates the metadata on the Node. This causes a delta update which will be propagated out to the
188193
// gossip network.
189194
func (n *Node) SetMetadata(ctx context.Context, message proto.Message) error {

whisper_test.go

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,9 @@ func (s *WhisperTestSuite) TestMultiNode() {
111111
t.Run("state is synchronised", func(t *testing.T) {
112112
for _, node := range nodes {
113113
t.Run(fmt.Sprintf("on node %d", node.ID()), func(t *testing.T) {
114-
client := dialPeer(t, testTLS, node.Address())
115-
116-
response, err := client.Status(ctx, &whispersvcv1.StatusRequest{})
114+
peers, err := node.Peers(ctx)
117115
require.NoError(t, err)
118-
require.Len(t, response.GetPeers(), len(nodes)-1)
116+
require.Len(t, peers, len(nodes))
119117
})
120118
}
121119
})
@@ -153,19 +151,15 @@ func (s *WhisperTestSuite) TestMultiNode() {
153151
}
154152

155153
t.Run(fmt.Sprintf("to node %d", node.ID()), func(t *testing.T) {
156-
client := dialPeer(t, testTLS, node.Address())
157-
158-
response, err := client.Status(ctx, &whispersvcv1.StatusRequest{})
154+
peers, err := node.Peers(ctx)
159155
require.NoError(t, err)
160156

161-
for _, p := range response.GetPeers() {
162-
if p.GetId() != target.ID() {
157+
for _, p := range peers {
158+
if p.ID != target.ID() {
163159
continue
164160
}
165161

166-
actual, err := p.GetMetadata().UnmarshalNew()
167-
require.NoError(t, err)
168-
assert.True(t, proto.Equal(expected, actual))
162+
assert.True(t, proto.Equal(expected, p.Metadata))
169163
}
170164
})
171165
}
@@ -184,17 +178,15 @@ func (s *WhisperTestSuite) TestMultiNode() {
184178
}
185179

186180
t.Run(fmt.Sprintf("detected by node %d", target.ID()), func(t *testing.T) {
187-
client := dialPeer(t, testTLS, target.Address())
188-
189-
response, err := client.Status(ctx, &whispersvcv1.StatusRequest{})
181+
peers, err := target.Peers(ctx)
190182
require.NoError(t, err)
191183

192-
for _, p := range response.GetPeers() {
193-
if p.GetId() != node.ID() {
184+
for _, p := range peers {
185+
if p.ID != node.ID() {
194186
continue
195187
}
196188

197-
assert.EqualValues(t, whisperv1.PeerStatus_PEER_STATUS_LEFT, p.GetStatus())
189+
assert.EqualValues(t, peer.StatusLeft, p.Status)
198190
break
199191
}
200192

@@ -217,8 +209,6 @@ func (s *WhisperTestSuite) TestFailureDetection() {
217209
nodes, stores := createNodes(t, s.logger, ca, key, 2)
218210
runNodes(t, ctx, nodes)
219211

220-
testTLS := testTLSConfig(t, ca, key, "test")
221-
222212
deadPeerKey, err := curve.GenerateKey(rand.Reader)
223213
require.NoError(t, err)
224214

@@ -240,16 +230,15 @@ func (s *WhisperTestSuite) TestFailureDetection() {
240230
t.Run("node is marked as gone", func(t *testing.T) {
241231
for _, node := range nodes {
242232
t.Run(fmt.Sprintf("on node %d", node.ID()), func(t *testing.T) {
243-
client := dialPeer(t, testTLS, node.Address())
244-
response, err := client.Status(ctx, &whispersvcv1.StatusRequest{})
233+
peers, err := node.Peers(ctx)
245234
require.NoError(t, err)
246235

247-
for _, p := range response.GetPeers() {
248-
if p.GetId() != deadPeer.ID {
236+
for _, p := range peers {
237+
if p.ID != deadPeer.ID {
249238
continue
250239
}
251240

252-
assert.EqualValues(t, whisperv1.PeerStatus_PEER_STATUS_GONE, p.GetStatus())
241+
assert.EqualValues(t, peer.StatusGone, p.Status)
253242
break
254243
}
255244
})
@@ -307,7 +296,7 @@ func runNodes(t *testing.T, ctx context.Context, nodes []*whisper.Node) []contex
307296
group, gCtx := errgroup.WithContext(ctx)
308297
for i, node := range nodes {
309298
if i > 0 {
310-
<-time.After(time.Second * 5)
299+
<-time.After(time.Second * 2)
311300
}
312301

313302
group.Go(func() error {

0 commit comments

Comments
 (0)