bitcoinkernel 0.2.0

Safe Rust bindings to libbitcoinkernel
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
use std::ffi::{c_char, c_void};

use libbitcoinkernel_sys::{
    btck_LogCategory, btck_LogLevel, btck_LoggingConnection, btck_LoggingOptions,
    btck_logging_connection_create, btck_logging_connection_destroy, btck_logging_disable,
    btck_logging_disable_category, btck_logging_enable_category, btck_logging_set_level_category,
    btck_logging_set_options,
};

use crate::{
    ffi::{
        c_helpers, BTCK_LOG_CATEGORY_ALL, BTCK_LOG_CATEGORY_BENCH, BTCK_LOG_CATEGORY_BLOCKSTORAGE,
        BTCK_LOG_CATEGORY_COINDB, BTCK_LOG_CATEGORY_KERNEL, BTCK_LOG_CATEGORY_LEVELDB,
        BTCK_LOG_CATEGORY_MEMPOOL, BTCK_LOG_CATEGORY_PRUNE, BTCK_LOG_CATEGORY_RAND,
        BTCK_LOG_CATEGORY_REINDEX, BTCK_LOG_CATEGORY_VALIDATION, BTCK_LOG_LEVEL_DEBUG,
        BTCK_LOG_LEVEL_INFO, BTCK_LOG_LEVEL_TRACE,
    },
    KernelError,
};

/// A function for handling log messages produced by the kernel library.
pub trait Log {
    fn log(&self, message: &str);
}

unsafe extern "C" fn log_callback<T: Log + 'static>(
    user_data: *mut c_void,
    message: *const c_char,
    message_len: usize,
) {
    let message = unsafe { c_helpers::to_string(message, message_len) };
    let log = user_data as *mut T;
    (*log).log(&message);
}

unsafe extern "C" fn destroy_log_callback<T>(user_data: *mut c_void) {
    if !user_data.is_null() {
        let _ = Box::from_raw(user_data as *mut T);
    }
}

/// The logger object logs kernel log messages into a user-defined log function.
/// Messages logged by the kernel before this object is created are buffered in
/// a 1MB buffer. The kernel library internally uses a global logging instance.
pub struct Logger {
    inner: *mut btck_LoggingConnection,
}

impl Drop for Logger {
    fn drop(&mut self) {
        unsafe {
            btck_logging_connection_destroy(self.inner);
        }
    }
}

/// Permanently disable logging and stop buffering.
///
/// # Warning
///
/// This should only be called once during the lifetime of the program.
pub fn disable_logging() {
    unsafe {
        btck_logging_disable();
    }
}

/// Set global logging options.
///
/// This changes global settings and will override settings for all existing
/// Logger instances.
pub fn set_logging_options(options: LoggingOptions) {
    let c_options = btck_LoggingOptions {
        log_timestamps: c_helpers::to_c_bool(options.log_timestamps),
        log_time_micros: c_helpers::to_c_bool(options.log_time_micros),
        log_threadnames: c_helpers::to_c_bool(options.log_threadnames),
        log_sourcelocations: c_helpers::to_c_bool(options.log_sourcelocations),
        always_print_category_levels: c_helpers::to_c_bool(options.always_print_category_levels),
    };

    unsafe {
        btck_logging_set_options(c_options);
    }
}

/// Options controlling the format of log messages.
#[derive(Debug, Clone, Copy)]
pub struct LoggingOptions {
    /// Prepend a timestamp to log messages.
    pub log_timestamps: bool,
    /// Log timestamps in microsecond precision.
    pub log_time_micros: bool,
    /// Prepend the name of the thread to log messages.
    pub log_threadnames: bool,
    /// Prepend the source location to log messages.
    pub log_sourcelocations: bool,
    /// Always print category and log level.
    pub always_print_category_levels: bool,
}

impl Default for LoggingOptions {
    fn default() -> Self {
        Self {
            log_timestamps: true,
            log_time_micros: false,
            log_threadnames: false,
            log_sourcelocations: false,
            always_print_category_levels: false,
        }
    }
}

impl Logger {
    /// Create a new Logger with the specified callback.
    ///
    /// Note: Logging options should be set using the global `set_logging_options`
    /// function before or after creating the Logger.
    pub fn new<T: Log + 'static>(log: T) -> Result<Logger, KernelError> {
        let log_ptr = Box::into_raw(Box::new(log));

