Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
use {super::*, crate::test_psql::create_shares_for_user, tokio_util::sync::CancellationToken};

pub(crate) static BATCH_COUNTER: AtomicUsize = AtomicUsize::new(0);

#[tokio::test]
async fn test_sync_batch_endpoint() {
    let server = TestServer::spawn_with_db().await;
    let db_url = server.database_url().unwrap();
    setup_test_schema(db_url.clone()).await.unwrap();

    let test_shares = create_test_shares(5, 800000);
    let test_block = create_test_block(800000);

    let batch = ShareBatch {
        block: Some(test_block.clone()),
        shares: test_shares,
        hostname: "test-node-1".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 5,
        start_id: 1,
        end_id: 5,
    };

    let response: SyncResponse = server.post_json("/sync/batch", &batch).await;

    assert_eq!(response.status, "OK");
    assert_eq!(response.received_count, 5);
    assert_eq!(response.batch_id, batch.batch_id);
    assert!(response.error_message.is_none());

    let pool = sqlx::PgPool::connect(&db_url).await.unwrap();

    let stored_shares: Vec<(i64, String)> =
        sqlx::query_as("SELECT id, origin FROM remote_shares WHERE origin = $1")
            .bind(&batch.hostname)
            .fetch_all(&pool)
            .await
            .unwrap();
    assert_eq!(stored_shares.len(), 5);

    let stored_block: Option<(i32, String)> =
        sqlx::query_as("SELECT blockheight, blockhash FROM blocks WHERE blockheight = $1")
            .bind(test_block.blockheight)
            .fetch_optional(&pool)
            .await
            .unwrap();
    assert!(stored_block.is_some());
    assert_eq!(stored_block.unwrap().1, test_block.blockhash);

    pool.close().await;
}

#[tokio::test]
async fn test_sync_with_auth() {
    let mut server = TestServer::spawn_with_db_args("--admin-token verysecrettoken").await;

    let db_url = server.database_url().unwrap();

    setup_test_schema(db_url.clone()).await.unwrap();

    let test_shares = create_test_shares(5, 800000);
    let test_block = create_test_block(800000);

    let batch = ShareBatch {
        block: Some(test_block.clone()),
        shares: test_shares,
        hostname: "test-node-1".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 5,
        start_id: 1,
        end_id: 5,
    };

    let fail: Response = server.post_json_raw("/sync/batch", &batch).await;

    assert_eq!(fail.status(), StatusCode::UNAUTHORIZED);

    server.admin_token = Some("verysecrettoken".into());

    let succ: SyncResponse = server.post_json("/sync/batch", &batch).await;

    assert_eq!(succ.status, "OK");
    assert_eq!(succ.received_count, 5);
    assert_eq!(succ.batch_id, batch.batch_id);
    assert!(succ.error_message.is_none());

    let pool = sqlx::PgPool::connect(&db_url).await.unwrap();

    let stored_shares: Vec<(i64, String)> =
        sqlx::query_as("SELECT id, origin FROM remote_shares WHERE origin = $1")
            .bind(&batch.hostname)
            .fetch_all(&pool)
            .await
            .unwrap();
    assert_eq!(stored_shares.len(), 5);

    let stored_block: Option<(i32, String)> =
        sqlx::query_as("SELECT blockheight, blockhash FROM blocks WHERE blockheight = $1")
            .bind(test_block.blockheight)
            .fetch_optional(&pool)
            .await
            .unwrap();

    assert!(stored_block.is_some());
    assert_eq!(stored_block.unwrap().1, test_block.blockhash);

    pool.close().await;
}

#[tokio::test]
async fn test_sync_empty_batch() {
    let server = TestServer::spawn_with_db().await;
    let db_url = server.database_url().unwrap();
    setup_test_schema(db_url.clone()).await.unwrap();

    let batch = ShareBatch {
        block: None,
        shares: vec![],
        hostname: "test-node-empty".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 0,
        start_id: 1,
        end_id: 1,
    };

    let response: SyncResponse = server.post_json("/sync/batch", &batch).await;

    assert_eq!(response.status, "OK");
    assert_eq!(response.received_count, 0);

    let pool = sqlx::PgPool::connect(&db_url).await.unwrap();

    let stored_shares: Vec<(i64, String)> =
        sqlx::query_as("SELECT id, origin FROM remote_shares WHERE origin = $1")
            .bind(&batch.hostname)
            .fetch_all(&pool)
            .await
            .unwrap();
    assert_eq!(stored_shares.len(), 0);

    let stored_block: Option<(i32, String)> =
        sqlx::query_as("SELECT blockheight, blockhash FROM blocks LIMIT 1")
            .fetch_optional(&pool)
            .await
            .unwrap();
    assert!(stored_block.is_none());

    pool.close().await;
}

