commit eca8d13e37920ffef999aab01f9c60a0a8dce9f6
parent a9edd170c7f712985666f4be0e98d48583eec35b
Author: ThomasV <thomasv@electrum.org>
Date: Fri, 13 Apr 2018 17:57:52 +0200
lnbase: fix tx amounts
Diffstat:
2 files changed, 18 insertions(+), 8 deletions(-)
diff --git a/lib/lnbase.py b/lib/lnbase.py
@@ -252,8 +252,13 @@ def get_obscured_ctn(ctn, local, remote):
mask = int.from_bytes(H256(local + remote)[-6:], byteorder="big")
return ctn ^ mask
+def overall_weight(num_htlc):
+ return 500 + 172 * num_htlc + 224
-def make_commitment(local_pubkey, remote_pubkey, payment_pubkey, remote_payment_pubkey, revocation_pubkey, delayed_pubkey, funding_txid, funding_pos, funding_satoshis):
+def make_commitment(local_pubkey, remote_pubkey,
+ payment_pubkey, remote_payment_pubkey, revocation_pubkey, delayed_pubkey,
+ funding_txid, funding_pos, funding_satoshis,
+ to_local_msat, to_remote_msat, local_feerate):
pubkeys = sorted([bh2u(local_pubkey), bh2u(remote_pubkey)])
obs = get_obscured_ctn(0, payment_pubkey, remote_payment_pubkey)
locktime = (0x20 << 24) + (obs & 0xffffff)
@@ -274,9 +279,10 @@ def make_commitment(local_pubkey, remote_pubkey, payment_pubkey, remote_payment_
# commitment tx outputs
local_script = bytes([opcodes.OP_IF]) + revocation_pubkey + bytes([opcodes.OP_ELSE, opcodes.OP_CSV, opcodes.OP_DROP]) + delayed_pubkey + bytes([opcodes.OP_ENDIF, opcodes.OP_CHECKSIG])
local_address = bitcoin.redeem_script_to_address('p2wsh', bh2u(local_script))
- local_amount = funding_satoshis
+ fee = local_feerate * overall_weight(0) // 1000
+ local_amount = to_local_msat // 1000 - fee
remote_address = bitcoin.pubkey_to_address('p2wpkh', bh2u(remote_pubkey))
- remote_amount = 0
+ remote_amount = to_remote_msat // 1000
to_local = (bitcoin.TYPE_ADDRESS, local_address, local_amount)
to_remote = (bitcoin.TYPE_ADDRESS, remote_address, remote_amount)
# no htlc for the moment
@@ -459,9 +465,12 @@ class Peer(PrintError):
funding_tx = wallet.mktx([funding_output], None, config, 1000)
funding_index = funding_tx.outputs().index(funding_output)
remote_payment_pubkey = accept_channel['payment_basepoint']
- c_tx = make_commitment(funding_pubkey, remote_pubkey, payment_pubkey, remote_payment_pubkey, revocation_pubkey, delayed_pubkey, funding_tx.txid(), funding_index, funding_satoshis)
+ c_tx = make_commitment(
+ funding_pubkey, remote_pubkey,
+ payment_pubkey, remote_payment_pubkey, revocation_pubkey, delayed_pubkey,
+ funding_tx.txid(), funding_index, funding_satoshis,
+ funding_satoshis*1000, 0, 20000)
c_tx.sign({bh2u(funding_pubkey): (funding_privkey, True)})
-
sig_index = pubkeys.index(bh2u(funding_pubkey))
sig = bytes.fromhex(c_tx.inputs()[0]["signatures"][sig_index])
self.print_error('sig', len(sig))
diff --git a/lib/tests/test_lnbase.py b/lib/tests/test_lnbase.py
@@ -42,7 +42,8 @@ class Test_LNBase(unittest.TestCase):
local_funding_pubkey, remote_funding_pubkey,
local_payment_basepoint, remote_payment_basepoint,
local_revocation_pubkey, local_delayedpubkey,
- funding_tx_id, funding_output_index, funding_amount_satoshi)
+ funding_tx_id, funding_output_index, funding_amount_satoshi,
+ to_local_msat, to_remote_msat, local_feerate_per_kw)
our_commit_tx.sign({bh2u(local_funding_pubkey): (local_funding_privkey[:-1], True)})
ref_commit_tx_str = '02000000000101bef67e4e2fb9ddeeb3461973cd4c62abb35050b1add772995b820b584a488489000000000038b02b8002c0c62d0000000000160014ccf1af2f2aabee14bb40fa3851ab2301de84311054a56a00000000002200204adb4e2f00643db396dd120d4e7dc17625f5f2c11a40d857accc862d6b7dd80e0400473044022051b75c73198c6deee1a875871c3961832909acd297c6b908d59e3319e5185a46022055c419379c5051a78d00dbbce11b5b664a0c22815fbcc6fcef6b1937c383693901483045022100f51d2e566a70ba740fc5d8c0f07b9b93d2ed741c3c0860c613173de7d39e7968022041376d520e9c0e1ad52248ddf4b22e12be8763007df977253ef45a4ca3bdb7c001475221023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb21030e9f7b623d2ccc7c9bd44d66d5ce21ce504c0acf6385a132cec6d3c39fa711c152ae3e195220'
ref_commit_tx = Transaction(ref_commit_tx_str)
@@ -50,7 +51,7 @@ class Test_LNBase(unittest.TestCase):
pubkeys, _x_pubkeys = our_commit_tx.get_sorted_pubkeys(our_commit_tx.inputs()[0])
index_of_pubkey = pubkeys.index(bh2u(remote_funding_pubkey))
our_commit_tx._inputs[0]["signatures"][index_of_pubkey] = remote_signature + "01"
-
+ print("our tx", str(our_commit_tx))
print("Reference inputs", json.dumps(ref_commit_tx.inputs(), indent=2))
print("Our inputs", json.dumps(our_commit_tx.inputs(), indent=2))
@@ -64,8 +65,8 @@ class Test_LNBase(unittest.TestCase):
output2adr = ref_commit_tx.outputs()[1][1]
self.assertTrue(bitcoin.redeem_script_to_address("p2wsh", "63210212a140cd0c6539d07cd08dfe09984dec3251ea808b892efeac3ede9402bf2b1967029000b2752103fd5960528dc152014952efdb702a88f71e3c1653b2314431701ec77e57fde83c68ac") in [output1adr, output2adr])
# todo check order and other output
-
self.assertEqual(str(our_commit_tx), ref_commit_tx_str)
+
def commitment_tx_with_all_five_HTLCs_untrimmed_minimum_feerate(self):
to_local_msat = 6988000000
to_remote_msat = 3000000000