|
| 1 | +use crate::{set_last_error, FFIArray, FFIErrorCode}; |
| 2 | +use dash_spv::chain::checkpoints::{mainnet_checkpoints, testnet_checkpoints, CheckpointManager}; |
| 3 | +use dashcore::Network; |
| 4 | +use dashcore::hashes::Hash; |
| 5 | +use key_wallet_ffi::FFINetwork; |
| 6 | + |
| 7 | +/// FFI representation of a checkpoint (height + block hash) |
| 8 | +#[repr(C)] |
| 9 | +pub struct FFICheckpoint { |
| 10 | + pub height: u32, |
| 11 | + pub block_hash: [u8; 32], |
| 12 | +} |
| 13 | + |
| 14 | +fn manager_for_network(network: FFINetwork) -> Result<CheckpointManager, String> { |
| 15 | + let net: Network = network.into(); |
| 16 | + match net { |
| 17 | + Network::Dash => Ok(CheckpointManager::new(mainnet_checkpoints())), |
| 18 | + Network::Testnet => Ok(CheckpointManager::new(testnet_checkpoints())), |
| 19 | + _ => Err("Checkpoints are only available for Dash and Testnet".to_string()), |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +/// Get the latest checkpoint for the given network. |
| 24 | +/// |
| 25 | +/// # Safety |
| 26 | +/// - `out_height` must be a valid pointer to a `u32`. |
| 27 | +/// - `out_hash` must point to at least 32 writable bytes. |
| 28 | +#[no_mangle] |
| 29 | +pub extern "C" fn dash_spv_ffi_checkpoint_latest( |
| 30 | + network: FFINetwork, |
| 31 | + out_height: *mut u32, |
| 32 | + out_hash: *mut u8, // expects at least 32 bytes |
| 33 | +) -> i32 { |
| 34 | + if out_height.is_null() || out_hash.is_null() { |
| 35 | + set_last_error("Null output pointer provided"); |
| 36 | + return FFIErrorCode::NullPointer as i32; |
| 37 | + } |
| 38 | + let mgr = match manager_for_network(network) { |
| 39 | + Ok(m) => m, |
| 40 | + Err(e) => { |
| 41 | + set_last_error(&e); |
| 42 | + return FFIErrorCode::InvalidArgument as i32; |
| 43 | + } |
| 44 | + }; |
| 45 | + if let Some(cp) = mgr.last_checkpoint() { |
| 46 | + unsafe { |
| 47 | + *out_height = cp.height; |
| 48 | + let hash = cp.block_hash.to_byte_array(); |
| 49 | + std::ptr::copy_nonoverlapping(hash.as_ptr(), out_hash, 32); |
| 50 | + } |
| 51 | + FFIErrorCode::Success as i32 |
| 52 | + } else { |
| 53 | + set_last_error("No checkpoints available for network"); |
| 54 | + FFIErrorCode::NotImplemented as i32 |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// Get the last checkpoint at or before a given height. |
| 59 | +/// |
| 60 | +/// # Safety |
| 61 | +/// - `out_height` must be a valid pointer to a `u32`. |
| 62 | +/// - `out_hash` must point to at least 32 writable bytes. |
| 63 | +#[no_mangle] |
| 64 | +pub extern "C" fn dash_spv_ffi_checkpoint_before_height( |
| 65 | + network: FFINetwork, |
| 66 | + height: u32, |
| 67 | + out_height: *mut u32, |
| 68 | + out_hash: *mut u8, // expects at least 32 bytes |
| 69 | +) -> i32 { |
| 70 | + if out_height.is_null() || out_hash.is_null() { |
| 71 | + set_last_error("Null output pointer provided"); |
| 72 | + return FFIErrorCode::NullPointer as i32; |
| 73 | + } |
| 74 | + let mgr = match manager_for_network(network) { |
| 75 | + Ok(m) => m, |
| 76 | + Err(e) => { |
| 77 | + set_last_error(&e); |
| 78 | + return FFIErrorCode::InvalidArgument as i32; |
| 79 | + } |
| 80 | + }; |
| 81 | + if let Some(cp) = mgr.last_checkpoint_before_height(height) { |
| 82 | + unsafe { |
| 83 | + *out_height = cp.height; |
| 84 | + let hash = cp.block_hash.to_byte_array(); |
| 85 | + std::ptr::copy_nonoverlapping(hash.as_ptr(), out_hash, 32); |
| 86 | + } |
| 87 | + FFIErrorCode::Success as i32 |
| 88 | + } else { |
| 89 | + set_last_error("No checkpoint at or before given height"); |
| 90 | + FFIErrorCode::ValidationError as i32 |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/// Get the last checkpoint at or before a given UNIX timestamp (seconds). |
| 95 | +/// |
| 96 | +/// # Safety |
| 97 | +/// - `out_height` must be a valid pointer to a `u32`. |
| 98 | +/// - `out_hash` must point to at least 32 writable bytes. |
| 99 | +#[no_mangle] |
| 100 | +pub extern "C" fn dash_spv_ffi_checkpoint_before_timestamp( |
| 101 | + network: FFINetwork, |
| 102 | + timestamp: u32, |
| 103 | + out_height: *mut u32, |
| 104 | + out_hash: *mut u8, // expects at least 32 bytes |
| 105 | +) -> i32 { |
| 106 | + if out_height.is_null() || out_hash.is_null() { |
| 107 | + set_last_error("Null output pointer provided"); |
| 108 | + return FFIErrorCode::NullPointer as i32; |
| 109 | + } |
| 110 | + let mgr = match manager_for_network(network) { |
| 111 | + Ok(m) => m, |
| 112 | + Err(e) => { |
| 113 | + set_last_error(&e); |
| 114 | + return FFIErrorCode::InvalidArgument as i32; |
| 115 | + } |
| 116 | + }; |
| 117 | + if let Some(cp) = mgr.last_checkpoint_before_timestamp(timestamp) { |
| 118 | + unsafe { |
| 119 | + *out_height = cp.height; |
| 120 | + let hash = cp.block_hash.to_byte_array(); |
| 121 | + std::ptr::copy_nonoverlapping(hash.as_ptr(), out_hash, 32); |
| 122 | + } |
| 123 | + FFIErrorCode::Success as i32 |
| 124 | + } else { |
| 125 | + set_last_error("No checkpoint at or before given timestamp"); |
| 126 | + FFIErrorCode::ValidationError as i32 |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/// Get all checkpoints between two heights (inclusive). |
| 131 | +/// |
| 132 | +/// Returns an `FFIArray` of `FFICheckpoint` items. The caller owns the memory and |
| 133 | +/// must free the array buffer using `dash_spv_ffi_array_destroy` when done. |
| 134 | +#[no_mangle] |
| 135 | +pub extern "C" fn dash_spv_ffi_checkpoints_between_heights( |
| 136 | + network: FFINetwork, |
| 137 | + start_height: u32, |
| 138 | + end_height: u32, |
| 139 | +) -> FFIArray { |
| 140 | + match manager_for_network(network) { |
| 141 | + Ok(mgr) => { |
| 142 | + // Collect checkpoints within inclusive range |
| 143 | + let mut out: Vec<FFICheckpoint> = Vec::new(); |
| 144 | + for &h in mgr.checkpoint_heights() { |
| 145 | + if h >= start_height && h <= end_height { |
| 146 | + if let Some(cp) = mgr.get_checkpoint(h) { |
| 147 | + out.push(FFICheckpoint { |
| 148 | + height: cp.height, |
| 149 | + block_hash: cp.block_hash.to_byte_array(), |
| 150 | + }); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + FFIArray::new(out) |
| 155 | + } |
| 156 | + Err(e) => { |
| 157 | + set_last_error(&e); |
| 158 | + // Return empty array on error |
| 159 | + FFIArray { |
| 160 | + data: std::ptr::null_mut(), |
| 161 | + len: 0, |
| 162 | + capacity: 0, |
| 163 | + elem_size: std::mem::size_of::<FFICheckpoint>(), |
| 164 | + elem_align: std::mem::align_of::<FFICheckpoint>(), |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments