blvm-consensus 0.1.82

Bitcoin Commons BLVM: Direct mathematical implementation of Bitcoin consensus rules from the Orange Paper
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
use super::*;
use crate::constants::MAX_TARGET;

fn header_hash_meets(header: &BlockHeader) -> bool {
    if header.bits & 0x0080_0000 != 0 {
        return false;
    }
    let Ok(target) = expand_target(header.bits) else {
        return false;
    };
    if target.is_zero() {
        return false;
    }
    let bytes = serialize_header(header);
    let hash1 = Sha256::digest(bytes);
    let hash2 = Sha256::digest(hash1);
    let mut hash_bytes = [0u8; 32];
    hash_bytes.copy_from_slice(&hash2);
    let target_bytes = target.to_le_bytes();
    for i in (0..32).rev() {
        if hash_bytes[i] != target_bytes[i] {
            return hash_bytes[i] < target_bytes[i];
        }
    }
    true
}

#[test]
fn negative_or_zero_target_fails_and_equal_hash_passes() {
    let header = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 1,
        bits: 0x1d80_0001,
        nonce: 0,
    };
    assert!(!check_proof_of_work(&header).unwrap());

    let mut zero = header;
    zero.bits = 0x0300_0000;
    assert!(!check_proof_of_work(&zero).unwrap());

    let bits = 0x1d00ffff;
    let target = expand_target(bits).unwrap();
    assert!(proof_hash_meets_target(bits, &target).unwrap());
    let above = target.checked_add(U256::one()).unwrap();
    assert!(!proof_hash_meets_target(bits, &above).unwrap());
}

#[test]
fn test_get_next_work_required_insufficient_headers() {
    let header = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 1231006505,
        bits: 0x1d00ffff,
        nonce: 0,
    };

    let prev_headers = vec![header.clone()];
    let result = get_next_work_required(&header, &prev_headers);

    // Should return error when insufficient headers
    assert!(result.is_err());
}

#[test]
fn test_get_next_work_required_normal_adjustment() {
    let header1 = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 1000000,
        bits: 0x1d00ffff,
        nonce: 0,
    };

    let header2 = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 1000000 + (DIFFICULTY_ADJUSTMENT_INTERVAL * TARGET_TIME_PER_BLOCK), // Exactly 2 weeks later
        bits: 0x1d00ffff,
        nonce: 0,
    };

    let prev_headers = vec![header1, header2.clone()];
    let result = get_next_work_required(&header2, &prev_headers).unwrap();

    // Should return same difficulty (adjustment = 1.0)
    assert_eq!(result, 0x1d00ffff);
}

#[test]
fn test_difficulty_from_bits() {
    // Genesis bits 0x1d00ffff → difficulty 1.0
    let d = difficulty_from_bits(0x1d00ffff).unwrap();
    assert!(
        (d - 1.0).abs() < 0.01,
        "Genesis difficulty should be ~1.0, got {d}"
    );
    // Harder target (smaller mantissa) → higher difficulty
    let d_harder = difficulty_from_bits(0x1d000800).unwrap();
    assert!(d_harder > d, "Harder target should have higher difficulty");
}

#[test]
fn test_expand_target() {
    // Test a reasonable target that won't overflow (exponent = 0x1d = 29, which is > 3)
    // Use a target with exponent <= 3 to avoid the conservative limit
    let target = expand_target(0x0300ffff).unwrap(); // exponent = 3, mantissa = 0x00ffff
    assert!(!target.is_zero());
}

#[test]
fn test_check_proof_of_work_genesis() {
    // Use a reasonable header with valid target
    let header = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 1231006505,
        bits: 0x0300ffff, // Valid target (exponent = 3)
        nonce: 0,
    };

    assert_eq!(
        check_proof_of_work(&header).unwrap(),
        header_hash_meets(&header)
    );
}

// ============================================================================
// COMPREHENSIVE POW TESTS
// ============================================================================

