bitcoinkernel 0.1.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
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
//! Chainstate manager for validation and chain state management.
//!
//! The [`ChainstateManager`] is the central component for:
//! - Processing and validating blocks
//! - Reading block data from disk
//! - Querying the active chain and block tree
//! - Managing chainstate databases
//!
//! # Example
//! ```no_run
//! # use bitcoinkernel::{Context, ChainType, ChainstateManager, KernelError};
//! #
//! # fn main() -> Result<(), KernelError> {
//!     let context = Context::builder().chain_type(ChainType::Regtest).build()?;
//!
//!     let chainman = ChainstateManager::builder(&context, "./data", "./blocks")?
//!         .chainstate_db_in_memory(true)
//!         .build()?;
//!
//!     chainman.import_blocks()?;
//!
//!     # let block = unimplemented!();
//!     chainman.process_block(&block);
//!
//! #     Ok(())
//! # }

use std::ffi::CString;

use libbitcoinkernel_sys::{
    btck_BlockHash, btck_ChainstateManager, btck_ChainstateManagerOptions, btck_block_read,
    btck_block_spent_outputs_read, btck_chainstate_manager_create, btck_chainstate_manager_destroy,
    btck_chainstate_manager_get_active_chain, btck_chainstate_manager_get_block_tree_entry_by_hash,
    btck_chainstate_manager_import_blocks, btck_chainstate_manager_options_create,
    btck_chainstate_manager_options_destroy, btck_chainstate_manager_options_set_wipe_dbs,
    btck_chainstate_manager_options_set_worker_threads_num,
    btck_chainstate_manager_options_update_block_tree_db_in_memory,
    btck_chainstate_manager_options_update_chainstate_db_in_memory,
    btck_chainstate_manager_process_block,
};

use crate::{
    ffi::{
        c_helpers,
        sealed::{AsPtr, FromMutPtr, FromPtr},
    },
    Block, BlockHash, BlockSpentOutputs, BlockTreeEntry, KernelError,
};

use super::{Chain, Context};

/// Result of processing a block with the [`ChainstateManager`].
///
/// Indicates whether a block was accepted (but not necessarily valid), rejected,
/// or was already known to the chainstate manager.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProcessBlockResult {
    /// Block was accepted and is new to the chainstate manager
    NewBlock,
    /// Block was accepted but was already known
    Duplicate,
    /// Block failed validation.
    ///
    /// The block violated one or more consensus rules. See
    /// [`crate::ContextBuilder::with_block_checked_validation`] for retrieving
    /// detailed error information.
    Rejected,
}

impl ProcessBlockResult {
    /// Returns true if the block was accepted and is new
    pub fn is_new_block(&self) -> bool {
        matches!(self, Self::NewBlock)
    }

    /// Returns true if the block was accepted but was already known
    pub fn is_duplicate(&self) -> bool {
        matches!(self, Self::Duplicate)
    }

    /// Returns true if the block was rejected
    pub fn is_rejected(&self) -> bool {
        matches!(self, Self::Rejected)
    }
}

/// The chainstate manager handles block validation and chain state.
///
/// This is the primary interface for interacting with the chain,
/// providing functionality to process and validate blocks, read block data from
/// disk, and query chain state.
///
/// # Lifetime
/// The chainstate manager holds a reference to the [`Context`] used to create it.
/// It is recommended to keep the context in scope for the lifetime of the
/// chainstate manager.
///
/// # Thread Safety
/// The chainstate manager is `Send` and `Sync`, allowing it to be shared
/// across threads safely.
///
/// # Examples
/// See module-level documentation for usage examples.
pub struct ChainstateManager {
    inner: *mut btck_ChainstateManager,
}

unsafe impl Send for ChainstateManager {}
unsafe impl Sync for ChainstateManager {}

