mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-31 17:31:36 +00:00
lnchannel: ctx output-ordering: identical htlcs are ordered by CLTV
This commit is contained in:
parent
b1f606eaed
commit
44761972cb
4 changed files with 15 additions and 2 deletions
|
@ -340,6 +340,7 @@ class Channel(Logger):
|
||||||
htlc_sigs = htlc_sigs[:] # copy cause we will delete now
|
htlc_sigs = htlc_sigs[:] # copy cause we will delete now
|
||||||
for htlcs, we_receive in [(self.included_htlcs(LOCAL, SENT, ctn=next_local_ctn), False),
|
for htlcs, we_receive in [(self.included_htlcs(LOCAL, SENT, ctn=next_local_ctn), False),
|
||||||
(self.included_htlcs(LOCAL, RECEIVED, ctn=next_local_ctn), True)]:
|
(self.included_htlcs(LOCAL, RECEIVED, ctn=next_local_ctn), True)]:
|
||||||
|
# FIXME this is quadratic. BOLT-02: "corresponding to the ordering of the commitment transaction"
|
||||||
for htlc in htlcs:
|
for htlc in htlcs:
|
||||||
idx = self.verify_htlc(htlc, htlc_sigs, we_receive, pending_local_commitment)
|
idx = self.verify_htlc(htlc, htlc_sigs, we_receive, pending_local_commitment)
|
||||||
del htlc_sigs[idx]
|
del htlc_sigs[idx]
|
||||||
|
|
|
@ -93,7 +93,11 @@ class FeeUpdate(NamedTuple):
|
||||||
|
|
||||||
ChannelConstraints = namedtuple("ChannelConstraints", ["capacity", "is_initiator", "funding_txn_minimum_depth"])
|
ChannelConstraints = namedtuple("ChannelConstraints", ["capacity", "is_initiator", "funding_txn_minimum_depth"])
|
||||||
|
|
||||||
ScriptHtlc = namedtuple('ScriptHtlc', ['redeem_script', 'htlc'])
|
|
||||||
|
class ScriptHtlc(NamedTuple):
|
||||||
|
redeem_script: bytes
|
||||||
|
htlc: 'UpdateAddHtlc'
|
||||||
|
|
||||||
|
|
||||||
class Outpoint(NamedTuple("Outpoint", [('txid', str), ('output_index', int)])):
|
class Outpoint(NamedTuple("Outpoint", [('txid', str), ('output_index', int)])):
|
||||||
def to_str(self):
|
def to_str(self):
|
||||||
|
@ -475,6 +479,13 @@ def make_commitment(ctn, local_funding_pubkey, remote_funding_pubkey,
|
||||||
remote_address = make_commitment_output_to_remote_address(remote_payment_pubkey)
|
remote_address = make_commitment_output_to_remote_address(remote_payment_pubkey)
|
||||||
# TODO trim htlc outputs here while also considering 2nd stage htlc transactions
|
# TODO trim htlc outputs here while also considering 2nd stage htlc transactions
|
||||||
|
|
||||||
|
# BOLT-03: "Transaction Input and Output Ordering
|
||||||
|
# Lexicographic ordering: see BIP69. In the case of identical HTLC outputs,
|
||||||
|
# the outputs are ordered in increasing cltv_expiry order."
|
||||||
|
# so we sort by cltv_expiry now; and the later BIP69-sort is assumed to be *stable*
|
||||||
|
htlcs = list(htlcs)
|
||||||
|
htlcs.sort(key=lambda x: x.htlc.cltv_expiry)
|
||||||
|
|
||||||
htlc_outputs, c_outputs_filtered = make_commitment_outputs(fees_per_participant, local_amount, remote_amount,
|
htlc_outputs, c_outputs_filtered = make_commitment_outputs(fees_per_participant, local_amount, remote_amount,
|
||||||
(bitcoin.TYPE_ADDRESS, local_address), (bitcoin.TYPE_ADDRESS, remote_address), htlcs, dust_limit_sat)
|
(bitcoin.TYPE_ADDRESS, local_address), (bitcoin.TYPE_ADDRESS, remote_address), htlcs, dust_limit_sat)
|
||||||
|
|
||||||
|
|
|
@ -495,7 +495,7 @@ class TestLNUtil(unittest.TestCase):
|
||||||
(1, 2000 * 1000),
|
(1, 2000 * 1000),
|
||||||
(3, 3000 * 1000),
|
(3, 3000 * 1000),
|
||||||
(4, 4000 * 1000)]:
|
(4, 4000 * 1000)]:
|
||||||
htlc_obj[num] = UpdateAddHtlc(amount_msat=msat, payment_hash=bitcoin.sha256(htlc_payment_preimage[num]), cltv_expiry=None, htlc_id=None, timestamp=0)
|
htlc_obj[num] = UpdateAddHtlc(amount_msat=msat, payment_hash=bitcoin.sha256(htlc_payment_preimage[num]), cltv_expiry=0, htlc_id=None, timestamp=0)
|
||||||
htlcs = [ScriptHtlc(htlc[x], htlc_obj[x]) for x in range(5)]
|
htlcs = [ScriptHtlc(htlc[x], htlc_obj[x]) for x in range(5)]
|
||||||
|
|
||||||
our_commit_tx = make_commitment(
|
our_commit_tx = make_commitment(
|
||||||
|
|
|
@ -958,6 +958,7 @@ class Transaction:
|
||||||
txin['sequence'] = nSequence
|
txin['sequence'] = nSequence
|
||||||
|
|
||||||
def BIP69_sort(self, inputs=True, outputs=True):
|
def BIP69_sort(self, inputs=True, outputs=True):
|
||||||
|
# NOTE: other parts of the code rely on these sorts being *stable* sorts
|
||||||
if inputs:
|
if inputs:
|
||||||
self._inputs.sort(key = lambda i: (i['prevout_hash'], i['prevout_n']))
|
self._inputs.sort(key = lambda i: (i['prevout_hash'], i['prevout_n']))
|
||||||
if outputs:
|
if outputs:
|
||||||
|
|
Loading…
Add table
Reference in a new issue