Coverage for tests/utils/wallet_equality_utils.py: 90%

277 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 19:49 +0000

1import logging 

2 

3from abc import ABC 

4from typing import Optional 

5 

6from monero import ( 

7 MoneroWallet, MoneroTxQuery, MoneroTransferQuery, 

8 MoneroOutputQuery, MoneroAccount, MoneroSubaddress, 

9 MoneroTxWallet, MoneroTransfer, MoneroOutputWallet, 

10 MoneroTx, MoneroOutgoingTransfer, MoneroIncomingTransfer, 

11 MoneroWalletFull 

12) 

13 

14from .gen_utils import GenUtils 

15from .assert_utils import AssertUtils 

16from .tx_wallet_utils import TxWalletUtils 

17from .test_utils import TestUtils as Utils 

18 

19logger: logging.Logger = logging.getLogger("WalletEqualityUtils") 

20 

21 

22class WalletEqualityUtils(ABC): 

23 """Test utilities to deep compare wallets.""" 

24 

25 @classmethod 

26 def test_wallet_equality_on_chain(cls, w1: MoneroWallet, w2: MoneroWallet) -> None: 

27 """Compares two wallets for equality using only on-chain data. 

28 

29 :param MoneroWallet w1: A wallet to compare. 

30 :param MoneroWallet w2: A wallet to compare. 

31 """ 

32 # wait for relayed txs associated with wallets to clear pool 

33 assert w1.is_connected_to_daemon() == w2.is_connected_to_daemon() 

34 if w1.is_connected_to_daemon(): 

35 # sync the wallets until same height 

36 while w1.get_height() != w2.get_height(): 

37 w1.sync() 

38 w2.sync() 

39 # wait for txs to clear the pool 

40 Utils.WALLET_TX_TRACKER.wait_for_txs_to_clear_pool([w1, w2]) 

41 

42 # test that wallets are equal using only on-chain data 

43 assert w1.get_height() == w2.get_height() 

44 assert w1.get_seed() == w2.get_seed() 

45 assert w1.get_primary_address() == w2.get_primary_address() 

46 assert w1.get_private_view_key() == w2.get_private_view_key() 

47 assert w1.get_private_spend_key() == w2.get_private_spend_key() 

48 

49 tx_query: MoneroTxQuery = MoneroTxQuery() 

50 tx_query.is_confirmed = True 

51 cls.test_tx_wallets_equal_on_chain(w1.get_txs(tx_query), w2.get_txs(tx_query)) 

52 tx_query.include_outputs = True 

53 cls.test_tx_wallets_equal_on_chain(w1.get_txs(tx_query), w2.get_txs(tx_query)) # fetch and compare outputs 

54 cls.test_accounts_equal_on_chain(w1.get_accounts(True), w2.get_accounts(True)) 

55 assert w1.get_balance() == w2.get_balance() 

56 assert w1.get_unlocked_balance() == w2.get_unlocked_balance() 

57 transfer_query: MoneroTransferQuery = MoneroTransferQuery() 

58 transfer_query.tx_query = MoneroTxQuery() 

59 transfer_query.tx_query.is_confirmed = True 

60 cls.test_transfers_equal_on_chain(w1.get_transfers(transfer_query), w2.get_transfers(transfer_query)) 

61 output_query: MoneroOutputQuery = MoneroOutputQuery() 

62 output_query.set_tx_query(MoneroTxQuery(), True) 

63 assert output_query.tx_query is not None 

64 output_query.tx_query.is_confirmed = True 

65 cls.test_output_wallets_equal_on_chain(w1.get_outputs(output_query), w2.get_outputs(output_query)) 

66 

67 # possible configuration: on chain xor local wallet data ("strict"), txs ordered same way? TBD 

68 @classmethod 

69 def test_wallet_full_equality_on_chain(cls, wallet1: MoneroWalletFull, wallet2: MoneroWalletFull) -> None: 