impl ChainstateManager {
    /// Creates a new chainstate manager builder for configuring and constructing a chainstate manager.
    ///
    /// This is the recommended way to create a [`ChainstateManager`], as it provides a fluent
    /// interface for configuring all available options before construction.
    ///
    /// # Arguments
    /// * `context` - The [`Context`] that this chainstate manager will use. It is recommended
    ///   to keep the context in scope for the lifetime of the chainstate manager.
    /// * `data_dir` - The directory path where the chainstate manager will write its data files.
    ///   This directory will be created if it doesn't exist.
    /// * `blocks_dir` - The directory path where block files will be stored.
    ///   This directory will be created if it doesn't exist.
    ///
    /// # Errors
    /// Returns [`KernelError`] if:
    /// - The directory paths contain null bytes
    /// - The underlying C++ library fails to create the builder options
    ///
    /// # Examples
    /// ```no_run
    /// use bitcoinkernel::{Context, ChainType, ChainstateManager};
    ///
    /// # fn main() -> Result<(), bitcoinkernel::KernelError> {
    /// let context = Context::builder().chain_type(ChainType::Regtest).build()?;
    ///
    /// let chainman = ChainstateManager::builder(&context, "./data", "./blocks")?
    ///     .chainstate_db_in_memory(true)
    ///     .worker_threads(4)
    ///     .build()?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # See Also
    /// - [`ChainstateManagerBuilder`] for available configuration options
    /// - [`new`](Self::new) for a simpler constructor with default options
    pub fn builder(
        context: &Context,
        data_dir: &str,
        blocks_dir: &str,
    ) -> Result<ChainstateManagerBuilder, KernelError> {
        ChainstateManagerBuilder::new(context, data_dir, blocks_dir)
    }

    /// Creates a new chainstate manager with default configuration.
    ///
    /// This is a convenience constructor that creates a chainstate manager with default settings.
    /// For more control over configuration options (such as in-memory databases, worker threads,
    /// or database wiping), use [`builder`](Self::builder) instead.
    ///
    /// # Arguments
    /// * `context` - The [`Context`] that this chainstate manager will use. It is recommended
    ///   to keep the context in scope for the lifetime of the chainstate manager.
    /// * `data_dir` - The directory path where the chainstate manager will write its data files.
    ///   This directory will be created if it doesn't exist.
    /// * `blocks_dir` - The directory path where block files will be stored.
    ///   This directory will be created if it doesn't exist.
    ///
    /// # Errors
    /// Returns [`KernelError`] if:
    /// - The directory paths contain null bytes
    /// - The underlying C++ library fails to create the chainstate manager
    /// - Invalid data directory paths
    /// - Insufficient permissions
    /// - Corrupted database files
    ///
    /// # Examples
    /// ```no_run
    /// use bitcoinkernel::{Context, ChainType, ChainstateManager};
    ///
    /// # fn main() -> Result<(), bitcoinkernel::KernelError> {
    /// let context = Context::builder().chain_type(ChainType::Regtest).build()?;
    /// let chainman = ChainstateManager::new(&context, "./data", "./blocks")?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # See Also
    /// - [`builder`](Self::builder) for configurable construction with additional options
    pub fn new(
        context: &Context,
        data_dir: &str,
        blocks_dir: &str,
    ) -> Result<ChainstateManager, KernelError> {
        ChainstateManagerBuilder::new(context, data_dir, blocks_dir)?.build()
    }

    /// Process and validate a block.
    ///
    /// Attempts to validate and add the block to the block tree. The block goes
    /// through full consensus validation including proof-of-work, transaction
    /// validity, and script verification.
    ///
    /// # Arguments
    /// * `block` - The [`Block`] to process
    ///
    /// # Returns
    /// A [`ProcessBlockResult`] indicating whether the block was:
    /// - Accepted and committed to the block tree ([`ProcessBlockResult::NewBlock`])
    /// - Accepted but already present in the block tree ([`ProcessBlockResult::Duplicate`])
    /// - Rejected due to validation failure ([`ProcessBlockResult::Rejected`])
    ///
    /// # Important Notes
    /// - [`ProcessBlockResult::NewBlock`] indicates the block was newly written to disk,
    ///   **not** that it was added to the active chain. The block may be valid but not
    ///   extend the current best chain.
    /// - To determine full validity and chain status, you **must** register a validation
    ///   interface callback. Use [`ContextBuilder::with_block_checked_validation`](crate::ContextBuilder::with_block_checked_validation)
    ///   or [`ContextBuilder::validation`](crate::ContextBuilder::validation) to receive detailed
    ///   validation state through the block checked callback.
    ///
    /// # Validation Details
    /// The block checked callback receives a [`BlockValidationStateRef`](crate::notifications::types::BlockValidationStateRef)
    /// containing validation results for the block. This callback fires for all validated blocks.
    /// To detect when a block extends the active chain, use [`ContextBuilder::with_block_connected_validation`](crate::ContextBuilder::with_block_connected_validation)
    /// or [`ContextBuilder::validation`](crate::ContextBuilder::validation).
    ///
    /// # Example
    /// ```no_run
    /// # use bitcoinkernel::{Block, ChainstateManager, ProcessBlockResult};
    /// # let chainman: ChainstateManager = unimplemented!();
    /// # let block: Block = unimplemented!();
    /// match chainman.process_block(&block) {
    ///     ProcessBlockResult::NewBlock => println!("Block validated and written to disk"),
    ///     ProcessBlockResult::Duplicate => println!("Block already known (valid)"),
    ///     ProcessBlockResult::Rejected => println!("Block validation failed"),
    /// }
    /// ```
    pub fn process_block(&self, block: &Block) -> ProcessBlockResult {
        let mut new_block: i32 = 0;
        let accepted = unsafe {
            btck_chainstate_manager_process_block(self.inner, block.as_ptr(), &mut new_block)
        };

        let is_accepted = c_helpers::success(accepted);
        let is_new = c_helpers::enabled(new_block);

        match (is_accepted, is_new) {
            (true, true) => ProcessBlockResult::NewBlock,
            (true, false) => ProcessBlockResult::Duplicate,
            (false, _) => ProcessBlockResult::Rejected,
        }
    }

