-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathclient_test.go
More file actions
684 lines (592 loc) · 16.7 KB
/
Copy pathclient_test.go
File metadata and controls
684 lines (592 loc) · 16.7 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
package authmailbox
import (
"bytes"
"context"
"net/url"
"os"
"testing"
"time"
"github.com/btcsuite/btclog/v2"
"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightninglabs/taproot-assets/internal/test"
"github.com/lightninglabs/taproot-assets/proof"
mboxrpc "github.com/lightninglabs/taproot-assets/taprpc/authmailboxrpc"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/keychain"
"github.com/stretchr/testify/require"
)
type clientHarness struct {
cfg *ClientConfig
key keychain.KeyDescriptor
filter MessageFilter
client *Client
msgChan chan *ReceivedMessages
subscription ReceiveSubscription
}
func newClientHarness(t *testing.T, cfg *ClientConfig,
key keychain.KeyDescriptor, filter MessageFilter,
subscribe bool) *clientHarness {
t.Helper()
client := NewClient(cfg)
require.NoError(t, client.Start())
h := &clientHarness{
cfg: cfg,
key: key,
filter: filter,
msgChan: make(chan *ReceivedMessages, 1),
client: client,
}
if subscribe {
h.subscribe(t)
h.assertConnected(t)
}
return h
}
func (h *clientHarness) subscribe(t *testing.T) {
t.Helper()
var (
ctx = context.Background()
err error
)
h.subscription, err = h.client.StartAccountSubscription(
ctx, h.msgChan, h.key, h.filter,
)
require.NoError(t, err)
}
func (h *clientHarness) assertConnected(t *testing.T) {
t.Helper()
require.Eventually(
t, h.subscription.IsSubscribed, testTimeout, testMinBackoff,
)
}
func (h *clientHarness) assertDisconnected(t *testing.T) {
t.Helper()
require.Eventually(t, func() bool {
return !h.subscription.IsSubscribed()
}, testTimeout, testMinBackoff)
}
func (h *clientHarness) readMessages(t *testing.T, targetID ...uint64) {
t.Helper()
select {
case inboundMsgs := <-h.msgChan:
receivedIDs := fn.Map(
inboundMsgs.Messages,
func(msg *mboxrpc.MailboxMessage) uint64 {
return msg.MessageId
},
)
for _, target := range targetID {
require.Contains(t, receivedIDs, target)
}
case <-time.After(testTimeout):
t.Fatalf("timeout waiting for message with ID %d", targetID)
}
}
func (h *clientHarness) expectNoMessage(t *testing.T) {
t.Helper()
select {
case msg := <-h.msgChan:
t.Fatalf("Received message when didn't expect one: %v", msg)
case <-time.After(testTimeout):
}
}
func (h *clientHarness) stop(t *testing.T) {
t.Helper()
t.Logf("Stopping client %x", h.key.PubKey.SerializeCompressed())
if h.subscription != nil {
err := h.subscription.Stop()
require.NoError(t, err)
}
err := h.client.Stop()
require.NoError(t, err)
}
func randProof(t *testing.T) proof.TxProof {
t.Helper()
randOp := test.RandOp(t)
return proof.TxProof{
ClaimedOutPoint: randOp,
}
}
// TestServerClientAuthAndRestart tests the server and client authentication
// process, and that the client can re-connect to the server after it has
// restarted. It also tests that the client can receive messages from the
// server's backlog.
func TestServerClientAuthAndRestart(t *testing.T) {
ctx := context.Background()
harness := NewMockServer(t)
clientCfg := harness.clientCfg
clientKey1, _ := test.RandKeyDesc(t)
clientKey2, _ := test.RandKeyDesc(t)
filter := MessageFilter{}
client1 := newClientHarness(t, clientCfg, clientKey1, filter, true)
client2 := newClientHarness(t, clientCfg, clientKey1, filter, true)
t.Cleanup(func() {
client1.stop(t)
client2.stop(t)
})
// We also add a multi-subscription to the same two keys, so we can make
// sure we can receive messages from multiple clients at once.
multiSub := NewMultiSubscription(*clientCfg)
err := multiSub.Subscribe(
ctx, url.URL{Host: clientCfg.ServerAddress}, clientKey1, filter,
)
require.NoError(t, err)
err = multiSub.Subscribe(
ctx, url.URL{Host: clientCfg.ServerAddress}, clientKey2, filter,
)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, multiSub.Stop())
})
assertMultiSubConnected := func(targetKeys ...keychain.KeyDescriptor) {
t.Helper()
serverURL := url.URL{Host: clientCfg.ServerAddress}
require.Eventually(t, func() bool {
multiSub.RLock()
defer multiSub.RUnlock()
client, ok := multiSub.clients[serverURL]
if !ok {
return false
}
for _, targetKey := range targetKeys {
key := asset.ToSerialized(targetKey.PubKey)
subscription, ok := client.subscriptions[key]
if !ok || !subscription.IsSubscribed() {
return false
}
}
return true
}, testTimeout, testMinBackoff)
}
assertMultiSubConnected(clientKey1, clientKey2)
msgChan := multiSub.MessageChan()
readMultiSub := func(targetID ...uint64) {
t.Helper()
select {
case inboundMsgs := <-msgChan:
receivedIDs := fn.Map(
inboundMsgs.Messages,
func(msg *mboxrpc.MailboxMessage) uint64 {
return msg.MessageId
},
)
for _, target := range targetID {
require.Contains(t, receivedIDs, target)
}
case <-time.After(testTimeout):
t.Fatalf("timeout waiting for message with ID %v",
targetID)
}
}
// Send a message to all clients.
msg1 := &Message{
ID: 1000,
ReceiverKey: *clientKey1.PubKey,
ArrivalTimestamp: time.Now(),
}
// We also store the message in the store, so we can retrieve it later.
_, err = harness.mockMsgStore.StoreMessage(ctx, randProof(t), msg1)
require.NoError(t, err)
harness.srv.publishMessage(msg1)
// We should be able to receive that message.
client1.readMessages(t, msg1.ID)
client2.readMessages(t, msg1.ID)
readMultiSub(msg1.ID)
// We now stop the server and assert that the subscription is no longer
// active.
harness.Stop(t)
client1.assertDisconnected(t)
client2.assertDisconnected(t)
// We wait a bit to simulate the server taking a while to start.
time.Sleep(testMinBackoff * 2)
// Let's start the server again and make sure the clients eventually
// re-connect.
harness.Start(t)
client1.assertConnected(t)
client2.assertConnected(t)
assertMultiSubConnected(clientKey1, clientKey2)
// Let's send another message to all clients.
msg2 := &Message{
ID: 1001,
ReceiverKey: *clientKey1.PubKey,
ArrivalTimestamp: time.Now(),
}
_, err = harness.mockMsgStore.StoreMessage(ctx, randProof(t), msg2)
require.NoError(t, err)
harness.srv.publishMessage(msg2)
// We should be able to receive that message.
client1.readMessages(t, msg2.ID)
client2.readMessages(t, msg2.ID)
readMultiSub(msg2.ID)
// If we now start a third client, we should be able to receive all
// three messages, given we are using the same key and specify the
// filter.
client3 := newClientHarness(t, clientCfg, clientKey1, MessageFilter{
After: time.Now().Add(-time.Hour),
}, true)
client3.readMessages(t, msg1.ID, msg2.ID)
// Make sure a client can disconnect and then re-connect again.
t.Logf("Disconnecting and re-connecting client")
require.NoError(t, client3.subscription.Stop())
client3.assertDisconnected(t)
client3.subscribe(t)
t.Cleanup(func() {
client3.stop(t)
})
client3.assertConnected(t)
client3.readMessages(t, msg1.ID, msg2.ID)
// We now make sure that messages are only delivered to the clients
// with the correct key.
client4 := newClientHarness(t, clientCfg, clientKey2, filter, true)
msg3 := &Message{
ID: 1000,
ReceiverKey: *clientKey1.PubKey,
ArrivalTimestamp: time.Now(),
}
harness.srv.publishMessage(msg3)
client4.expectNoMessage(t)
client1.readMessages(t, msg3.ID)
client2.readMessages(t, msg3.ID)
client3.readMessages(t, msg3.ID)
readMultiSub(msg3.ID)
// Let's make sure that a message sent to the second key is only
// received by the fourth client and the multi-subscription.
msg4 := &Message{
ID: 1001,
ReceiverKey: *clientKey2.PubKey,
ArrivalTimestamp: time.Now(),
}
harness.srv.publishMessage(msg4)
client1.expectNoMessage(t)
client2.expectNoMessage(t)
client3.expectNoMessage(t)
client4.readMessages(t, msg4.ID)
readMultiSub(msg4.ID)
}
// TestSendMessage tests the SendMessage RPC of the server and its ability to
// rate limit messages by validating the transaction proofs.
func TestSendMessage(t *testing.T) {
txProof1 := proof.MockTxProof(t)
txProof2 := proof.MockTxProof(t)
txProof3 := proof.MockTxProof(t)
clientKey1, _ := test.RandKeyDesc(t)
clientKey2, _ := test.RandKeyDesc(t)
clientKey3, _ := test.RandKeyDesc(t)
proofWithHeight := func(p proof.TxProof, h uint32) proof.TxProof {
p.BlockHeight = h
return p
}
// To be able to have these test cases be fully independent of each
// other, we allow each test case to send multiple messages, using
// different proofs and expecting different errors.
testCases := []struct {
name string
txProofs []proof.TxProof
recvKeys []keychain.KeyDescriptor
sendKey keychain.KeyDescriptor
msgs [][]byte
expectedErrs []string
}{
{
name: "empty payload",
txProofs: []proof.TxProof{*txProof1},
recvKeys: []keychain.KeyDescriptor{clientKey2},
sendKey: clientKey1,
msgs: [][]byte{nil},
expectedErrs: []string{"empty payload"},
},
{
name: "long payload",
txProofs: []proof.TxProof{*txProof1},
recvKeys: []keychain.KeyDescriptor{clientKey2},
sendKey: clientKey1,
msgs: [][]byte{
bytes.Repeat([]byte("foo"), MsgMaxSize),
},
expectedErrs: []string{ErrMessageTooLong.Error()},
},
{
name: "success",
txProofs: []proof.TxProof{
proofWithHeight(*txProof1, 100002),
},
recvKeys: []keychain.KeyDescriptor{clientKey2},
sendKey: clientKey1,
msgs: [][]byte{[]byte("yoooo")},
},
{
name: "duplicate proof",
txProofs: []proof.TxProof{
proofWithHeight(*txProof1, 100002),
proofWithHeight(*txProof1, 100002),
},
recvKeys: []keychain.KeyDescriptor{
clientKey2,
clientKey3,
},
sendKey: clientKey1,
msgs: [][]byte{
[]byte("yoooo"),
[]byte("imma try again"),
},
expectedErrs: []string{
"",
proof.ErrTxMerkleProofExists.Error(),
},
},
{
name: "multiple successful proofs",
txProofs: []proof.TxProof{
proofWithHeight(*txProof1, 100002),
proofWithHeight(*txProof2, 100002),
proofWithHeight(*txProof3, 100002),
},
recvKeys: []keychain.KeyDescriptor{
clientKey2,
clientKey2,
clientKey2,
},
sendKey: clientKey1,
msgs: [][]byte{
[]byte("yoooo"),
[]byte("imma try again"),
[]byte("and again"),
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
harness := NewMockServer(t)
clientCfg := harness.clientCfg
filter := MessageFilter{
After: time.Now().Add(-time.Hour),
}
client1 := newClientHarness(
t, clientCfg, clientKey1, filter, false,
)
client2 := newClientHarness(
t, clientCfg, clientKey2, filter, true,
)
t.Cleanup(func() {
client1.stop(t)
client2.stop(t)
})
for idx := range tc.msgs {
msg := tc.msgs[idx]
txProof := tc.txProofs[idx]
recvKey := tc.recvKeys[idx]
msgID, err := client1.client.SendMessage(
ctx, *recvKey.PubKey, msg, txProof,
)
if len(tc.expectedErrs) > 0 &&
tc.expectedErrs[idx] != "" {
require.ErrorContains(
t, err, tc.expectedErrs[idx],
)
return
}
require.NoError(t, err)
// We should be able to read the message if
// there was no error sending it.
client2.readMessages(t, msgID)
// Sending the same message again should result
// in the same message ID, but should not cause
// another message to be sent to any recipients.
msgIDReSend, err := client1.client.SendMessage(
ctx, *recvKey.PubKey, msg, txProof,
)
require.NoError(t, err)
require.Equal(t, msgID, msgIDReSend)
client2.expectNoMessage(t)
}
})
}
}
type msgOpt func(*Message)
func withTimestamp(ts time.Time) msgOpt {
return func(msg *Message) {
msg.ArrivalTimestamp = ts
}
}
func withArrivalBlockHeight(height uint32) msgOpt {
return func(msg *Message) {
msg.ProofBlockHeight = height
}
}
func makeMessage(c clock.Clock, id uint64, key keychain.KeyDescriptor,
opts ...msgOpt) *Message {
msg := &Message{
ID: id,
ReceiverKey: *key.PubKey,
ArrivalTimestamp: c.Now(),
}
for _, opt := range opts {
opt(msg)
}
return msg
}
// TestReceiveBacklog tests that the client can receive messages from the
// server's backlog, using custom filters.
func TestReceiveBacklog(t *testing.T) {
harness := NewMockServer(t)
ctx := context.Background()
receiver1, _ := test.RandKeyDesc(t)
receiver2, _ := test.RandKeyDesc(t)
receiver3, _ := test.RandKeyDesc(t)
// We create and store a bunch of messages with different properties.
// Timestamps are converted to Unix timestamps, so we lose sub-second
// precision.
c := clock.NewTestClock(time.Now().Truncate(time.Second))
yesterday := c.Now().Add(-24 * time.Hour).Truncate(time.Second)
lastHour := c.Now().Add(-time.Hour).Truncate(time.Second)
lastHalfHour := c.Now().Add(-time.Hour / 2).Truncate(time.Second)
messages := []*Message{
// For receiver 1.
makeMessage(c, 1, receiver1),
makeMessage(c, 2, receiver1, withTimestamp(lastHour)),
makeMessage(c, 3, receiver1, withArrivalBlockHeight(123)),
makeMessage(
c, 4, receiver1, withTimestamp(lastHour),
withArrivalBlockHeight(345),
),
// For receiver 2.
makeMessage(c, 5, receiver2),
makeMessage(c, 6, receiver2, withTimestamp(lastHour)),
makeMessage(c, 7, receiver2, withArrivalBlockHeight(123)),
makeMessage(
c, 8, receiver2, withTimestamp(lastHour),
withArrivalBlockHeight(345),
),
// For receiver 3.
makeMessage(c, 9, receiver3),
makeMessage(c, 10, receiver3, withTimestamp(lastHour)),
makeMessage(c, 11, receiver3, withArrivalBlockHeight(123)),
makeMessage(
c, 12, receiver3, withTimestamp(lastHour),
withArrivalBlockHeight(345),
),
}
for _, msg := range messages {
_, err := harness.mockMsgStore.StoreMessage(
ctx, randProof(t), msg,
)
require.NoError(t, err)
}
testCases := []struct {
name string
receiver keychain.KeyDescriptor
filter MessageFilter
expected []uint64
expectNoMessages bool
}{
{
name: "timestamp is exclusive",
receiver: receiver1,
filter: MessageFilter{
After: lastHour,
},
expected: []uint64{1, 3},
},
{
name: "block height is inclusive",
receiver: receiver1,
filter: MessageFilter{
StartBlock: 123,
},
expected: []uint64{3},
},
{
name: "id is exclusive",
receiver: receiver1,
filter: MessageFilter{
AfterID: 1,
},
expected: []uint64{2, 3, 4},
},
{
name: "receiver 2, no filter means no " +
"backlog",
receiver: receiver2,
filter: MessageFilter{},
expectNoMessages: true,
},
{
name: "receiver 2, all",
receiver: receiver2,
filter: MessageFilter{
After: yesterday,
},
expected: []uint64{5, 6, 7, 8},
},
{
name: "receiver 3, all values set",
receiver: receiver3,
filter: MessageFilter{
AfterID: 9,
After: lastHalfHour,
StartBlock: 123,
},
expected: []uint64{11},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
clientCfg := harness.clientCfg
client := newClientHarness(
t, clientCfg, tc.receiver, tc.filter, true,
)
defer client.stop(t)
if tc.expectNoMessages {
client.expectNoMessage(t)
return
}
client.readMessages(t, tc.expected...)
})
}
}
// TestReconnectAfterStreamError tests that the client reconnects after
// a non-graceful server shutdown (transport error, no EndOfStream).
func TestReconnectAfterStreamError(t *testing.T) {
ctx := context.Background()
harness := NewMockServer(t)
clientCfg := harness.clientCfg
clientKey, _ := test.RandKeyDesc(t)
filter := MessageFilter{}
client := newClientHarness(t, clientCfg, clientKey, filter, true)
t.Cleanup(func() {
client.stop(t)
})
// Send a message and verify it arrives.
msg1 := &Message{
ID: 1000,
ReceiverKey: *clientKey.PubKey,
ArrivalTimestamp: time.Now(),
}
_, err := harness.mockMsgStore.StoreMessage(ctx, randProof(t), msg1)
require.NoError(t, err)
harness.srv.publishMessage(msg1)
client.readMessages(t, msg1.ID)
// Force-kill the server — no EndOfStream is sent.
harness.ForceStop(t)
client.assertDisconnected(t)
// Restart the server. The client is already retrying in the
// background via HandleServerShutdown.
harness.Start(t)
client.assertConnected(t)
// Send another message and verify receipt.
msg2 := &Message{
ID: 1001,
ReceiverKey: *clientKey.PubKey,
ArrivalTimestamp: time.Now(),
}
_, err = harness.mockMsgStore.StoreMessage(ctx, randProof(t), msg2)
require.NoError(t, err)
harness.srv.publishMessage(msg2)
client.readMessages(t, msg2.ID)
}
func init() {
logger := btclog.NewSLogger(btclog.NewDefaultHandler(os.Stdout))
UseLogger(logger.SubSystem(Subsystem))
}