70 """Compares two full wallets for equality using only on-chain data. 

71 

72 :param MoneroWalletFull wallet1: A full wallet to compare. 

73 :param MoneroWalletFull wallet2: A full wallet to compare. 

74 """ 

75 WalletEqualityUtils.test_wallet_equality_on_chain(wallet1, wallet2) 

76 assert wallet1.get_network_type() == wallet2.get_network_type() 

77 wallet1_restore_height: int = wallet1.get_restore_height() 

78 wallet2_restore_height: int = wallet2.get_restore_height() 

79 assert wallet1_restore_height == wallet2_restore_height, f"{wallet1_restore_height} != {wallet2_restore_height}" 

80 AssertUtils.assert_equals(wallet1.get_daemon_connection(), wallet2.get_daemon_connection()) 

81 assert wallet1.get_seed_language() == wallet2.get_seed_language() 

82 # TODO more pybind specific extensions 

83 

84 @classmethod 

85 def test_account(cls, accounts: list[MoneroAccount], j: int, size: int) -> None: 

86 """Test that accounts past a common index are unused (zero balance, no used subaddresses). 

87 

88 :param list[MoneroAccount] accounts: accounts to test. 

89 :param int j: index to start testing from. 

90 :param int size: number of accounts, i.e. the index to test up to (exclusive). 

91 """ 

92 while j < size: 

93 assert 0 == accounts[j].balance 

94 assert len(accounts[j].subaddresses) >= 1 

95 for subaddress in accounts[j].subaddresses: 

96 assert subaddress.is_used is False 

97 j += 1 

98 

99 @classmethod 

100 def test_accounts_equal_on_chain(cls, accounts1: list[MoneroAccount], accounts2: list[MoneroAccount]) -> None: 

101 """Test account lists equality based on on-chain data. 

102 

103 :param list[MoneroAccount] accounts1: first account list to compare on-chain data. 

104 :param list[MoneroAccount] accounts2: second account list to compare on-chain data. 

105 """ 

106 accounts1_size: int = len(accounts1) 

107 accounts2_size: int = len(accounts2) 

108 size: int = accounts1_size if accounts1_size > accounts2_size else accounts2_size 

109 

110 for i in range(size): 

111 if i < accounts1_size and i < accounts2_size: 

112 cls.test_account_equal_on_chain(accounts1[i], accounts2[i]) 

113 elif i >= accounts1_size: 

114 cls.test_account(accounts2, i, accounts2_size) 

115 return 

116 else: 

117 cls.test_account(accounts1, i, accounts1_size) 

118 return 

119 

120 @classmethod 

121 def test_account_equal_on_chain(cls, account1: MoneroAccount, account2: MoneroAccount) -> None: 

122 """Test account equality based on on-chain data. 

123 

124 :param MoneroAccount account1: first account to compare on-chain data. 

125 :param MoneroAccount account2: second account to compare on-chain data. 

126 """ 

127 # nullify off-chain data for comparison 

128 subaddresses1: list[MoneroSubaddress] = account1.subaddresses 

129 subaddresses2: list[MoneroSubaddress] = account2.subaddresses 

130 account1.subaddresses.clear() 

131 account2.subaddresses.clear() 

132 account1.tag = None 

133 account2.tag = None 

134 

135 # test account equality 

136 AssertUtils.assert_equals(account1, account2, "Accounts are not equal") 

137 cls.test_subaddresses_equal_on_chain(subaddresses1, subaddresses2) 

138 

139 @classmethod 

140 def test_subaddresses_equal_on_chain( 

141 cls, 

142 subaddresses1: list[MoneroSubaddress], 

143 subaddresses2: list[MoneroSubaddress] 

144 ) -> None: 

145 """Test subaddresses equality based on on-chain data. 

146 

147 :param list[MoneroSubaddress] subaddresses1: first subaddress list to compare on-chain data. 

148 :param list[MoneroSubaddress] subaddresses2: second subaddress list to compare on-chain data. 

149 """ 

150 subaddresses1_len: int = len(subaddresses1) 

