blvm-consensus 0.1.10

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
//! Time-based consensus edge cases
//!
//! Comprehensive tests for BIP65 (CLTV), BIP112 (CSV), and BIP113 (median time-past)
//! time-based consensus rules.
//!
//! Coverage:
//! - BIP65 CLTV: all edge cases (height vs. time, exact boundaries)
//! - BIP112 CSV: all sequence number combinations
//! - BIP113 locktime: median time past calculations
//! - Locktime interactions with soft fork activation
//! - Time-based consensus at exact boundaries

use blvm_consensus::bip113::get_median_time_past;
use blvm_consensus::types::BlockHeader;
use blvm_consensus::types::{OutPoint, Transaction, TransactionInput, TransactionOutput};
use blvm_consensus::BIP112_CSV_ACTIVATION_MAINNET;

/// Test BIP65 CLTV (CheckLockTimeVerify) - height-based locktime
///
/// BIP65: CLTV allows output to be locked until a certain block height or time.
#[test]
fn test_bip65_cltv_height() {
    // Note: encode_locktime function not available - use direct locktime values

    // Test CLTV with block height locktime
    let locktime_value = 1000u32; // Block height 1000
    let encoded = blvm_consensus::locktime::encode_locktime_value(locktime_value);

    // Encoded value should use minimal encoding (1000 = 0xe8 0x03 = 2 bytes)
    // Minimal encoding: only include bytes up to highest non-zero byte
    assert_eq!(encoded.len(), 2); // 1000 = 0x03e8 = 2 bytes in little-endian

    // Test with transaction locktime at exact boundary
    let tx = Transaction {
        version: 1,
        inputs: vec![TransactionInput {
            prevout: OutPoint {
                hash: [0; 32],
                index: 0,
            },
            script_sig: vec![],
            sequence: 0xffffffff,
        }]
        .into(),
        outputs: vec![TransactionOutput {
            value: 1000,
            script_pubkey: vec![],
        }]
        .into(),
        lock_time: 1000, // Same as CLTV value
    };

    // Transaction should be valid (locktime matches)
    assert_eq!(tx.lock_time, 1000);
}

/// Test BIP65 CLTV (CheckLockTimeVerify) - time-based locktime
///
/// BIP65: CLTV can use Unix timestamp (>= 500000000) for time-based locking.
#[test]
fn test_bip65_cltv_time() {
    // Note: encode_locktime function not available - use direct locktime values

    // Test CLTV with Unix timestamp locktime
    let locktime_value = 500000000u32; // Unix timestamp
    let encoded = blvm_consensus::locktime::encode_locktime_value(locktime_value);

    // Encoded value should be correct
    assert_eq!(encoded.len(), 4);

    // Test with transaction locktime at exact boundary
    let tx = Transaction {
        version: 1,
        inputs: vec![TransactionInput {
            prevout: OutPoint {
                hash: [0; 32],
                index: 0,
            },
            script_sig: vec![],
            sequence: 0xffffffff,
        }]
        .into(),
        outputs: vec![TransactionOutput {
            value: 1000,
            script_pubkey: vec![],
        }]
        .into(),
        lock_time: 500000000, // Same as CLTV value
    };

    // Transaction should be valid (locktime matches)
    assert_eq!(tx.lock_time, 500000000);
}

/// Test BIP65 CLTV boundary conditions
///
/// Tests exact boundaries between height and time locktime.
#[test]
fn test_bip65_cltv_boundaries() {
    // Boundary between height and time: 500000000
    // Values < 500000000 are block heights
    // Values >= 500000000 are Unix timestamps

    // Test just below boundary (block height)
    let height_locktime = 499999999u32;
    assert!(height_locktime < 500000000);

    // Test at boundary (Unix timestamp)
    let time_locktime = 500000000u32;
    assert!(time_locktime >= 500000000);

    // Test just above boundary (Unix timestamp)
    let time_locktime2 = 500000001u32;
    assert!(time_locktime2 >= 500000000);
}

/// Test BIP112 CSV (CheckSequenceVerify) - sequence numbers
///
/// BIP112: CSV allows output to be locked until a certain sequence number.
#[test]
fn test_bip112_csv_sequence() {
    // Test CSV with various sequence numbers
    let sequence_values = vec![
        0x00000000,           // No CSV
        0x00000001,           // CSV enabled, relative locktime = 1
        0x0000ffff,           // CSV enabled, relative locktime = 65535
        0x80000000u32 as i32, // CSV disabled (sequence disabled bit)
        0x80000001u32 as i32, // CSV disabled + relative locktime = 1
    ];

    for sequence in sequence_values {
        let tx = Transaction {
            version: 1,
            inputs: vec![TransactionInput {
                prevout: OutPoint {
                    hash: [0; 32],
                    index: 0,
                },
                script_sig: vec![],
                sequence: sequence as u64,
            }]
            .into(),
            outputs: vec![TransactionOutput {
                value: 1000,
                script_pubkey: vec![],
            }]
            .into(),
            lock_time: 0,
        };

        // Verify sequence is set correctly
        assert_eq!(tx.inputs[0].sequence, sequence as u64);
    }
}

