diff options
Diffstat (limited to 'impls/monero.rs')
| -rw-r--r-- | impls/monero.rs/src/lib.rs | 43 | ||||
| -rw-r--r-- | impls/monero.rs/tests/integration_tests.rs | 34 |
2 files changed, 75 insertions, 2 deletions
diff --git a/impls/monero.rs/src/lib.rs b/impls/monero.rs/src/lib.rs index 4fb9373..6937bed 100644 --- a/impls/monero.rs/src/lib.rs +++ b/impls/monero.rs/src/lib.rs @@ -62,6 +62,8 @@ pub struct WalletManager { ptr: NonNull<c_void>, } +pub type BlockHeight = u64; + impl WalletManager { /// Creates a new `WalletManager` using the statically linked `MONERO_WalletManagerFactory_getWalletManager`. /// @@ -266,6 +268,29 @@ impl WalletManager { } } } + + /// Retrieves the current blockchain height. + /// + /// This method communicates with the connected daemon to obtain the latest + /// blockchain height. It returns a `BlockHeight` on success or a `WalletError` on failure. + /// + /// # Example + /// + /// ```rust + /// use monero_c_rust::{WalletManager, NetworkType}; + /// + /// let manager = WalletManager::new().unwrap(); + /// let height = manager.get_height().unwrap(); + /// println!("Current blockchain height: {}", height); + /// ``` + pub fn get_height(&self) -> WalletResult<BlockHeight> { + unsafe { + let height = bindings::MONERO_WalletManager_blockchainHeight(self.ptr.as_ptr()); + // Assuming the FFI call does not set an error, directly return the height. + // If error handling is required, additional checks should be implemented here. + Ok(height) + } + } } impl Wallet { @@ -455,9 +480,9 @@ impl Wallet { /// let wallet_str = wallet_path.to_str().unwrap(); /// /// let manager = WalletManager::new().unwrap(); - /// let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet).unwrap(); + /// let _wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet).unwrap(); /// - /// let balance = wallet.get_balance(0); + /// let balance = _wallet.get_balance(0); /// assert!(balance.is_ok(), "Failed to get balance: {:?}", balance.err()); /// /// // Clean up wallet files. @@ -969,3 +994,17 @@ fn test_close_wallet() { teardown(&temp_dir).expect("Failed to clean up after test"); } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_get_height_success() { + let manager = WalletManager::new().unwrap(); + let height = manager.get_height().unwrap(); + // assert!(height > 0, "Blockchain height should be greater than 0"); + // The test should not assume network connectivity/any syncing progress, so: + assert!(height == 0, "Blockchain height should be equal to 0"); + } +} diff --git a/impls/monero.rs/tests/integration_tests.rs b/impls/monero.rs/tests/integration_tests.rs index 0310d4d..da7a6c6 100644 --- a/impls/monero.rs/tests/integration_tests.rs +++ b/impls/monero.rs/tests/integration_tests.rs @@ -474,3 +474,37 @@ fn test_close_wallet_integration() { // Clean up. teardown(&temp_dir).expect("Failed to clean up after test"); } + +#[test] +fn test_get_height_integration() { + println!("Running test_get_height_integration"); + let (manager, temp_dir) = setup().expect("Failed to set up test environment"); + + // Construct the full path for the wallet within temp_dir. + let wallet_path = temp_dir.path().join("test_wallet_height"); + let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string"); + + // Create the wallet. + let wallet = manager + .create_wallet(wallet_str, "password", "English", NetworkType::Mainnet) + .expect("Failed to create wallet"); + + // Fetch the blockchain height. + println!("Fetching blockchain height..."); + let start = Instant::now(); + let height_result = manager.get_height(); + let duration = start.elapsed(); + println!("Blockchain height retrieval took {:?}", duration); + + assert!( + height_result.is_ok(), + "Failed to fetch blockchain height: {:?}", + height_result.err() + ); + + let height = height_result.unwrap(); + println!("Current blockchain height: {}", height); + assert!(height == 0, "Blockchain height should be equal to 0."); + + teardown(&temp_dir).expect("Failed to clean up after test"); +} |
