forked from breadwallet/breadwallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestBwm.c
More file actions
1512 lines (1288 loc) · 69.3 KB
/
Copy pathtestBwm.c
File metadata and controls
1512 lines (1288 loc) · 69.3 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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// test.c
//
// Created by Michael Carrara on 8/18/19.
// Copyright (c) 2019 breadwallet LLC
//
// See the LICENSE file at the project root for license information.
// See the CONTRIBUTORS file at the project root for a list of contributors.
//
#include <inttypes.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "bcash/BRBCashParams.h"
#include "bitcoin/BRChainParams.h"
#include "support/BRArray.h"
#include "support/BRBIP39Mnemonic.h"
#include "bitcoin/BRPeerManager.h"
#include "bitcoin/BRTransaction.h"
#include "bitcoin/BRWallet.h"
#include "bitcoin/BRWalletManager.h"
#ifdef __ANDROID__
#include <android/log.h>
#define fprintf(...) __android_log_print(ANDROID_LOG_ERROR, "testBwm", _va_rest(__VA_ARGS__, NULL))
#define printf(...) __android_log_print(ANDROID_LOG_INFO, "testBwm", __VA_ARGS__)
#define _va_first(first, ...) first
#define _va_rest(first, ...) __VA_ARGS__
#endif
///
///
///
static void
_testTransactionEventCallback (BRWalletManagerClientContext context,
BRWalletManager manager,
BRWallet *wallet,
BRTransaction *transaction,
BRTransactionEvent event) {
printf ("TST: TransactionEvent: %d\n", event.type);
}
static void
_testWalletEventCallback (BRWalletManagerClientContext context,
BRWalletManager manager,
BRWallet *wallet,
BRWalletEvent event) {
printf ("TST: WalletEvent: %d\n", event.type);
}
static int syncDone = 0;
static void
_testWalletManagerEventCallback (BRWalletManagerClientContext context,
BRWalletManager manager,
BRWalletManagerEvent event) {
printf ("TST: WalletManagerEvent: %d\n", event.type);
switch (event.type) {
case BITCOIN_WALLET_MANAGER_CONNECTED:
break;
case BITCOIN_WALLET_MANAGER_SYNC_STARTED:
break;
case BITCOIN_WALLET_MANAGER_SYNC_STOPPED:
syncDone = 1;
break;
default:
break;
}
}
extern int BRRunTestWalletManagerSync (const char *paperKey,
const char *storagePath,
int isBTC,
int isMainnet) {
const BRChainParams *params = (isBTC & isMainnet ? BRMainNetParams
: (isBTC & !isMainnet ? BRTestNetParams
: (isMainnet ? BRBCashParams : BRBCashTestNetParams)));
uint32_t epoch = 1483228800; // 1/1/17
epoch += (365 + 365/2) * 24 * 60 * 60;
printf ("***\n***\nPaperKey (Start): \"%s\"\n***\n***\n", paperKey);
UInt512 seed = UINT512_ZERO;
BRBIP39DeriveKey (seed.u8, paperKey, NULL);
BRMasterPubKey mpk = BRBIP32MasterPubKey(&seed, sizeof (seed));
BRWalletManagerClient client = {
NULL,
(BRGetBlockNumberCallback) NULL,
(BRGetTransactionsCallback) NULL,
(BRSubmitTransactionCallback) NULL,
_testTransactionEventCallback,
_testWalletEventCallback,
_testWalletManagerEventCallback
};
BRCryptoSyncMode mode = CRYPTO_SYNC_MODE_P2P_ONLY;
BRWalletManager manager = BRWalletManagerNew (client, mpk, params, epoch, mode, storagePath, 0, 6);
BRWalletManagerStart (manager);
syncDone = 0;
BRWalletManagerConnect (manager);
int err = 0;
while (err == 0 && !syncDone) {
err = sleep(1);
}
err = 0;
int seconds = 120;
while (err == 0 && seconds-- > 0) {
err = sleep(1);
}
printf ("***\n***\nPaperKey (Done): \"%s\"\n***\n***\n", paperKey);
BRWalletManagerDisconnect (manager);
sleep (2);
BRWalletManagerStop (manager);
sleep (2);
BRWalletManagerFree (manager);
return 1;
}
///
/// MARK: BRWalletManager Connect/Disconnect/Scan Tests
///
typedef enum {
SYNC_EVENT_WALLET_MANAGER_TYPE,
SYNC_EVENT_WALLET_TYPE,
SYNC_EVENT_TXN_TYPE,
} BRWalletManagerSyncEventType;
typedef struct BRWalletManagerSyncEventRecord {
BRWalletManagerSyncEventType type;
union {
struct {
BRWalletManager manager;
BRWalletManagerEvent event;
} m;
struct {
BRWalletManager manager;
BRWallet *wallet;
BRWalletEvent event;
} w;
struct {
BRWalletManager manager;
BRWallet *wallet;
BRTransaction *transaction;
BRTransactionEvent event;
} t;
} u;
} BRWalletManagerSyncEvent;
const char *
BRWalletManagerSyncEventTypeString (BRWalletManagerSyncEventType type) {
switch (type) {
case SYNC_EVENT_WALLET_MANAGER_TYPE:
return "SYNC_EVENT_WALLET_MANAGER_TYPE";
case SYNC_EVENT_WALLET_TYPE:
return "SYNC_EVENT_WALLET_TYPE";
case SYNC_EVENT_TXN_TYPE:
return "SYNC_EVENT_TXN_TYPE";
}
}
static BRWalletManagerSyncEvent
BRWalletManagerSyncEventForWalletManagerType(BRWalletManagerEventType type) {
return (BRWalletManagerSyncEvent) {
SYNC_EVENT_WALLET_MANAGER_TYPE,
{
.m = {
NULL,
(BRWalletManagerEvent) {
type
}
}
}
};
}
static BRWalletManagerSyncEvent
BRWalletManagerSyncEventForWalletType(BRWalletEventType type) {
return (BRWalletManagerSyncEvent) {
SYNC_EVENT_WALLET_TYPE,
{
.w = {
NULL,
NULL,
(BRWalletEvent) {
type
}
}
}
};
}
static int
BRWalletManagerSyncEventEqual (BRWalletManagerSyncEvent *e1, BRWalletManagerSyncEvent *e2) {
int success = 1;
if (e1->type != e2->type) {
success = 0;
}
if (success) {
switch (e1->type) {
case SYNC_EVENT_WALLET_MANAGER_TYPE: {
if (e1->u.m.event.type != e2->u.m.event.type) {
success = 0;
}
break;
}
case SYNC_EVENT_WALLET_TYPE: {
if (e1->u.w.event.type != e2->u.w.event.type) {
success = 0;
}
break;
}
case SYNC_EVENT_TXN_TYPE: {
if (e1->u.t.event.type != e2->u.t.event.type) {
success = 0;
}
break;
}
}
}
return success;
}
static char *
BRWalletManagerSyncEventString (BRWalletManagerSyncEvent *e) {
const char * subtypeString = NULL;
const char * typeString = BRWalletManagerSyncEventTypeString (e->type);
switch (e->type) {
case SYNC_EVENT_WALLET_MANAGER_TYPE:
subtypeString = BRWalletManagerEventTypeString (e->u.m.event.type);
break;
case SYNC_EVENT_WALLET_TYPE:
subtypeString = BRWalletEventTypeString (e->u.w.event.type);
break;
case SYNC_EVENT_TXN_TYPE:
subtypeString = BRTransactionEventTypeString (e->u.t.event.type);
break;
}
const char * fmtString = "BRWalletManagerSyncEventString(%s -> %s)";
size_t fmtStringLength = strlen (fmtString);
size_t eventStringLength = fmtStringLength + strlen(typeString) + strlen(subtypeString) + 1;
char * eventString = calloc (eventStringLength, sizeof(char));
snprintf (eventString, eventStringLength, fmtString, typeString, subtypeString);
return eventString;
}
typedef struct {
uint64_t blockHeight;
uint8_t silent;
BRArrayOf(BRWalletManagerSyncEvent *) events;
pthread_mutex_t lock;
} BRRunTestWalletManagerSyncState;
static void
_testGetBlockNumberNopCallback (BRWalletManagerClientContext context,
BRWalletManager manager,
int rid) {
// do nothing
}
static void
_testGetTransactionsNopCallback (BRWalletManagerClientContext context,
BRWalletManager manager,
OwnershipKept const char **addresses,
size_t addressCount,
uint64_t begBlockNumber,
uint64_t endBlockNumber,
int rid) {
// do nothing
}
static void
_testSubmitTransactionNopCallback (BRWalletManagerClientContext context,
BRWalletManager manager,
BRWallet *wallet,
OwnershipKept uint8_t *transaction,
size_t transactionLength,
UInt256 transactionHash,
int rid) {
// do nothing
}
static void
_testTransactionEventRecordingCallback (BRWalletManagerClientContext context,
BRWalletManager manager,
BRWallet *wallet,
BRTransaction *transaction,
BRTransactionEvent event) {
BRRunTestWalletManagerSyncState *state = (BRRunTestWalletManagerSyncState*) context;
BRWalletManagerSyncEvent *record = calloc (1, sizeof (BRWalletManagerSyncEvent));
record->type = SYNC_EVENT_TXN_TYPE;
record->u.t.manager= manager;
record->u.t.wallet= wallet;
record->u.t.transaction = transaction;
record->u.t.event = event;
pthread_mutex_lock (&state->lock);
array_add (state->events, record);
if (!state->silent) printf ("Added TXN event: %s (%zu total)\n", BRTransactionEventTypeString (event.type), array_count (state->events));
pthread_mutex_unlock (&state->lock);
}
static void
_testWalletEventRecordingCallback (BRWalletManagerClientContext context,
BRWalletManager manager,
BRWallet *wallet,
BRWalletEvent event) {
BRRunTestWalletManagerSyncState *state = (BRRunTestWalletManagerSyncState*) context;
BRWalletManagerSyncEvent *record = calloc (1, sizeof (BRWalletManagerSyncEvent));
record->type = SYNC_EVENT_WALLET_TYPE;
record->u.w.manager= manager;
record->u.w.wallet= wallet;
record->u.w.event = event;
pthread_mutex_lock (&state->lock);
array_add (state->events, record);
if (!state->silent) printf ("Added WALLET event: %s (%zu total)\n", BRWalletEventTypeString (event.type), array_count (state->events));
pthread_mutex_unlock (&state->lock);
}
static void
_testWalletManagerEventRecordingCallback (BRWalletManagerClientContext context,
BRWalletManager manager,
BRWalletManagerEvent event) {
BRRunTestWalletManagerSyncState *state = (BRRunTestWalletManagerSyncState*) context;
BRWalletManagerSyncEvent *record = calloc (1, sizeof (BRWalletManagerSyncEvent));
record->type = SYNC_EVENT_WALLET_MANAGER_TYPE;
record->u.m.manager= manager;
record->u.m.event = event;
pthread_mutex_lock (&state->lock);
array_add (state->events, record);
if (!state->silent) printf ("Added MANAGER event: %s (%zu total)\n", BRWalletManagerEventTypeString (event.type), array_count (state->events));
pthread_mutex_unlock (&state->lock);
}
typedef struct {
uint8_t kill;
BRWalletManager manager;
} BRRunTestWalletManagerSyncThreadState;
static void *
_testBRWalletManagerConnectThread (void *context) {
BRRunTestWalletManagerSyncThreadState *state = (BRRunTestWalletManagerSyncThreadState *) context;
while (!state->kill) {
BRWalletManagerConnect (state->manager);
}
return NULL;
}
static void *
_testBRWalletManagerDisconnectThread (void *context) {
BRRunTestWalletManagerSyncThreadState *state = (BRRunTestWalletManagerSyncThreadState *) context;
while (!state->kill) {
BRWalletManagerDisconnect (state->manager);
}
return NULL;
}
static void *
_testBRWalletManagerScanThread (void *context) {
BRRunTestWalletManagerSyncThreadState *state = (BRRunTestWalletManagerSyncThreadState *) context;
while (!state->kill) {
BRWalletManagerScan (state->manager);
}
return NULL;
}
static void *
_testBRWalletManagerSwapThread (void *context) {
BRRunTestWalletManagerSyncThreadState *state = (BRRunTestWalletManagerSyncThreadState *) context;
while (!state->kill) {
switch (BRWalletManagerGetMode (state->manager)) {
case CRYPTO_SYNC_MODE_API_ONLY:
BRWalletManagerSetMode (state->manager, CRYPTO_SYNC_MODE_P2P_ONLY);
break;
case CRYPTO_SYNC_MODE_P2P_ONLY:
BRWalletManagerSetMode (state->manager, CRYPTO_SYNC_MODE_API_ONLY);
break;
default:
break;
}
}
return NULL;
}
static size_t
BRRunTestWalletManagerSyncTestGetEventByType(BRRunTestWalletManagerSyncState *state,
size_t startIndex,
BRWalletManagerSyncEventType type) {
for (size_t index = startIndex; index < array_count(state->events); index++) {
if (state->events[index]->type == type) {
return index;
}
}
return SIZE_MAX;
}
static void
BRRunTestWalletManagerSyncTestSetup (BRRunTestWalletManagerSyncState *state,
uint64_t blockHeight,
uint8_t isSilent) {
state->blockHeight = blockHeight;
state->silent = isSilent;
array_new (state->events, 100);
pthread_mutex_init (&state->lock, NULL);
}
static void
BRRunTestWalletManagerSyncTestSetupAsDefault (BRRunTestWalletManagerSyncState *state,
uint64_t blockHeight) {
BRRunTestWalletManagerSyncTestSetup (state, blockHeight, 0);
}
static int
BRRunTestWalletManagerSyncTestVerifyEventSequence (BRRunTestWalletManagerSyncState *state,
uint8_t isCompleteSequence,
BRWalletManagerSyncEvent *expected,
size_t expectedCount,
BRWalletManagerSyncEvent *ignored,
size_t ignoredCount) {
int success = 1;
pthread_mutex_lock (&state->lock);
size_t index = 0;
size_t count = array_count (state->events);
size_t expectedIndex = 0;
for (; success && index < count; index++) {
if (!isCompleteSequence && expectedIndex == expectedCount) {
break;
} else if ((success = (expectedIndex < expectedCount &&
BRWalletManagerSyncEventEqual (state->events[index], &expected[expectedIndex])))) {
expectedIndex++;
} else {
for (size_t ignoredIndex = 0; !success && ignoredIndex < ignoredCount; ignoredIndex++) {
success = BRWalletManagerSyncEventEqual (state->events[index], &ignored[ignoredIndex]);
}
}
if (!success) {
printf("%s: failed due to mismatched event types (expected at idx %zu -> %s, received at idx %zu -> %s)\n",
__func__,
expectedIndex,
BRWalletManagerSyncEventString (&expected[expectedIndex]),
index,
BRWalletManagerSyncEventString (state->events[index]));
}
}
pthread_mutex_unlock (&state->lock);
if (success &&
isCompleteSequence && index != count && expectedIndex == expectedCount) {
success = 0;
printf("%s: failed due to reaching the end of expected events but more occurred\n",
__func__);
}
if (success &&
index == count && expectedIndex != expectedCount) {
success = 0;
printf("%s: failed due to reaching the end of occurred events but more expected\n",
__func__);
}
return success;
}
static int
BRRunTestWalletManagerSyncTestVerifyEventPairs (BRRunTestWalletManagerSyncState *state) {
int success = 1;
pthread_mutex_lock (&state->lock);
// check that each manager event pair is allowed
{
size_t index = BRRunTestWalletManagerSyncTestGetEventByType (state, 0, SYNC_EVENT_WALLET_MANAGER_TYPE);
size_t nextIndex = 0;
while (SIZE_MAX != (nextIndex = BRRunTestWalletManagerSyncTestGetEventByType (state, index+1, SYNC_EVENT_WALLET_MANAGER_TYPE))) {
if (!BRWalletManagerEventTypeIsValidPair (state->events[index]->u.m.event.type, state->events[nextIndex]->u.m.event.type)) {
success = 0;
printf("%s: failed due to invalid wallet manager event pair (%s, %s) test\n",
__func__,
BRWalletManagerEventTypeString (state->events[index]->u.m.event.type),
BRWalletManagerEventTypeString (state->events[nextIndex]->u.m.event.type));
break;
}
index = nextIndex;
}
}
// check that each wallet event pair is allowed
{
size_t index = BRRunTestWalletManagerSyncTestGetEventByType (state, 0, SYNC_EVENT_WALLET_TYPE);
size_t nextIndex = 0;
while (SIZE_MAX != (nextIndex = BRRunTestWalletManagerSyncTestGetEventByType (state, index+1, SYNC_EVENT_WALLET_TYPE))) {
if (!BRWalletEventTypeIsValidPair (state->events[index]->u.w.event.type, state->events[nextIndex]->u.w.event.type)) {
success = 0;
printf("%s: failed due to invalid wallet event pair (%s, %s) test\n",
__func__,
BRWalletEventTypeString (state->events[index]->u.w.event.type),
BRWalletEventTypeString (state->events[nextIndex]->u.w.event.type));
break;
}
index = nextIndex;
}
}
pthread_mutex_unlock (&state->lock);
return success;
}
static void
BRRunTestWalletManagerSyncTestTeardown (BRRunTestWalletManagerSyncState *state) {
for (size_t index = 0; index < array_count(state->events); index++)
free (state->events[index]);
array_free (state->events);
pthread_mutex_destroy (&state->lock);
}
static BRWalletManager
BRRunTestWalletManagerSyncBwmSetup (BRCryptoSyncMode mode,
BRWalletManagerClientContext context,
const char *paperKey,
const char *storagePath,
uint32_t earliestKeyTime,
uint64_t blockHeight,
int isBTC,
int isMainnet)
{
BRWalletManagerClient client = {
context,
_testGetBlockNumberNopCallback, _testGetTransactionsNopCallback, _testSubmitTransactionNopCallback,
_testTransactionEventRecordingCallback, _testWalletEventRecordingCallback, _testWalletManagerEventRecordingCallback};
UInt512 seed = UINT512_ZERO;
BRBIP39DeriveKey (seed.u8, paperKey, NULL);
BRMasterPubKey mpk = BRBIP32MasterPubKey(&seed, sizeof (seed));
const BRChainParams *params = NULL;
if (isBTC) {
params = isMainnet ? BRMainNetParams : BRTestNetParams;
} else {
params = isMainnet ? BRBCashParams : BRBCashTestNetParams;
}
return BRWalletManagerNew (client, mpk, params, earliestKeyTime, mode, storagePath, blockHeight, 6);
}
static int
BRRunTestWalletManagerSyncForMode (const char *testName,
BRCryptoSyncMode mode,
const char *paperKey,
const char *storagePath,
uint32_t earliestKeyTime,
uint64_t blockHeight,
int isBTC,
int isMainnet) {
int success = 1;
printf("%s testing BRWalletManager events for %s mode, %u epoch, %" PRIu64 " height on \"%s:%s\" with %s as storage...\n",
testName,
cryptoSyncModeString (mode),
earliestKeyTime,
blockHeight,
isBTC ? "btc": "bch",
isMainnet ? "mainnet" : "testnet",
storagePath);
printf("Testing BRWalletManager connect, disconnect...\n");
{
// Test setup
BRRunTestWalletManagerSyncState state = {0};
BRRunTestWalletManagerSyncTestSetupAsDefault (&state, blockHeight);
BRWalletManager manager = BRRunTestWalletManagerSyncBwmSetup (mode, &state, paperKey, storagePath, earliestKeyTime, blockHeight, isBTC, isMainnet);
BRWalletManagerStart (manager);
// connect, disconnect cycle
BRWalletManagerConnect (manager);
sleep(1);
BRWalletManagerDisconnect (manager);
sleep(1);
BRWalletManagerStop (manager);
BRWalletManagerFree (manager);
// Verification
success = BRRunTestWalletManagerSyncTestVerifyEventSequence(&state,
1,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_CREATED),
BRWalletManagerSyncEventForWalletType(BITCOIN_WALLET_CREATED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_CONNECTED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_STARTED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_STOPPED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_DISCONNECTED),
},
6,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_PROGRESS),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_BLOCK_HEIGHT_UPDATED),
},
2);
if (!success) {
fprintf(stderr, "***FAILED*** %s:%d: BRRunTestWalletManagerSyncTestVerifyEventSequence failed\n", testName, __LINE__);
return success;
}
success = BRRunTestWalletManagerSyncTestVerifyEventPairs (&state);
if (!success) {
fprintf(stderr, "***FAILED*** %s:%d: BRRunTestWalletManagerSyncTestVerifyEventPairs failed\n", testName, __LINE__);
return success;
}
// Test teardown
BRRunTestWalletManagerSyncTestTeardown (&state);
}
printf("Testing BRWalletManager repeated connect attempts...\n");
{
// Test setup
BRRunTestWalletManagerSyncState state = {0};
BRRunTestWalletManagerSyncTestSetupAsDefault (&state, blockHeight);
BRWalletManager manager = BRRunTestWalletManagerSyncBwmSetup (mode, &state, paperKey, storagePath, earliestKeyTime, blockHeight, isBTC, isMainnet);
BRWalletManagerStart (manager);
// repeated connect attempts
BRWalletManagerConnect (manager);
sleep(1);
BRWalletManagerConnect (manager);
sleep(1);
BRWalletManagerConnect (manager);
sleep(1);
BRWalletManagerDisconnect (manager);
sleep(1);
BRWalletManagerStop (manager);
BRWalletManagerFree (manager);
// Verification
success = BRRunTestWalletManagerSyncTestVerifyEventSequence(&state,
1,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_CREATED),
BRWalletManagerSyncEventForWalletType(BITCOIN_WALLET_CREATED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_CONNECTED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_STARTED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_STOPPED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_DISCONNECTED),
},
6,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_PROGRESS),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_BLOCK_HEIGHT_UPDATED),
},
2);
if (!success) {
fprintf(stderr, "***FAILED*** %s:%d: BRRunTestWalletManagerSyncTestVerifyEventSequence failed\n", testName, __LINE__);
return success;
}
success = BRRunTestWalletManagerSyncTestVerifyEventPairs (&state);
if (!success) {
fprintf(stderr, "***FAILED*** %s:%d: BRRunTestWalletManagerSyncTestVerifyEventPairs failed\n", testName, __LINE__);
return success;
}
// Test teardown
BRRunTestWalletManagerSyncTestTeardown (&state);
}
printf("Testing BRWalletManager repeated disconnect attempts...\n");
{
// Test setup
BRRunTestWalletManagerSyncState state = {0};
BRRunTestWalletManagerSyncTestSetupAsDefault (&state, blockHeight);
BRWalletManager manager = BRRunTestWalletManagerSyncBwmSetup (mode, &state, paperKey, storagePath, earliestKeyTime, blockHeight, isBTC, isMainnet);
BRWalletManagerStart (manager);
// repeated disconnect attempts
BRWalletManagerDisconnect (manager);
sleep(1);
BRWalletManagerDisconnect (manager);
sleep(1);
BRWalletManagerDisconnect (manager);
sleep(1);
BRWalletManagerStop (manager);
BRWalletManagerFree (manager);
// Verification
success = BRRunTestWalletManagerSyncTestVerifyEventSequence(&state,
1,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_CREATED),
BRWalletManagerSyncEventForWalletType(BITCOIN_WALLET_CREATED)
},
2,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_PROGRESS),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_BLOCK_HEIGHT_UPDATED),
},
2);
if (!success) {
fprintf(stderr, "***FAILED*** %s:%d: BRRunTestWalletManagerSyncTestVerifyEventSequence failed\n", testName, __LINE__);
return success;
}
success = BRRunTestWalletManagerSyncTestVerifyEventPairs (&state);
if (!success) {
fprintf(stderr, "***FAILED*** %s:%d: BRRunTestWalletManagerSyncTestVerifyEventPairs failed\n", testName, __LINE__);
return success;
}
// Test teardown
BRRunTestWalletManagerSyncTestTeardown (&state);
}
printf("Testing BRWalletManager connect, scan, disconnect...\n");
{
// Test setup
BRRunTestWalletManagerSyncState state = {0};
BRRunTestWalletManagerSyncTestSetupAsDefault (&state, blockHeight);
BRWalletManager manager = BRRunTestWalletManagerSyncBwmSetup (mode, &state, paperKey, storagePath, earliestKeyTime, blockHeight, isBTC, isMainnet);
BRWalletManagerStart (manager);
// connect, scan, disconnect
BRWalletManagerConnect (manager);
sleep(1);
BRWalletManagerScan (manager);
sleep(1);
BRWalletManagerDisconnect (manager);
sleep(1);
BRWalletManagerStop (manager);
BRWalletManagerFree (manager);
// Verification
// multiple cases accepted as with P2P, depending on peer connections, could see:
// - a reconnect, if the BRWalletManagerScan was after the P2P connection and after receiving blocks (case 1)
// - a sync restart, if the BRWalletManagerScan was after the P2P connection but before the sync received blocks (case 2)
// - a sync, if the BRWalletManagerScan beat the P2P connections being established (case 3)
success = (BRRunTestWalletManagerSyncTestVerifyEventSequence(&state,
1,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_CREATED),
BRWalletManagerSyncEventForWalletType(BITCOIN_WALLET_CREATED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_CONNECTED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_STARTED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_STOPPED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_DISCONNECTED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_CONNECTED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_STARTED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_STOPPED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_DISCONNECTED),
},
10,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_PROGRESS),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_BLOCK_HEIGHT_UPDATED),
},
2) ||
BRRunTestWalletManagerSyncTestVerifyEventSequence(&state,
1,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_CREATED),
BRWalletManagerSyncEventForWalletType(BITCOIN_WALLET_CREATED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_CONNECTED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_STARTED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_STOPPED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_STARTED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_STOPPED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_DISCONNECTED),
},
8,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_PROGRESS),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_BLOCK_HEIGHT_UPDATED),
},
2) ||
BRRunTestWalletManagerSyncTestVerifyEventSequence(&state,
1,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_CREATED),
BRWalletManagerSyncEventForWalletType(BITCOIN_WALLET_CREATED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_CONNECTED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_STARTED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_STOPPED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_DISCONNECTED),
},
6,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_PROGRESS),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_BLOCK_HEIGHT_UPDATED),
},
2));
if (!success) {
fprintf(stderr, "***FAILED*** %s:%d: BRRunTestWalletManagerSyncTestVerifyEventSequence failed\n", testName, __LINE__);
return success;
}
success = BRRunTestWalletManagerSyncTestVerifyEventPairs (&state);
if (!success) {
fprintf(stderr, "***FAILED*** %s:%d: BRRunTestWalletManagerSyncTestVerifyEventPairs failed\n", testName, __LINE__);
return success;
}
// Test teardown
BRRunTestWalletManagerSyncTestTeardown (&state);
}
printf("Testing BRWalletManager scan, connect, disconnect...\n");
{
// Test setup
BRRunTestWalletManagerSyncState state = {0};
BRRunTestWalletManagerSyncTestSetupAsDefault (&state, blockHeight);
BRWalletManager manager = BRRunTestWalletManagerSyncBwmSetup (mode, &state, paperKey, storagePath, earliestKeyTime, blockHeight, isBTC, isMainnet);
BRWalletManagerStart (manager);
// scan, connect, disconnect
BRWalletManagerScan (manager);
sleep(1);
BRWalletManagerConnect (manager);
sleep(1);
BRWalletManagerDisconnect (manager);
sleep(1);
BRWalletManagerStop (manager);
BRWalletManagerFree (manager);
// Verification
success = BRRunTestWalletManagerSyncTestVerifyEventSequence(&state,
1,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_CREATED),
BRWalletManagerSyncEventForWalletType(BITCOIN_WALLET_CREATED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_CONNECTED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_STARTED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_STOPPED),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_DISCONNECTED),
},
6,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_PROGRESS),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_BLOCK_HEIGHT_UPDATED),
},
2);
if (!success) {
fprintf(stderr, "***FAILED*** %s:%d: BRRunTestWalletManagerSyncTestVerifyEventSequence failed\n", testName, __LINE__);
return success;
}
success = BRRunTestWalletManagerSyncTestVerifyEventPairs (&state);
if (!success) {
fprintf(stderr, "***FAILED*** %s:%d: BRRunTestWalletManagerSyncTestVerifyEventPairs failed\n", testName, __LINE__);
return success;
}
// Test teardown
BRRunTestWalletManagerSyncTestTeardown (&state);
}
printf("Testing BRWalletManager scan, disconnect...\n");
{
// Test setup
BRRunTestWalletManagerSyncState state = {0};
BRRunTestWalletManagerSyncTestSetupAsDefault (&state, blockHeight);
BRWalletManager manager = BRRunTestWalletManagerSyncBwmSetup (mode, &state, paperKey, storagePath, earliestKeyTime, blockHeight, isBTC, isMainnet);
BRWalletManagerStart (manager);
// scan, disconnect
BRWalletManagerScan (manager);
sleep(1);
BRWalletManagerDisconnect (manager);
sleep(1);
BRWalletManagerStop (manager);
BRWalletManagerFree (manager);
// Verification
success = BRRunTestWalletManagerSyncTestVerifyEventSequence(&state,
1,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_CREATED),
BRWalletManagerSyncEventForWalletType(BITCOIN_WALLET_CREATED),
},
2,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_SYNC_PROGRESS),
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_BLOCK_HEIGHT_UPDATED),
},
2);
if (!success) {
fprintf(stderr, "***FAILED*** %s:%d: BRRunTestWalletManagerSyncTestVerifyEventSequence failed\n", testName, __LINE__);
return success;
}
success = BRRunTestWalletManagerSyncTestVerifyEventPairs (&state);
if (!success) {
fprintf(stderr, "***FAILED*** %s:%d: BRRunTestWalletManagerSyncTestVerifyEventPairs failed\n", testName, __LINE__);
return success;
}
// Test teardown
BRRunTestWalletManagerSyncTestTeardown (&state);
}
printf("Testing BRWalletManager threading...\n");
if (mode == CRYPTO_SYNC_MODE_P2P_ONLY) {
// TODO(fix): There is a thread-related issue in BRPeerManager/BRPeer where we have a use after free; re-enable once that is fixed
fprintf(stderr, "***WARNING*** %s:%d: BRWalletManager threading test is disabled for CRYPTO_SYNC_MODE_P2P_ONLY\n", testName, __LINE__);
} else {
// Test setup
BRRunTestWalletManagerSyncState state = {0};
BRRunTestWalletManagerSyncTestSetup (&state, blockHeight, 1);
BRWalletManager manager = BRRunTestWalletManagerSyncBwmSetup (mode, &state, paperKey, storagePath, earliestKeyTime, blockHeight, isBTC, isMainnet);
BRWalletManagerStart (manager);
BRRunTestWalletManagerSyncThreadState threadState = {0, manager};
pthread_t connectThread = (pthread_t) NULL, disconnectThread = (pthread_t) NULL, scanThread = (pthread_t) NULL;
success = (0 == pthread_create (&connectThread, NULL, _testBRWalletManagerConnectThread, (void*) &threadState) &&
0 == pthread_create (&disconnectThread, NULL, _testBRWalletManagerDisconnectThread, (void*) &threadState) &&
0 == pthread_create (&scanThread, NULL, _testBRWalletManagerScanThread, (void*) &threadState));
if (!success) {
fprintf(stderr, "***FAILED*** %s:%d: pthread_creates failed\n", testName, __LINE__);
return success;
}
sleep (5);
threadState.kill = 1;
success = (0 == pthread_join (connectThread, NULL) &&
0 == pthread_join (disconnectThread, NULL) &&
0 == pthread_join (scanThread, NULL));
if (!success) {
fprintf(stderr, "***FAILED*** %s:%d: pthread_joins failed\n", testName, __LINE__);
return success;
}
BRWalletManagerStop (manager);
BRWalletManagerFree (manager);
// Verification
success = BRRunTestWalletManagerSyncTestVerifyEventSequence(&state,
0,
(BRWalletManagerSyncEvent []) {
BRWalletManagerSyncEventForWalletManagerType (BITCOIN_WALLET_MANAGER_CREATED),
BRWalletManagerSyncEventForWalletType(BITCOIN_WALLET_CREATED)},
2,
NULL,
0);
if (!success) {
fprintf(stderr, "***FAILED*** %s:%d: BRRunTestWalletManagerSyncTestVerifyEventSequence failed\n", testName, __LINE__);
return success;
}
success = BRRunTestWalletManagerSyncTestVerifyEventPairs (&state);
if (!success) {
fprintf(stderr, "***FAILED*** %s:%d: BRRunTestWalletManagerSyncTestVerifyEventPairs failed\n", testName, __LINE__);
return success;