Browse Source

lnpeer: code factorization

hard-fail-on-bad-server-string
ThomasV 5 years ago
parent
commit
7472eba78c
  1. 76
      electrum/lnpeer.py

76
electrum/lnpeer.py

@ -580,18 +580,9 @@ class Peer(Logger):
funding_index = funding_tx.outputs().index(funding_output) funding_index = funding_tx.outputs().index(funding_output)
# remote commitment transaction # remote commitment transaction
channel_id, funding_txid_bytes = channel_id_from_funding_tx(funding_txid, funding_index) channel_id, funding_txid_bytes = channel_id_from_funding_tx(funding_txid, funding_index)
chan_dict = { outpoint = Outpoint(funding_txid, funding_index)
"node_id": self.pubkey, constraints = ChannelConstraints(capacity=funding_sat, is_initiator=True, funding_txn_minimum_depth=funding_txn_minimum_depth)
"channel_id": channel_id, chan_dict = self.create_channel_storage(channel_id, outpoint, local_config, remote_config, constraints)
"short_channel_id": None,
"funding_outpoint": Outpoint(funding_txid, funding_index),
"remote_config": remote_config,
"local_config": local_config,
"constraints": ChannelConstraints(capacity=funding_sat, is_initiator=True, funding_txn_minimum_depth=funding_txn_minimum_depth),
"remote_update": None,
"state": channel_states.PREOPENING.name,
"revocation_store": {},
}
chan = Channel(chan_dict, chan = Channel(chan_dict,
sweep_address=self.lnworker.sweep_address, sweep_address=self.lnworker.sweep_address,
lnworker=self.lnworker, lnworker=self.lnworker,
@ -609,6 +600,21 @@ class Peer(Logger):
chan.open_with_first_pcp(remote_per_commitment_point, remote_sig) chan.open_with_first_pcp(remote_per_commitment_point, remote_sig)
return chan, funding_tx return chan, funding_tx
def create_channel_storage(self, channel_id, outpoint, local_config, remote_config, constraints):
chan_dict = {
"node_id": self.pubkey.hex(),
"channel_id": channel_id.hex(),
"short_channel_id": None,
"funding_outpoint": outpoint,
"remote_config": remote_config,
"local_config": local_config,
"constraints": constraints,
"remote_update": None,
"state": channel_states.PREOPENING.name,
"revocation_store": {},
}
return chan_dict
async def on_open_channel(self, payload): async def on_open_channel(self, payload):
# payload['channel_flags'] # payload['channel_flags']
if payload['chain_hash'] != constants.net.rev_genesis_bytes(): if payload['chain_hash'] != constants.net.rev_genesis_bytes():
@ -647,33 +653,25 @@ class Peer(Logger):
remote_balance_sat = funding_sat * 1000 - push_msat remote_balance_sat = funding_sat * 1000 - push_msat
remote_dust_limit_sat = int.from_bytes(payload['dust_limit_satoshis'], byteorder='big') # TODO validate remote_dust_limit_sat = int.from_bytes(payload['dust_limit_satoshis'], byteorder='big') # TODO validate
remote_reserve_sat = self.validate_remote_reserve(payload['channel_reserve_satoshis'], remote_dust_limit_sat, funding_sat) remote_reserve_sat = self.validate_remote_reserve(payload['channel_reserve_satoshis'], remote_dust_limit_sat, funding_sat)
chan_dict = { remote_config = RemoteConfig(
"node_id": self.pubkey, payment_basepoint=OnlyPubkeyKeypair(payload['payment_basepoint']),
"channel_id": channel_id, multisig_key=OnlyPubkeyKeypair(payload['funding_pubkey']),
"short_channel_id": None, htlc_basepoint=OnlyPubkeyKeypair(payload['htlc_basepoint']),
"funding_outpoint": Outpoint(funding_txid, funding_idx), delayed_basepoint=OnlyPubkeyKeypair(payload['delayed_payment_basepoint']),
"remote_config": RemoteConfig( revocation_basepoint=OnlyPubkeyKeypair(payload['revocation_basepoint']),
payment_basepoint=OnlyPubkeyKeypair(payload['payment_basepoint']), to_self_delay=int.from_bytes(payload['to_self_delay'], 'big'),
multisig_key=OnlyPubkeyKeypair(payload['funding_pubkey']), dust_limit_sat=remote_dust_limit_sat,
htlc_basepoint=OnlyPubkeyKeypair(payload['htlc_basepoint']), max_htlc_value_in_flight_msat=int.from_bytes(payload['max_htlc_value_in_flight_msat'], 'big'), # TODO validate
delayed_basepoint=OnlyPubkeyKeypair(payload['delayed_payment_basepoint']), max_accepted_htlcs=int.from_bytes(payload['max_accepted_htlcs'], 'big'), # TODO validate
revocation_basepoint=OnlyPubkeyKeypair(payload['revocation_basepoint']), initial_msat=remote_balance_sat,
to_self_delay=int.from_bytes(payload['to_self_delay'], 'big'), reserve_sat = remote_reserve_sat,
dust_limit_sat=remote_dust_limit_sat, htlc_minimum_msat=int.from_bytes(payload['htlc_minimum_msat'], 'big'), # TODO validate
max_htlc_value_in_flight_msat=int.from_bytes(payload['max_htlc_value_in_flight_msat'], 'big'), # TODO validate next_per_commitment_point=payload['first_per_commitment_point'],
max_accepted_htlcs=int.from_bytes(payload['max_accepted_htlcs'], 'big'), # TODO validate current_per_commitment_point=None,
initial_msat=remote_balance_sat, )
reserve_sat = remote_reserve_sat, constraints = ChannelConstraints(capacity=funding_sat, is_initiator=False, funding_txn_minimum_depth=min_depth)
htlc_minimum_msat=int.from_bytes(payload['htlc_minimum_msat'], 'big'), # TODO validate outpoint = Outpoint(funding_txid, funding_idx)
next_per_commitment_point=payload['first_per_commitment_point'], chan_dict = self.create_channel_storage(channel_id, outpoint, local_config, remote_config, constraints)
current_per_commitment_point=None,
),
"local_config": local_config,
"constraints": ChannelConstraints(capacity=funding_sat, is_initiator=False, funding_txn_minimum_depth=min_depth),
"remote_update": None,
"state": channel_states.PREOPENING.name,
"revocation_store": {},
}
chan = Channel(chan_dict, chan = Channel(chan_dict,
sweep_address=self.lnworker.sweep_address, sweep_address=self.lnworker.sweep_address,
lnworker=self.lnworker, lnworker=self.lnworker,

Loading…
Cancel
Save