|
5 | 5 |
|
6 | 6 | use key_wallet::wallet::initialization::WalletAccountCreationOptions; |
7 | 7 | use key_wallet::wallet::managed_wallet_info::transaction_building::AccountTypePreference; |
| 8 | +use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; |
8 | 9 | use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; |
9 | 10 | use key_wallet::{mnemonic::Language, Mnemonic, Network}; |
10 | 11 | use key_wallet_manager::wallet_manager::{WalletError, WalletManager}; |
@@ -155,8 +156,75 @@ fn test_balance_calculation() { |
155 | 156 | fn test_block_height_tracking() { |
156 | 157 | let mut manager = WalletManager::<ManagedWalletInfo>::new(Network::Testnet); |
157 | 158 |
|
| 159 | + // Initial state |
158 | 160 | assert_eq!(manager.current_height(), 0); |
159 | 161 |
|
| 162 | + // Set height before adding wallets |
| 163 | + manager.update_height(1000); |
| 164 | + assert_eq!(manager.current_height(), 1000); |
| 165 | + |
| 166 | + let mnemonic1 = Mnemonic::generate(12, Language::English).unwrap(); |
| 167 | + let wallet_id1 = manager |
| 168 | + .create_wallet_from_mnemonic( |
| 169 | + &mnemonic1.to_string(), |
| 170 | + "", |
| 171 | + 0, |
| 172 | + WalletAccountCreationOptions::Default, |
| 173 | + ) |
| 174 | + .unwrap(); |
| 175 | + |
| 176 | + let mnemonic2 = Mnemonic::generate(12, Language::English).unwrap(); |
| 177 | + let wallet_id2 = manager |
| 178 | + .create_wallet_from_mnemonic( |
| 179 | + &mnemonic2.to_string(), |
| 180 | + "", |
| 181 | + 0, |
| 182 | + WalletAccountCreationOptions::Default, |
| 183 | + ) |
| 184 | + .unwrap(); |
| 185 | + |
| 186 | + assert_eq!(manager.wallet_count(), 2); |
| 187 | + |
| 188 | + // Verify both wallets have synced_height of 0 initially |
| 189 | + for wallet_info in manager.get_all_wallet_infos().values() { |
| 190 | + assert_eq!(wallet_info.synced_height(), 0); |
| 191 | + } |
| 192 | + |
| 193 | + // Update height - should propagate to all wallets |
160 | 194 | manager.update_height(12345); |
161 | 195 | assert_eq!(manager.current_height(), 12345); |
| 196 | + |
| 197 | + // Verify all wallets got updated |
| 198 | + let wallet_info1 = manager.get_wallet_info(&wallet_id1).unwrap(); |
| 199 | + let wallet_info2 = manager.get_wallet_info(&wallet_id2).unwrap(); |
| 200 | + assert_eq!(wallet_info1.synced_height(), 12345); |
| 201 | + assert_eq!(wallet_info2.synced_height(), 12345); |
| 202 | + |
| 203 | + // Update again - verify subsequent updates work |
| 204 | + manager.update_height(20000); |
| 205 | + assert_eq!(manager.current_height(), 20000); |
| 206 | + |
| 207 | + for wallet_info in manager.get_all_wallet_infos().values() { |
| 208 | + assert_eq!(wallet_info.synced_height(), 20000); |
| 209 | + } |
| 210 | + |
| 211 | + // Update wallets individually to different heights |
| 212 | + let wallet_info1 = manager.get_wallet_info_mut(&wallet_id1).unwrap(); |
| 213 | + wallet_info1.update_synced_height(30000); |
| 214 | + |
| 215 | + let wallet_info2 = manager.get_wallet_info_mut(&wallet_id2).unwrap(); |
| 216 | + wallet_info2.update_synced_height(25000); |
| 217 | + |
| 218 | + // Verify each wallet has its own synced_height |
| 219 | + let wallet_info1 = manager.get_wallet_info(&wallet_id1).unwrap(); |
| 220 | + let wallet_info2 = manager.get_wallet_info(&wallet_id2).unwrap(); |
| 221 | + assert_eq!(wallet_info1.synced_height(), 30000); |
| 222 | + assert_eq!(wallet_info2.synced_height(), 25000); |
| 223 | + |
| 224 | + // Manager update_height still syncs all wallets |
| 225 | + manager.update_height(40000); |
| 226 | + let wallet_info1 = manager.get_wallet_info(&wallet_id1).unwrap(); |
| 227 | + let wallet_info2 = manager.get_wallet_info(&wallet_id2).unwrap(); |
| 228 | + assert_eq!(wallet_info1.synced_height(), 40000); |
| 229 | + assert_eq!(wallet_info2.synced_height(), 40000); |
162 | 230 | } |
0 commit comments