twallet: some clean-up re get_address_history vs db.get_addr_history - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit 8e89c0c971645c11e205a163b38e4b35fa3e85de DIR parent d81110014e5a1d32ff424c7a325c7283cd895732 HTML Author: SomberNight <somber.night@protonmail.com> Date: Sat, 7 Dec 2019 05:42:28 +0100 wallet: some clean-up re get_address_history vs db.get_addr_history note: tests needed changing due to behavioural change in wallet.get_receiving_address() Previously wallet.get_receiving_address used wallet.db.get_addr_history, now it (indirectly) uses wallet.get_address_history, which now also considers local txns. Diffstat: M electrum/address_synchronizer.py | 17 +++++++++++------ M electrum/tests/test_wallet_vertica… | 26 +++++++++++++------------- M electrum/wallet.py | 17 ++++++++--------- 3 files changed, 32 insertions(+), 28 deletions(-) --- DIR diff --git a/electrum/address_synchronizer.py b/electrum/address_synchronizer.py t@@ -110,7 +110,13 @@ class AddressSynchronizer(Logger): def get_addresses(self): return sorted(self.db.get_history()) - def get_address_history(self, addr): + def get_address_history(self, addr: str) -> Sequence[Tuple[str, int]]: + """Returns the history for the address, in the format that would be returned by a server. + + Note: The difference between db.get_addr_history and this method is that + db.get_addr_history stores the response from a server, so it only includes txns + a server sees, i.e. that does not contain local and future txns. + """ h = [] # we need self.transaction_lock but get_tx_height will take self.lock # so we need to take that too here, to enforce order of locks t@@ -378,7 +384,7 @@ class AddressSynchronizer(Logger): @profiler def load_local_history(self): - self._history_local = {} # address -> set(txid) + self._history_local = {} # type: Dict[str, Set[str]] # address -> set(txid) self._address_history_changed_events = defaultdict(asyncio.Event) # address -> Event for txid in itertools.chain(self.db.list_txi(), self.db.list_txo()): self._add_tx_to_local_history(txid) t@@ -841,11 +847,10 @@ class AddressSynchronizer(Logger): xx += x return cc, uu, xx - def is_used(self, address): - h = self.db.get_addr_history(address) - return len(h) != 0 + def is_used(self, address: str) -> bool: + return self.get_address_history_len(address) != 0 - def is_empty(self, address): + def is_empty(self, address: str) -> bool: c, u, x = self.get_addr_balance(address) return c+u+x == 0 DIR diff --git a/electrum/tests/test_wallet_vertical.py b/electrum/tests/test_wallet_vertical.py t@@ -603,10 +603,10 @@ class TestWalletSending(TestCaseForTestnet): tx_copy = tx_from_any(tx.serialize()) self.assertTrue(wallet2.is_mine(wallet2.get_txin_address(tx_copy.inputs()[0]))) - self.assertEqual('0100000001e228327e4c0bb80661d258d625f516307e7c127c7f3e2b476a22e89b4dae063c000000006a47304402207d352950df94d1243917abe4fa5c4f74e0ad7eb47ee02232a2939f52d87d6e57022047603b9ffb32a200d530f5c54eed5245f1034e6ed77462d23e3417b8e763f25c0121030b482838721a38d94847699fed8818b5c5f56500ef72f13489e365b65e5749cffeffffff02a0860100000000001600148a28bddb7f61864bdcf58b2ad13d5aeb3abc3c4268360200000000001976a914ca4c60999c46c2108326590b125aefd476dcb11888ac00000000', + self.assertEqual('0100000001e228327e4c0bb80661d258d625f516307e7c127c7f3e2b476a22e89b4dae063c000000006a47304402200c7b06ff882db5ffe9d6e2a3cc2cabf5cd1b4224f1453d1e3dadd13b3d391e2c02201d23fde8482b05837f27d43021d17a1be2ee619dfc889ee80d4c2761e7c7ffb20121030b482838721a38d94847699fed8818b5c5f56500ef72f13489e365b65e5749cffeffffff02a086010000000000160014284520c815980d426264766d8d930013dd20aa6068360200000000001976a914ca4c60999c46c2108326590b125aefd476dcb11888ac00000000', str(tx_copy)) - self.assertEqual('d0e40c8a8586f7faf33505269df64ebad317e8188f008649faaf48cbeb49dae8', tx_copy.txid()) - self.assertEqual('d0e40c8a8586f7faf33505269df64ebad317e8188f008649faaf48cbeb49dae8', tx_copy.wtxid()) + self.assertEqual('4ff22c31dd884dedbb905fae275508d1f7bb4948c1c979d2567132848fdff24a', tx_copy.txid()) + self.assertEqual('4ff22c31dd884dedbb905fae275508d1f7bb4948c1c979d2567132848fdff24a', tx_copy.wtxid()) self.assertEqual(tx.wtxid(), tx_copy.wtxid()) wallet1.receive_tx_callback(tx.txid(), tx, TX_HEIGHT_UNCONFIRMED) t@@ -684,10 +684,10 @@ class TestWalletSending(TestCaseForTestnet): tx_copy = tx_from_any(tx.serialize()) self.assertTrue(wallet2.is_mine(wallet2.get_txin_address(tx_copy.inputs()[0]))) - self.assertEqual('01000000011e83f7d0783543a4fbbb20636bcbc7fbdf04392a5b8aa1d2551e180819ee08b5000000008a47304402205cfbf0925018883e4d1ca4168443646669ab37eeb39943fbf10b02954e00022c02201208806b63a88b42d67eb89afec3aa6b959f793fcf77c63d85082e477d74cbe50141045f7ba332df2a7b4f5d13f246e307c9174cfa9b8b05f3b83410a3c23ef8958d610be285963d67c7bc1feb082f168fa9877c25999963ff8b56b242a852b23e25edfeffffff02a08601000000000017a91480c2353f6a7bc3c71e99e062655b19adb3dd2e4887280b0400000000001976a914ca14915184a2662b5d1505ce7142c8ca066c70e288ac00000000', + self.assertEqual('01000000011e83f7d0783543a4fbbb20636bcbc7fbdf04392a5b8aa1d2551e180819ee08b5000000008a473044022007569f938b5d7a7f529ceccc413363d84325c11d589c1897660bebfd5fd1cc4302203ef71fa42f9b31bb1e816af13b0bf725c493a0405433390c783cd9374713c5880141045f7ba332df2a7b4f5d13f246e307c9174cfa9b8b05f3b83410a3c23ef8958d610be285963d67c7bc1feb082f168fa9877c25999963ff8b56b242a852b23e25edfeffffff02a08601000000000017a914efe136b8275f49bc0f9871eebb9a48d0516229fd87280b0400000000001976a914ca14915184a2662b5d1505ce7142c8ca066c70e288ac00000000', str(tx_copy)) - self.assertEqual('a306dcfce59813b572c811cf842fe8a5de23afb8d187a0f7e10e28726e2cf798', tx_copy.txid()) - self.assertEqual('a306dcfce59813b572c811cf842fe8a5de23afb8d187a0f7e10e28726e2cf798', tx_copy.wtxid()) + self.assertEqual('30f6eec4db5e6b1dfe572dfbc7077661df9a15a2a1b7701612b906d3e1bee3d8', tx_copy.txid()) + self.assertEqual('30f6eec4db5e6b1dfe572dfbc7077661df9a15a2a1b7701612b906d3e1bee3d8', tx_copy.wtxid()) self.assertEqual(tx.wtxid(), tx_copy.wtxid()) wallet1a.receive_tx_callback(tx.txid(), tx, TX_HEIGHT_UNCONFIRMED) t@@ -779,7 +779,7 @@ class TestWalletSending(TestCaseForTestnet): tx = wallet2a.mktx(outputs=outputs, password=None, fee=5000, tx_version=1) txid = tx.txid() partial_tx = tx.serialize_as_bytes().hex() parazyd.org:70 /git/electrum/commit/8e89c0c971645c11e205a163b38e4b35fa3e85de.gph:99: line too long