151 subaddresses2_len: int = len(subaddresses2) 

152 size: int = subaddresses1_len if subaddresses1_len > subaddresses2_len else subaddresses2_len 

153 

154 for i in range(size): 

155 if i < subaddresses1_len and i < subaddresses2_len: 

156 cls.test_subaddress_equal_on_chain(subaddresses1[i], subaddresses2[i]) 

157 elif i >= subaddresses1_len: 

158 j: int = i 

159 while j < subaddresses2_len: 

160 assert 0 == subaddresses2[j].balance 

161 assert False is subaddresses2[j].is_used 

162 j += 1 

163 

164 return 

165 else: 

166 j: int = i 

167 while j < subaddresses1_len: 

168 assert 0 == subaddresses1[i].balance 

169 assert False is subaddresses1[j].is_used 

170 

171 return 

172 

173 @classmethod 

174 def test_subaddress_equal_on_chain(cls, subaddress1: MoneroSubaddress, subaddress2: MoneroSubaddress) -> None: 

175 """Test subaddress equality based on on-chain data. 

176 

177 :param MoneroSubaddress subaddress1: first subaddress to test. 

178 :param MoneroSubaddress subaddress2: second subaddress to test. 

179 """ 

180 subaddress1.label = None # nullify off-chain data for comparison 

181 subaddress2.label = None 

182 AssertUtils.assert_equals(subaddress1, subaddress2) 

183 

184 @classmethod 

185 def test_txs_wallet_equality(cls, txs1: list[MoneroTxWallet], txs2: list[MoneroTxWallet]) -> None: 

186 """Test that every tx in `txs1` matches its counterpart (by hash) in `txs2`. 

187 

188 :param list[MoneroTxWallet] txs1: first tx list to compare. 

189 :param list[MoneroTxWallet] txs2: second tx list to compare. 

190 """ 

191 for tx1 in txs1: 

192 found: bool = False 

193 for tx2 in txs2: 

194 assert tx1.hash is not None 

195 assert tx2.hash is not None 

196 if tx1.hash != tx2.hash: 

197 continue 

198 # transfer cached info if known for comparison 

199 if tx1.outgoing_transfer is not None and len(tx1.outgoing_transfer.destinations) > 0: 

200 if tx2.outgoing_transfer is None or len(tx2.outgoing_transfer.destinations) == 0: 

201 cls.transfer_cached_info(tx1, tx2) 

202 elif tx2.outgoing_transfer is not None and len(tx2.outgoing_transfer.destinations) > 0: 

203 cls.transfer_cached_info(tx2, tx1) 

204 

205 # test tx equality 

206 tx1_str: str = tx1.serialize() 

207 tx2_str: str = tx2.serialize() 

208 assert TxWalletUtils.txs_mergeable(tx1, tx2), f"Txs are not mergeable: tx1: {tx1_str}, tx2: {tx2_str}" 

209 AssertUtils.assert_equals(tx1, tx2) 

210 found = True 

211 

212 # test block equality except txs to ignore order 

213 assert tx1.block is not None 

214 assert tx2.block is not None 

215 block_txs1: list[MoneroTx] = tx1.block.txs 

216 block_txs2: list[MoneroTx] = tx2.block.txs 

217 tx1.block.txs = [] 

218 tx2.block.txs = [] 

219 AssertUtils.assert_equals(block_txs1, block_txs2) 

220 tx1.block.txs = block_txs1 

221 tx2.block.txs = block_txs2 

222 

223 # each tx must have one and only one match 

224 assert found, "Tx not found" 

225 

226 @classmethod 

227 def test_tx_wallets_equal_on_chain(cls, txs_1: list[MoneroTxWallet], txs_2: list[MoneroTxWallet]) -> None: 

228 """Test wallet txs equality based on on-chain data. 

229 

230 :param list[MoneroTxWallet] txs_1: first wallet tx list to compare on-chain data. 

231 :param list[MoneroTxWallet] txs_2: second wallet tx list to compare on-chain data. 

232 """ 