#[tokio::test]
#[ignore]
async fn test_sync_batch_block_find_notification_e2e() {
    let channel = alerts::generate_test_channel();

    let server = TestServer::spawn_with_db_args(format!("--alerts-ntfy-channel {channel}")).await;
    let db_url = server.database_url().unwrap();
    setup_test_schema(db_url.clone()).await.unwrap();

    let blockheight: i32 = 9_999_999;
    let sentinel_hash = format!("deadbeefdeadbeef{:048x}", blockheight);
    assert_eq!(sentinel_hash.len(), 64);
    let hostname = "test-block-find-e2e".to_string();

    let mut shares = create_shares_for_user("test_user", &[blockheight], 1);
    shares.extend(create_shares_for_user("other_user", &[blockheight], 2));
    assert_eq!(shares.len(), 2);

    let shares_batch = ShareBatch {
        block: None,
        shares: shares.clone(),
        hostname: hostname.clone(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: shares.len(),
        start_id: 1,
        end_id: 2,
    };
    let resp1: SyncResponse = server.post_json("/sync/batch", &shares_batch).await;
    assert_eq!(resp1.status, "OK");
    assert_eq!(resp1.received_count, 2);

    let pool = sqlx::PgPool::connect(&db_url).await.unwrap();

    let stored_shares: i64 =
        sqlx::query_scalar("SELECT COUNT(*) FROM remote_shares WHERE origin = $1")
            .bind(&hostname)
            .fetch_one(&pool)
            .await
            .unwrap();
    assert_eq!(stored_shares, 2);

    let accounts: Vec<(String, Option<String>, i64)> =
        sqlx::query_as("SELECT username, lnurl, total_diff FROM accounts ORDER BY username")
            .fetch_all(&pool)
            .await
            .unwrap();
    assert_eq!(accounts.len(), 2);
    assert_eq!(accounts[0].0, "other_user");
    assert_eq!(accounts[1].0, "test_user");
    assert!(
        accounts[0].1.is_some(),
        "other_user lnurl should be populated"
    );
    assert!(
        accounts[1].1.is_some(),
        "test_user lnurl should be populated"
    );
    assert!(accounts[0].2 > 0);
    assert!(accounts[1].2 > 0);

    // --- Batch 2: block-only batch triggering payouts + notification ---
    let test_block = FoundBlockRecord {
        id: blockheight,
        blockheight,
        blockhash: sentinel_hash.clone(),
        confirmed: Some(true),
        workername: Some("test_worker".to_string()),
        username: Some("test_user".to_string()),
        diff: Some(1_000_000.0),
        coinbasevalue: Some(625_000_000),
        rewards_processed: Some(false),
    };

    let block_batch = ShareBatch {
        block: Some(test_block.clone()),
        shares: vec![],
        hostname: hostname.clone(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 0,
        start_id: 3,
        end_id: 3,
    };
    let resp2: SyncResponse = server.post_json("/sync/batch", &block_batch).await;
    assert_eq!(resp2.status, "OK");

    let stored: (i32, String) =
        sqlx::query_as("SELECT blockheight, blockhash FROM blocks WHERE blockheight = $1")
            .bind(blockheight)
            .fetch_one(&pool)
            .await
            .expect("block row should exist after upsert");
    assert_eq!(stored.1, sentinel_hash);

    let payouts: Vec<(String, i64, i64, String)> = sqlx::query_as(
        "SELECT a.username, p.amount, p.diff_paid, p.status
           FROM payouts p JOIN accounts a ON a.id = p.account_id
           WHERE p.blockheight_end = $1
           ORDER BY a.username",
    )
    .bind(blockheight)
    .fetch_all(&pool)
    .await
    .unwrap();
    assert_eq!(payouts.len(), 2, "expected finder + one participant payout");

    let (other, test) = (&payouts[0], &payouts[1]);
    assert_eq!(other.0, "other_user");
    assert_eq!(other.3, "pending");
    assert!(other.1 > 0, "participant should receive a non-zero amount");

    assert_eq!(test.0, "test_user");
    assert_eq!(test.3, "success");
    assert_eq!(test.1, 0, "finder payout amount should be zero");

    tokio::time::sleep(Duration::from_millis(2500)).await;
    let received = crate::alerts::listen_for_ntfy_messages(&channel, Duration::from_secs(10)).await;
    assert!(
        received.len() >= 2,
        "expected >=2 messages (block + attachment), got {}: {received:?}",
        received.len()
    );

    let block_msg = received
        .iter()
        .find(|m| m.title.as_deref().unwrap_or("").contains("New Block Found"))
        .expect("block-found notification missing");
    let block_title = block_msg.title.clone().unwrap_or_default();
    let block_body = block_msg.message.clone().unwrap_or_default();
    assert!(
        block_title.contains("[TEST]"),
        "title missing [TEST]: {block_title:?}"
    );
    assert!(
        block_title.contains(&blockheight.to_string()),
        "title missing height: {block_title:?}"
    );
    assert!(
        block_body.contains("[TEST]"),
        "body missing [TEST]: {block_body:?}"
    );
    assert!(
        block_body.contains("6.25000000 BTC"),
        "body missing coinbase: {block_body:?}"
    );
    assert!(
        block_body.contains("test_user"),
        "body missing miner: {block_body:?}"
    );
    assert_eq!(block_msg.priority, Some(5));

    let attach_msg = received
        .iter()
        .find(|m| m.attachment.is_some())
        .expect("payouts attachment message missing");
    let attachment = attach_msg.attachment.as_ref().unwrap();
    assert_eq!(attachment.name, format!("payouts-{blockheight}.json"));
    assert!(
        attach_msg.title.as_deref().unwrap_or("").contains("[TEST]"),
        "attachment title missing [TEST]: {:?}",
        attach_msg.title
    );

    // Fetch the attachment and verify its payouts match the DB state.
    let body = reqwest::get(&attachment.url)
        .await
        .unwrap()
        .text()
        .await
        .unwrap();
    let payouts_json: serde_json::Value = serde_json::from_str(&body).unwrap();
    let payouts_arr = payouts_json.as_array().expect("payouts JSON is an array");
    // Only other_user has a pending payout (finder has status='success').
    assert_eq!(
        payouts_arr.len(),
        1,
        "expected 1 pending payout, got: {body}"
    );
    let entry = &payouts_arr[0];
    assert_eq!(entry["btc_address"], "other_user");
    assert!(entry["amount_sats"].as_i64().unwrap() > 0);

    pool.close().await;
}

#[tokio::test]
async fn test_sync_batch_with_block_only() {
    let server = TestServer::spawn_with_db().await;
    let db_url = server.database_url().unwrap();
    setup_test_schema(db_url.clone()).await.unwrap();

    let test_block = create_test_block(800001);

    let batch = ShareBatch {
        block: Some(test_block.clone()),
        shares: vec![],
        hostname: "test-node-block".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 0,
        start_id: 1,
        end_id: 1,
    };

    let response: SyncResponse = server.post_json("/sync/batch", &batch).await;

    assert_eq!(response.status, "OK");
    assert_eq!(response.received_count, 0);

    let pool = sqlx::PgPool::connect(&db_url).await.unwrap();

    let stored_shares: Vec<(i64, String)> =
        sqlx::query_as("SELECT id, origin FROM remote_shares WHERE origin = $1")
            .bind(&batch.hostname)
            .fetch_all(&pool)
            .await
            .unwrap();
    assert_eq!(stored_shares.len(), 0);

    let stored_block: Option<(i32, String)> =
        sqlx::query_as("SELECT blockheight, blockhash FROM blocks WHERE blockheight = $1")
            .bind(test_block.blockheight)
            .fetch_optional(&pool)
            .await
            .unwrap();
    assert!(stored_block.is_some());
    assert_eq!(stored_block.unwrap().1, test_block.blockhash);

    pool.close().await;
}

#[tokio::test]
#[timeout(90000)]
#[ignore]
async fn test_sync_large_batch() {
    let record_count_in_large_batch = 40000;
    let server = TestServer::spawn_with_db().await;
    let db_url = server.database_url().unwrap();
    setup_test_schema(db_url.clone()).await.unwrap();

    let test_shares = create_test_shares(record_count_in_large_batch, 800002);
    let test_block = create_test_block(800002);

    let batch = ShareBatch {
        block: Some(test_block.clone()),
        shares: test_shares,
        hostname: "test-node-large".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: record_count_in_large_batch as usize,
        start_id: 1,
        end_id: record_count_in_large_batch as i64,
    };

    let response: SyncResponse = server.post_json("/sync/batch", &batch).await;

    assert_eq!(response.status, "OK");
    assert_eq!(response.received_count as u32, record_count_in_large_batch);

    let pool = sqlx::PgPool::connect(&db_url).await.unwrap();

    let stored_shares: Vec<(i64, String)> =
        sqlx::query_as("SELECT id, origin FROM remote_shares WHERE origin = $1")
            .bind(&batch.hostname)
            .fetch_all(&pool)
            .await
            .unwrap();

    assert_eq!(stored_shares.len() as u32, record_count_in_large_batch);

    let stored_block: Option<(i32, String)> =
        sqlx::query_as("SELECT blockheight, blockhash FROM blocks WHERE blockheight = $1")
            .bind(test_block.blockheight)
            .fetch_optional(&pool)
            .await
            .unwrap();

    assert!(stored_block.is_some());
    assert_eq!(stored_block.unwrap().1, test_block.blockhash);

    pool.close().await;
}

#[tokio::test]
async fn test_sync_multiple_batches_different_blocks() {
    let server = TestServer::spawn_with_db().await;
    let db_url = server.database_url().unwrap();
    setup_test_schema(db_url.clone()).await.unwrap();

    for block_height in 800010..800015 {
        let test_shares = create_test_shares(10, block_height);
        let test_block = create_test_block(block_height);

        let batch = ShareBatch {
            block: Some(test_block),
            shares: test_shares,
            hostname: format!("test-node-{}", block_height),
            batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
            total_shares: 10,
            start_id: (block_height - 800010) * 10 + 1,
            end_id: (block_height - 800010 + 1) * 10,
        };

        let response: SyncResponse = server.post_json("/sync/batch", &batch).await;
        assert_eq!(response.status, "OK");
        assert_eq!(response.received_count, 10);
    }

    let pool = sqlx::PgPool::connect(&db_url).await.unwrap();

    let stored_shares: Vec<(i64, String)> = sqlx::query_as("SELECT id, origin FROM remote_shares")
        .fetch_all(&pool)
        .await
        .unwrap();

    assert_eq!(stored_shares.len(), 50);

    for block_height in 800010..800015 {
        let test_block = create_test_block(block_height);
        let stored_block: Option<(i32, String)> =
            sqlx::query_as("SELECT blockheight, blockhash FROM blocks WHERE blockheight = $1")
                .bind(test_block.blockheight)
                .fetch_optional(&pool)
                .await
                .unwrap();

        assert!(stored_block.is_some());
        assert_eq!(stored_block.unwrap().1, test_block.blockhash);
    }

    pool.close().await;
}

#[tokio::test]
async fn test_sync_duplicate_batch_id() {
    // batch_id serves only as validation that the synced batch matches
    // test acts as a canary against changing this behavior without consideration

    let server = TestServer::spawn_with_db().await;
    let db_url = server.database_url().unwrap();
    setup_test_schema(db_url.clone()).await.unwrap();

    let batch_id = BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64;
    let test_shares = create_test_shares(3, 800020);

    let batch1 = ShareBatch {
        block: None,
        shares: test_shares.clone(),
        hostname: "test-node-dup1".to_string(),
        batch_id,
        total_shares: 3,
        start_id: 1,
        end_id: 3,
    };

    let batch2 = ShareBatch {
        block: None,
        shares: test_shares,
        hostname: "test-node-dup2".to_string(),
        batch_id,
        total_shares: 3,
        start_id: 4,
        end_id: 6,
    };

    let response1: SyncResponse = server.post_json("/sync/batch", &batch1).await;
    assert_eq!(response1.status, "OK");

    let response2: SyncResponse = server.post_json("/sync/batch", &batch2).await;
    assert_eq!(response2.status, "OK");

    let pool = sqlx::PgPool::connect(&db_url).await.unwrap();

    let stored_shares: Vec<(i64, String)> = sqlx::query_as("SELECT id, origin FROM remote_shares")
        .fetch_all(&pool)
        .await
        .unwrap();

    assert_eq!(stored_shares.len() as u32, 6);

    let stored_block: Option<(i32, String)> =
        sqlx::query_as("SELECT blockheight, blockhash FROM blocks LIMIT 1")
            .fetch_optional(&pool)
            .await
            .unwrap();

    assert!(stored_block.is_none());

    pool.close().await;
}

#[tokio::test]
async fn test_sync_batch_creates_accounts() {
    let server = TestServer::spawn_with_db().await;
    let db_url = server.database_url().unwrap();
    setup_test_schema(db_url.clone()).await.unwrap();

    let test_shares = create_test_shares(5, 800000);
    let test_block = create_test_block(800000);

    let batch = ShareBatch {
        block: Some(test_block.clone()),
        shares: test_shares,
        hostname: "test-node".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 5,
        start_id: 1,
        end_id: 5,
    };

    let response: SyncResponse = server.post_json("/sync/batch", &batch).await;

    assert_eq!(response.status, "OK");
    assert_eq!(response.received_count, 5);

    let database = Database::new(db_url.clone()).await.unwrap();

    for i in 0..5 {
        let username = format!("user_{}", i);
        let account = database.get_account(&username).await.unwrap().unwrap();
        assert_eq!(account.btc_address, username);
        assert_eq!(
            account.ln_address,
            Some(format!("lnurl{}@test.gov", i)),
            "Account should have lnurl from share"
        );
        assert_eq!(
            account.total_diff,
            1000 + i,
            "Account should have diff from single share"
        );
    }
}

#[tokio::test]
async fn test_sync_batch_with_migrate_accounts_flag() {
    let psql_binpath = match Command::new("pg_config").arg("--bindir").output() {
        Ok(output) if output.status.success() => String::from_utf8(output.stdout)
            .ok()
            .map(|s| PathBuf::from(s.trim())),
        _ => None,
    };
    let pg_db = PgTempDB::from_builder(PgTempDBBuilder {
        initdb_args: Default::default(),
        temp_dir_prefix: None,
        db_user: None,
        password: None,
        port: None,
        dbname: None,
        persist_data_dir: false,
        dump_path: None,
        load_path: None,
        server_configs: Default::default(),
        bin_path: psql_binpath,
    });

    let db_url = pg_db.connection_uri();
    setup_test_schema(db_url.clone()).await.unwrap();

    insert_test_remote_shares(db_url.clone(), 5, 800028)
        .await
        .unwrap();
    insert_test_remote_shares(db_url.clone(), 3, 800029)
        .await
        .unwrap();
    insert_test_remote_shares(db_url.clone(), 5, 800030)
        .await
        .unwrap();

    let server = TestServer::spawn_with_db_override(["--migrate-accounts"], pg_db).await;

    let database = Database::new(db_url.clone()).await.unwrap();
    let test_shares = create_test_shares(2, 800031);
    let test_block = create_test_block(800031);

    let batch = ShareBatch {
        block: Some(test_block.clone()),
        shares: test_shares,
        hostname: "test-node-migrate".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 2,
        start_id: 100,
        end_id: 101,
    };

    let mut response: SyncResponse = server.post_json("/sync/batch", &batch).await;
    let mut attempts = 0;
    while response.status == "UNAVAILABLE" && attempts < 10 {
        sleep(Duration::from_millis(100)).await;
        response = server.post_json("/sync/batch", &batch).await;
        attempts += 1;
    }

    assert_eq!(response.status, "OK");
    assert_eq!(response.received_count, 2);

    let account = database.get_account("user_0").await.unwrap().unwrap();
    assert_eq!(account.btc_address, "user_0");
    assert_eq!(
        account.ln_address,
        Some("lnurl0@test.gov".to_string()),
        "Account should have lnurl from migrated share"
    );
    assert_eq!(
        account.total_diff, 4000,
        "Account in both sync and migration"
    );

    let account_new = database.get_account("user_4").await.unwrap().unwrap();
    assert_eq!(account_new.btc_address, "user_4");
    assert_eq!(
        account_new.total_diff, 2008,
        "Account not in current sync, handled by migration"
    );
}

#[tokio::test]
async fn test_sync_endpoint_to_endpoint() {
    let source_server = TestServer::spawn_with_db().await;
    let target_server = TestServer::spawn_with_db().await;

    let source_db_url = source_server.database_url().unwrap();
    setup_test_schema(source_db_url.clone()).await.unwrap();

    let target_db_url = target_server.database_url().unwrap();
    setup_test_schema(target_db_url.clone()).await.unwrap();

    for block_height in 800030..=800032 {
        insert_test_shares(source_db_url.clone(), 100, block_height)
            .await
            .unwrap();
        insert_test_block(source_db_url.clone(), block_height)
            .await
            .unwrap();
    }
    insert_test_shares(source_db_url.clone(), 1, 800033)
        .await
        .unwrap();

    let sync_sender = Sync::default()
        .with_endpoint(target_server.url().to_string())
        .with_database_url(source_db_url.clone())
        .with_terminate_when_complete(true)
        .with_temp_file();

    let client = reqwest::Client::new();

    let health_check = client
        .get(target_server.url().join("/sync/batch").unwrap())
        .send()
        .await;

    assert!(health_check.is_ok());

    sync_sender
        .run(CancellationToken::new())
        .await
        .expect("Syncing between servers failed!");

    let pool = sqlx::PgPool::connect(&target_db_url).await.unwrap();

    let stored_shares: Vec<(i64, String)> = sqlx::query_as("SELECT id, origin FROM remote_shares")
        .fetch_all(&pool)
        .await
        .unwrap();

    assert_eq!(stored_shares.len() as u32, 300);

    let stored_block: Option<(i32, String)> =
        sqlx::query_as("SELECT blockheight, blockhash FROM blocks ORDER BY blockheight LIMIT 1")
            .fetch_optional(&pool)
            .await
            .unwrap();

    assert!(stored_block.is_some());
    assert_eq!(stored_block.unwrap().0, 800030);

    let block_count: (i64, i32, i32) =
        sqlx::query_as("SELECT count(*), min(blockheight), max(blockheight) FROM blocks")
            .fetch_one(&pool)
            .await
            .unwrap();

    assert_eq!(block_count.0, 3);
    assert_eq!(block_count.1, 800030);
    assert_eq!(block_count.2, 800032);

    pool.close().await;
}

#[tokio::test]
async fn test_sync_batch_creates_block_count() {
    let server = TestServer::spawn_with_db().await;
    let db_url = server.database_url().unwrap();
    setup_test_schema(db_url.clone()).await.unwrap();

    let shares = create_shares_for_user(
        "bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297",
        &[100000, 100001, 100002],
        1,
    );

    let batch = ShareBatch {
        block: None,
        shares,
        hostname: "test-node".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 3,
        start_id: 1,
        end_id: 3,
    };

    let response: SyncResponse = server.post_json("/sync/batch", &batch).await;
    assert_eq!(response.status, "OK");

    let pool = sqlx::PgPool::connect(&db_url).await.unwrap();

    let block_count: Option<(i64,)> = sqlx::query_as(
        "SELECT (data->>'block_count')::bigint FROM account_metadata am
         JOIN accounts a ON a.id = am.account_id
         WHERE a.username = $1",
    )
    .bind("bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297")
    .fetch_optional(&pool)
    .await
    .unwrap();

    assert!(block_count.is_some(), "account_metadata should exist");
    assert_eq!(block_count.unwrap().0, 3, "Should count 3 distinct blocks");

    pool.close().await;
}

#[tokio::test]
async fn test_sync_batch_block_count_deduplicates_within_batch() {
    let server = TestServer::spawn_with_db().await;
    let db_url = server.database_url().unwrap();
    setup_test_schema(db_url.clone()).await.unwrap();

    let shares = create_shares_for_user(
        "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq",
        &[200000, 200000, 200000, 200001],
        1,
    );

    let batch = ShareBatch {
        block: None,
        shares,
        hostname: "test-node".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 4,
        start_id: 1,
        end_id: 4,
    };

    let response: SyncResponse = server.post_json("/sync/batch", &batch).await;
    assert_eq!(response.status, "OK");

    let pool = sqlx::PgPool::connect(&db_url).await.unwrap();

    let block_count: Option<(i64,)> = sqlx::query_as(
        "SELECT (data->>'block_count')::bigint FROM account_metadata am
         JOIN accounts a ON a.id = am.account_id
         WHERE a.username = $1",
    )
    .bind("bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq")
    .fetch_optional(&pool)
    .await
    .unwrap();

    assert_eq!(
        block_count.unwrap().0,
        2,
        "Should count only 2 distinct blocks"
    );

    pool.close().await;
}

#[tokio::test]
async fn test_sync_batch_block_count_increments_across_batches() {
    let server = TestServer::spawn_with_db().await;
    let db_url = server.database_url().unwrap();
    setup_test_schema(db_url.clone()).await.unwrap();

    let shares1 =
        create_shares_for_user("3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy", &[300000, 300001], 1);
    let batch1 = ShareBatch {
        block: None,
        shares: shares1,
        hostname: "test-node".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 2,
        start_id: 1,
        end_id: 2,
    };

    let response1: SyncResponse = server.post_json("/sync/batch", &batch1).await;
    assert_eq!(response1.status, "OK");

    let pool = sqlx::PgPool::connect(&db_url).await.unwrap();

    let block_count1: (i64,) = sqlx::query_as(
        "SELECT (data->>'block_count')::bigint FROM account_metadata am
         JOIN accounts a ON a.id = am.account_id
         WHERE a.username = $1",
    )
    .bind("3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy")
    .fetch_one(&pool)
    .await
    .unwrap();

    assert_eq!(block_count1.0, 2, "Should have 2 blocks after first batch");

    let shares2 = create_shares_for_user(
        "3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy",
        &[300002, 300003, 300004],
        10,
    );
    let batch2 = ShareBatch {
        block: None,
        shares: shares2,
        hostname: "test-node".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 3,
        start_id: 10,
        end_id: 12,
    };

    let response2: SyncResponse = server.post_json("/sync/batch", &batch2).await;
    assert_eq!(response2.status, "OK");

    let block_count2: (i64,) = sqlx::query_as(
        "SELECT (data->>'block_count')::bigint FROM account_metadata am
         JOIN accounts a ON a.id = am.account_id
         WHERE a.username = $1",
    )
    .bind("3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy")
    .fetch_one(&pool)
    .await
    .unwrap();

    assert_eq!(
        block_count2.0, 5,
        "Should have 5 blocks total after second batch"
    );

    pool.close().await;
}

#[tokio::test]
async fn test_sync_batch_block_count_preserves_other_metadata() {
    let server = TestServer::spawn_with_db().await;
    let db_url = server.database_url().unwrap();
    setup_test_schema(db_url.clone()).await.unwrap();

    let shares1 = create_shares_for_user("1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2", &[400000], 1);
    let batch1 = ShareBatch {
        block: None,
        shares: shares1,
        hostname: "test-node".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 1,
        start_id: 1,
        end_id: 1,
    };

    let _: SyncResponse = server.post_json("/sync/batch", &batch1).await;

    let pool = sqlx::PgPool::connect(&db_url).await.unwrap();

    sqlx::query(
        "UPDATE account_metadata SET data = data || '{\"custom_field\": \"test_value\"}'::jsonb
         WHERE account_id = (SELECT id FROM accounts WHERE username = $1)",
    )
    .bind("1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2")
    .execute(&pool)
    .await
    .unwrap();

    let shares2 = create_shares_for_user("1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2", &[400001], 10);
    let batch2 = ShareBatch {
        block: None,
        shares: shares2,
        hostname: "test-node".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 1,
        start_id: 10,
        end_id: 10,
    };

    let _: SyncResponse = server.post_json("/sync/batch", &batch2).await;

    let metadata: (i64, String) = sqlx::query_as(
        "SELECT (data->>'block_count')::bigint, data->>'custom_field'
         FROM account_metadata am
         JOIN accounts a ON a.id = am.account_id
         WHERE a.username = $1",
    )
    .bind("1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2")
    .fetch_one(&pool)
    .await
    .unwrap();

    assert_eq!(metadata.0, 2, "block_count should be 2");
    assert_eq!(metadata.1, "test_value", "custom_field should be preserved");

    pool.close().await;
}

#[tokio::test]
async fn test_sync_batch_block_count_multiple_users() {
    let server = TestServer::spawn_with_db().await;
    let db_url = server.database_url().unwrap();
    setup_test_schema(db_url.clone()).await.unwrap();

    let mut shares = create_shares_for_user(
        "bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297",
        &[500000, 500001],
        1,
    );
    shares.extend(create_shares_for_user(
        "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq",
        &[500000, 500001, 500002],
        10,
    ));

    let batch = ShareBatch {
        block: None,
        shares,
        hostname: "test-node".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 5,
        start_id: 1,
        end_id: 12,
    };

    let response: SyncResponse = server.post_json("/sync/batch", &batch).await;
    assert_eq!(response.status, "OK");

    let pool = sqlx::PgPool::connect(&db_url).await.unwrap();

    let first_count: (i64,) = sqlx::query_as(
        "SELECT (data->>'block_count')::bigint FROM account_metadata am
         JOIN accounts a ON a.id = am.account_id
         WHERE a.username = $1",
    )
    .bind("bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297")
    .fetch_one(&pool)
    .await
    .unwrap();

    let second_count: (i64,) = sqlx::query_as(
        "SELECT (data->>'block_count')::bigint FROM account_metadata am
         JOIN accounts a ON a.id = am.account_id
         WHERE a.username = $1",
    )
    .bind("bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq")
    .fetch_one(&pool)
    .await
    .unwrap();

    assert_eq!(
        first_count.0, 2,
        "bc1p5d7rjq7g6rdk2yhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297 should have 2 blocks"
    );
    assert_eq!(
        second_count.0, 3,
        "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq should have 3 blocks"
    );

    pool.close().await;
}

#[tokio::test]
async fn test_sync_batch_block_count_ignores_failed_shares() {
    let server = TestServer::spawn_with_db().await;
    let db_url = server.database_url().unwrap();
    setup_test_schema(db_url.clone()).await.unwrap();

    let mut shares =
        create_shares_for_user("3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy", &[600000, 600001], 1);

    shares[1].result = Some(false);

    let batch = ShareBatch {
        block: None,
        shares,
        hostname: "test-node".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 2,
        start_id: 1,
        end_id: 2,
    };

    let response: SyncResponse = server.post_json("/sync/batch", &batch).await;
    assert_eq!(response.status, "OK");

    let pool = sqlx::PgPool::connect(&db_url).await.unwrap();

    let block_count: (i64,) = sqlx::query_as(
        "SELECT (data->>'block_count')::bigint FROM account_metadata am
         JOIN accounts a ON a.id = am.account_id
         WHERE a.username = $1",
    )
    .bind("3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy")
    .fetch_one(&pool)
    .await
    .unwrap();

    assert_eq!(
        block_count.0, 1,
        "Should only count block from successful share"
    );

    pool.close().await;
}

#[tokio::test]
async fn test_sync_batch_block_count_ignores_lower_blocks() {
    let server = TestServer::spawn_with_db().await;
    let db_url = server.database_url().unwrap();
    setup_test_schema(db_url.clone()).await.unwrap();

    let shares1 =
        create_shares_for_user("3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy", &[800005, 800006], 1);
    let batch1 = ShareBatch {
        block: None,
        shares: shares1,
        hostname: "test-node".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 2,
        start_id: 1,
        end_id: 2,
    };

    let _: SyncResponse = server.post_json("/sync/batch", &batch1).await;

    let pool = sqlx::PgPool::connect(&db_url).await.unwrap();

    let shares2 = create_shares_for_user(
        "3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy",
        &[800001, 800002, 800003],
        10,
    );
    let batch2 = ShareBatch {
        block: None,
        shares: shares2,
        hostname: "test-node".to_string(),
        batch_id: BATCH_COUNTER.fetch_add(1, Ordering::SeqCst) as u64,
        total_shares: 3,
        start_id: 10,
        end_id: 12,
    };

    let _: SyncResponse = server.post_json("/sync/batch", &batch2).await;

    let metadata: (i64, i32) = sqlx::query_as(
        "SELECT (data->>'block_count')::bigint, (data->>'highest_blockheight')::int
         FROM account_metadata am
         JOIN accounts a ON a.id = am.account_id
         WHERE a.username = $1",
    )
    .bind("3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy")
    .fetch_one(&pool)
    .await
    .unwrap();

    assert_eq!(
        metadata.0, 2,
        "Should still have 2 blocks - lower blocks not counted"
    );
    assert_eq!(
        metadata.1, 800006,
        "highest_blockheight unchanged at 800006"
    );

    pool.close().await;
}

pub trait SyncSendTestExt {
    fn with_database_url(self, database_url: String) -> Self;
    fn with_terminate_when_complete(self, terminate: bool) -> Self;
    fn with_temp_file(self) -> Self;
}

impl SyncSendTestExt for Sync {
    fn with_database_url(mut self, database_url: String) -> Self {
        self.database_url = database_url;
        self
    }

    fn with_terminate_when_complete(mut self, terminate: bool) -> Self {
        self.terminate_when_complete = terminate;
        self
    }

    fn with_temp_file(mut self) -> Self {
        self.id_file = tempdir()
            .unwrap()
            .path()
            .join("id.txt")
            .to_str()
            .unwrap()
            .to_string();
        self
    }
}