From 392008a7d3c2d1a4297f93910facee5adae77792 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 7 Mar 2017 10:28:30 +1030 Subject: [PATCH] BOLT 3,5: update weight calculations for revocation key hash in script. Signed-off-by: Rusty Russell --- 03-transactions.md | 92 +++++++++++++++++++++++----------------------- 05-onchain.md | 35 ++++++++---------- 2 files changed, 63 insertions(+), 64 deletions(-) diff --git a/03-transactions.md b/03-transactions.md index 4600f1b..190250a 100644 --- a/03-transactions.md +++ b/03-transactions.md @@ -258,8 +258,8 @@ Thus we use a simplified formula for *expected weight*, which assumes: This gives us the following *expected weights* (details of the computation in [Appendix A](#appendix-a-expected-weights)): Commitment weight: 724 + 172 * num-untrimmed-htlc-outputs - HTLC-timeout weight: 669 - HTLC-success weight: 718 + HTLC-timeout weight: 663 + HTLC-success weight: 703 Note that we refer to the "base fee" for a commitment transaction in the requirements below, which is what the funder pays. The actual fee may be higher than the amount calculated here, due to rounding and trimmed outputs. @@ -267,11 +267,11 @@ Note that we refer to the "base fee" for a commitment transaction in the require The fee for an HTLC-timeout transaction MUST BE calculated to match: -1. Multiply `feerate-per-kw` by 669 and divide by 1000 (rounding down). +1. Multiply `feerate-per-kw` by 663 and divide by 1000 (rounding down). The fee for an HTLC-success transaction MUST BE calculated to match: -1. Multiply `feerate-per-kw` by 718 and divide by 1000 (rounding down). +1. Multiply `feerate-per-kw` by 703 and divide by 1000 (rounding down). The base fee for a commitment transaction MUST BE calculated to match: @@ -288,8 +288,8 @@ For example, suppose that we have a `feerate-per-kw` of 5000, a `dust-limit-sato * 2 offered HTLCs of 5000000 and 1000000 millisatoshis (5000 and 1000 satoshis) * 2 received HTLCs of 7000000 and 800000 millisatoshis (7000 and 800 satoshis) -The HTLC timeout transaction weight is 669, thus fee would be 3345 satoshis. -The HTLC success transaction weight is 718, thus fee would be 3590 satoshis +The HTLC timeout transaction weight is 663, thus fee would be 3315 satoshis. +The HTLC success transaction weight is 703, thus fee would be 3515 satoshis The commitment transaction weight would be calculated as follows: @@ -607,48 +607,51 @@ Multiplying non-witness data by 4, this gives a weight of: The *expected weight* of an HTLC transaction is calculated as follows: - accepted_htlc_script: 156 bytes - - OP_DATA: 1 byte (remotekey length) - - remotekey: 33 bytes - - OP_SWAP: 1 byte - - OP_SIZE: 1 byte - - OP_DATA: 1 byte (32 length) - - 32: 1 byte - - OP_EQUAL: 1 byte - - OP_IF: 1 byte - - OP_HASH160: 1 byte - - OP_DATA: 1 byte (ripemd-of-payment-hash length) - - ripemd-of-payment-hash: 20 bytes - - OP_EQUALVERIFY: 1 byte - - 2: 1 byte - - OP_SWAP: 1 byte - - OP_DATA: 1 byte (localkey length) - - localkey: 33 bytes - - 2: 1 byte - - OP_CHECKMULTISIG: 1 byte - - OP_ELSE: 1 byte - - OP_SIZE: 1 byte - - 0: 1 byte + accepted_htlc_script: 139 bytes + - OP_DUP: 1 byte + - OP_HASH160: 1 byte + - OP_DATA: 1 byte (revocationkey-hash length) + - revocationkey-hash: 20 bytes - OP_EQUAL: 1 byte - OP_IF: 1 byte - - OP_DROP: 1 byte - - OP_DATA: 1 byte (locktime length) - - locktime: 3 bytes - - OP_CHECKLOCKTIMEVERIFY: 1 byte - - OP_DROP: 1 byte - OP_CHECKSIG: 1 byte - OP_ELSE: 1 byte + - OP_DATA: 1 byte (remotekey length) + - remotekey: 33 bytes - OP_SWAP: 1 byte + - OP_SIZE: 1 byte + - 32: 2 bytes + - OP_EQUAL: 1 byte + - OP_IF: 1 byte + - OP_HASH160: 1 byte + - OP_DATA: 1 byte (ripemd-of-payment-hash length) + - ripemd-of-payment-hash: 20 bytes + - OP_EQUALVERIFY: 1 byte - 2: 1 byte - OP_SWAP: 1 byte - - OP_DATA: 1 byte (revocationkey length) - - revocationkey: 33 bytes + - OP_DATA: 1 byte (localkey length) + - localkey: 33 bytes - 2: 1 byte - OP_CHECKMULTISIG: 1 byte + - OP_ELSE: 1 byte + - OP_DROP: 1 byte + - OP_DATA: 1 byte (locktime length) + - locktime: 3 bytes + - OP_CHECKLOCKTIMEVERIFY: 1 byte + - OP_DROP: 1 byte + - OP_CHECKSIG: 1 byte + - OP_ENDIF: 1 byte - OP_ENDIF: 1 byte - - OP_ENDIF: 1 byte - offered_htlc_script: 139 bytes + offered_htlc_script: 133 bytes + - OP_DUP: 1 byte + - OP_HASH160: 1 byte + - OP_DATA: 1 byte (revocationkey-hash length) + - revocationkey-hash: 20 bytes + - OP_EQUAL: 1 byte + - OP_IF: 1 byte + - OP_CHECKSIG: 1 byte + - OP_ELSE: 1 byte - OP_DATA: 1 byte (remotekey length) - remotekey: 33 bytes - OP_SWAP: 1 byte @@ -662,9 +665,7 @@ The *expected weight* of an HTLC transaction is calculated as follows: - OP_SWAP: 1 byte - OP_DATA: 1 byte (localkey length) - localkey: 33 bytes - - OP_DATA: 1 byte (revocationkey length) - - revocationkey: 33 bytes - - 3: 1 byte + - 2: 1 byte - OP_CHECKMULTISIG: 1 byte - OP_ELSE: 1 byte - OP_HASH160: 1 byte @@ -673,8 +674,9 @@ The *expected weight* of an HTLC transaction is calculated as follows: - OP_EQUALVERIFY: 1 byte - OP_CHECKSIG: 1 byte - OP_ENDIF: 1 byte + - OP_ENDIF: 1 byte - timeout_witness: 291 bytes + timeout_witness: 285 bytes - number_of_witness_elements: 1 byte - nil_length: 1 byte - sig_alice_length: 1 byte @@ -685,7 +687,7 @@ The *expected weight* of an HTLC transaction is calculated as follows: - witness_script_length: 1 byte - witness_script (offered_htlc_script) - success_witness: 340 bytes + success_witness: 325 bytes - number_of_witness_elements: 1 byte - nil_length: 1 byte - sig_alice_length: 1 byte @@ -723,11 +725,11 @@ The *expected weight* of an HTLC transaction is calculated as follows: - lock_time: 4 bytes Multiplying non-witness data by 4, this gives a weight of 376. Adding -the witness data for each case (291 + 2 for HTLC-timeout, 340 + 2 for +the witness data for each case (285 + 2 for HTLC-timeout, 325 + 2 for HTLC-success) gives a weight of: - 669 (HTLC-timeout) - 718 (HTLC-success) + 663 (HTLC-timeout) + 703 (HTLC-success) # Appendix C: Funding Transaction Test Vectors diff --git a/05-onchain.md b/05-onchain.md index c8c4cd6..179efcd 100644 --- a/05-onchain.md +++ b/05-onchain.md @@ -325,14 +325,14 @@ should cover this. [FIXME: May have to divide and conquer here, since they may b There are three different scripts for penalty transactions, with the following witnesses weight (details of the computation in [Appendix A](#appendix-a-expected-weights)): to_local_penalty_witness: 154 bytes - offered_htlc_penalty_witness: 291 bytes - accepted_htlc_penalty_witness: 307 bytes + offered_htlc_penalty_witness: 243 bytes + accepted_htlc_penalty_witness: 249 bytes The penalty txinput itself takes 41 bytes, thus has a weight of 164, making the weight of each input: to_local_penalty_input_weight: 318 bytes - offered_htlc_penalty_input_weight: 455 bytes - accepted_htlc_penalty_input_weight: 471 bytes + offered_htlc_penalty_input_weight: 407 bytes + accepted_htlc_penalty_input_weight: 413 bytes The rest of the penalty transaction takes 4+3+1+8+1+34+4=55 bytes assuming it has a pay-to-witness-script-hash (the largest standard @@ -343,9 +343,9 @@ us to spend from the commitment transaction. With a maximum standard weight of 400000: - max_num_htlcs = (400000 - 318 - 55) / 471 = 848 + max_num_htlcs = (400000 - 318 - 55) / 413 = 967 -Thus we could allow 424 HTLCs in each direction (with one output to-self) and still resolve it with a single penalty +Thus we could allow 483 HTLCs in each direction (with one output to-self) and still resolve it with a single penalty transaction. # General Requirements @@ -402,35 +402,32 @@ The *expected weight* is calculated as follows: The *expected weight* is calculated as follows (some calculations have already been made in [BOLT #3](03-transactions.md)): - offered_htlc_script: 139 bytes + offered_htlc_script: 133 bytes - offered_htlc_penalty_witness: 291 bytes + offered_htlc_penalty_witness: 243 bytes - number_of_witness_elements: 1 byte - - nil_length: 1 byte - - remote_sig_length: 1 byte - - remote_sig: 73 bytes - revocation_sig_length: 1 byte - revocation_sig: 73 bytes - - nil_length: 1 byte + - revocation_key_length: 1 byte + - revocation_key: 33 bytes - witness_script_length: 1 byte - - witness_script (to_local_script) + - witness_script (offered_htlc_script) ## Expected weight of the received-htlc penalty transaction witness The *expected weight* is calculated as follows (some calculations have already been made in [BOLT #3](03-transactions.md)): - accepted_htlc_script: 156 bytes + accepted_htlc_script: 139 bytes - accepted_htlc_penalty_witness: 307 bytes + accepted_htlc_penalty_witness: 249 bytes - number_of_witness_elements: 1 byte - - nil_length: 1 byte - - remote_sig_length: 1 byte - - remote_sig: 73 bytes - revocation_sig_length: 1 byte - revocation_sig: 73 bytes + - revocation_key_length: 1 byte + - revocation_key: 33 bytes - witness_script_length: 1 byte - - witness_script (to_local_script) + - witness_script (accepted_htlc_script) ![Creative Commons License](https://i.creativecommons.org/l/by/4.0/88x31.png "License CC-BY")