#[test]
fn test_get_next_work_required_fast_blocks() {
    let header1 = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 1000000,
        bits: 0x1d00ffff,
        nonce: 0,
    };

    // Fast blocks: 1 week instead of 2 weeks
    let header2 = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 1000000 + (DIFFICULTY_ADJUSTMENT_INTERVAL * TARGET_TIME_PER_BLOCK / 2),
        bits: 0x1d00ffff,
        nonce: 0,
    };

    let prev_headers = vec![header1, header2.clone()];
    let result = get_next_work_required(&header2, &prev_headers).unwrap();

    // The current implementation clamps adjustment, so target may not change
    // Just verify it returns a valid result
    assert!(result <= 0x1d00ffff);
}

#[test]
fn test_get_next_work_required_slow_blocks() {
    let header1 = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 1000000,
        bits: 0x1d00ffff,
        nonce: 0,
    };

    // Slow blocks: 4 weeks instead of 2 weeks
    let header2 = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 1000000 + (DIFFICULTY_ADJUSTMENT_INTERVAL * TARGET_TIME_PER_BLOCK * 2),
        bits: 0x1d00ffff,
        nonce: 0,
    };

    let prev_headers = vec![header1, header2.clone()];
    let result = get_next_work_required(&header2, &prev_headers).unwrap();

    // The current implementation clamps adjustment, so target may not change
    // Just verify it returns a valid result
    assert!(result <= 0x1d00ffff);
}

#[test]
fn test_get_next_work_required_extreme_fast_blocks() {
    let header1 = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 1000000,
        bits: 0x1d00ffff,
        nonce: 0,
    };

    // Extremely fast blocks: 1 day instead of 2 weeks
    let header2 = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 1000000 + (DIFFICULTY_ADJUSTMENT_INTERVAL * TARGET_TIME_PER_BLOCK / 14),
        bits: 0x1d00ffff,
        nonce: 0,
    };

    let prev_headers = vec![header1, header2.clone()];
    let result = get_next_work_required(&header2, &prev_headers).unwrap();

    // The current implementation clamps adjustment, so target may not change
    // Just verify it returns a valid result
    assert!(result <= 0x1d00ffff);
}

#[test]
fn test_get_next_work_required_extreme_slow_blocks() {
    let header1 = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 1000000,
        bits: 0x1d00ffff,
        nonce: 0,
    };

    // Extremely slow blocks: 8 weeks instead of 2 weeks
    let header2 = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 1000000 + (DIFFICULTY_ADJUSTMENT_INTERVAL * TARGET_TIME_PER_BLOCK * 4),
        bits: 0x1d00ffff,
        nonce: 0,
    };

    let prev_headers = vec![header1, header2.clone()];
    let result = get_next_work_required(&header2, &prev_headers).unwrap();

    // The current implementation clamps adjustment, so target may not change
    // Just verify it returns a valid result
    assert!(result <= 0x1d00ffff);
}

#[test]
fn test_expand_target_zero_mantissa() {
    let result = expand_target(0x1d000000).unwrap();
    assert!(result.is_zero());
}

#[test]
fn test_expand_target_invalid_exponent_too_small() {
    let result = expand_target(0x0200ffff);
    assert!(result.is_err());
}

#[test]
fn test_expand_target_invalid_exponent_too_large() {
    let result = expand_target(0x2100ffff);
    assert!(result.is_err());
}

#[test]
fn test_expand_target_exponent_31() {
    let result = expand_target(0x1f00ffff).unwrap(); // exponent = 31
    assert!(!result.is_zero());
}

#[test]
fn test_expand_target_exponent_32_regtest_bits() {
    // Regtest minimum-difficulty ceiling uses nBits like 0x207fffff (exponent 32).
    let result = expand_target(0x2000ffff).unwrap();
    assert!(!result.is_zero());
}

#[test]
fn test_gbt_target_hex_regtest_minimum_difficulty() {
    let target = expand_target(0x207fffff).expect("regtest nBits");
    let hex = target.gbt_target_hex();
    assert_eq!(hex.len(), 64);
    assert_ne!(hex, "0".repeat(64));
    // Bitcoin display order: high limbs appear first in the hex string.
    assert!(hex.starts_with("7fffff"));
}

