lssa/wallet-ffi/src/sync.rs
2026-01-26 10:29:37 +01:00

150 lines
4.1 KiB
Rust

//! Block synchronization functions.
use crate::block_on;
use crate::error::{print_error, WalletFfiError};
use crate::types::WalletHandle;
use crate::wallet::get_wallet;
/// Synchronize private accounts to a specific block.
///
/// This scans the blockchain from the last synced block to the specified block,
/// updating private account balances based on any relevant transactions.
///
/// # Parameters
/// - `handle`: Valid wallet handle
/// - `block_id`: Target block number to sync to
///
/// # Returns
/// - `Success` if synchronization completed
/// - `SyncError` if synchronization failed
/// - Error code on other failures
///
/// # Note
/// This operation can take a while for large block ranges. The wallet
/// internally uses a progress bar which may output to stdout.
///
/// # Safety
/// - `handle` must be a valid wallet handle from `wallet_ffi_create_new` or `wallet_ffi_open`
#[no_mangle]
pub unsafe extern "C" fn wallet_ffi_sync_to_block(
handle: *mut WalletHandle,
block_id: u64,
) -> WalletFfiError {
let wrapper = match get_wallet(handle) {
Ok(w) => w,
Err(e) => return e,
};
let mut wallet = match wrapper.core.lock() {
Ok(w) => w,
Err(e) => {
print_error(format!("Failed to lock wallet: {}", e));
return WalletFfiError::InternalError;
}
};
match block_on(wallet.sync_to_block(block_id)) {
Ok(Ok(())) => WalletFfiError::Success,
Ok(Err(e)) => {
print_error(format!("Sync failed: {}", e));
WalletFfiError::SyncError
}
Err(e) => e,
}
}
/// Get the last synced block number.
///
/// # Parameters
/// - `handle`: Valid wallet handle
/// - `out_block_id`: Output pointer for the block number
///
/// # Returns
/// - `Success` on success
/// - Error code on failure
///
/// # Safety
/// - `handle` must be a valid wallet handle from `wallet_ffi_create_new` or `wallet_ffi_open`
/// - `out_block_id` must be a valid pointer to a `u64`
#[no_mangle]
pub unsafe extern "C" fn wallet_ffi_get_last_synced_block(
handle: *mut WalletHandle,
out_block_id: *mut u64,
) -> WalletFfiError {
let wrapper = match get_wallet(handle) {
Ok(w) => w,
Err(e) => return e,
};
if out_block_id.is_null() {
print_error("Null output pointer");
return WalletFfiError::NullPointer;
}
let wallet = match wrapper.core.lock() {
Ok(w) => w,
Err(e) => {
print_error(format!("Failed to lock wallet: {}", e));
return WalletFfiError::InternalError;
}
};
unsafe {
*out_block_id = wallet.last_synced_block;
}
WalletFfiError::Success
}
/// Get the current block height from the sequencer.
///
/// # Parameters
/// - `handle`: Valid wallet handle
/// - `out_block_height`: Output pointer for the current block height
///
/// # Returns
/// - `Success` on success
/// - `NetworkError` if the sequencer is unreachable
/// - Error code on other failures
///
/// # Safety
/// - `handle` must be a valid wallet handle from `wallet_ffi_create_new` or `wallet_ffi_open`
/// - `out_block_height` must be a valid pointer to a `u64`
#[no_mangle]
pub unsafe extern "C" fn wallet_ffi_get_current_block_height(
handle: *mut WalletHandle,
out_block_height: *mut u64,
) -> WalletFfiError {
let wrapper = match get_wallet(handle) {
Ok(w) => w,
Err(e) => return e,
};
if out_block_height.is_null() {
print_error("Null output pointer");
return WalletFfiError::NullPointer;
}
let wallet = match wrapper.core.lock() {
Ok(w) => w,
Err(e) => {
print_error(format!("Failed to lock wallet: {}", e));
return WalletFfiError::InternalError;
}
};
match block_on(wallet.sequencer_client.get_last_block()) {
Ok(Ok(response)) => {
unsafe {
*out_block_height = response.last_block;
}
WalletFfiError::Success
}
Ok(Err(e)) => {
print_error(format!("Failed to get block height: {:?}", e));
WalletFfiError::NetworkError
}
Err(e) => e,
}
}