/// Test BIP112 CSV boundary conditions
///
/// Tests exact boundaries for sequence number interpretation.
#[test]
fn test_bip112_csv_boundaries() {
    // Sequence number format (from BIP68):
    // - Bit 31 (0x80000000): Type flag (0 = block height, 1 = seconds)
    // - Bits 22-30: Reserved
    // - Bits 0-15: Locktime value

    // Test block height type (bit 31 = 0)
    let block_height_sequence = 0x00000001; // Block height = 1
    assert_eq!(block_height_sequence & 0x80000000u32 as i32, 0);

    // Test seconds type (bit 31 = 1)
    let seconds_sequence = 0x80000001u32 as i32; // Seconds = 1
    assert_eq!(
        seconds_sequence & 0x80000000u32 as i32,
        0x80000000u32 as i32
    );

    // Test maximum locktime value (16 bits)
    let max_locktime = 0x0000ffff; // Maximum 16-bit value
    assert_eq!(max_locktime & 0xffff, 0xffff);
}

/// Test BIP113 median time-past calculation
///
/// BIP113: Uses median time-past of last 11 blocks for time-based locktime.
#[test]
fn test_bip113_median_time_past() {
    // Create 11 block headers with timestamps
    let mut headers = Vec::new();
    for i in 0..11 {
        headers.push(BlockHeader {
            version: 1,
            prev_block_hash: [i as u8; 32],
            merkle_root: [0; 32],
            timestamp: 1000 + (i * 100), // Increasing timestamps
            bits: 0x1d00ffff,
            nonce: 0,
        });
    }

    // Calculate median time-past
    let median = get_median_time_past(&headers);

    // Median should be the middle value (6th block: 1000 + 5*100 = 1500)
    // Actually, with 11 blocks, median is the 6th value (0-indexed: 5)
    assert!(median >= 1000);
    assert!(median <= 2000);
}

/// Test BIP113 median time-past with fewer than 11 blocks
#[test]
fn test_bip113_median_time_past_few_blocks() {
    // Test with fewer than 11 blocks
    let mut headers = Vec::new();
    for i in 0..5 {
        headers.push(BlockHeader {
            version: 1,
            prev_block_hash: [i as u8; 32],
            merkle_root: [0; 32],
            timestamp: 1000 + (i * 100),
            bits: 0x1d00ffff,
            nonce: 0,
        });
    }

    // Calculate median time-past
    let median = get_median_time_past(&headers);

    // Should return median of available blocks
    assert!(median >= 1000);
    assert!(median <= 1500);
}

/// Test BIP113 median time-past with empty headers
#[test]
fn test_bip113_median_time_past_empty() {
    let headers: Vec<BlockHeader> = Vec::new();

    // Calculate median time-past
    let median = get_median_time_past(&headers);

    // Should return 0 for empty headers
    assert_eq!(median, 0);
}

/// Test locktime interaction with soft fork activation
///
/// Verifies that locktime rules work correctly at soft fork activation heights.
#[test]
fn test_locktime_soft_fork_interaction() {
    // BIP112 (CSV) on mainnet: `BIP112_CSV_ACTIVATION_MAINNET` (same height as BIP65 CLTV there).
    // BIP113 (median time-past) ties to SegWit deployment: `SEGWIT_ACTIVATION_MAINNET`.

    // Test transaction before CSV activation
    let _pre_cltv_height = BIP112_CSV_ACTIVATION_MAINNET - 1;
    let tx = Transaction {
        version: 1,
        inputs: vec![TransactionInput {
            prevout: OutPoint {
                hash: [0; 32],
                index: 0,
            },
            script_sig: vec![],
            sequence: 0xffffffff,
        }]
        .into(),
        outputs: vec![TransactionOutput {
            value: 1000,
            script_pubkey: vec![],
        }]
        .into(),
        lock_time: 0,
    };

    // Transaction should be valid (no CLTV/CSV at this height)
    assert_eq!(tx.lock_time, 0);

    // Test transaction after CSV activation
    let _post_cltv_height = BIP112_CSV_ACTIVATION_MAINNET;
    // Same transaction should be valid with CLTV/CSV enabled
    assert_eq!(tx.lock_time, 0);
}