    /// Initialize the chainstate manager and optionally trigger a reindex.
    ///
    /// This should be called after creating the chainstate manager to complete
    /// initialization. If the `wipe_db` option was set, this will trigger a
    /// blockchain reindex.
    ///
    /// # Errors
    /// Returns [`KernelError::Internal`] if initialization fails.
    pub fn import_blocks(&self) -> Result<(), KernelError> {
        let result = unsafe {
            btck_chainstate_manager_import_blocks(
                self.inner,
                std::ptr::null_mut(),
                std::ptr::null_mut(),
                0,
            )
        };
        match c_helpers::success(result) {
            true => Ok(()),
            false => Err(KernelError::Internal(
                "Failed to import blocks.".to_string(),
            )),
        }
    }

    /// Read a block's full data from disk.
    ///
    /// # Arguments
    /// * `entry` - The [`BlockTreeEntry`] identifying which block to read
    ///
    /// # Returns
    /// The complete [`Block`] including all transactions.
    ///
    /// # Errors
    /// Returns [`KernelError::Internal`] if:
    /// - The block file cannot be read
    /// - The block data is corrupted
    /// - The block has been pruned
    pub fn read_block_data(&self, entry: &BlockTreeEntry) -> Result<Block, KernelError> {
        let inner = unsafe { btck_block_read(self.inner, entry.as_ptr()) };
        if inner.is_null() {
            return Err(KernelError::Internal("Failed to read block.".to_string()));
        }
        Ok(unsafe { Block::from_ptr(inner) })
    }

    /// Read a block's spent outputs (undo data) from disk.
    ///
    /// Retrieves the spent outputs associated with a specific block. Spent outputs
    /// contain information about the transaction outputs that were consumed when the
    /// block's transactions were applied to the UTXO set. This data is essential for
    /// rolling back blocks during chain reorganizations.
    ///
    /// # Arguments
    /// * `entry` - The [`BlockTreeEntry`] identifying which block's spent outputs to read
    ///
    /// # Returns
    /// The [`BlockSpentOutputs`] for the specified block, containing the transaction
    /// outputs that were consumed by the block's transactions.
    ///
    /// # Errors
    /// Returns [`KernelError::Internal`] if:
    /// - The undo data file cannot be read
    /// - The undo data is corrupted
    /// - The block has been pruned
    pub fn read_spent_outputs(
        &self,
        entry: &BlockTreeEntry,
    ) -> Result<BlockSpentOutputs, KernelError> {
        let inner = unsafe { btck_block_spent_outputs_read(self.inner, entry.as_ptr()) };
        if inner.is_null() {
            return Err(KernelError::Internal(
                "Failed to read undo data.".to_string(),
            ));
        }
        Ok(unsafe { BlockSpentOutputs::from_ptr(inner) })
    }