        let inner = unsafe {
            btck_logging_connection_create(
                Some(log_callback::<T>),
                log_ptr as *mut c_void,
                Some(destroy_log_callback::<T>),
            )
        };

        if inner.is_null() {
            unsafe {
                let _ = Box::from_raw(log_ptr);
            }
            return Err(KernelError::Internal(
                "Failed to create new logging connection.".to_string(),
            ));
        }

        Ok(Logger { inner })
    }

    /// Create a new Logger with the specified callback and options.
    ///
    /// This is a convenience method that sets the global logging options
    /// and then creates the Logger.
    pub fn new_with_options<T: Log + 'static>(
        log: T,
        options: LoggingOptions,
    ) -> Result<Logger, KernelError> {
        set_logging_options(options);
        Self::new(log)
    }

    /// Sets the logging level for a specific category.
    ///
    /// This changes a global setting and will override settings for all existing
    /// Logger instances.
    pub fn set_level_category(&self, category: LogCategory, level: LogLevel) {
        unsafe {
            btck_logging_set_level_category(category.into(), level.into());
        }
    }

    /// Enables logging for a specific category.
    ///
    /// This changes a global setting and will override settings for all existing
    /// Logger instances.
    pub fn enable_category(&self, category: LogCategory) {
        unsafe {
            btck_logging_enable_category(category.into());
        }
    }

    /// Disables logging for a specific category.
    ///
    /// This changes a global setting and will override settings for all existing
    /// Logger instances.
    pub fn disable_category(&self, category: LogCategory) {
        unsafe {
            btck_logging_disable_category(category.into());
        }
    }
}

/// Logging categories for Bitcoin Kernel messages.
///
/// Controls which types of log messages are emitted by the kernel library.
/// Categories can be combined to enable multiple types of logging simultaneously.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum LogCategory {
    /// All logging categories enabled
    All = BTCK_LOG_CATEGORY_ALL,
    /// Benchmark and performance logging
    Bench = BTCK_LOG_CATEGORY_BENCH,
    /// Block storage operations
    BlockStorage = BTCK_LOG_CATEGORY_BLOCKSTORAGE,
    /// Coin database operations
    CoinDb = BTCK_LOG_CATEGORY_COINDB,
    /// LevelDB operations
    LevelDb = BTCK_LOG_CATEGORY_LEVELDB,
    /// Memory pool operations
    Mempool = BTCK_LOG_CATEGORY_MEMPOOL,
    /// Block pruning operations
    Prune = BTCK_LOG_CATEGORY_PRUNE,
    /// Random number generation
    Rand = BTCK_LOG_CATEGORY_RAND,
    /// Block reindexing operations
    Reindex = BTCK_LOG_CATEGORY_REINDEX,
    /// Block and transaction validation
    Validation = BTCK_LOG_CATEGORY_VALIDATION,
    /// Kernel-specific operations
    Kernel = BTCK_LOG_CATEGORY_KERNEL,
}

impl From<LogCategory> for btck_LogCategory {
    fn from(category: LogCategory) -> Self {
        category as btck_LogCategory
    }
}

impl From<btck_LogCategory> for LogCategory {
    fn from(value: btck_LogCategory) -> Self {
        match value {
            BTCK_LOG_CATEGORY_ALL => LogCategory::All,
            BTCK_LOG_CATEGORY_BENCH => LogCategory::Bench,
            BTCK_LOG_CATEGORY_BLOCKSTORAGE => LogCategory::BlockStorage,
            BTCK_LOG_CATEGORY_COINDB => LogCategory::CoinDb,
            BTCK_LOG_CATEGORY_LEVELDB => LogCategory::LevelDb,
            BTCK_LOG_CATEGORY_MEMPOOL => LogCategory::Mempool,
            BTCK_LOG_CATEGORY_PRUNE => LogCategory::Prune,
            BTCK_LOG_CATEGORY_RAND => LogCategory::Rand,
            BTCK_LOG_CATEGORY_REINDEX => LogCategory::Reindex,
            BTCK_LOG_CATEGORY_VALIDATION => LogCategory::Validation,
            BTCK_LOG_CATEGORY_KERNEL => LogCategory::Kernel,
            _ => panic!("Unknown log category: {}", value),
        }
    }
}

