Skip to content

Commit 6c8222e

Browse files
committed
Make context an Arc in the chainman
1 parent 55ecd88 commit 6c8222e

File tree

4 files changed

+60
-48
lines changed

4 files changed

+60
-48
lines changed

examples/src/silentpaymentscanner.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::env;
22
use std::fmt;
33
use std::process;
4+
use std::sync::Arc;
45

56
use bitcoin::consensus::deserialize;
67
use bitcoin::hashes::Hash;
@@ -33,11 +34,13 @@ fn setup_logging() -> Result<Logger<MainLog>, KernelError> {
3334
Logger::new(MainLog {})
3435
}
3536

36-
fn create_context() -> Context {
37-
ContextBuilder::new()
38-
.chain_type(ChainType::REGTEST)
39-
.build()
40-
.unwrap()
37+
fn create_context() -> Arc<Context> {
38+
Arc::new(
39+
ContextBuilder::new()
40+
.chain_type(ChainType::REGTEST)
41+
.build()
42+
.unwrap(),
43+
)
4144
}
4245

4346
fn vec_to_hex_string(data: &Vec<u8>) -> String {
@@ -227,7 +230,7 @@ fn main() {
227230
let chainman = ChainstateManager::new(
228231
ChainstateManagerOptions::new(&context, &data_dir).unwrap(),
229232
BlockManagerOptions::new(&context, &blocks_dir).unwrap(),
230-
&context,
233+
Arc::clone(&context),
231234
)
232235
.unwrap();
233236
chainman

fuzz/fuzz_targets/fuzz_target_chainman.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![no_main]
22

3-
use std::sync::Once;
3+
use std::sync::{Arc, Once};
44

55
use libfuzzer_sys::fuzz_target;
66

@@ -12,20 +12,22 @@ use bitcoinkernel::{
1212
KernelNotificationInterfaceCallbackHolder,
1313
};
1414

15-
fn create_context(chain_type: ChainType) -> Context {
16-
ContextBuilder::new()
17-
.chain_type(chain_type)
18-
.kn_callbacks(Box::new(KernelNotificationInterfaceCallbackHolder {
19-
kn_block_tip: Box::new(|_state, _block_index| {}),
20-
kn_header_tip: Box::new(|_state, _height, _timestamp, _presync| {}),
21-
kn_progress: Box::new(|_title, _progress, _resume_possible| {}),
22-
kn_warning_set: Box::new(|_warning, _message| {}),
23-
kn_warning_unset: Box::new(|_warning| {}),
24-
kn_flush_error: Box::new(|_message| {}),
25-
kn_fatal_error: Box::new(|_message| {}),
26-
}))
27-
.build()
28-
.unwrap()
15+
fn create_context(chain_type: ChainType) -> Arc<Context> {
16+
Arc::new(
17+
ContextBuilder::new()
18+
.chain_type(chain_type)
19+
.kn_callbacks(Box::new(KernelNotificationInterfaceCallbackHolder {
20+
kn_block_tip: Box::new(|_state, _block_index| {}),
21+
kn_header_tip: Box::new(|_state, _height, _timestamp, _presync| {}),
22+
kn_progress: Box::new(|_title, _progress, _resume_possible| {}),
23+
kn_warning_set: Box::new(|_warning, _message| {}),
24+
kn_warning_unset: Box::new(|_warning| {}),
25+
kn_flush_error: Box::new(|_message| {}),
26+
kn_fatal_error: Box::new(|_message| {}),
27+
}))
28+
.build()
29+
.unwrap(),
30+
)
2931
}
3032

3133
#[derive(Debug, Arbitrary)]
@@ -82,7 +84,8 @@ fuzz_target!(|data: ChainstateManagerInput| {
8284
Err(err) => panic!("this should never happen: {}", err),
8385
};
8486
let blockman_opts = BlockManagerOptions::new(&context, &blocks_dir).unwrap();
85-
let chainman = ChainstateManager::new(chainman_opts, blockman_opts, &context).unwrap();
87+
let chainman =
88+
ChainstateManager::new(chainman_opts, blockman_opts, Arc::clone(&context)).unwrap();
8689

8790
match chainman.load_chainstate(
8891
ChainstateLoadOptions::new()

src/lib.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::ffi::{CStr, CString, NulError};
66
use std::fmt;
77
use std::marker::PhantomData;
88
use std::os::raw::{c_char, c_void};
9+
use std::sync::Arc;
910

1011
use libbitcoinkernel_sys::*;
1112

@@ -297,9 +298,12 @@ impl Drop for ChainParams {
297298
/// The main context struct. This should be setup through the [`ContextBuilder`] and
298299
/// has to be kept in memory for the duration of context-dependent library
299300
/// operations.
301+
///
300302
pub struct Context {
301303
inner: *mut kernel_Context,
302-
pub kn_callbacks: Box<KernelNotificationInterfaceCallbackHolder>,
304+
// We need something to hold this in memory.
305+
#[allow(dead_code)]
306+
kn_callbacks: Option<Box<KernelNotificationInterfaceCallbackHolder>>,
303307
}
304308

305309
unsafe impl Send for Context {}
@@ -350,7 +354,7 @@ impl ContextBuilder {
350354
unsafe { kernel_context_options_destroy(self.inner) };
351355
Ok(Context {
352356
inner,
353-
kn_callbacks: self.kn_callbacks.unwrap(),
357+
kn_callbacks: self.kn_callbacks,
354358
})
355359
}
356360

@@ -773,24 +777,24 @@ impl Drop for Block {
773777
/// tree once it is loaded. The [`BlockIndex`] points to an entry in this tree.
774778
/// It is only valid as long as the [`ChainstateManager`] it was retrieved from
775779
/// remains in scope.
776-
pub struct BlockIndex<'a> {
780+
pub struct BlockIndex {
777781
inner: *mut kernel_BlockIndex,
778-
marker: PhantomData<ChainstateManager<'a>>,
782+
marker: PhantomData<ChainstateManager>,
779783
}
780784

781-
unsafe impl Send for BlockIndex<'_> {}
782-
unsafe impl Sync for BlockIndex<'_> {}
785+
unsafe impl Send for BlockIndex {}
786+
unsafe impl Sync for BlockIndex {}
783787

784788
/// A type for a Block hash.
785789
#[derive(Debug, Clone)]
786790
pub struct BlockHash {
787791
pub hash: [u8; 32],
788792
}
789793

790-
impl<'a> BlockIndex<'a> {
794+
impl BlockIndex {
791795
/// Move to the previous entry in the block tree. E.g. from height n to
792796
/// height n-1.
793-
pub fn prev(self) -> Result<BlockIndex<'a>, KernelError> {
797+
pub fn prev(self) -> Result<BlockIndex, KernelError> {
794798
let inner = unsafe { kernel_get_previous_block_index(self.inner) };
795799
if inner.is_null() {
796800
return Err(KernelError::OutOfBounds);
@@ -808,7 +812,7 @@ impl<'a> BlockIndex<'a> {
808812
}
809813

810814
/// Get the current block hash associated with this BlockIndex.
811-
pub fn info(&self) -> BlockHash {
815+
pub fn block_hash(&self) -> BlockHash {
812816
let hash = unsafe { kernel_block_index_get_block_hash(self.inner) };
813817
let res = BlockHash {
814818
hash: unsafe { (&*hash).hash },
@@ -818,7 +822,7 @@ impl<'a> BlockIndex<'a> {
818822
}
819823
}
820824

821-
impl<'a> Drop for BlockIndex<'a> {
825+
impl<'a> Drop for BlockIndex {
822826
fn drop(&mut self) {
823827
unsafe { kernel_block_index_destroy(self.inner) };
824828
}
@@ -996,19 +1000,19 @@ impl Drop for ChainstateLoadOptions {
9961000
/// was created remains in memory.
9971001
///
9981002
/// Its functionality will be more and more exposed in the future.
999-
pub struct ChainstateManager<'a> {
1003+
pub struct ChainstateManager {
10001004
inner: *mut kernel_ChainstateManager,
1001-
context: &'a Context,
1005+
context: Arc<Context>,
10021006
}
10031007

1004-
unsafe impl Send for ChainstateManager<'_> {}
1005-
unsafe impl Sync for ChainstateManager<'_> {}
1008+
unsafe impl Send for ChainstateManager {}
1009+
unsafe impl Sync for ChainstateManager {}
10061010

1007-
impl<'a> ChainstateManager<'a> {
1011+
impl<'a> ChainstateManager {
10081012
pub fn new(
10091013
chainman_opts: ChainstateManagerOptions,
10101014
blockman_opts: BlockManagerOptions,
1011-
context: &'a Context,
1015+
context: Arc<Context>,
10121016
) -> Result<Self, KernelError> {
10131017
let inner = unsafe {
10141018
kernel_chainstate_manager_create(
@@ -1178,7 +1182,7 @@ impl<'a> ChainstateManager<'a> {
11781182
}
11791183
}
11801184

1181-
impl<'a> Drop for ChainstateManager<'a> {
1185+
impl Drop for ChainstateManager {
11821186
fn drop(&mut self) {
11831187
unsafe {
11841188
kernel_chainstate_manager_destroy(self.inner, self.context.inner);

tests/test.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod tests {
1111
};
1212
use std::fs::File;
1313
use std::io::{BufRead, BufReader};
14-
use std::sync::Once;
14+
use std::sync::{Arc, Once};
1515
use tempdir::TempDir;
1616

1717
struct TestLog {}
@@ -78,11 +78,11 @@ mod tests {
7878
validation_interface
7979
}
8080

81-
fn testing_setup() -> (Context, ValidationInterfaceWrapper, String) {
81+
fn testing_setup() -> (Arc<Context>, ValidationInterfaceWrapper, String) {
8282
START.call_once(|| {
8383
setup_logging();
8484
});
85-
let context = create_context();
85+
let context = Arc::new(create_context());
8686
let validation_interface = setup_validation_interface(&context);
8787

8888
let temp_dir = TempDir::new("test_chainman_regtest").unwrap();
@@ -114,7 +114,7 @@ mod tests {
114114
let chainman = ChainstateManager::new(
115115
ChainstateManagerOptions::new(&context, &data_dir).unwrap(),
116116
BlockManagerOptions::new(&context, &blocks_dir).unwrap(),
117-
&context,
117+
Arc::clone(&context),
118118
)
119119
.unwrap();
120120
chainman
@@ -129,7 +129,7 @@ mod tests {
129129
let chainman = ChainstateManager::new(
130130
ChainstateManagerOptions::new(&context, &data_dir).unwrap(),
131131
BlockManagerOptions::new(&context, &blocks_dir).unwrap(),
132-
&context,
132+
Arc::clone(&context),
133133
)
134134
.unwrap();
135135
chainman
@@ -148,7 +148,7 @@ mod tests {
148148
let chainman = ChainstateManager::new(
149149
ChainstateManagerOptions::new(&context, &data_dir).unwrap(),
150150
BlockManagerOptions::new(&context, &blocks_dir).unwrap(),
151-
&context,
151+
Arc::clone(&context),
152152
)
153153
.unwrap();
154154
chainman
@@ -181,6 +181,7 @@ mod tests {
181181

182182
#[test]
183183
fn test_scan_tx() {
184+
#[allow(dead_code)]
184185
#[derive(Debug)]
185186
struct Input {
186187
prevout: Vec<u8>,
@@ -191,6 +192,7 @@ mod tests {
191192
#[derive(Debug)]
192193
struct ScanTxHelper {
193194
ins: Vec<Input>,
195+
#[allow(dead_code)]
194196
outs: Vec<Vec<u8>>,
195197
}
196198

@@ -200,7 +202,7 @@ mod tests {
200202
let chainman = ChainstateManager::new(
201203
ChainstateManagerOptions::new(&context, &data_dir).unwrap(),
202204
BlockManagerOptions::new(&context, &blocks_dir).unwrap(),
203-
&context,
205+
Arc::clone(&context),
204206
)
205207
.unwrap();
206208
chainman
@@ -271,7 +273,7 @@ mod tests {
271273
let chainman = ChainstateManager::new(
272274
ChainstateManagerOptions::new(&context, &data_dir).unwrap(),
273275
BlockManagerOptions::new(&context, &blocks_dir).unwrap(),
274-
&context,
276+
Arc::clone(&context),
275277
)
276278
.unwrap();
277279
chainman
@@ -294,7 +296,7 @@ mod tests {
294296
let chainman = ChainstateManager::new(
295297
ChainstateManagerOptions::new(&context, &data_dir).unwrap(),
296298
BlockManagerOptions::new(&context, &blocks_dir).unwrap(),
297-
&context,
299+
Arc::clone(&context),
298300
)
299301
.unwrap();
300302
chainman

0 commit comments

Comments
 (0)