#[test]
fn test_expand_target_exponent_3() {
    let result = expand_target(0x0300ffff).unwrap();
    assert!(!result.is_zero());
}

#[test]
fn test_expand_target_exponent_4() {
    let result = expand_target(0x0400ffff).unwrap();
    assert!(!result.is_zero());
}

#[test]
fn test_expand_target_exponent_29() {
    let result = expand_target(0x1d00ffff).unwrap();
    assert!(!result.is_zero());
}

#[test]
fn test_check_proof_of_work_invalid_target() {
    // `expand_target` accepts exponent in 3..=32 (mainnet + regtest ceiling); exponent 31 is valid.
    // Exponent 2 is outside the allowed range and must error.
    let header = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 1231006505,
        bits: 0x0200ffff, // exponent = 2 (invalid)
        nonce: 0,
    };

    let result = check_proof_of_work(&header);
    assert!(result.is_err());
}

#[test]
fn test_check_proof_of_work_valid_target() {
    let header = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 1231006505,
        bits: 0x1d00ffff, // Valid target (exponent = 29)
        nonce: 0,
    };

    assert_eq!(
        check_proof_of_work(&header).unwrap(),
        header_hash_meets(&header)
    );
    let mut negative = header;
    negative.bits |= 0x0080_0000;
    assert!(!check_proof_of_work(&negative).unwrap());
}

#[test]
fn test_get_block_proof_genesis_mainnet() {
    let work = get_block_proof(0x1d00ffff).unwrap();
    assert_eq!(
        work.gbt_target_hex(),
        "0000000000000000000000000000000000000000000000000000000100010001"
    );
    assert!(work > U256::zero());
}

#[test]
fn test_get_block_proof_regtest_minimum_difficulty() {
    let work = get_block_proof(0x207fffff).unwrap();
    assert!(work > U256::zero());
    assert!(work < U256::from_u128(10));
}

#[test]
fn test_u256_zero() {
    let zero = U256::zero();
    assert!(zero.is_zero());
}

#[test]
fn test_u256_from_u32() {
    let value = U256::from_u32(0x12345678);
    assert!(!value.is_zero());
}

#[test]
fn test_u256_from_u64() {
    let value = U256::from_u64(0x123456789abcdef0);
    assert!(!value.is_zero());
}

#[test]
fn test_u256_shl_zero_shift() {
    let value = U256::from_u32(0x12345678);
    let result = value.shl(0);
    assert_eq!(result, value);
}

#[test]
fn test_u256_shl_large_shift() {
    let value = U256::from_u32(0x12345678);
    let result = value.shl(300); // > 256
    assert!(result.is_zero());
}

#[test]
fn test_u256_shr_zero_shift() {
    let value = U256::from_u32(0x12345678);
    let result = value.shr(0);
    assert_eq!(result, value);
}

#[test]
fn test_u256_shr_large_shift() {
    let value = U256::from_u32(0x12345678);
    let result = value.shr(300); // > 256
    assert!(result.is_zero());
}

#[test]
fn test_u256_shl_small_shift() {
    let value = U256::from_u32(0x12345678);
    let result = value.shl(8);
    assert!(!result.is_zero());
    assert_ne!(result, value);
}

#[test]
fn test_u256_shr_small_shift() {
    let value = U256::from_u32(0x12345678);
    let result = value.shr(8);
    assert!(!result.is_zero());
    assert_ne!(result, value);
}

#[test]
fn test_u256_to_bytes() {
    let value = U256::from_u32(0x12345678);
    let bytes = value.to_bytes();
    assert_eq!(bytes.len(), 32);
}

#[test]
fn test_u256_from_bytes() {
    let mut bytes = [0u8; 32];
    bytes[0] = 0x78;
    bytes[1] = 0x56;
    bytes[2] = 0x34;
    bytes[3] = 0x12;
    let value = U256::from_bytes(&bytes);
    assert!(!value.is_zero());
}

