Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions src/notifications/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,43 @@ where

/// Callback for when a new PoW valid block is found.
pub trait NewPoWValidBlockCallback: Send + Sync {
fn on_new_pow_valid_block<'a>(&self, block: Block, pindex: BlockTreeEntry<'a>);
fn on_new_pow_valid_block<'a>(&self, block: Block, entry: BlockTreeEntry<'a>);
}

impl<F> NewPoWValidBlockCallback for F
where
F: for<'a> Fn(BlockTreeEntry<'a>, Block) + Send + Sync + 'static,
{
fn on_new_pow_valid_block<'a>(&self, block: Block, pindex: BlockTreeEntry<'a>) {
self(pindex, block)
fn on_new_pow_valid_block<'a>(&self, block: Block, entry: BlockTreeEntry<'a>) {
self(entry, block)
}
}

/// Callback for when a block is connected to the chain.
pub trait BlockConnectedCallback: Send + Sync {
fn on_block_connected<'a>(&self, block: Block, pindex: BlockTreeEntry<'a>);
fn on_block_connected<'a>(&self, block: Block, entry: BlockTreeEntry<'a>);
}

impl<F> BlockConnectedCallback for F
where
F: for<'a> Fn(Block, BlockTreeEntry<'a>) + Send + Sync + 'static,
{
fn on_block_connected<'a>(&self, block: Block, pindex: BlockTreeEntry<'a>) {
self(block, pindex)
fn on_block_connected<'a>(&self, block: Block, entry: BlockTreeEntry<'a>) {
self(block, entry)
}
}

/// Callback for when a block is disconnected from the chain.
pub trait BlockDisconnectedCallback: Send + Sync {
fn on_block_disconnected<'a>(&self, block: Block, pindex: BlockTreeEntry<'a>);
fn on_block_disconnected<'a>(&self, block: Block, entry: BlockTreeEntry<'a>);
}

impl<F> BlockDisconnectedCallback for F
where
F: for<'a> Fn(Block, BlockTreeEntry<'a>) + Send + Sync + 'static,
{
fn on_block_disconnected<'a>(&self, block: Block, pindex: BlockTreeEntry<'a>) {
self(block, pindex)
fn on_block_disconnected<'a>(&self, block: Block, entry: BlockTreeEntry<'a>) {
self(block, entry)
}
}