    /// Get a reference to the currently active blockchain.
    ///
    /// Returns the active chain, which represents the chain with the most
    /// accumulated proof-of-work. This is the canonical chain used for validation.
    ///
    /// # Returns
    /// A [`Chain`] reference representing the active chain.
    ///
    /// # Lifetime
    /// The returned [`Chain`] reference is tied to the lifetime of the
    /// [`ChainstateManager`] and becomes invalid when the manager is dropped.
    pub fn active_chain(&self) -> Chain<'_> {
        let ptr = unsafe { btck_chainstate_manager_get_active_chain(self.inner) };
        unsafe { Chain::from_ptr(ptr) }
    }

    /// Get a block tree entry by its block hash.
    ///
    /// Looks up a block in the block tree using its hash. The block tree contains
    /// metadata about all known blocks, including those not on the active chain.
    ///
    /// # Arguments
    /// * `block_hash` - The [`BlockHash`] of the block to look up
    ///
    /// # Returns
    /// * `Some(`[`BlockTreeEntry`]`)` - If a block with the given hash exists in the block tree
    /// * `None` - If no block with the given hash is found
    ///
    /// # Lifetime
    /// The returned [`BlockTreeEntry`] reference is tied to the lifetime of the
    /// [`ChainstateManager`] and becomes invalid when the manager is dropped.
    pub fn get_block_tree_entry(&self, block_hash: &BlockHash) -> Option<BlockTreeEntry<'_>> {
        let ptr = unsafe {
            btck_chainstate_manager_get_block_tree_entry_by_hash(
                self.inner,
                block_hash as *const _ as *const btck_BlockHash,
            )
        };
        if ptr.is_null() {
            None
        } else {
            Some(unsafe { BlockTreeEntry::from_ptr(ptr) })
        }
    }
}

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

/// Builder for configuring and creating a [`ChainstateManager`].
///
/// Provides a fluent interface for configuring how the chainstate manager
/// will initialize and operate. Options control database locations, in-memory
/// operation, worker thread allocation, and database initialization behavior.
///
/// # Usage
/// Create a builder using [`ChainstateManager::builder`], configure options
/// by chaining method calls, then call [`build`](Self::build) to create the
/// chainstate manager.
///
/// # Example
/// ```no_run
/// use bitcoinkernel::{ChainType, ChainstateManager, ContextBuilder, KernelError};
///
/// # fn main() -> Result<(), KernelError> {
/// let context = ContextBuilder::new()
///     .chain_type(ChainType::Regtest)
///     .build()?;
///
/// let chainman = ChainstateManager::builder(&context, "/data", "/blocks")?
///     .worker_threads(4)
///     .chainstate_db_in_memory(true)
///     .block_tree_db_in_memory(true)
///     .build()?;
/// # Ok(())
/// # }
/// ```
pub struct ChainstateManagerBuilder {
    inner: *mut btck_ChainstateManagerOptions,
}

impl ChainstateManagerBuilder {
    /// Creates a new chainstate manager builder.
    ///
    /// This is typically called via [`ChainstateManager::builder`] rather than directly.
    ///
    /// # Arguments
    /// * `context` - The [`Context`] that configures chain parameters and
    ///   notification callbacks. It is recommended to keep the context in scope
    ///   for the lifetime of the [`ChainstateManager`].
    /// * `data_dir` - Path to the directory where chainstate data will be stored.
    ///   This includes the UTXO set database and other chain state information.
    /// * `blocks_dir` - Path to the directory where block data will be stored.
    ///   This includes the raw block files and the block index database.
    ///
    /// # Errors
    /// Returns [`KernelError`] if:
    /// - The paths contain null bytes (invalid C strings)
    /// - The underlying C++ library fails to create the builder
    pub fn new(context: &Context, data_dir: &str, blocks_dir: &str) -> Result<Self, KernelError> {
        let c_data_dir = CString::new(data_dir)?;
        let c_blocks_dir = CString::new(blocks_dir)?;
        let inner = unsafe {
            btck_chainstate_manager_options_create(
                context.as_ptr(),
                c_data_dir.as_ptr(),
                c_data_dir.as_bytes().len(),
                c_blocks_dir.as_ptr(),
                c_blocks_dir.as_bytes().len(),
            )
        };
        if inner.is_null() {
            return Err(KernelError::Internal(
                "Failed to create chainstate manager options.".to_string(),
            ));
        }
        Ok(Self { inner })
    }

    /// Sets the number of worker threads for validation.
    ///
    /// Block validation can be parallelized across multiple threads to improve
    /// performance. More threads generally result in faster validation, but with
    /// diminishing returns beyond the number of available CPU cores.
    ///
    /// # Arguments
    /// * `worker_threads` - Number of worker threads to use for validation.
    ///   Valid range is 0-15 (values outside this range are clamped). When set to 0,
    ///   no parallel verification is performed.
    pub fn worker_threads(self, worker_threads: i32) -> Self {
        unsafe {
            btck_chainstate_manager_options_set_worker_threads_num(self.inner, worker_threads);
        }
        self
    }