#[test]
fn test_u256_ordering() {
    let small = U256::from_u32(0x12345678);
    let large = U256::from_u32(0x87654321);

    assert!(small < large);
    assert!(large > small);
    assert_eq!(small.cmp(&small), std::cmp::Ordering::Equal);
}

#[test]
fn test_expand_compress_round_trip() {
    // Test that expand_target and compress_target are inverse operations
    let test_bits = vec![
        0x1d00ffff, // Genesis target
        0x1b0404cb, // Example target
        0x0300ffff, // Small target (exponent 3)
                    // Note: 0x1a05db8b has precision loss in MSB word due to compact format limitations
                    // This is expected behavior - compact format may not perfectly round-trip for all values
                    // 0x1a05db8b, // Another example (skipped due to known precision loss)
    ];

    for &bits in &test_bits {
        // Expand to full target
        let expanded = match expand_target(bits) {
            Ok(t) => t,
            Err(_) => continue, // Skip invalid targets
        };

        // Compress back to bits
        let compressed = match compress_target(&expanded) {
            Ok(b) => b,
            Err(_) => {
                // Compression might produce slightly different result due to normalization
                // This is acceptable as long as it expands back to same target
                continue;
            }
        };

        // Verify the compressed bits expand to the same target
        let re_expanded = match expand_target(compressed) {
            Ok(t) => t,
            Err(_) => continue,
        };

        // Compact format may lose precision in lower bits during compression.
        // When we compress and re-expand, the result should be <= original
        // (since compression truncates lower bits). For most cases they should be equal.
        if re_expanded > expanded {
            panic!(
                "Round-trip failed for bits 0x{bits:08x}: re-expanded > original (compression should truncate, not add)"
            );
        }
        // For most practical targets, they should be equal. If not equal, the difference
        // should only be in lower bits that were truncated (acceptable precision loss).
        // U256 stores words as [0, 1, 2, 3] where 0 is LSB and 3 is MSB.
        // Compact format precision loss can affect multiple low-order words.
        // We only check the most significant words (2, 3) are equal.
        // Words 0 and 1 may differ due to truncation - this is acceptable for compact format.
        #[allow(clippy::eq_op)]
        let significant_words_match =
            expanded.0[2] == re_expanded.0[2] && expanded.0[3] == re_expanded.0[3];
        if !significant_words_match {
            panic!(
                "Round-trip failed for bits 0x{:08x}: significant bits differ (expanded: {:?}, re-expanded: {:?})",
                bits, expanded.0, re_expanded.0
            );
        }
        // Words 0 and 1 (least significant) may differ due to truncation - this is acceptable
    }
}

#[test]
fn test_compress_target_genesis() {
    // Test compression of genesis block target
    let genesis_bits = 0x1d00ffff;
    let expanded = expand_target(genesis_bits).unwrap();
    let compressed = compress_target(&expanded).unwrap();

    // Compressed should be valid (within bounds)
    assert!(compressed <= MAX_TARGET as u64);
    assert!(compressed > 0);

    // Verify it expands back to same target
    let re_expanded = expand_target(compressed).unwrap();
    assert_eq!(expanded, re_expanded);
}

#[test]
fn test_serialize_header() {
    let header = BlockHeader {
        version: 1,
        prev_block_hash: [1; 32],
        merkle_root: [2; 32],
        timestamp: 1234567890,
        bits: 0x1d00ffff,
        nonce: 0x12345678,
    };

    let bytes = serialize_header(&header);
    assert_eq!(bytes.len(), 80); // 4 + 32 + 32 + 4 + 4 + 4 = 80 bytes
}

// ==========================================================================
// REGRESSION TESTS: serialize_header returns [u8; 80] (stack-allocated)
// ==========================================================================

#[test]
fn test_serialize_header_returns_fixed_80_bytes() {
    // Verify the function returns exactly [u8; 80], not Vec<u8>
    let header = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 0,
        bits: 0,
        nonce: 0,
    };
    let bytes: [u8; 80] = serialize_header(&header);
    assert_eq!(bytes.len(), 80);
}

