adb: take lock in more places
for example, adb.get_utxos() could previously return an inconsistent result
This commit is contained in:
@@ -105,6 +105,7 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
def diagnostic_name(self):
|
def diagnostic_name(self):
|
||||||
return self.name or ""
|
return self.name or ""
|
||||||
|
|
||||||
|
@with_lock
|
||||||
def load_and_cleanup(self):
|
def load_and_cleanup(self):
|
||||||
self.load_local_history()
|
self.load_local_history()
|
||||||
self.check_history()
|
self.check_history()
|
||||||
@@ -146,6 +147,7 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
"""Return number of transactions where address is involved."""
|
"""Return number of transactions where address is involved."""
|
||||||
return len(self._history_local.get(addr, ()))
|
return len(self._history_local.get(addr, ()))
|
||||||
|
|
||||||
|
@with_lock
|
||||||
def get_txin_address(self, txin: TxInput) -> Optional[str]:
|
def get_txin_address(self, txin: TxInput) -> Optional[str]:
|
||||||
if txin.address:
|
if txin.address:
|
||||||
return txin.address
|
return txin.address
|
||||||
@@ -160,6 +162,7 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
return tx.outputs()[prevout_n].address
|
return tx.outputs()[prevout_n].address
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@with_lock
|
||||||
def get_txin_value(self, txin: TxInput, *, address: str = None) -> Optional[int]:
|
def get_txin_value(self, txin: TxInput, *, address: str = None) -> Optional[int]:
|
||||||
if txin.value_sats() is not None:
|
if txin.value_sats() is not None:
|
||||||
return txin.value_sats()
|
return txin.value_sats()
|
||||||
@@ -179,6 +182,7 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
return tx.outputs()[prevout_n].value
|
return tx.outputs()[prevout_n].value
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@with_lock
|
||||||
def load_unverified_transactions(self):
|
def load_unverified_transactions(self):
|
||||||
# review transactions that are in the history
|
# review transactions that are in the history
|
||||||
for addr in self.db.get_history():
|
for addr in self.db.get_history():
|
||||||
@@ -198,8 +202,9 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
|
|
||||||
@event_listener
|
@event_listener
|
||||||
def on_event_blockchain_updated(self, *args):
|
def on_event_blockchain_updated(self, *args):
|
||||||
self._get_balance_cache = {} # invalidate cache
|
with self.lock:
|
||||||
self.db.put('stored_height', self.get_local_height())
|
self._get_balance_cache = {} # invalidate cache
|
||||||
|
self.db.put('stored_height', self.get_local_height())
|
||||||
|
|
||||||
async def stop(self):
|
async def stop(self):
|
||||||
if self.network:
|
if self.network:
|
||||||
@@ -252,6 +257,7 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
conflicting_txns -= {tx_hash}
|
conflicting_txns -= {tx_hash}
|
||||||
return conflicting_txns
|
return conflicting_txns
|
||||||
|
|
||||||
|
@with_lock
|
||||||
def get_transaction(self, txid: str) -> Optional[Transaction]:
|
def get_transaction(self, txid: str) -> Optional[Transaction]:
|
||||||
tx = self.db.get_transaction(txid)
|
tx = self.db.get_transaction(txid)
|
||||||
if tx:
|
if tx:
|
||||||
@@ -422,6 +428,7 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
children |= self.get_depending_transactions(other_hash)
|
children |= self.get_depending_transactions(other_hash)
|
||||||
return children
|
return children
|
||||||
|
|
||||||
|
@with_lock
|
||||||
def receive_tx_callback(self, tx: Transaction, *, tx_height: Optional[int] = None) -> None:
|
def receive_tx_callback(self, tx: Transaction, *, tx_height: Optional[int] = None) -> None:
|
||||||
txid = tx.txid()
|
txid = tx.txid()
|
||||||
assert txid is not None
|
assert txid is not None
|
||||||
@@ -430,18 +437,18 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
self.add_unverified_or_unconfirmed_tx(txid, tx_height)
|
self.add_unverified_or_unconfirmed_tx(txid, tx_height)
|
||||||
self.add_transaction(tx, allow_unrelated=True)
|
self.add_transaction(tx, allow_unrelated=True)
|
||||||
|
|
||||||
|
@with_lock
|
||||||
def receive_history_callback(self, addr: str, hist, tx_fees: Dict[str, int]):
|
def receive_history_callback(self, addr: str, hist, tx_fees: Dict[str, int]):
|
||||||
with self.lock:
|
old_hist = self.get_address_history(addr)
|
||||||
old_hist = self.get_address_history(addr)
|
for tx_hash, height in old_hist.items():
|
||||||
for tx_hash, height in old_hist.items():
|
if (tx_hash, height) not in hist:
|
||||||
if (tx_hash, height) not in hist:
|
# make tx local
|
||||||
# make tx local
|
self.unverified_tx.pop(tx_hash, None)
|
||||||
self.unverified_tx.pop(tx_hash, None)
|
self.unconfirmed_tx.pop(tx_hash, None)
|
||||||
self.unconfirmed_tx.pop(tx_hash, None)
|
self.db.remove_verified_tx(tx_hash)
|
||||||
self.db.remove_verified_tx(tx_hash)
|
if self.verifier:
|
||||||
if self.verifier:
|
self.verifier.remove_spv_proof_for_tx(tx_hash)
|
||||||
self.verifier.remove_spv_proof_for_tx(tx_hash)
|
self.db.set_addr_history(addr, hist)
|
||||||
self.db.set_addr_history(addr, hist)
|
|
||||||
|
|
||||||
for tx_hash, tx_height in hist:
|
for tx_hash, tx_height in hist:
|
||||||
# add it in case it was previously unconfirmed
|
# add it in case it was previously unconfirmed
|
||||||
@@ -460,6 +467,7 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
for tx_hash, fee_sat in tx_fees.items():
|
for tx_hash, fee_sat in tx_fees.items():
|
||||||
self.db.add_tx_fee_from_server(tx_hash, fee_sat)
|
self.db.add_tx_fee_from_server(tx_hash, fee_sat)
|
||||||
|
|
||||||
|
@with_lock
|
||||||
@profiler
|
@profiler
|
||||||
def load_local_history(self):
|
def load_local_history(self):
|
||||||
self._history_local = {} # type: Dict[str, Set[str]] # address -> set(txid)
|
self._history_local = {} # type: Dict[str, Set[str]] # address -> set(txid)
|
||||||
@@ -467,6 +475,7 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
for txid in itertools.chain(self.db.list_txi(), self.db.list_txo()):
|
for txid in itertools.chain(self.db.list_txi(), self.db.list_txo()):
|
||||||
self._add_tx_to_local_history(txid)
|
self._add_tx_to_local_history(txid)
|
||||||
|
|
||||||
|
@with_lock
|
||||||
@profiler
|
@profiler
|
||||||
def check_history(self):
|
def check_history(self):
|
||||||
hist_addrs_mine = list(filter(lambda k: self.is_mine(k), self.db.get_history()))
|
hist_addrs_mine = list(filter(lambda k: self.is_mine(k), self.db.get_history()))
|
||||||
@@ -482,6 +491,7 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
if tx is not None:
|
if tx is not None:
|
||||||
self.add_transaction(tx, allow_unrelated=True)
|
self.add_transaction(tx, allow_unrelated=True)
|
||||||
|
|
||||||
|
@with_lock
|
||||||
def remove_local_transactions_we_dont_have(self):
|
def remove_local_transactions_we_dont_have(self):
|
||||||
for txid in itertools.chain(self.db.list_txi(), self.db.list_txo()):
|
for txid in itertools.chain(self.db.list_txi(), self.db.list_txo()):
|
||||||
tx_height = self.get_tx_height(txid).height
|
tx_height = self.get_tx_height(txid).height
|
||||||
@@ -749,6 +759,7 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
delta += v
|
delta += v
|
||||||
return delta
|
return delta
|
||||||
|
|
||||||
|
@with_lock
|
||||||
def get_tx_fee(self, txid: str) -> Optional[int]:
|
def get_tx_fee(self, txid: str) -> Optional[int]:
|
||||||
"""Returns tx_fee or None. Use server fee only if tx is unconfirmed and not mine.
|
"""Returns tx_fee or None. Use server fee only if tx is unconfirmed and not mine.
|
||||||
|
|
||||||
@@ -784,16 +795,15 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
return None
|
return None
|
||||||
# compute fee if possible
|
# compute fee if possible
|
||||||
v_in = v_out = 0
|
v_in = v_out = 0
|
||||||
with self.lock:
|
for txin in tx.inputs():
|
||||||
for txin in tx.inputs():
|
addr = self.get_txin_address(txin)
|
||||||
addr = self.get_txin_address(txin)
|
value = self.get_txin_value(txin, address=addr)
|
||||||
value = self.get_txin_value(txin, address=addr)
|
if value is None:
|
||||||
if value is None:
|
v_in = None
|
||||||
v_in = None
|
elif v_in is not None:
|
||||||
elif v_in is not None:
|
v_in += value
|
||||||
v_in += value
|
for txout in tx.outputs():
|
||||||
for txout in tx.outputs():
|
v_out += txout.value
|
||||||
v_out += txout.value
|
|
||||||
if v_in is not None:
|
if v_in is not None:
|
||||||
fee = v_in - v_out
|
fee = v_in - v_out
|
||||||
else:
|
else:
|
||||||
@@ -918,6 +928,7 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
self._get_balance_cache[cache_key] = result
|
self._get_balance_cache[cache_key] = result
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@with_lock
|
||||||
@with_local_height_cached
|
@with_local_height_cached
|
||||||
def get_utxos(
|
def get_utxos(
|
||||||
self,
|
self,
|
||||||
@@ -974,6 +985,7 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
coins = self.get_addr_utxo(address)
|
coins = self.get_addr_utxo(address)
|
||||||
return not bool(coins)
|
return not bool(coins)
|
||||||
|
|
||||||
|
@with_lock
|
||||||
@with_local_height_cached
|
@with_local_height_cached
|
||||||
def address_is_old(self, address: str, *, req_conf: int = 3) -> bool:
|
def address_is_old(self, address: str, *, req_conf: int = 3) -> bool:
|
||||||
"""Returns whether address has any history that is deeply confirmed.
|
"""Returns whether address has any history that is deeply confirmed.
|
||||||
@@ -993,7 +1005,8 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
max_conf = max(max_conf, tx_age)
|
max_conf = max(max_conf, tx_age)
|
||||||
return max_conf >= req_conf
|
return max_conf >= req_conf
|
||||||
|
|
||||||
def get_spender(self, outpoint: str) -> str:
|
@with_lock
|
||||||
|
def get_spender(self, outpoint: str) -> Optional[str]:
|
||||||
"""
|
"""
|
||||||
returns txid spending outpoint.
|
returns txid spending outpoint.
|
||||||
subscribes to addresses as a side effect.
|
subscribes to addresses as a side effect.
|
||||||
@@ -1005,7 +1018,7 @@ class AddressSynchronizer(Logger, EventListener):
|
|||||||
if tx_mined_status.height in [TX_HEIGHT_LOCAL, TX_HEIGHT_FUTURE]:
|
if tx_mined_status.height in [TX_HEIGHT_LOCAL, TX_HEIGHT_FUTURE]:
|
||||||
spender_txid = None
|
spender_txid = None
|
||||||
if not spender_txid:
|
if not spender_txid:
|
||||||
return
|
return None
|
||||||
spender_tx = self.get_transaction(spender_txid)
|
spender_tx = self.get_transaction(spender_txid)
|
||||||
for i, o in enumerate(spender_tx.outputs()):
|
for i, o in enumerate(spender_tx.outputs()):
|
||||||
if o.address is None:
|
if o.address is None:
|
||||||
|
|||||||
Reference in New Issue
Block a user