Skip to content

Commit 1dc3471

Browse files
clippy fixes
1 parent 77a3d84 commit 1dc3471

File tree

2 files changed

+74
-80
lines changed

2 files changed

+74
-80
lines changed

dash-spv-ffi/include/dash_spv_ffi.h

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ typedef enum FFIValidationMode {
3030
*/
3131
typedef struct FFIDashSpvClient FFIDashSpvClient;
3232

33+
/**
34+
* FFI-safe array that transfers ownership of memory to the C caller.
35+
*
36+
* # Safety
37+
*
38+
* This struct represents memory that has been allocated by Rust but ownership
39+
* has been transferred to the C caller. The caller is responsible for:
40+
* - Not accessing the memory after it has been freed
41+
* - Calling `dash_spv_ffi_array_destroy` to properly deallocate the memory
42+
* - Ensuring the data, len, and capacity fields remain consistent
43+
*/
44+
typedef struct FFIArray {
45+
void *data;
46+
uintptr_t len;
47+
uintptr_t capacity;
48+
uintptr_t elem_size;
49+
uintptr_t elem_align;
50+
} FFIArray;
51+
3352
typedef ClientConfig FFIClientConfig;
3453

3554
typedef struct FFIString {
@@ -129,25 +148,6 @@ typedef struct FFIEventCallbacks {
129148
void *user_data;
130149
} FFIEventCallbacks;
131150

132-
/**
133-
* FFI-safe array that transfers ownership of memory to the C caller.
134-
*
135-
* # Safety
136-
*
137-
* This struct represents memory that has been allocated by Rust but ownership
138-
* has been transferred to the C caller. The caller is responsible for:
139-
* - Not accessing the memory after it has been freed
140-
* - Calling `dash_spv_ffi_array_destroy` to properly deallocate the memory
141-
* - Ensuring the data, len, and capacity fields remain consistent
142-
*/
143-
typedef struct FFIArray {
144-
void *data;
145-
uintptr_t len;
146-
uintptr_t capacity;
147-
uintptr_t elem_size;
148-
uintptr_t elem_align;
149-
} FFIArray;
150-
151151
/**
152152
* Handle for Core SDK that can be passed to Platform SDK
153153
*/
@@ -196,6 +196,49 @@ typedef struct FFIUnconfirmedTransaction {
196196
uintptr_t addresses_len;
197197
} FFIUnconfirmedTransaction;
198198

199+
/**
200+
* Get the latest checkpoint for the given network.
201+
*
202+
* # Safety
203+
* - `out_height` must be a valid pointer to a `u32`.
204+
* - `out_hash` must point to at least 32 writable bytes.
205+
*/
206+
int32_t dash_spv_ffi_checkpoint_latest(FFINetwork network, uint32_t *out_height, uint8_t *out_hash);
207+
208+
/**
209+
* Get the last checkpoint at or before a given height.
210+
*
211+
* # Safety
212+
* - `out_height` must be a valid pointer to a `u32`.
213+
* - `out_hash` must point to at least 32 writable bytes.
214+
*/
215+
int32_t dash_spv_ffi_checkpoint_before_height(FFINetwork network,
216+
uint32_t height,
217+
uint32_t *out_height,
218+
uint8_t *out_hash);
219+
220+
/**
221+
* Get the last checkpoint at or before a given UNIX timestamp (seconds).
222+
*
223+
* # Safety
224+
* - `out_height` must be a valid pointer to a `u32`.
225+
* - `out_hash` must point to at least 32 writable bytes.
226+
*/
227+
int32_t dash_spv_ffi_checkpoint_before_timestamp(FFINetwork network,
228+
uint32_t timestamp,
229+
uint32_t *out_height,
230+
uint8_t *out_hash);
231+
232+
/**
233+
* Get all checkpoints between two heights (inclusive).
234+
*
235+
* Returns an `FFIArray` of `FFICheckpoint` items. The caller owns the memory and
236+
* must free the array buffer using `dash_spv_ffi_array_destroy` when done.
237+
*/
238+
struct FFIArray dash_spv_ffi_checkpoints_between_heights(FFINetwork network,
239+
uint32_t start_height,
240+
uint32_t end_height);
241+
199242
struct FFIDashSpvClient *dash_spv_ffi_client_new(const FFIClientConfig *config);
200243

201244
/**
@@ -562,49 +605,6 @@ int32_t dash_spv_ffi_config_set_start_from_height(FFIClientConfig *config,
562605
int32_t dash_spv_ffi_config_set_wallet_creation_time(FFIClientConfig *config,
563606
uint32_t timestamp);
564607

565-
/**
566-
* Get the latest checkpoint for the given network.
567-
*
568-
* # Safety
569-
* - `out_height` must be a valid pointer to a `u32`.
570-
* - `out_hash` must point to at least 32 writable bytes.
571-
*/
572-
int32_t dash_spv_ffi_checkpoint_latest(FFINetwork network, uint32_t *out_height, uint8_t *out_hash);
573-
574-
/**
575-
* Get the last checkpoint at or before a given height.
576-
*
577-
* # Safety
578-
* - `out_height` must be a valid pointer to a `u32`.
579-
* - `out_hash` must point to at least 32 writable bytes.
580-
*/
581-
int32_t dash_spv_ffi_checkpoint_before_height(FFINetwork network,
582-
uint32_t height,
583-
uint32_t *out_height,
584-
uint8_t *out_hash);
585-
586-
/**
587-
* Get the last checkpoint at or before a given UNIX timestamp (seconds).
588-
*
589-
* # Safety
590-
* - `out_height` must be a valid pointer to a `u32`.
591-
* - `out_hash` must point to at least 32 writable bytes.
592-
*/
593-
int32_t dash_spv_ffi_checkpoint_before_timestamp(FFINetwork network,
594-
uint32_t timestamp,
595-
uint32_t *out_height,
596-
uint8_t *out_hash);
597-
598-
/**
599-
* Get all checkpoints between two heights (inclusive).
600-
*
601-
* Returns an `FFIArray` of `FFICheckpoint` items. The caller owns the memory and
602-
* must free the array buffer using `dash_spv_ffi_array_destroy` when done.
603-
*/
604-
struct FFIArray dash_spv_ffi_checkpoints_between_heights(FFINetwork network,
605-
uint32_t start_height,
606-
uint32_t end_height);
607-
608608
const char *dash_spv_ffi_get_last_error(void);
609609

610610
void dash_spv_ffi_clear_error(void);

dash-spv-ffi/src/checkpoints.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn manager_for_network(network: FFINetwork) -> Result<CheckpointManager, String>
2626
/// - `out_height` must be a valid pointer to a `u32`.
2727
/// - `out_hash` must point to at least 32 writable bytes.
2828
#[no_mangle]
29-
pub extern "C" fn dash_spv_ffi_checkpoint_latest(
29+
pub unsafe extern "C" fn dash_spv_ffi_checkpoint_latest(
3030
network: FFINetwork,
3131
out_height: *mut u32,
3232
out_hash: *mut u8, // expects at least 32 bytes
@@ -43,11 +43,9 @@ pub extern "C" fn dash_spv_ffi_checkpoint_latest(
4343
}
4444
};
4545
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-
}
46+
*out_height = cp.height;
47+
let hash = cp.block_hash.to_byte_array();
48+
std::ptr::copy_nonoverlapping(hash.as_ptr(), out_hash, 32);
5149
FFIErrorCode::Success as i32
5250
} else {
5351
set_last_error("No checkpoints available for network");
@@ -61,7 +59,7 @@ pub extern "C" fn dash_spv_ffi_checkpoint_latest(
6159
/// - `out_height` must be a valid pointer to a `u32`.
6260
/// - `out_hash` must point to at least 32 writable bytes.
6361
#[no_mangle]
64-
pub extern "C" fn dash_spv_ffi_checkpoint_before_height(
62+
pub unsafe extern "C" fn dash_spv_ffi_checkpoint_before_height(
6563
network: FFINetwork,
6664
height: u32,
6765
out_height: *mut u32,
@@ -79,11 +77,9 @@ pub extern "C" fn dash_spv_ffi_checkpoint_before_height(
7977
}
8078
};
8179
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-
}
80+
*out_height = cp.height;
81+
let hash = cp.block_hash.to_byte_array();
82+
std::ptr::copy_nonoverlapping(hash.as_ptr(), out_hash, 32);
8783
FFIErrorCode::Success as i32
8884
} else {
8985
set_last_error("No checkpoint at or before given height");
@@ -97,7 +93,7 @@ pub extern "C" fn dash_spv_ffi_checkpoint_before_height(
9793
/// - `out_height` must be a valid pointer to a `u32`.
9894
/// - `out_hash` must point to at least 32 writable bytes.
9995
#[no_mangle]
100-
pub extern "C" fn dash_spv_ffi_checkpoint_before_timestamp(
96+
pub unsafe extern "C" fn dash_spv_ffi_checkpoint_before_timestamp(
10197
network: FFINetwork,
10298
timestamp: u32,
10399
out_height: *mut u32,
@@ -115,11 +111,9 @@ pub extern "C" fn dash_spv_ffi_checkpoint_before_timestamp(
115111
}
116112
};
117113
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-
}
114+
*out_height = cp.height;
115+
let hash = cp.block_hash.to_byte_array();
116+
std::ptr::copy_nonoverlapping(hash.as_ptr(), out_hash, 32);
123117
FFIErrorCode::Success as i32
124118
} else {
125119
set_last_error("No checkpoint at or before given timestamp");

0 commit comments

Comments
 (0)