比特币闪电网络中的dust limit

1. 引言

比特币闪电网络协议中,dust_limit_satoshis设置了不会提交到比特币链上的transaction output。

在比特币共识网络中并没有规定低于某dust阈值的output将为invalid或unspendable,但是,在很多流行的比特币实现中,有阻止包含低于dust阈值的交易转发。

Bitcoin Core中定义了如下dust阈值:【基于Bitcoin Core实现中设置的3000 sat/kB feerate】

  • pay to pubkey hash (p2pkh): 546 satoshis
  • pay to script hash (p2sh): 540 satoshis
  • pay to witness pubkey hash (p2wpkh): 294 satoshis
  • pay to witness script hash (p2wsh): 330 satoshis
  • unknown segwit versions: 354 satoshis

2. Pay to pubkey hash (p2pkh)

A p2pkh output is 34 bytes:

  • 8 bytes for the output amount
  • 1 byte for the script length
  • 25 bytes for the script (OP_DUP OP_HASH160 20 20-bytes OP_EQUALVERIFY OP_CHECKSIG)

A p2pkh input is at least 148 bytes:

  • 36 bytes for the previous output (32 bytes hash + 4 bytes index)
  • 4 bytes for the sequence
  • 1 byte for the script sig length
  • 107 bytes for the script sig:
    • 1 byte for the items count
    • 1 byte for the signature length
    • 71 bytes for the signature
    • 1 byte for the public key length
    • 33 bytes for the public key

The p2pkh dust threshold is then (34 + 148) * 3000 / 1000 = 546 satoshis。

3. Pay to script hash (p2sh)

A p2sh output is 32 bytes:

  • 8 bytes for the output amount
  • 1 byte for the script length
  • 23 bytes for the script (OP_HASH160 20 20-bytes OP_EQUAL)

A p2sh input doesn’t have a fixed size, since it depends on the underlying script, so we use 148 bytes as a lower bound.

The p2sh dust threshold is then (32 + 148) * 3000 / 1000 = 540 satoshis。

4. Pay to witness pubkey hash (p2wpkh)

A p2wpkh output is 31 bytes:

  • 8 bytes for the output amount
  • 1 byte for the script length
  • 22 bytes for the script (OP_0 20 20-bytes)

A p2wpkh input is at least 67 bytes (depending on the signature length):

  • 36 bytes for the previous output (32 bytes hash + 4 bytes index)
  • 4 bytes for the sequence
  • 1 byte for the script sig length
  • 26 bytes for the witness (rounded down from 26.75, with the 75% segwit discount applied):
    • 1 byte for the items count
    • 1 byte for the signature length
    • 71 bytes for the signature
    • 1 byte for the public key length
    • 33 bytes for the public key

The p2wpkh dust threshold is then (31 + 67) * 3000 / 1000 = 294 satoshis

5. Pay to witness script hash (p2wsh)

A p2wsh output is 43 bytes:

  • 8 bytes for the output amount
  • 1 byte for the script length
  • 34 bytes for the script (OP_0 32 32-bytes)

A p2wsh input doesn’t have a fixed size, since it depends on the underlying script, so we use 67 bytes as a lower bound.

The p2wsh dust threshold is then (43 + 67) * 3000 / 1000 = 330 satoshis

6. Unknown segwit versions

Unknown segwit outputs are at most 51 bytes:

  • 8 bytes for the output amount
  • 1 byte for the script length
  • 42 bytes for the script (OP_1 through OP_16 inclusive, followed by a single push of 2 to 40 bytes)

The input doesn’t have a fixed size, since it depends on the underlying script, so we use 67 bytes as a lower bound.

The unknown segwit version dust threshold is then (51 + 67) * 3000 / 1000 = 354 satoshis

7. 闪电网络中commitment transaction的expected weight

闪电网络中commitment transaction的expected weight计算方式为:

p2wsh: 34 bytes
	- OP_0: 1 byte
	- OP_DATA: 1 byte (witness_script_SHA256 length)
	- witness_script_SHA256: 32 bytes

p2wpkh: 22 bytes
	- OP_0: 1 byte
	- OP_DATA: 1 byte (public_key_HASH160 length)
	- public_key_HASH160: 20 bytes

multi_sig: 71 bytes
	- OP_2: 1 byte
	- OP_DATA: 1 byte (pub_key_alice length)
	- pub_key_alice: 33 bytes
	- OP_DATA: 1 byte (pub_key_bob length)
	- pub_key_bob: 33 bytes
	- OP_2: 1 byte
	- OP_CHECKMULTISIG: 1 byte