#[test]
fn test_serialize_header_field_layout() {
    // Verify each field is serialized in the correct position and byte order
    let header = BlockHeader {
        version: 0x01020304,
        prev_block_hash: {
            let mut h = [0u8; 32];
            h[0] = 0xAA;
            h[31] = 0xBB;
            h
        },
        merkle_root: {
            let mut h = [0u8; 32];
            h[0] = 0xCC;
            h[31] = 0xDD;
            h
        },
        timestamp: 0x05060708,
        bits: 0x090A0B0C,
        nonce: 0x0D0E0F10,
    };

    let bytes = serialize_header(&header);

    // Version: bytes [0..4], little-endian u32
    assert_eq!(bytes[0], 0x04); // LE: least significant byte first
    assert_eq!(bytes[1], 0x03);
    assert_eq!(bytes[2], 0x02);
    assert_eq!(bytes[3], 0x01);

    // Prev block hash: bytes [4..36], raw bytes
    assert_eq!(bytes[4], 0xAA);
    assert_eq!(bytes[35], 0xBB);

    // Merkle root: bytes [36..68], raw bytes
    assert_eq!(bytes[36], 0xCC);
    assert_eq!(bytes[67], 0xDD);

    // Timestamp: bytes [68..72], little-endian u32
    assert_eq!(bytes[68], 0x08);
    assert_eq!(bytes[69], 0x07);
    assert_eq!(bytes[70], 0x06);
    assert_eq!(bytes[71], 0x05);

    // Bits: bytes [72..76], little-endian u32
    assert_eq!(bytes[72], 0x0C);
    assert_eq!(bytes[73], 0x0B);
    assert_eq!(bytes[74], 0x0A);
    assert_eq!(bytes[75], 0x09);

    // Nonce: bytes [76..80], little-endian u32
    assert_eq!(bytes[76], 0x10);
    assert_eq!(bytes[77], 0x0F);
    assert_eq!(bytes[78], 0x0E);
    assert_eq!(bytes[79], 0x0D);
}

#[test]
fn test_serialize_header_deterministic() {
    let header = BlockHeader {
        version: 1,
        prev_block_hash: [0xFF; 32],
        merkle_root: [0xAA; 32],
        timestamp: 1231006505,
        bits: 0x1d00ffff,
        nonce: 2083236893,
    };

    let bytes1 = serialize_header(&header);
    let bytes2 = serialize_header(&header);
    assert_eq!(bytes1, bytes2, "Header serialization must be deterministic");
}

#[test]
fn test_serialize_header_different_headers_different_bytes() {
    let header1 = BlockHeader {
        version: 1,
        prev_block_hash: [0; 32],
        merkle_root: [0; 32],
        timestamp: 1231006505,
        bits: 0x1d00ffff,
        nonce: 0,
    };

    let mut header2 = header1.clone();
    header2.nonce = 1;

    let bytes1 = serialize_header(&header1);
    let bytes2 = serialize_header(&header2);
    assert_ne!(
        bytes1, bytes2,
        "Different nonces must produce different serializations"
    );

    // Specifically, only the nonce bytes (76-79) should differ
    assert_eq!(
        bytes1[..76],
        bytes2[..76],
        "Non-nonce bytes should be identical"
    );
    assert_ne!(bytes1[76..], bytes2[76..], "Nonce bytes should differ");
}

#[test]
fn test_u256_from_bytes_simple() {
    let bytes = [0u8; 32];
    let value = u256_from_bytes(&bytes);
    assert_eq!(value, 0);
}

#[test]
fn test_u256_from_bytes_with_data() {
    let mut bytes = [0u8; 32];
    bytes[0] = 0x78;
    bytes[1] = 0x56;
    bytes[2] = 0x34;
    bytes[3] = 0x12;
    let value = u256_from_bytes(&bytes);
    // The function reads bytes in big-endian order from the first 16 bytes
    // So 0x78, 0x56, 0x34, 0x12 becomes 0x78563412...
    assert_eq!(value, 0x78563412000000000000000000000000);
}

