wallet: store wanted_height in future_tx, instead of remaining blocks
This commit is contained in:
@@ -86,7 +86,7 @@ class AddressSynchronizer(Logger):
|
|||||||
# locks: if you need to take multiple ones, acquire them in the order they are defined here!
|
# locks: if you need to take multiple ones, acquire them in the order they are defined here!
|
||||||
self.lock = threading.RLock()
|
self.lock = threading.RLock()
|
||||||
self.transaction_lock = threading.RLock()
|
self.transaction_lock = threading.RLock()
|
||||||
self.future_tx = {} # type: Dict[str, int] # txid -> blocks remaining
|
self.future_tx = {} # type: Dict[str, int] # txid -> wanted height
|
||||||
# Transactions pending verification. txid -> tx_height. Access with self.lock.
|
# Transactions pending verification. txid -> tx_height. Access with self.lock.
|
||||||
self.unverified_tx = defaultdict(int)
|
self.unverified_tx = defaultdict(int)
|
||||||
# true when synchronized
|
# true when synchronized
|
||||||
@@ -626,12 +626,11 @@ class AddressSynchronizer(Logger):
|
|||||||
return cached_local_height
|
return cached_local_height
|
||||||
return self.network.get_local_height() if self.network else self.db.get('stored_height', 0)
|
return self.network.get_local_height() if self.network else self.db.get('stored_height', 0)
|
||||||
|
|
||||||
def add_future_tx(self, tx: Transaction, num_blocks: int) -> bool:
|
def add_future_tx(self, tx: Transaction, wanted_height: int) -> bool:
|
||||||
assert num_blocks > 0, num_blocks
|
|
||||||
with self.lock:
|
with self.lock:
|
||||||
tx_was_added = self.add_transaction(tx)
|
tx_was_added = self.add_transaction(tx)
|
||||||
if tx_was_added:
|
if tx_was_added:
|
||||||
self.future_tx[tx.txid()] = num_blocks
|
self.future_tx[tx.txid()] = wanted_height
|
||||||
return tx_was_added
|
return tx_was_added
|
||||||
|
|
||||||
def get_tx_height(self, tx_hash: str) -> TxMinedInfo:
|
def get_tx_height(self, tx_hash: str) -> TxMinedInfo:
|
||||||
@@ -646,9 +645,11 @@ class AddressSynchronizer(Logger):
|
|||||||
height = self.unverified_tx[tx_hash]
|
height = self.unverified_tx[tx_hash]
|
||||||
return TxMinedInfo(height=height, conf=0)
|
return TxMinedInfo(height=height, conf=0)
|
||||||
elif tx_hash in self.future_tx:
|
elif tx_hash in self.future_tx:
|
||||||
num_blocks_remainining = self.future_tx[tx_hash]
|
num_blocks_remainining = self.future_tx[tx_hash] - self.get_local_height()
|
||||||
assert num_blocks_remainining > 0, num_blocks_remainining
|
if num_blocks_remainining > 0:
|
||||||
return TxMinedInfo(height=TX_HEIGHT_FUTURE, conf=-num_blocks_remainining)
|
return TxMinedInfo(height=TX_HEIGHT_FUTURE, conf=-num_blocks_remainining)
|
||||||
|
else:
|
||||||
|
return TxMinedInfo(height=TX_HEIGHT_LOCAL, conf=0)
|
||||||
else:
|
else:
|
||||||
# local transaction
|
# local transaction
|
||||||
return TxMinedInfo(height=TX_HEIGHT_LOCAL, conf=0)
|
return TxMinedInfo(height=TX_HEIGHT_LOCAL, conf=0)
|
||||||
|
|||||||
@@ -411,16 +411,16 @@ class LNWalletWatcher(LNWatcher):
|
|||||||
async def try_redeem(self, prevout: str, sweep_info: 'SweepInfo', chan_id_for_log: str, name: str) -> None:
|
async def try_redeem(self, prevout: str, sweep_info: 'SweepInfo', chan_id_for_log: str, name: str) -> None:
|
||||||
prev_txid, prev_index = prevout.split(':')
|
prev_txid, prev_index = prevout.split(':')
|
||||||
broadcast = True
|
broadcast = True
|
||||||
|
local_height = self.network.get_local_height()
|
||||||
if sweep_info.cltv_expiry:
|
if sweep_info.cltv_expiry:
|
||||||
local_height = self.network.get_local_height()
|
wanted_height = sweep_info.cltv_expiry - local_height
|
||||||
remaining = sweep_info.cltv_expiry - local_height
|
if wanted_height - local_height > 0:
|
||||||
if remaining > 0:
|
|
||||||
broadcast = False
|
broadcast = False
|
||||||
reason = 'waiting for {}: CLTV ({} > {}), prevout {}'.format(name, local_height, sweep_info.cltv_expiry, prevout)
|
reason = 'waiting for {}: CLTV ({} > {}), prevout {}'.format(name, local_height, sweep_info.cltv_expiry, prevout)
|
||||||
if sweep_info.csv_delay:
|
if sweep_info.csv_delay:
|
||||||
prev_height = self.get_tx_height(prev_txid)
|
prev_height = self.get_tx_height(prev_txid)
|
||||||
remaining = sweep_info.csv_delay - prev_height.conf
|
wanted_height = sweep_info.csv_delay + prev_height.height - 1
|
||||||
if remaining > 0:
|
if wanted_height - local_height > 0:
|
||||||
broadcast = False
|
broadcast = False
|
||||||
reason = 'waiting for {}: CSV ({} >= {}), prevout: {}'.format(name, prev_height.conf, sweep_info.csv_delay, prevout)
|
reason = 'waiting for {}: CSV ({} >= {}), prevout: {}'.format(name, prev_height.conf, sweep_info.csv_delay, prevout)
|
||||||
tx = sweep_info.gen_tx()
|
tx = sweep_info.gen_tx()
|
||||||
@@ -432,13 +432,13 @@ class LNWalletWatcher(LNWatcher):
|
|||||||
if broadcast:
|
if broadcast:
|
||||||
await self.network.try_broadcasting(tx, name)
|
await self.network.try_broadcasting(tx, name)
|
||||||
else:
|
else:
|
||||||
if self.lnworker.wallet.db.get_transaction(txid):
|
if txid in self.lnworker.wallet.future_tx:
|
||||||
return
|
return
|
||||||
self.logger.debug(f'(chan {chan_id_for_log}) trying to redeem {name}: {prevout}')
|
self.logger.debug(f'(chan {chan_id_for_log}) trying to redeem {name}: {prevout}')
|
||||||
self.logger.info(reason)
|
self.logger.info(reason)
|
||||||
# it's OK to add local transaction, the fee will be recomputed
|
# it's OK to add local transaction, the fee will be recomputed
|
||||||
try:
|
try:
|
||||||
tx_was_added = self.lnworker.wallet.add_future_tx(tx, remaining)
|
tx_was_added = self.lnworker.wallet.add_future_tx(tx, wanted_height)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.info(f'could not add future tx: {name}. prevout: {prevout} {str(e)}')
|
self.logger.info(f'could not add future tx: {name}. prevout: {prevout} {str(e)}')
|
||||||
tx_was_added = False
|
tx_was_added = False
|
||||||
|
|||||||
Reference in New Issue
Block a user