Expand Down Expand Up @@ -134,47 +134,47 @@ pub(crate) unsafe extern "C" fn validation_block_checked_wrapper(
pub(crate) unsafe extern "C" fn validation_new_pow_valid_block_wrapper(
user_data: *mut c_void,
block: *mut btck_Block,
pindex: *const btck_BlockTreeEntry,
entry: *const btck_BlockTreeEntry,
) {
let block = Block::from_ptr(block);
let registry = &*(user_data as *mut ValidationCallbackRegistry);

if let Some(ref handler) = registry.new_pow_valid_block_handler {
handler.on_new_pow_valid_block(
block,
BlockTreeEntry::from_ptr(pindex as *mut btck_BlockTreeEntry),
BlockTreeEntry::from_ptr(entry as *mut btck_BlockTreeEntry),
);
}
}

pub(crate) unsafe extern "C" fn validation_block_connected_wrapper(
user_data: *mut c_void,
block: *mut btck_Block,
pindex: *const btck_BlockTreeEntry,
entry: *const btck_BlockTreeEntry,
) {
let block = Block::from_ptr(block);
let registry = &*(user_data as *mut ValidationCallbackRegistry);

if let Some(ref handler) = registry.block_connected_handler {
handler.on_block_connected(
block,
BlockTreeEntry::from_ptr(pindex as *mut btck_BlockTreeEntry),
BlockTreeEntry::from_ptr(entry as *mut btck_BlockTreeEntry),
);
}
}

pub(crate) unsafe extern "C" fn validation_block_disconnected_wrapper(
user_data: *mut c_void,
block: *mut btck_Block,
pindex: *const btck_BlockTreeEntry,
entry: *const btck_BlockTreeEntry,
) {
let block = Block::from_ptr(block);
let registry = &*(user_data as *mut ValidationCallbackRegistry);

if let Some(ref handler) = registry.block_disconnected_handler {
handler.on_block_disconnected(
block,
BlockTreeEntry::from_ptr(pindex as *mut btck_BlockTreeEntry),
BlockTreeEntry::from_ptr(entry as *mut btck_BlockTreeEntry),
);
}
}
Expand Down Expand Up @@ -213,7 +213,7 @@ mod tests {

#[test]
fn test_new_pow_valid_block_registration() {
fn handler(_pindex: BlockTreeEntry, _block: Block) {}
fn handler(_entry: BlockTreeEntry, _block: Block) {}

let mut registry = ValidationCallbackRegistry::new();
registry.register_new_pow_valid_block(handler);
Expand All @@ -222,7 +222,7 @@ mod tests {

#[test]
fn test_block_connected_registration() {
fn handler(_block: Block, _pindex: BlockTreeEntry) {}
fn handler(_block: Block, _entry: BlockTreeEntry) {}

let mut registry = ValidationCallbackRegistry::new();
registry.register_block_connected(handler);
Expand All @@ -231,7 +231,7 @@ mod tests {

#[test]
fn test_block_disconnected_registration() {
fn handler(_block: Block, _pindex: BlockTreeEntry) {}
fn handler(_block: Block, _entry: BlockTreeEntry) {}

let mut registry = ValidationCallbackRegistry::new();
registry.register_block_disconnected(handler);
Expand Down Expand Up @@ -272,14 +272,14 @@ mod tests {
let called_clone = Arc::clone(&called);

let mut registry = ValidationCallbackRegistry::new();
registry.register_new_pow_valid_block(move |_pindex: BlockTreeEntry, _block: Block| {
registry.register_new_pow_valid_block(move |_entry: BlockTreeEntry, _block: Block| {
*called_clone.lock().unwrap() = true;
});

if let Some(ref handler) = registry.new_pow_valid_block_handler {
let block = unsafe { Block::from_ptr(std::ptr::null_mut()) };
let pindex = unsafe { BlockTreeEntry::from_ptr(std::ptr::null_mut()) };
handler.on_new_pow_valid_block(block, pindex);
let entry = unsafe { BlockTreeEntry::from_ptr(std::ptr::null_mut()) };
handler.on_new_pow_valid_block(block, entry);
}

assert!(*called.lock().unwrap());
Expand All @@ -291,14 +291,14 @@ mod tests {
let called_clone = Arc::clone(&called);

let mut registry = ValidationCallbackRegistry::new();
registry.register_block_connected(move |_block: Block, _pindex: BlockTreeEntry| {
registry.register_block_connected(move |_block: Block, _entry: BlockTreeEntry| {
*called_clone.lock().unwrap() = true;
});

if let Some(ref handler) = registry.block_connected_handler {
let block = unsafe { Block::from_ptr(std::ptr::null_mut()) };
let pindex = unsafe { BlockTreeEntry::from_ptr(std::ptr::null_mut()) };
handler.on_block_connected(block, pindex);
let entry = unsafe { BlockTreeEntry::from_ptr(std::ptr::null_mut()) };
handler.on_block_connected(block, entry);
}

assert!(*called.lock().unwrap());
Expand All @@ -310,14 +310,14 @@ mod tests {
let called_clone = Arc::clone(&called);

let mut registry = ValidationCallbackRegistry::new();
registry.register_block_disconnected(move |_block: Block, _pindex: BlockTreeEntry| {
registry.register_block_disconnected(move |_block: Block, _entry: BlockTreeEntry| {
*called_clone.lock().unwrap() = true;
});

if let Some(ref handler) = registry.block_disconnected_handler {
let block = unsafe { Block::from_ptr(std::ptr::null_mut()) };
let pindex = unsafe { BlockTreeEntry::from_ptr(std::ptr::null_mut()) };
handler.on_block_disconnected(block, pindex);
let entry = unsafe { BlockTreeEntry::from_ptr(std::ptr::null_mut()) };
handler.on_block_disconnected(block, entry);
}

assert!(*called.lock().unwrap());
Expand Down
32 changes: 16 additions & 16 deletions src/state/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ impl ContextBuilder {
///
/// # Arguments
/// * `handler` - The callback function or closure that receives:
/// - `pindex` - The [`BlockTreeEntry`](crate::BlockTreeEntry) for the new block
/// - `entry` - The [`BlockTreeEntry`](crate::BlockTreeEntry) for the new block
/// - `block` - The [`Block`](crate::Block) data
///
/// # Returns
Expand All @@ -822,9 +822,9 @@ impl ContextBuilder {
/// use bitcoinkernel::{Block, BlockTreeEntry, ContextBuilder, KernelError};
///
/// let context = ContextBuilder::new()
/// .with_new_pow_valid_block_validation(|pindex: BlockTreeEntry<'_>, block: Block| {
/// .with_new_pow_valid_block_validation(|entry: BlockTreeEntry<'_>, block: Block| {
/// println!("New PoW-valid block at height {}: {}",
/// pindex.height(), block.hash());
/// entry.height(), block.hash());
/// })
/// .build()?;
/// # Ok::<(), KernelError>(())
Expand All @@ -850,7 +850,7 @@ impl ContextBuilder {
/// # Arguments
/// * `handler` - The callback function or closure that receives:
/// - `block` - The [`Block`](crate::Block) that was connected
/// - `pindex` - The [`BlockTreeEntry`](crate::BlockTreeEntry) representing the block's position in the chain
/// - `entry` - The [`BlockTreeEntry`](crate::BlockTreeEntry) representing the block's position in the chain
///
/// # Returns
/// The builder instance for method chaining.
Expand All @@ -860,9 +860,9 @@ impl ContextBuilder {
/// use bitcoinkernel::{Block, BlockTreeEntry, ContextBuilder, KernelError};
///
/// let context = ContextBuilder::new()
/// .with_block_connected_validation(|block: Block, pindex: BlockTreeEntry<'_>| {
/// .with_block_connected_validation(|block: Block, entry: BlockTreeEntry<'_>| {
/// println!("Block connected at height {}: {}",
/// pindex.height(), block.hash());
/// entry.height(), block.hash());
/// })
/// .build()?;
/// # Ok::<(), KernelError>(())
Expand All @@ -889,7 +889,7 @@ impl ContextBuilder {
/// # Arguments
/// * `handler` - The callback function or closure that receives:
/// - `block` - The [`Block`](crate::Block) that was disconnected
/// - `pindex` - The [`BlockTreeEntry`](crate::BlockTreeEntry) for the disconnected block
/// - `entry` - The [`BlockTreeEntry`](crate::BlockTreeEntry) for the disconnected block
///
/// # Returns
/// The builder instance for method chaining.
Expand All @@ -899,9 +899,9 @@ impl ContextBuilder {
/// use bitcoinkernel::{Block, BlockTreeEntry, ContextBuilder, KernelError};
///
/// let context = ContextBuilder::new()
/// .with_block_disconnected_validation(|block: Block, pindex: BlockTreeEntry<'_>| {
/// .with_block_disconnected_validation(|block: Block, entry: BlockTreeEntry<'_>| {
/// println!("Block disconnected from height {}: {} (reorg)",
/// pindex.height(), block.hash());
/// entry.height(), block.hash());
/// })
/// .build()?;
/// # Ok::<(), KernelError>(())
Expand Down Expand Up @@ -938,11 +938,11 @@ impl ContextBuilder {
/// registry.register_block_checked(|block: Block, _state: BlockValidationStateRef<'_>| {
/// println!("Checked: {}", block.hash());
/// });
/// registry.register_block_connected(|_block, pindex: BlockTreeEntry<'_>| {
/// println!("Connected at height {}", pindex.height());
/// registry.register_block_connected(|_block, entry: BlockTreeEntry<'_>| {
/// println!("Connected at height {}", entry.height());
/// });
/// registry.register_block_disconnected(|_block, pindex: BlockTreeEntry<'_>| {
/// println!("Disconnected from height {}", pindex.height());
/// registry.register_block_disconnected(|_block, entry: BlockTreeEntry<'_>| {
/// println!("Disconnected from height {}", entry.height());
/// });
/// })
/// .build()?;
Expand Down Expand Up @@ -1187,9 +1187,9 @@ mod tests {

#[test]
fn test_advanced_validation_configuration() {
fn pow_handler(_pindex: crate::BlockTreeEntry, _block: crate::Block) {}
fn connected_handler(_block: crate::Block, _pindex: crate::BlockTreeEntry) {}
fn disconnected_handler(_block: crate::Block, _pindex: crate::BlockTreeEntry) {}
fn pow_handler(_entry: crate::BlockTreeEntry, _block: crate::Block) {}
fn connected_handler(_block: crate::Block, _entry: crate::BlockTreeEntry) {}
fn disconnected_handler(_block: crate::Block, _entry: crate::BlockTreeEntry) {}

let mut builder = ContextBuilder::new();

Expand Down
6 changes: 3 additions & 3 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ mod tests {
}

fn create_context() -> Context {
fn pow_handler(_pindex: BlockTreeEntry, _block: Block) {
fn pow_handler(_entry: BlockTreeEntry, _block: Block) {
log::info!("New PoW valid block!");
}

fn connected_handler(_block: Block, _pindex: BlockTreeEntry) {
fn connected_handler(_block: Block, _entry: BlockTreeEntry) {
log::info!("Block connected!");
}

fn disconnected_handler(_block: Block, _pindex: BlockTreeEntry) {
fn disconnected_handler(_block: Block, _entry: BlockTreeEntry) {
log::info!("Block disconnected!");
}

Expand Down
Loading