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
2 changes: 2 additions & 0 deletions packages/rs-sdk-ffi/src/protocol_version/queries/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Protocol version queries
pub mod refresh;
pub mod upgrade_state;
pub mod upgrade_vote_status;

// Re-export all public functions for convenient access
pub use refresh::dash_sdk_refresh_protocol_version;
pub use upgrade_state::dash_sdk_protocol_version_get_upgrade_state;
pub use upgrade_vote_status::dash_sdk_protocol_version_get_upgrade_vote_status;
76 changes: 76 additions & 0 deletions packages/rs-sdk-ffi/src/protocol_version/queries/refresh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use crate::sdk::SDKWrapper;
use crate::types::SDKHandle;
use crate::{DashSDKError, DashSDKErrorCode, DashSDKResult};
use std::ffi::CString;

/// Refresh the SDK's protocol version from the connected network.
///
/// Issues an unproved `getStatus` against the network and ratchets this SDK's
/// auto-detected protocol version up to the network's current Drive protocol
/// version (see [`dash_sdk::Sdk::refresh_protocol_version`]). The resulting
/// protocol version number propagates to every clone of this SDK — including
/// the `Sdk` clone held by a `PlatformWalletManager` — because the version is
/// stored in a shared `Arc<AtomicU32>`.
///
/// Call this on app start and after every network switch so fee-sensitive
/// flows (shielded pool shield/unshield/transfer/withdraw) reserve against the
/// network's actual protocol version instead of the SDK's seed version.
///
/// # Parameters
/// * `sdk_handle` - Handle to the SDK instance.
///
/// # Returns
/// * On success, a `DashSDKResult` carrying a heap-allocated C string with the
/// decimal protocol version number (e.g. `"12"`).
/// * On failure, a `DashSDKResult` carrying an error.
///
/// # Safety
/// - `sdk_handle` must be a valid, non-null pointer to an initialized `SDKHandle`.
/// - The function does not retain references to the input pointer beyond the duration of the call.
/// - On success, the returned `DashSDKResult` contains a heap-allocated C string; the caller must
/// free it using the SDK's string-free routine to avoid leaks.
/// - Passing a dangling or invalid pointer for `sdk_handle` results in undefined behavior.
#[no_mangle]
pub unsafe extern "C" fn dash_sdk_refresh_protocol_version(
sdk_handle: *const SDKHandle,
) -> DashSDKResult {
if sdk_handle.is_null() {
return DashSDKResult::error(DashSDKError::new(
DashSDKErrorCode::InvalidParameter,
"SDK handle is null".to_string(),
));
}

let wrapper = &*(sdk_handle as *const SDKWrapper);

let result = wrapper
.runtime
.block_on(wrapper.sdk.refresh_protocol_version());

match result {
Ok(version) => match CString::new(version.to_string()) {
Ok(c_str) => DashSDKResult::success_string(c_str.into_raw()),
Err(e) => DashSDKResult::error(DashSDKError::new(
DashSDKErrorCode::InternalError,
format!("Failed to create CString: {}", e),
)),
},
Err(e) => DashSDKResult::error(DashSDKError::new(
DashSDKErrorCode::InternalError,
format!("Failed to refresh protocol version: {}", e),
)),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_refresh_protocol_version_null_handle() {
unsafe {
let result = dash_sdk_refresh_protocol_version(std::ptr::null());
assert!(!result.error.is_null());
}
}
}
Loading
Loading