/// Logging levels for controlling message verbosity.
///
/// Determines the minimum severity level of messages that will be logged.
/// Higher levels include all messages from lower levels.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum LogLevel {
    /// Detailed trace information for debugging
    Trace = BTCK_LOG_LEVEL_TRACE,
    /// Debug information for development
    Debug = BTCK_LOG_LEVEL_DEBUG,
    /// General informational messages
    Info = BTCK_LOG_LEVEL_INFO,
}

impl From<LogLevel> for btck_LogLevel {
    fn from(level: LogLevel) -> Self {
        level as btck_LogLevel
    }
}

impl From<btck_LogLevel> for LogLevel {
    fn from(value: btck_LogLevel) -> Self {
        match value {
            BTCK_LOG_LEVEL_TRACE => LogLevel::Trace,
            BTCK_LOG_LEVEL_DEBUG => LogLevel::Debug,
            BTCK_LOG_LEVEL_INFO => LogLevel::Info,
            _ => panic!("Unknown log level: {}", value),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // LogCategory tests
    #[test]
    fn test_log_category_conversions() {
        let all = LogCategory::All;
        let btck_all: btck_LogCategory = all.into();
        let back_to_all: LogCategory = btck_all.into();
        assert_eq!(all, back_to_all);

        let bench = LogCategory::Bench;
        let btck_bench: btck_LogCategory = bench.into();
        let back_to_bench: LogCategory = btck_bench.into();
        assert_eq!(bench, back_to_bench);

        let block_storage = LogCategory::BlockStorage;
        let btck_block_storage: btck_LogCategory = block_storage.into();
        let back_to_block_storage: LogCategory = btck_block_storage.into();
        assert_eq!(block_storage, back_to_block_storage);

        let coin_db = LogCategory::CoinDb;
        let btck_coin_db: btck_LogCategory = coin_db.into();
        let back_to_coin_db: LogCategory = btck_coin_db.into();
        assert_eq!(coin_db, back_to_coin_db);

        let level_db = LogCategory::LevelDb;
        let btck_level_db: btck_LogCategory = level_db.into();
        let back_to_level_db: LogCategory = btck_level_db.into();
        assert_eq!(level_db, back_to_level_db);

        let mempool = LogCategory::Mempool;
        let btck_mempool: btck_LogCategory = mempool.into();
        let back_to_mempool: LogCategory = btck_mempool.into();
        assert_eq!(mempool, back_to_mempool);

        let prune = LogCategory::Prune;
        let btck_prune: btck_LogCategory = prune.into();
        let back_to_prune: LogCategory = btck_prune.into();
        assert_eq!(prune, back_to_prune);

        let rand = LogCategory::Rand;
        let btck_rand: btck_LogCategory = rand.into();
        let back_to_rand: LogCategory = btck_rand.into();
        assert_eq!(rand, back_to_rand);

        let reindex = LogCategory::Reindex;
        let btck_reindex: btck_LogCategory = reindex.into();
        let back_to_reindex: LogCategory = btck_reindex.into();
        assert_eq!(reindex, back_to_reindex);

        let validation = LogCategory::Validation;
        let btck_validation: btck_LogCategory = validation.into();
        let back_to_validation: LogCategory = btck_validation.into();
        assert_eq!(validation, back_to_validation);

        let kernel = LogCategory::Kernel;
        let btck_kernel: btck_LogCategory = kernel.into();
        let back_to_kernel: LogCategory = btck_kernel.into();
        assert_eq!(kernel, back_to_kernel);
    }

    #[test]
    fn test_log_category_equality() {
        assert_eq!(LogCategory::All, LogCategory::All);
        assert_ne!(LogCategory::All, LogCategory::Bench);
        assert_ne!(LogCategory::Validation, LogCategory::Kernel);
    }

    #[test]
    fn test_log_category_clone() {
        let validation = LogCategory::Validation;
        let cloned = validation;
        assert_eq!(validation, cloned);
    }

    // LogLevel tests
    #[test]
    fn test_log_level_conversions() {
        let trace = LogLevel::Trace;
        let btck_trace: btck_LogLevel = trace.into();
        let back_to_trace: LogLevel = btck_trace.into();
        assert_eq!(trace, back_to_trace);

        let debug = LogLevel::Debug;
        let btck_debug: btck_LogLevel = debug.into();
        let back_to_debug: LogLevel = btck_debug.into();
        assert_eq!(debug, back_to_debug);

        let info = LogLevel::Info;
        let btck_info: btck_LogLevel = info.into();
        let back_to_info: LogLevel = btck_info.into();
        assert_eq!(info, back_to_info);
    }

    #[test]
    fn test_log_level_equality() {
        assert_eq!(LogLevel::Info, LogLevel::Info);
        assert_ne!(LogLevel::Info, LogLevel::Debug);
        assert_ne!(LogLevel::Debug, LogLevel::Trace);
    }

    #[test]
    fn test_log_level_clone() {
        let info = LogLevel::Info;
        let cloned = info;
        assert_eq!(info, cloned);
    }

    #[test]
    fn test_logging_options_default() {
        let options = LoggingOptions::default();
        assert!(options.log_timestamps);
        assert!(!options.log_time_micros);
        assert!(!options.log_threadnames);
        assert!(!options.log_sourcelocations);
        assert!(!options.always_print_category_levels);
    }

    // Logger tests
    struct TestLog {
        messages: std::sync::Arc<std::sync::Mutex<Vec<String>>>,
    }

    impl Log for TestLog {
        fn log(&self, message: &str) {
            self.messages.lock().unwrap().push(message.to_string());
        }
    }

    #[test]
    fn test_logger_creation() {
        let messages = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let test_log = TestLog {
            messages: messages.clone(),
        };

        let _logger = Logger::new(test_log);
        assert!(_logger.is_ok());
    }

    #[test]
    fn test_logger_creation_with_options() {
        let messages = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let test_log = TestLog {
            messages: messages.clone(),
        };

        let options = LoggingOptions {
            log_timestamps: true,
            log_time_micros: true,
            log_threadnames: true,
            log_sourcelocations: false,
            always_print_category_levels: true,
        };

        let _logger = Logger::new_with_options(test_log, options);
        assert!(_logger.is_ok());
    }

    #[test]
    fn test_logger_set_level_category() {
        let messages = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let test_log = TestLog {
            messages: messages.clone(),
        };

        let logger = Logger::new(test_log).unwrap();
        logger.set_level_category(LogCategory::Validation, LogLevel::Debug);
        logger.set_level_category(LogCategory::Kernel, LogLevel::Info);
    }

    #[test]
    fn test_logger_enable_category() {
        let messages = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let test_log = TestLog {
            messages: messages.clone(),
        };

        let logger = Logger::new(test_log).unwrap();
        logger.enable_category(LogCategory::Validation);
        logger.enable_category(LogCategory::Kernel);
    }

    #[test]
    fn test_logger_disable_category() {
        let messages = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let test_log = TestLog {
            messages: messages.clone(),
        };

        let logger = Logger::new(test_log).unwrap();
        logger.disable_category(LogCategory::Validation);
        logger.disable_category(LogCategory::Kernel);
    }

    #[test]
    fn test_all_log_categories() {
        let categories = [
            LogCategory::All,
            LogCategory::Bench,
            LogCategory::BlockStorage,
            LogCategory::CoinDb,
            LogCategory::LevelDb,
            LogCategory::Mempool,
            LogCategory::Prune,
            LogCategory::Rand,
            LogCategory::Reindex,
            LogCategory::Validation,
            LogCategory::Kernel,
        ];

        let messages = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let test_log = TestLog {
            messages: messages.clone(),
        };

        let logger = Logger::new(test_log).unwrap();

        for category in categories {
            logger.enable_category(category);
            logger.set_level_category(category, LogLevel::Debug);
        }
    }

    #[test]
    fn test_all_log_levels() {
        let levels = [LogLevel::Trace, LogLevel::Debug, LogLevel::Info];

        let messages = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let test_log = TestLog {
            messages: messages.clone(),
        };

        let logger = Logger::new(test_log).unwrap();

        for level in levels {
            logger.set_level_category(LogCategory::Validation, level);
        }
    }

    #[test]
    fn test_global_set_logging_options() {
        let options = LoggingOptions {
            log_timestamps: false,
            log_time_micros: true,
            log_threadnames: true,
            log_sourcelocations: true,
            always_print_category_levels: false,
        };

        set_logging_options(options);
    }
}