    /// Configures database wiping behavior.
    ///
    /// When enabled, this will delete and recreate the specified databases on
    /// initialization. After wiping, [`ChainstateManager::import_blocks`] will
    /// trigger a reindex to rebuild the databases from the block files.
    ///
    /// # Arguments
    /// * `wipe_block_tree` - If true, wipe the block tree database (block index).
    ///   Must be false if `wipe_chainstate` is false.
    /// * `wipe_chainstate` - If true, wipe the chainstate database (UTXO set)
    ///
    /// # Reindex Behavior
    /// - Wiping both databases triggers a full reindex
    /// - Wiping only the chainstate triggers a chainstate-only reindex
    ///
    /// # Errors
    /// Returns [`KernelError::InvalidOptions`] if `wipe_block_tree` is true but
    /// `wipe_chainstate` is false, as this combination is currently unsupported.
    ///
    /// # Examples
    /// ```no_run
    /// # use bitcoinkernel::{ChainType, ChainstateManager, ContextBuilder, KernelError};
    /// # fn main() -> Result<(), KernelError> {
    /// # let context = ContextBuilder::new().chain_type(ChainType::Regtest).build()?;
    /// // Wipe both databases for a full reindex
    /// let chainman = ChainstateManager::builder(&context, "/data", "/blocks")?
    ///     .wipe_db(true, true)?
    ///     .build()?;
    ///
    /// // Only wipe chainstate (e.g., to rebuild UTXO set)
    /// let chainman = ChainstateManager::builder(&context, "/data", "/blocks")?
    ///     .wipe_db(false, true)?
    ///     .build()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn wipe_db(
        self,
        wipe_block_tree: bool,
        wipe_chainstate: bool,
    ) -> Result<Self, KernelError> {
        let result = unsafe {
            btck_chainstate_manager_options_set_wipe_dbs(
                self.inner,
                c_helpers::to_c_bool(wipe_block_tree),
                c_helpers::to_c_bool(wipe_chainstate),
            )
        };
        if c_helpers::success(result) {
            Ok(self)
        } else {
            Err(KernelError::InvalidOptions(
                "Wiping the block tree without also wiping the chainstate is currently unsupported"
                    .to_string(),
            ))
        }
    }

    /// Configures the block tree database to run entirely in memory.
    ///
    /// When enabled, the block tree database (which stores the block index and
    /// metadata about all known blocks) will be stored in RAM rather than on disk.
    /// This can improve performance but requires sufficient memory and means the
    /// database will be lost when the process exits.
    ///
    /// # Arguments
    /// * `block_tree_db_in_memory` - If true, run the block tree database in memory
    pub fn block_tree_db_in_memory(self, block_tree_db_in_memory: bool) -> Self {
        unsafe {
            btck_chainstate_manager_options_update_block_tree_db_in_memory(
                self.inner,
                c_helpers::to_c_bool(block_tree_db_in_memory),
            );
        }
        self
    }

    /// Configures the chainstate database to run entirely in memory.
    ///
    /// When enabled, the chainstate database (which stores the current UTXO set)
    /// will be stored in RAM rather than on disk. This can significantly improve
    /// performance but requires substantial memory (several gigabytes for mainnet)
    /// and means the database will be lost when the process exits.
    ///
    /// # Arguments
    /// * `chainstate_db_in_memory` - If true, run the chainstate database in memory
    ///
    /// # Example
    /// ```no_run
    /// # use bitcoinkernel::{ChainType, ChainstateManager, ContextBuilder, KernelError};
    /// # fn main() -> Result<(), KernelError> {
    /// # let context = ContextBuilder::new().chain_type(ChainType::Regtest).build()?;
    /// // Use in-memory chainstate for fast testing
    /// let chainman = ChainstateManager::builder(&context, "/data", "/blocks")?
    ///     .chainstate_db_in_memory(true)
    ///     .build()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn chainstate_db_in_memory(self, chainstate_db_in_memory: bool) -> Self {
        unsafe {
            btck_chainstate_manager_options_update_chainstate_db_in_memory(
                self.inner,
                c_helpers::to_c_bool(chainstate_db_in_memory),
            );
        }
        self
    }

    /// Builds the [`ChainstateManager`] with the configured options.
    ///
    /// Consumes the builder and creates a new chainstate manager instance.
    ///
    /// # Errors
    /// Returns [`KernelError::Internal`] if the underlying C++ library fails
    /// to create the chainstate manager.
    pub fn build(self) -> Result<ChainstateManager, KernelError> {
        let inner = unsafe { btck_chainstate_manager_create(self.inner) };
        if inner.is_null() {
            return Err(KernelError::Internal(
                "Failed to create chainstate manager.".to_string(),
            ));
        }
        Ok(ChainstateManager { inner })
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{ChainType, ContextBuilder};
    use tempdir::TempDir;

    fn create_test_context() -> Context {
        ContextBuilder::new()
            .chain_type(ChainType::Regtest)
            .build()
            .unwrap()
    }

    fn create_test_dirs() -> (TempDir, String, String) {
        let temp_dir = TempDir::new("test_chainman").unwrap();
        let data_dir = temp_dir.path().to_str().unwrap().to_string();
        let blocks_dir = format!("{}/blocks", data_dir);
        (temp_dir, data_dir, blocks_dir)
    }

    #[test]
    fn test_chainstate_manager_options_new() {
        let context = create_test_context();
        let (_temp_dir, data_dir, blocks_dir) = create_test_dirs();

        let builder = ChainstateManagerBuilder::new(&context, &data_dir, &blocks_dir);
        assert!(builder.is_ok());
    }

    #[test]
    fn test_chainstate_manager_options_invalid_path() {
        let context = create_test_context();

        let invalid_path = "test\0path";
        let blocks_dir = "blocks";

        let builder = ChainstateManagerBuilder::new(&context, invalid_path, blocks_dir);
        assert!(builder.is_err());
    }

    #[test]
    fn test_wipe_block_tree_without_chainstate_fails() {
        let context = create_test_context();
        let (_temp_dir, data_dir, blocks_dir) = create_test_dirs();

        let result = ChainstateManagerBuilder::new(&context, &data_dir, &blocks_dir)
            .unwrap()
            .wipe_db(true, false);

        assert!(result.is_err());
        if let Err(e) = result {
            assert!(matches!(e, KernelError::InvalidOptions(_)));
        }
    }

    #[test]
    fn test_chainstate_manager_creation() {
        let context = create_test_context();
        let (_temp_dir, data_dir, blocks_dir) = create_test_dirs();

        let chainman = ChainstateManagerBuilder::new(&context, &data_dir, &blocks_dir)
            .unwrap()
            .block_tree_db_in_memory(true)
            .chainstate_db_in_memory(true)
            .wipe_db(false, true)
            .unwrap()
            .worker_threads(4)
            .build();

        assert!(chainman.is_ok());
    }

    #[test]
    fn test_process_block_result_new_block() {
        let result = ProcessBlockResult::NewBlock;

        assert_eq!(result, ProcessBlockResult::NewBlock);
        assert!(result.is_new_block());
        assert!(!result.is_duplicate());
        assert!(!result.is_rejected());
    }

    #[test]
    fn test_process_block_result_duplicate() {
        let result = ProcessBlockResult::Duplicate;

        assert_eq!(result, ProcessBlockResult::Duplicate);
        assert!(!result.is_new_block());
        assert!(result.is_duplicate());
        assert!(!result.is_rejected());
    }

    #[test]
    fn test_process_block_result_rejected() {
        let result = ProcessBlockResult::Rejected;

        assert_eq!(result, ProcessBlockResult::Rejected);
        assert!(!result.is_new_block());
        assert!(!result.is_duplicate());
        assert!(result.is_rejected());
    }

    #[test]
    fn test_process_block_result_match() {
        let result = ProcessBlockResult::NewBlock;

        let message = match result {
            ProcessBlockResult::NewBlock => "new",
            ProcessBlockResult::Duplicate => "duplicate",
            ProcessBlockResult::Rejected => "rejected",
        };

        assert_eq!(message, "new");
    }

    #[test]
    fn test_process_block_result_equality() {
        assert_eq!(ProcessBlockResult::NewBlock, ProcessBlockResult::NewBlock);
        assert_ne!(ProcessBlockResult::NewBlock, ProcessBlockResult::Rejected);
        assert_ne!(ProcessBlockResult::Duplicate, ProcessBlockResult::Rejected);
    }

    #[test]
    fn test_process_block_result_debug() {
        let result = ProcessBlockResult::NewBlock;
        let debug_str = format!("{:?}", result);
        assert_eq!(debug_str, "NewBlock");
    }
}