calc short_channel_id after funding locked

This commit is contained in:
SomberNight 2018-05-15 16:28:32 +02:00 committed by Janus
parent a9cfa65f64
commit 1fc0b1378d
2 changed files with 23 additions and 6 deletions

View File

@ -503,16 +503,19 @@ class Abstract_Wallet(PrintError):
return TX_HEIGHT_LOCAL, 0, None
def get_txpos(self, tx_hash):
"return position, even if the tx is unverified"
"""Return (block_height, tx_pos_in_block).
If tx is unverified, tx_pos_in_block is -1.
"""
with self.lock:
if tx_hash in self.verified_tx:
height, timestamp, pos = self.verified_tx[tx_hash]
return height, pos
elif tx_hash in self.unverified_tx:
height = self.unverified_tx[tx_hash]
return (height, 0) if height > 0 else ((1e9 - height), 0)
return (height, -1) if height > 0 else ((1e9 - height), -1)
else:
return (1e9+1, 0)
return (1e9+1, -1)
def is_found(self):
return self.history.values() != [[]] * len(self.history)

View File

@ -546,6 +546,14 @@ def make_commitment(ctn, local_funding_pubkey, remote_funding_pubkey, remote_pay
return tx
def calc_short_channel_id(block_height: int, tx_pos_in_block: int, output_index: int) -> bytes:
bh = block_height.to_bytes(3, byteorder='big')
tpos = tx_pos_in_block.to_bytes(3, byteorder='big')
oi = output_index.to_bytes(2, byteorder='big')
return bh + tpos + oi
def sign_and_get_sig_string(tx, local_config, remote_config):
pubkeys = sorted([bh2u(local_config.multisig_key.pubkey), bh2u(remote_config.multisig_key.pubkey)])
tx.sign({bh2u(local_config.multisig_key.pubkey): (local_config.multisig_key.privkey, True)})
@ -979,17 +987,23 @@ class Peer(PrintError):
if conf >= chan.constraints.funding_txn_minimum_depth:
async def set_local_funding_locked_result():
try:
self.local_funding_locked[channel_id].set_result(1)
self.local_funding_locked[channel_id].set_result(short_channel_id)
except (asyncio.InvalidStateError, KeyError) as e:
# FIXME race condition if updates come in quickly, set_result might be called multiple times
# or self.local_funding_locked[channel_id] might be deleted already
self.print_error('local_funding_locked.set_result error for channel {}: {}'.format(channel_id, e))
block_height, tx_pos = wallet.get_txpos(chan.funding_outpoint.txid)
if tx_pos == -1:
self.print_error('funding tx is not yet SPV verified.. but there are '
'already enough confirmations (currently {})'.format(conf))
return
short_channel_id = calc_short_channel_id(block_height, tx_pos, chan.funding_outpoint.output_index)
asyncio.run_coroutine_threadsafe(set_local_funding_locked_result(), asyncio.get_event_loop())
self.network.unregister_callback(on_network_update)
self.network.register_callback(on_network_update, ['updated']) # thread safe
self.network.register_callback(on_network_update, ['updated', 'verified']) # thread safe
try:
await self.local_funding_locked[channel_id]
short_channel_id = await self.local_funding_locked[channel_id]
finally:
del self.local_funding_locked[channel_id]