233 # remove pool or failed txs for comparison 

234 txs1: list[MoneroTxWallet] = list(filter(lambda tx: not tx.in_tx_pool and not tx.is_failed, txs_1)) 

235 txs2: list[MoneroTxWallet] = list(filter(lambda tx: not tx.in_tx_pool and not tx.is_failed, txs_2)) 

236 

237 # nullify off-chain data for comparison 

238 all_txs: list[MoneroTxWallet] = txs1.copy() 

239 all_txs.extend(txs2) 

240 for tx in all_txs: 

241 tx.note = None 

242 if tx.outgoing_transfer is not None: 

243 tx.outgoing_transfer.addresses = [] 

244 

245 # compare txs 

246 assert len(txs1) == len(txs2) 

247 cls.test_txs_wallet_equality(txs1, txs2) 

248 

249 @classmethod 

250 def transfer_cached_info(cls, src: MoneroTxWallet, tgt: MoneroTxWallet) -> None: 

251 """Transfer cached wallet transaction info. 

252 

253 :param MoneroTxWallet src: wallet tx with cached info. 

254 :param MoneroTxWallet tgt: wallet tx to copy cached info to. 

255 """ 

256 # fill in missing incoming transfers when sending from/to the same account 

257 if len(src.incoming_transfers) > 0: 

258 for in_transfer in src.incoming_transfers: 

259 assert src.outgoing_transfer is not None 

260 if in_transfer.account_index == src.outgoing_transfer.account_index: 

261 tgt.incoming_transfers.append(in_transfer) 

262 

263 tgt.incoming_transfers.sort() 

264 

265 # transfer info to outgoing transfer 

266 if tgt.outgoing_transfer is None: 

267 tgt.outgoing_transfer = src.outgoing_transfer 

268 else: 

269 assert src.outgoing_transfer is not None 

270 tgt.outgoing_transfer.destinations = src.outgoing_transfer.destinations.copy() 

271 tgt.outgoing_transfer.amount = src.outgoing_transfer.amount 

272 

273 # transfer payment id if outgoing 

274 # TODO monero-wallet-rpc does not provide payment id for outgoing transfer when cache missing 

275 # https://github.com/monero-project/monero/issues/8378 

276 if tgt.outgoing_transfer is not None: 

277 tgt.payment_id = src.payment_id 

278 

279 @classmethod 

280 def compare_transfers(cls, txs_transfers_1: dict[str, list[MoneroTransfer]], txs_transfers_2: dict[str, list[MoneroTransfer]]) -> None: 

281 """Compare transfers collected per tx hash for equality. 

282 

283 :param dict[str, list[MoneroTransfer]] txs_transfers_1: first collection of transfers, keyed by tx hash. 

284 :param dict[str, list[MoneroTransfer]] txs_transfers_2: second collection of transfers, keyed by tx hash. 

285 """ 

286 # compare collected transfers per tx for equality 

287 for tx_hash in txs_transfers_1: 

288 tx_transfers1: list[MoneroTransfer] = txs_transfers_1[tx_hash] 

289 tx_transfers2: list[MoneroTransfer] = txs_transfers_2[tx_hash] 

290 assert len(tx_transfers1) == len(tx_transfers2) 

291 

292 # normalize and compare transfers 

293 for i, transfer1 in enumerate(tx_transfers1): 

294 transfer2: MoneroTransfer = tx_transfers2[i] 

295 

296 # normalize outgoing transfers 

297 if isinstance(transfer1, MoneroOutgoingTransfer): 

298 assert isinstance(transfer2, MoneroOutgoingTransfer) 

299 

300 # transfer destination info if known for comparison 

301 assert transfer1.tx is not None 

302 assert transfer2.tx is not None 

303 if len(transfer1.destinations) > 0: 

304 if len(transfer2.destinations) == 0: 

305 cls.transfer_cached_info(transfer1.tx, transfer2.tx) 