witness: 222 bytes
	- number_of_witness_elements: 1 byte
	- nil_length: 1 byte
	- sig_alice_length: 1 byte
	- sig_alice: 73 bytes
	- sig_bob_length: 1 byte
	- sig_bob: 73 bytes
	- witness_script_length: 1 byte
	- witness_script (multi_sig)

funding_input: 41 bytes
	- previous_out_point: 36 bytes
		- hash: 32 bytes
		- index: 4 bytes
	- var_int: 1 byte (script_sig length)
	- script_sig: 0 bytes
	- witness <----	"witness" is used instead of "script_sig" for
 			transaction validation; however, "witness" is stored
 			separately, and the cost for its size is smaller. So,
 		    the calculation of ordinary data is separated
 			from the witness data.
	- sequence: 4 bytes

output_paying_to_local: 43 bytes
	- value: 8 bytes
	- var_int: 1 byte (pk_script length)
	- pk_script (p2wsh): 34 bytes

output_paying_to_remote (no option_anchors): 31 bytes
	- value: 8 bytes
	- var_int: 1 byte (pk_script length)
	- pk_script (p2wpkh): 22 bytes

output_paying_to_remote (option_anchors): 43 bytes
	- value: 8 bytes
	- var_int: 1 byte (pk_script length)
	- pk_script (p2wsh): 34 bytes

output_anchor (option_anchors): 43 bytes
	- value: 8 bytes
	- var_int: 1 byte (pk_script length)
	- pk_script (p2wsh): 34 bytes

htlc_output: 43 bytes
	- value: 8 bytes
	- var_int: 1 byte (pk_script length)
	- pk_script (p2wsh): 34 bytes

 witness_header: 2 bytes
	- flag: 1 byte
	- marker: 1 byte

 commitment_transaction (no option_anchors): 125 + 43 * num-htlc-outputs bytes
	- version: 4 bytes
	- witness_header <---- part of the witness data
	- count_tx_in: 1 byte
	- tx_in: 41 bytes
		funding_input
	- count_tx_out: 1 byte
	- tx_out: 74 + 43 * num-htlc-outputs bytes
		output_paying_to_remote,
		output_paying_to_local,
		....htlc_output's...
	- lock_time: 4 bytes

 commitment_transaction (option_anchors): 225 + 43 * num-htlc-outputs bytes
	- version: 4 bytes
	- witness_header <---- part of the witness data
	- count_tx_in: 1 byte
	- tx_in: 41 bytes
		funding_input
	- count_tx_out: 3 byte
	- tx_out: 172 + 43 * num-htlc-outputs bytes
		output_paying_to_remote,
		output_paying_to_local,
		output_anchor,
		output_anchor,
		....htlc_output's...
	- lock_time: 4 bytes

Multiplying non-witness data by 4 results in a weight of:

// 500 + 172 * num-htlc-outputs weight (no option_anchors)
// 900 + 172 * num-htlc-outputs weight (option_anchors)
commitment_transaction_weight = 4 * commitment_transaction

// 224 weight
witness_weight = witness_header + witness

overall_weight (no option_anchors) = 500 + 172 * num-htlc-outputs + 224 weight
overall_weight (option_anchors) = 900 + 172 * num-htlc-outputs + 224 weight

8. 闪电网络中HTLC-timeout和HTLC-success交易的expected weight

闪电网络中HTLC-timeout和HTLC-success交易的expected weight计算方式为:

accepted_htlc_script: 140 bytes (143 bytes with option_anchors)
    - OP_DUP: 1 byte
    - OP_HASH160: 1 byte
    - OP_DATA: 1 byte (RIPEMD160(SHA256(revocationpubkey)) length)
    - RIPEMD160(SHA256(revocationpubkey)): 20 bytes
    - OP_EQUAL: 1 byte
    - OP_IF: 1 byte
    - OP_CHECKSIG: 1 byte
    - OP_ELSE: 1 byte
    - OP_DATA: 1 byte (remotepubkey length)
    - remotepubkey: 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 (RIPEMD160(payment_hash) length)
	- RIPEMD160(payment_hash): 20 bytes
    - OP_EQUALVERIFY: 1 byte
    - 2: 1 byte
    - OP_SWAP: 1 byte
	- OP_DATA: 1 byte (localpubkey length)
	- localpubkey: 33 bytes
    - 2: 1 byte
    - OP_CHECKMULTISIG: 1 byte
    - OP_ELSE: 1 byte
    - OP_DROP: 1 byte
	- OP_DATA: 1 byte (cltv_expiry length)
	- cltv_expiry: 4 bytes
    - OP_CHECKLOCKTIMEVERIFY: 1 byte
    - OP_DROP: 1 byte
    - OP_CHECKSIG: 1 byte
    - OP_ENDIF: 1 byte
    - OP_1: 1 byte (option_anchors)
    - OP_CHECKSEQUENCEVERIFY: 1 byte (option_anchors)
    - OP_DROP: 1 byte (option_anchors)
    - OP_ENDIF: 1 byte

