Skip to content

Commit 8f2104e

Browse files
authored
Peers need to check each other's dust limit (#894)
Since HTLCs below this amount will not appear in the commitment tx, they are effectively converted to miner fees. The peer could use this to grief you by broadcasting its commitment once it contains a lot of dust HTLCs. Add network dust thresholds computation details, as implemented in Bitcoin Core's default relay policy. Drop non-segwit support in shutdown: this allows dust limit to go as low as 354 sats without creating relay issues with default node policies. We add a requirement that dust limit cannot be lower than 354 sats. This ensures implementers don't have to figure this subtlety on their own. Fixes #696 and #905
1 parent e832059 commit 8f2104e

File tree

2 files changed

+133
-12
lines changed

2 files changed

+133
-12
lines changed

02-peer-protocol.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ The receiving node MAY fail the channel if:
258258
- it considers `max_htlc_value_in_flight_msat` too small.
259259
- it considers `channel_reserve_satoshis` too large.
260260
- it considers `max_accepted_htlcs` too small.
261-
- it considers `dust_limit_satoshis` too small and plans to rely on the sending node publishing its commitment transaction in the event of a data loss (see [message-retransmission](02-peer-protocol.md#message-retransmission)).
261+
- it considers `dust_limit_satoshis` too large.
262262

263263
The receiving node MUST fail the channel if:
264264
- the `chain_hash` value is set to a hash of a chain that is unknown to the receiver.
@@ -269,6 +269,7 @@ The receiving node MUST fail the channel if:
269269
- `funding_pubkey`, `revocation_basepoint`, `htlc_basepoint`, `payment_basepoint`, or `delayed_payment_basepoint`
270270
are not valid secp256k1 pubkeys in compressed format.
271271
- `dust_limit_satoshis` is greater than `channel_reserve_satoshis`.
272+
- `dust_limit_satoshis` is smaller than `354 satoshis` (see [BOLT 3](03-transactions.md#dust-limits)).
272273
- the funder's amount for the initial commitment transaction is not sufficient for full [fee payment](03-transactions.md#fee-payment).
273274
- both `to_local` and `to_remote` amounts for the initial commitment transaction are less than or equal to `channel_reserve_satoshis` (see [BOLT 3](03-transactions.md#commitment-transaction-outputs)).
274275
- `funding_satoshis` is greater than or equal to 2^24 and the receiver does not support `option_support_large_channel`.
@@ -279,7 +280,7 @@ The receiving node MUST NOT:
279280

280281
#### Rationale
281282

282-
The requirement for `funding_satoshis` to be less than 2^24 satoshi was a temporary self-imposed limit while implementations were not yet considered stable, it can be lifted by advertising `option_support_large_channel`.
283+
The requirement for `funding_satoshis` to be less than 2^24 satoshi was a temporary self-imposed limit while implementations were not yet considered stable, it can be lifted by advertising `option_support_large_channel`.
283284

284285
The *channel reserve* is specified by the peer's `channel_reserve_satoshis`: 1% of the channel total is suggested. Each side of a channel maintains this reserve so it always has something to lose if it were to try to broadcast an old, revoked commitment transaction. Initially, this reserve may not be met, as only one side has funds; but the protocol ensures that there is always progress toward meeting this reserve, and once met, it is maintained.
285286

@@ -295,6 +296,10 @@ would be eliminated as dust. The similar requirements in
295296
`accept_channel` ensure that both sides' `channel_reserve_satoshis`
296297
are above both `dust_limit_satoshis`.
297298

299+
The receiver should not accept large `dust_limit_satoshis`, as this could be
300+
used in griefing attacks, where the peer publishes its commitment with a lot
301+
of dust htlcs, which effectively become miner fees.
302+
298303
Details for how to handle a channel failure can be found in [BOLT 5:Failing a Channel](05-onchain.md#failing-a-channel).
299304

300305
### The `accept_channel` Message
@@ -353,7 +358,6 @@ The receiver:
353358
- if `channel_type` is set, and `channel_type` was set in `open_channel`, and they are not equal types:
354359
- MUST reject the channel.
355360

356-
357361
Other fields have the same requirements as their counterparts in `open_channel`.
358362

359363
### The `funding_created` Message
@@ -543,12 +547,9 @@ A sending node:
543547
- MUST send the same value in `scriptpubkey`.
544548
- MUST set `scriptpubkey` in one of the following forms:
545549

546-
1. `OP_DUP` `OP_HASH160` `20` 20-bytes `OP_EQUALVERIFY` `OP_CHECKSIG`
547-
(pay to pubkey hash), OR
548-
2. `OP_HASH160` `20` 20-bytes `OP_EQUAL` (pay to script hash), OR
549-
3. `OP_0` `20` 20-bytes (version 0 pay to witness pubkey hash), OR
550-
4. `OP_0` `32` 32-bytes (version 0 pay to witness script hash), OR
551-
5. if (and only if) `option_shutdown_anysegwit` is negotiated:
550+
1. `OP_0` `20` 20-bytes (version 0 pay to witness pubkey hash), OR
551+
2. `OP_0` `32` 32-bytes (version 0 pay to witness script hash), OR
552+
3. if (and only if) `option_shutdown_anysegwit` is negotiated:
552553
* `OP_1` through `OP_16` inclusive, followed by a single push of 2 to 40 bytes
553554
(witness program versions 1 through 16)
554555

@@ -576,9 +577,11 @@ may immediately begin closing negotiation, so we ban further updates
576577
to the commitment transaction (in particular, `update_fee` would be
577578
possible otherwise).
578579

579-
The `scriptpubkey` forms include only standard forms accepted by the
580-
Bitcoin network, which ensures the resulting transaction will
581-
propagate to miners.
580+
The `scriptpubkey` forms include only standard segwit forms accepted by
581+
the Bitcoin network, which ensures the resulting transaction will
582+
propagate to miners. However old nodes may send non-segwit scripts, which
583+
may be accepted for backwards-compatibility (with a caveat to force-close
584+
if this output doesn't meet dust relay requirements).
582585

583586
The `option_upfront_shutdown_script` feature means that the node
584587
wanted to pre-commit to `shutdown_scriptpubkey` in case it was
@@ -674,6 +677,10 @@ The receiving node:
674677
- MUST propose a value "strictly between" the received `fee_satoshis`
675678
and its previously-sent `fee_satoshis`.
676679

680+
The receiving node:
681+
- if one of the outputs in the closing transaction is below the dust limit for its `scriptpubkey` (see [BOLT 3](03-transactions.md#dust-limits)):
682+
- MUST fail the channel
683+
677684
#### Rationale
678685

679686
When `fee_range` is not provided, the "strictly between" requirement ensures
@@ -690,6 +697,12 @@ to have a maximum feerate. It may want a minimum feerate, however, to ensure
690697
that the transaction propagates. It can always use CPFP later to speed up
691698
confirmation if necessary, so that minimum should be low.
692699

700+
It may happen that the closing transaction doesn't meet bitcoin's default relay
701+
policies (e.g. when using a non-segwit shutdown script for an output below 546
702+
satoshis, which is possible if `dust_limit_satoshis` is below 546 satoshis).
703+
No funds are at risk when that happens, but the channel must be force-closed as
704+
the closing transaction will likely never reach miners.
705+
693706
## Normal Operation
694707

695708
Once both nodes have exchanged `funding_locked` (and optionally [`announcement_signatures`](07-routing-gossip.md#the-announcement_signatures-message)), the channel can be used to make payments via Hashed Time Locked Contracts.

03-transactions.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ This details the exact format of on-chain transactions, which both sides need to
2121
* [Fees](#fees)
2222
* [Fee Calculation](#fee-calculation)
2323
* [Fee Payment](#fee-payment)
24+
* [Dust Limits](#dust-limits)
25+
* [Commitment Transaction Construction](#commitment-transaction-construction)
2426
* [Keys](#keys)
2527
* [Key Derivation](#key-derivation)
2628
* [`localpubkey`, `remotepubkey`, `local_htlcpubkey`, `remote_htlcpubkey`, `local_delayedpubkey`, and `remote_delayedpubkey` Derivation](#localpubkey-remotepubkey-local_htlcpubkey-remote_htlcpubkey-local_delayedpubkey-and-remote_delayedpubkey-derivation)
@@ -484,6 +486,112 @@ A node:
484486
- if the resulting fee rate is too low:
485487
- MAY fail the channel.
486488

489+
## Dust Limits
490+
491+
The `dust_limit_satoshis` parameter is used to configure the threshold below
492+
which nodes will not produce on-chain transaction outputs.
493+
494+
There is no consensus rule in Bitcoin that makes outputs below dust thresholds
495+
invalid or unspendable, but policy rules in popular implementations will prevent
496+
relaying transactions that contain such outputs.
497+
498+
Bitcoin Core defines the following dust thresholds:
499+
500+
- pay to pubkey hash (p2pkh): 546 satoshis
501+
- pay to script hash (p2sh): 540 satoshis
502+
- pay to witness pubkey hash (p2wpkh): 294 satoshis
503+
- pay to witness script hash (p2wsh): 330 satoshis
504+
- unknown segwit versions: 354 satoshis
505+
506+
The rationale of this calculation (implemented [here](https://github.com/bitcoin/bitcoin/blob/0.21/src/policy/policy.cpp))
507+
is explained in the following sections.
508+
509+
In all these sections, the calculations are done with a feerate of 3000 sat/kB
510+
as per Bitcoin Core's implementation.
511+
512+
### Pay to pubkey hash (p2pkh)
513+
514+
A p2pkh output is 34 bytes:
515+
516+
- 8 bytes for the output amount
517+
- 1 byte for the script length
518+
- 25 bytes for the script (`OP_DUP` `OP_HASH160` `20` 20-bytes `OP_EQUALVERIFY` `OP_CHECKSIG`)
519+
520+
A p2pkh input is at least 148 bytes:
521+
522+
- 36 bytes for the previous output (32 bytes hash + 4 bytes index)
523+
- 4 bytes for the sequence
524+
- 1 byte for the script sig length
525+
- 107 bytes for the script sig:
526+
- 1 byte for the items count
527+
- 1 byte for the signature length
528+
- 71 bytes for the signature
529+
- 1 byte for the public key length
530+
- 33 bytes for the public key
531+
532+
The p2pkh dust threshold is then `(34 + 148) * 3000 / 1000 = 546 satoshis`
533+
534+
### Pay to script hash (p2sh)
535+
536+
A p2sh output is 32 bytes:
537+
538+
- 8 bytes for the output amount
539+
- 1 byte for the script length
540+
- 23 bytes for the script (`OP_HASH160` `20` 20-bytes `OP_EQUAL`)
541+
542+
A p2sh input doesn't have a fixed size, since it depends on the underlying
543+
script, so we use 148 bytes as a lower bound.
544+
545+
The p2sh dust threshold is then `(32 + 148) * 3000 / 1000 = 540 satoshis`
546+
547+
### Pay to witness pubkey hash (p2wpkh)
548+
549+
A p2wpkh output is 31 bytes:
550+
551+
- 8 bytes for the output amount
552+
- 1 byte for the script length
553+
- 22 bytes for the script (`OP_0` `20` 20-bytes)
554+
555+
A p2wpkh input is at least 67 bytes (depending on the signature length):
556+
557+
- 36 bytes for the previous output (32 bytes hash + 4 bytes index)
558+
- 4 bytes for the sequence
559+
- 1 byte for the script sig length
560+
- 26 bytes for the witness (rounded down from 26.75, with the 75% segwit discount applied):
561+
- 1 byte for the items count
562+
- 1 byte for the signature length
563+
- 71 bytes for the signature
564+
- 1 byte for the public key length
565+
- 33 bytes for the public key
566+
567+
The p2wpkh dust threshold is then `(31 + 67) * 3000 / 1000 = 294 satoshis`
568+
569+
### Pay to witness script hash (p2wsh)
570+
571+
A p2wsh output is 43 bytes:
572+
573+
- 8 bytes for the output amount
574+
- 1 byte for the script length
575+
- 34 bytes for the script (`OP_0` `32` 32-bytes)
576+
577+
A p2wsh input doesn't have a fixed size, since it depends on the underlying
578+
script, so we use 67 bytes as a lower bound.
579+
580+
The p2wsh dust threshold is then `(43 + 67) * 3000 / 1000 = 330 satoshis`
581+
582+
### Unknown segwit versions
583+
584+
Unknown segwit outputs are at most 51 bytes:
585+
586+
- 8 bytes for the output amount
587+
- 1 byte for the script length
588+
- 42 bytes for the script (`OP_1` through `OP_16` inclusive, followed by a single push of 2 to 40 bytes)
589+
590+
The input doesn't have a fixed size, since it depends on the underlying
591+
script, so we use 67 bytes as a lower bound.
592+
593+
The unknown segwit version dust threshold is then `(51 + 67) * 3000 / 1000 = 354 satoshis`
594+
487595
## Commitment Transaction Construction
488596

489597
This section ties the previous sections together to detail the

0 commit comments

Comments
 (0)