306 elif len(transfer2.destinations) > 0: 

307 cls.transfer_cached_info(transfer2.tx, transfer1.tx) 

308 

309 # nullify other local wallet data 

310 transfer1.addresses = [] 

311 transfer2.addresses = [] 

312 else: 

313 # normalize incoming transfers 

314 assert isinstance(transfer1, MoneroIncomingTransfer) 

315 assert isinstance(transfer2, MoneroIncomingTransfer) 

316 transfer1.address = None 

317 transfer2.address = None 

318 

319 # compare transfer equality 

320 AssertUtils.assert_equals(transfer1, transfer2) 

321 

322 @classmethod 

323 def test_transfers_equal_on_chain(cls, transfers1: list[MoneroTransfer], transfers2: list[MoneroTransfer]) -> None: 

324 """Test transfers equality based on on-chain data. 

325 

326 :param list[MoneroTransfer] transfers1: first transfer list to compare on-chain data. 

327 :param list[MoneroTransfer] transfers2: second transfer list to compare on-chain data. 

328 """ 

329 

330 assert len(transfers1) == len(transfers2) 

331 

332 # test and collect transfers per transaction 

333 txs_transfers_1: dict[str, list[MoneroTransfer]] = {} 

334 txs_transfers_2: dict[str, list[MoneroTransfer]] = {} 

335 last_height: Optional[int] = None 

336 last_tx1: Optional[MoneroTxWallet] = None 

337 last_tx2: Optional[MoneroTxWallet] = None 

338 

339 for i, transfer1 in enumerate(transfers1): 

340 transfer2: MoneroTransfer = transfers2[i] 

341 tx1: MoneroTxWallet | None = transfer1.tx 

342 tx2: MoneroTxWallet | None = transfer2.tx 

343 assert tx1 is not None 

344 assert tx2 is not None 

345 

346 # transfers must have same height even if they don't belong to same tx 

347 # (because tx ordering within blocks is not currently provided by wallet2) 

348 assert tx1.get_height() == tx2.get_height() 

349 

350 # transfers must be in ascending order by height 

351 if last_height is None: 

352 last_height = tx1.get_height() 

353 else: 

354 transfer_height: int | None = tx1.get_height() 

355 assert transfer_height is not None 

356 assert last_height <= transfer_height 

357 

358 assert tx1.hash is not None 

359 assert tx2.hash is not None 

360 

361 # transfers must be consecutive per transaction 

362 if last_tx1 != tx1: 

363 assert not GenUtils.has_key(tx1.hash, txs_transfers_1) 

364 # cannot be seen before 

365 last_tx1 = tx1 

366 

367 if last_tx2 != tx2: 

368 assert not GenUtils.has_key(tx2.hash, txs_transfers_2) 

369 # cannot be seen before 

370 last_tx2 = tx2 

371 

372 # collect tx1 transfer 

373 tx_transfers1: list[MoneroTransfer] | None = txs_transfers_1.get(tx1.hash) 

374 if tx_transfers1 is None: 

375 tx_transfers1 = [] 

376 txs_transfers_1[tx1.hash] = tx_transfers1 

377 

378 tx_transfers1.append(transfer1) 

379 

380 # collect tx2 transfer 

381 tx_transfers2: list[MoneroTransfer] | None = txs_transfers_2.get(tx2.hash) 

382 if tx_transfers2 is None: 

383 tx_transfers2 = [] 

384 txs_transfers_2[tx2.hash] = tx_transfers2 

385 

386 tx_transfers2.append(transfer2) 

387 

388 cls.compare_transfers(txs_transfers_1, txs_transfers_2) 

389 

390 @classmethod 

391 def compare_outputs(cls, txs_outputs1: dict[str, list[MoneroOutputWallet]], txs_outputs2: dict[str, list[MoneroOutputWallet]]) -> None: 