offered_htlc_script: 133 bytes (136 bytes with option_anchors)
    - OP_DUP: 1 byte
    - OP_HASH160: 1 byte
    - OP_DATA: 1 byte (RIPEMD160(SHA256(revocationpubkey)) length)
    - RIPEMD160(SHA256(revocationpubkey)): 20 bytes
    - OP_EQUAL: 1 byte
    - OP_IF: 1 byte
    - OP_CHECKSIG: 1 byte
    - OP_ELSE: 1 byte
	- OP_DATA: 1 byte (remotepubkey length)
	- remotepubkey: 33 bytes
	- OP_SWAP: 1 byte
	- OP_SIZE: 1 byte
	- OP_DATA: 1 byte (32 length)
	- 32: 1 byte
	- OP_EQUAL: 1 byte
	- OP_NOTIF: 1 byte
	- OP_DROP: 1 byte
	- 2: 1 byte
	- OP_SWAP: 1 byte
	- OP_DATA: 1 byte (localpubkey length)
	- localpubkey: 33 bytes
	- 2: 1 byte
	- OP_CHECKMULTISIG: 1 byte
	- OP_ELSE: 1 byte
	- OP_HASH160: 1 byte
	- OP_DATA: 1 byte (RIPEMD160(payment_hash) length)
	- RIPEMD160(payment_hash): 20 bytes
	- OP_EQUALVERIFY: 1 byte
	- OP_CHECKSIG: 1 byte
	- OP_ENDIF: 1 byte
    - OP_1: 1 byte (option_anchors)
    - OP_CHECKSEQUENCEVERIFY: 1 byte (option_anchors)
    - OP_DROP: 1 byte (option_anchors)
    - OP_ENDIF: 1 byte

timeout_witness: 285 bytes (288 bytes with option_anchors)
	- number_of_witness_elements: 1 byte
	- nil_length: 1 byte
	- sig_alice_length: 1 byte
	- sig_alice: 73 bytes
	- sig_bob_length: 1 byte
	- sig_bob: 73 bytes
	- nil_length: 1 byte
	- witness_script_length: 1 byte
	- witness_script (offered_htlc_script)

success_witness: 324 bytes (327 bytes with option_anchors)
	- number_of_witness_elements: 1 byte
	- nil_length: 1 byte
	- sig_alice_length: 1 byte
	- sig_alice: 73 bytes
	- sig_bob_length: 1 byte
	- sig_bob: 73 bytes
	- preimage_length: 1 byte
	- preimage: 32 bytes
	- witness_script_length: 1 byte
	- witness_script (accepted_htlc_script)

commitment_input: 41 bytes
	- previous_out_point: 36 bytes
		- hash: 32 bytes
		- index: 4 bytes
	- var_int: 1 byte (script_sig length)
	- script_sig: 0 bytes
	- witness (success_witness or timeout_witness)
	- sequence: 4 bytes

htlc_output: 43 bytes
	- value: 8 bytes
	- var_int: 1 byte (pk_script length)
	- pk_script (p2wsh): 34 bytes

htlc_transaction:
	- version: 4 bytes
	- witness_header <---- part of the witness data
	- count_tx_in: 1 byte
	- tx_in: 41 bytes
		commitment_input
	- count_tx_out: 1 byte
	- tx_out: 43
		htlc_output
	- lock_time: 4 bytes

Multiplying non-witness data by 4 results in a weight of 376. Adding the witness data for each case (285 or 288 + 2 for HTLC-timeout, 324 or 327 + 2 for HTLC-success) results in weights of:

663 (HTLC-timeout) (666 with option_anchors))
703 (HTLC-success) (706 with option_anchors))
            - (really 702 and 705, but we use these numbers for historical reasons)

参考资料

[1] 比特币闪电网络中的Dust Limits

猜你喜欢

转载自blog.csdn.net/mutourend/article/details/124131083