fn ancestor_from(headers: &[(u64, u64, u64)]) -> impl Fn(u64) -> Option<(u64, u64)> + '_ {
    move |height| {
        headers
            .iter()
            .find(|(h, _, _)| *h == height)
            .map(|(_, bits, ts)| (*bits, *ts))
    }
}

#[test]
fn off_boundary_copies_parent_bits() {
    let headers = [(99u64, 0x1b012dcd, 1_000u64)];
    let bits = next_required_bits(Network::Mainnet, 100, 2_000, &ancestor_from(&headers))
        .unwrap()
        .unwrap();
    assert_eq!(bits, 0x1b012dcd);
}

#[test]
fn testnet4_retarget_uses_the_first_block_of_the_period() {
    let timespan = 2016 * 600;
    let headers = [
        (0u64, 0x1b012dcd, 1_700_000_000u64),
        (2015, 0x1d00ffff, 1_700_000_000 + timespan),
    ];
    let lookup = ancestor_from(&headers);
    let testnet3 = next_required_bits(Network::Testnet, 2016, 0, &lookup)
        .unwrap()
        .unwrap();
    let testnet4 = next_required_bits(Network::Testnet4, 2016, 0, &lookup)
        .unwrap()
        .unwrap();
    assert_eq!(testnet3, 0x1d00ffff);
    assert_eq!(testnet4, 0x1b012dcd);
}

#[test]
fn testnet_late_block_uses_pow_limit() {
    let headers = [(99u64, 0x1b012dcd, 1_000u64)];
    let late = next_required_bits(
        Network::Testnet,
        100,
        1_000 + 1_201,
        &ancestor_from(&headers),
    )
    .unwrap()
    .unwrap();
    assert_eq!(late, 0x1d00ffff);
    let on_time = next_required_bits(
        Network::Testnet,
        100,
        1_000 + 1_200,
        &ancestor_from(&headers),
    )
    .unwrap()
    .unwrap();
    assert_eq!(on_time, 0x1b012dcd);
}

#[test]
fn testnet_walks_back_past_minimum_difficulty_blocks() {
    let headers = [
        (8u64, 0x1b012dcd, 1_000u64),
        (9, 0x1d00ffff, 1_600),
        (10, 0x1d00ffff, 2_200),
    ];
    let bits = next_required_bits(Network::Testnet, 11, 2_800, &ancestor_from(&headers))
        .unwrap()
        .unwrap();
    assert_eq!(bits, 0x1b012dcd);
}

#[test]
fn regtest_does_not_retarget_and_uses_its_own_limit() {
    let headers = [(143u64, 0x181bc330, 10_000u64)];
    let bits = next_required_bits(Network::Regtest, 144, 20_000, &ancestor_from(&headers))
        .unwrap()
        .unwrap();
    assert_eq!(bits, 0x181bc330);

    let min = [(0u64, 0x207fffff, 1_000u64), (1, 0x207fffff, 1_600)];
    let late = next_required_bits(Network::Regtest, 2, 1_600 + 1_201, &ancestor_from(&min))
        .unwrap()
        .unwrap();
    assert_eq!(late, 0x207fffff);
    let walked = next_required_bits(Network::Regtest, 2, 1_600 + 600, &ancestor_from(&min))
        .unwrap()
        .unwrap();
    assert_eq!(walked, 0x207fffff);
}

#[test]
fn mainnet_retarget_height_112896_matches_chain_bits() {
    let first_ts = 1_298_800_760u64;
    let last_ts = 1_299_683_275u64;
    let period_bits = 453_062_093u64;
    let headers = [
        (110_880u64, period_bits, first_ts),
        (112_895, period_bits, last_ts),
    ];
    let bits = next_required_bits(
        Network::Mainnet,
        112_896,
        1_299_684_355,
        &ancestor_from(&headers),
    )
    .unwrap()
    .unwrap();
    assert_eq!(bits, 453_041_201);
}

#[test]
fn genesis_has_no_required_bits() {
    assert!(
        next_required_bits(Network::Mainnet, 0, 1, &|_| None)
            .unwrap()
            .is_none()
    );
}