392 """Compare outputs collected per tx hash for equality. 

393 

394 :param dict[str, list[MoneroOutputWallet]] txs_outputs1: first collection of outputs, keyed by tx hash. 

395 :param dict[str, list[MoneroOutputWallet]] txs_outputs2: second collection of outputs, keyed by tx hash. 

396 """ 

397 # compare collected outputs per tx for equality 

398 for tx_hash in txs_outputs2: 

399 tx_outputs1: list[MoneroOutputWallet] = txs_outputs1[tx_hash] 

400 tx_outputs2: list[MoneroOutputWallet] = txs_outputs2[tx_hash] 

401 assert len(tx_outputs1) == len(tx_outputs2) 

402 

403 # normalize and compare outputs 

404 for i, output1 in enumerate(tx_outputs1): 

405 output2: MoneroOutputWallet = tx_outputs2[i] 

406 assert output1.tx is not None 

407 assert output2.tx is not None 

408 assert output1.tx.hash == output2.tx.hash 

409 AssertUtils.assert_equals(output1, output2) 

410 

411 @classmethod 

412 def test_output_wallets_equal_on_chain(cls, outputs1: list[MoneroOutputWallet], outputs2: list[MoneroOutputWallet]) -> None: 

413 """Test wallet outputs equality based on on-chain data. 

414 

415 :param list[MoneroOutputWallet] outputs1: first output list to compare on-chain data. 

416 :param list[MoneroOutputWallet] outputs2: second output list to compare on-chain data. 

417 """ 

418 assert len(outputs1) == len(outputs2) 

419 # test and collect outputs per transaction 

420 txs_outputs1: dict[str, list[MoneroOutputWallet]] = {} 

421 txs_outputs2: dict[str, list[MoneroOutputWallet]] = {} 

422 last_height: Optional[int] = None 

423 last_tx1: Optional[MoneroTxWallet] = None 

424 last_tx2: Optional[MoneroTxWallet] = None 

425 

426 for i, output1 in enumerate(outputs1): 

427 output2: MoneroOutputWallet = outputs2[i] 

428 tx1: MoneroTx | None = output1.tx 

429 tx2: MoneroTx | None = output2.tx 

430 assert tx1 is not None 

431 assert tx2 is not None 

432 

433 # outputs must have same height even if they don't belong to same tx 

434 # (because tx ordering within blocks is not currently provided by wallet2) 

435 assert tx1.get_height() == tx2.get_height() 

436 

437 # outputs must be in ascending order by height 

438 if last_height is None: 

439 last_height = tx1.get_height() 

440 else: 

441 output_height: int | None = tx1.get_height() 

442 assert output_height is not None 

443 assert last_height <= output_height 

444 

445 assert tx1.hash is not None 

446 assert tx2.hash is not None 

447 

448 # outputs must be consecutive per transaction 

449 if last_tx1 != tx1: 

450 # cannot be seen before 

451 assert not GenUtils.has_key(tx1.hash, txs_outputs1) 

452 assert isinstance(tx1, MoneroTxWallet) 

453 last_tx1 = tx1 

454 

455 if last_tx2 != tx2: 

456 # cannot be seen before 

457 assert not GenUtils.has_key(tx2.hash, txs_outputs2) 

458 assert isinstance(tx2, MoneroTxWallet) 

459 last_tx2 = tx2 

460 

461 # collect tx1 output 

462 tx_outputs1: Optional[list[MoneroOutputWallet]] = txs_outputs1.get(tx1.hash) 

463 if tx_outputs1 is None: 

464 tx_outputs1 = [] 

465 txs_outputs1[tx1.hash] = tx_outputs1 

466 

467 tx_outputs1.append(output1) 

468 

469 # collect tx2 output 

470 tx_outputs2: Optional[list[MoneroOutputWallet]] = txs_outputs2.get(tx2.hash) 

471 if tx_outputs2 is None: 

472 tx_outputs2 = [] 

473 txs_outputs2[tx2.hash] = tx_outputs2 

474 

475 tx_outputs2.append(output2) 

476 

477 cls.compare_outputs(txs_outputs1, txs_outputs2)