44/// which can panic if given an invalid value (> 16,777,215).
55///
66/// This file shows the problem and how to catch it.
7-
87use bdk_wallet:: descriptor;
98
109#[ test]
1110fn test_older_with_invalid_rellocktime_too_large ( ) {
1211 // Value with high bit set causes RelLockTime::from_consensus() to fail
1312 let invalid_value = 0x80000000 ; // 2147483648
14-
13+
1514 // This should now return an error instead of panicking
1615 let result = descriptor ! ( wsh( older( invalid_value) ) ) ;
17- assert ! ( result. is_err( ) , "Invalid RelLockTime {} should return an error" , invalid_value) ;
18-
16+ assert ! (
17+ result. is_err( ) ,
18+ "Invalid RelLockTime {} should return an error" ,
19+ invalid_value
20+ ) ;
21+
1922 // Check that it's the right kind of error
2023 if let Err ( descriptor:: DescriptorError :: RelLockTime ( _) ) = result {
2124 // Good, it's the expected error type
@@ -36,7 +39,7 @@ fn test_older_with_valid_rellocktime_max() {
3639fn test_older_with_valid_rellocktime_min ( ) {
3740 // Min valid value is 1 (0 is not valid for RelLockTime)
3841 let min_value = 1 ;
39-
42+
4043 let result = descriptor ! ( wsh( older( min_value) ) ) ;
4144 assert ! ( result. is_ok( ) , "Min valid RelLockTime should work" ) ;
4245}
@@ -45,20 +48,16 @@ fn test_older_with_valid_rellocktime_min() {
4548fn test_older_with_valid_common_values ( ) {
4649 // Common usage: blocks or seconds
4750 let test_cases = vec ! [
48- 1 , // 1 block/second
49- 144 , // ~1 day in blocks
50- 1000 , // Common value
51- 65535 , // Max 16-bit value (common for CSV)
52- 4_209_713 , // Valid but large
51+ 1 , // 1 block/second
52+ 144 , // ~1 day in blocks
53+ 1000 , // Common value
54+ 65535 , // Max 16-bit value (common for CSV)
55+ 4_209_713 , // Valid but large
5356 ] ;
54-
57+
5558 for value in test_cases {
5659 let result = descriptor ! ( wsh( older( value) ) ) ;
57- assert ! (
58- result. is_ok( ) ,
59- "Valid RelLockTime {} should work" ,
60- value
61- ) ;
60+ assert ! ( result. is_ok( ) , "Valid RelLockTime {} should work" , value) ;
6261 }
6362}
6463
@@ -67,20 +66,19 @@ fn test_older_with_valid_common_values() {
6766fn test_demonstrate_proper_error_handling ( ) {
6867 // This is what the fix should look like - proper Result handling
6968 // instead of .expect() which panics
70-
69+
7170 use miniscript:: RelLockTime ;
72-
71+
7372 // Valid case
7473 match RelLockTime :: from_consensus ( 144 ) {
7574 Ok ( lock_time) => println ! ( "Valid: {:?}" , lock_time) ,
7675 Err ( e) => println ! ( "Invalid: {}" , e) ,
7776 }
78-
77+
7978 // Invalid case - properly handled
8079 match RelLockTime :: from_consensus ( 16_777_216 ) {
8180 Ok ( lock_time) => println ! ( "Valid: {:?}" , lock_time) ,
8281 Err ( e) => {
83- // Instead of panicking, we could return a proper DescriptorError
8482 println ! ( "Invalid RelLockTime value: {}" , e) ;
8583 }
8684 }
0 commit comments