/// Test time-based consensus at exact boundaries
///
/// Tests behavior at exact locktime boundaries.
#[test]
fn test_time_consensus_boundaries() {
    // Test exact boundary between height and time
    let boundary = 500000000u32;

    // Just below boundary (height)
    let height_value = boundary - 1;
    assert!(height_value < 500000000);

    // At boundary (time)
    let time_value = boundary;
    assert!(time_value >= 500000000);

    // Just above boundary (time)
    let time_value2 = boundary + 1;
    assert!(time_value2 >= 500000000);
}

/// Test BIP65 CLTV with transaction locktime
///
/// Verifies that CLTV works correctly with transaction locktime.
#[test]
fn test_bip65_cltv_with_tx_locktime() {
    // Transaction with locktime should work with CLTV
    let tx = Transaction {
        version: 1,
        inputs: vec![TransactionInput {
            prevout: OutPoint {
                hash: [0; 32],
                index: 0,
            },
            script_sig: vec![],
            sequence: 0xffffffff, // Final sequence
        }]
        .into(),
        outputs: vec![TransactionOutput {
            value: 1000,
            script_pubkey: vec![],
        }]
        .into(),
        lock_time: 1000, // Block height locktime
    };

    // Transaction locktime should be valid
    assert_eq!(tx.lock_time, 1000);

    // If all inputs have final sequence, locktime is enforced
    assert!(tx.inputs.iter().all(|input| input.sequence == 0xffffffff));
}

/// Test BIP112 CSV with sequence numbers
///
/// Verifies that CSV works correctly with various sequence number combinations.
#[test]
fn test_bip112_csv_sequence_combinations() {
    // Test all sequence number combinations
    // CSV disabled when bit 31 (0x80000000) is set
    let combinations = vec![
        (0x80000000u32 as i32, false, 0),     // CSV disabled, no locktime
        (0x00000001, true, 1),                // CSV enabled, block height = 1
        (0x80000001u32 as i32, false, 1),     // CSV disabled, seconds = 1
        (0x0000ffff, true, 65535),            // CSV enabled, block height = 65535
        (0x8000ffffu32 as i32, false, 65535), // CSV disabled, seconds = 65535
    ];

    for (sequence, csv_enabled, locktime_value) in combinations {
        let tx = Transaction {
            version: 1,
            inputs: vec![TransactionInput {
                prevout: OutPoint {
                    hash: [0; 32],
                    index: 0,
                },
                script_sig: vec![],
                sequence: sequence as u64,
            }]
            .into(),
            outputs: vec![TransactionOutput {
                value: 1000,
                script_pubkey: vec![],
            }]
            .into(),
            lock_time: 0,
        };

        // Verify sequence is set correctly
        assert_eq!(tx.inputs[0].sequence, sequence as u64);

        // Verify CSV enabled/disabled bit
        let is_csv_disabled = (sequence & 0x80000000u32 as i32) != 0;
        assert_eq!(is_csv_disabled, !csv_enabled);

        // Verify locktime value (lower 16 bits)
        let extracted_locktime = sequence & 0xffff;
        assert_eq!(extracted_locktime, locktime_value);
    }
}

/// Test median time-past calculation with out-of-order timestamps
///
/// Verifies that median calculation works correctly even with unsorted timestamps.
#[test]
fn test_median_time_past_unsorted() {
    // Create headers with out-of-order timestamps
    let headers = vec![
        BlockHeader {
            version: 1,
            prev_block_hash: [0; 32],
            merkle_root: [0; 32],
            timestamp: 2000,
            bits: 0x1d00ffff,
            nonce: 0,
        },
        BlockHeader {
            version: 1,
            prev_block_hash: [1; 32],
            merkle_root: [0; 32],
            timestamp: 1000,
            bits: 0x1d00ffff,
            nonce: 0,
        },
        BlockHeader {
            version: 1,
            prev_block_hash: [2; 32],
            merkle_root: [0; 32],
            timestamp: 3000,
            bits: 0x1d00ffff,
            nonce: 0,
        },
        BlockHeader {
            version: 1,
            prev_block_hash: [3; 32],
            merkle_root: [0; 32],
            timestamp: 1500,
            bits: 0x1d00ffff,
            nonce: 0,
        },
        BlockHeader {
            version: 1,
            prev_block_hash: [4; 32],
            merkle_root: [0; 32],
            timestamp: 2500,
            bits: 0x1d00ffff,
            nonce: 0,
        },
    ];

    // Calculate median time-past
    let median = get_median_time_past(&headers);

    // Should return median of sorted timestamps (1500)
    assert!(median >= 1000);
    assert!(median <= 3000);
}