lnd/lnwire
Olaoluwa Osuntokun 9cad57bfce peer: enforce onion message rate limits at ingress
This commit plumbs the combined IngressLimiter (per-peer + global)
through peer.Config and consults it from the readHandler's
*lnwire.OnionMessage case. The decision is factored into a small
allowOnionMessage helper so that the ingress policy is directly
unit-testable without standing up a full Brontide harness. Per-peer is
checked first inside the IngressLimiter: if we consulted the global
limiter first, a peer whose own bucket was already empty would still
get to burn a global token on each attempt, letting a single hostile
peer drain the shared budget and starve legitimate peers.

peer.Config carries a single OnionLimiter field of IngressLimiter type;
the brontide readHandler calls a single AllowN per incoming onion
message and dispatches on sentinel errors via errors.Is for the
first-drop log path. Nil limiter values are treated as "disabled"
throughout, which both preserves the pre-change behavior when onion
messaging is entirely turned off and keeps the brontide test harness
from needing to construct real limiters. Per-peer bucket state is
retained across disconnect at the IngressLimiter layer so a peer
cannot cycle the connection to reset its per-peer allowance.

OnionMessage also gains a WireSize method that computes the
on-the-wire size directly from the in-memory fields (no round-trip
through Encode) so the hot ingress path can charge the right number of
byte tokens without paying for a full serialization.

The accompanying unit tests cover the nil/disabled path, the
per-peer-rejects-first ordering invariant (asserting the global
limiter is not consulted when the per-peer bucket is empty), the
global rejection path, per-peer isolation across distinct pubkeys, and
a small concurrent stress test that asserts every attempt is accounted
for as either accepted or dropped and that the total accepted count
equals the configured burst under -race. A property-based rapid test
on WireSize guards against silent divergence from WriteMessage if the
OnionMessage wire format ever gains a TLV extension.
2026-04-15 13:23:50 -07:00
..
accept_channel.go lnwire: add new TestMessage interface for property tests 2025-03-20 18:28:07 -07:00
accept_channel_test.go multi: use btcd's btcec/v2 and btcutil modules 2022-03-09 19:02:37 +01:00
announcement_signatures.go lnwire: let gossip messages implement GossipMessage 2025-11-12 22:54:03 +08:00
announcement_signatures_2.go lnwire: let gossip messages implement GossipMessage 2025-11-12 22:54:03 +08:00
announcement_signatures_2_test.go lnwire: update AnnounceSigs2 to use pure TLV 2025-09-01 12:10:30 +02:00
channel_announcement.go lnwire: let gossip messages implement GossipMessage 2025-11-12 22:54:03 +08:00
channel_announcement_2.go lnwire: let gossip messages implement GossipMessage 2025-11-12 22:54:03 +08:00
channel_announcement_2_test.go lnwire: add Outpoint to ChannelAnnouncement2 2025-10-01 13:13:32 +02:00
channel_id.go lnwire: let ChannelID implement RecordProducer 2025-09-01 11:05:23 +02:00
channel_id_test.go multi: make NewChanIDFromOutpoint accept value instead of pointer 2024-03-08 15:47:55 -08:00
channel_ready.go lnwire: add new SerializedSize method to all wire messages 2025-03-20 18:27:52 -07:00
channel_reestablish.go multi: use feature bits to pick which taproot nonce field to use 2026-04-13 12:21:42 -07:00
channel_type.go lnwire: create common encoder/decoder for raw feature vectors 2024-03-27 09:36:38 -04:00
channel_type_test.go lnwire: add new ChannelType field as TLV record to Open/AcceptChannel 2021-08-30 19:17:21 -07:00
channel_update.go lnwire: let gossip messages implement GossipMessage 2025-11-12 22:54:03 +08:00
channel_update_2.go graph/db: add v2 fields to ChannelEdgePolicy model 2026-02-13 07:21:24 +02:00
channel_update_2_test.go lnwire+netann: update ChannelUpdate2 2025-09-01 12:10:30 +02:00
closing_complete.go lnwallet/chancloser: address lint and PR review feedback 2026-03-27 14:04:25 -07:00
closing_sig.go lnwallet/chancloser: address lint and PR review feedback 2026-03-27 14:04:25 -07:00
closing_signed.go lnwire: add new TestMessage interface for property tests 2025-03-20 18:28:07 -07:00
commit_sig.go lnwire: add new TestMessage interface for property tests 2025-03-20 18:28:07 -07:00
commit_sig_test.go lnwire: validate MuSig2 nonce points on wire decode 2026-03-19 11:11:03 -03:00
custom.go lnwire: add new TestMessage interface for property tests 2025-03-20 18:28:07 -07:00
custom_records.go refactor: use maps.Copy for cleaner map handling 2025-03-25 01:19:55 +08:00
custom_records_test.go multi: update to fn v2 2024-12-04 13:19:00 -07:00
dns_addr.go lnwire: let DNSAddress implement RecordProducer 2025-10-01 13:49:15 +02:00
dns_addr_test.go lnwire: let DNSAddress implement RecordProducer 2025-10-01 13:49:15 +02:00
dyn_ack.go lnwire: make LocalNonce an optional tlv record and fix extra data 2025-07-11 04:11:30 +03:00
dyn_ack_test.go lnwire: patch uint test for DynAck 2025-07-11 04:11:30 +03:00
dyn_commit.go lnwire: simplify dyn decode methods 2025-07-11 04:11:31 +03:00
dyn_commit_test.go lnwire: patch test and fix extra data in DynCommit 2025-07-11 04:11:31 +03:00
dyn_propose.go lnwire: simplify dyn decode methods 2025-07-11 04:11:31 +03:00
dyn_propose_test.go lnwire: patch unit test for DynPropose 2025-07-11 04:09:11 +03:00
dyn_reject.go lnwire: add new SerializedSize method to all wire messages 2025-03-20 18:27:52 -07:00
encoding.go lnwire: add type CustomRecords 2024-08-22 18:33:24 +02:00
error.go multi: allow invalid tlv data when decoding payment fail info 2025-11-03 12:54:29 +01:00
extra_bytes.go lnwire: add method ParseAndExtractExtraData 2025-07-11 04:11:30 +03:00
extra_bytes_test.go lnwire: add ExtraOpaqueData helper functions and methods 2024-09-05 11:18:15 +02:00
features.go multi: actor-based onion message forwarding 2026-03-02 15:46:21 +01:00
features_test.go multi: add restriction on maximum feature bit in invoices 2023-05-04 10:35:45 -04:00
funding_created.go lnwire: add new SerializedSize method to all wire messages 2025-03-20 18:27:52 -07:00
funding_signed.go lnwire: add new TestMessage interface for property tests 2025-03-20 18:28:07 -07:00
fuzz_test.go lnwire: add onion message type 2025-11-12 22:54:03 +08:00
gossip_timestamp_range.go lnwire: add new TestMessage interface for property tests 2025-03-20 18:28:07 -07:00
init_message.go lnwire: add custom records to init 2025-09-23 19:02:59 +02:00
init_message_test.go lnwire: update tests for init message 2025-09-23 19:05:46 +02:00
interfaces.go lnwire: let gossip messages implement GossipMessage 2025-11-12 22:54:03 +08:00
kickoff_sig.go lnwire: add new TestMessage interface for property tests 2025-03-20 18:28:07 -07:00
lnwire.go lnwire: re-usable address descriptor parsers 2025-09-03 08:32:37 +02:00
lnwire_test.go lnwire: use BigSize for encoding btc amount 2025-07-11 04:09:11 +03:00
local_nonces.go lnwire: validate MuSig2 nonces in LocalNoncesData decode 2026-03-31 09:22:00 -03:00
local_nonces_test.go lnwire: validate MuSig2 nonces in LocalNoncesData decode 2026-03-31 09:22:00 -03:00
message.go lnwire: add onion message type 2025-11-12 22:54:03 +08:00
message_test.go multi: rename lnwire.NodeAnnouncement 2025-10-01 13:13:32 +02:00
msat.go go.mod+lnwire: bump TLV dep and fix MilliSatoshi Record 2025-05-07 19:36:25 +02:00
msat_test.go multi: use btcd's btcec/v2 and btcutil modules 2022-03-09 19:02:37 +01:00
musig2.go lnwire: validate MuSig2 nonce points on wire decode 2026-03-19 11:11:03 -03:00
musig2_test.go lnwire: validate MuSig2 nonce points on wire decode 2026-03-19 11:11:03 -03:00
netaddress.go multi: use btcd's btcec/v2 and btcutil modules 2022-03-09 19:02:37 +01:00
netaddress_test.go multi: move many t.Fatalf calls to require.NoError 2022-06-17 04:26:55 +00:00
node_announcement.go lnwire: let gossip messages implement GossipMessage 2025-11-12 22:54:03 +08:00
node_announcement_2.go lnwire: let gossip messages implement GossipMessage 2025-11-12 22:54:03 +08:00
node_announcement_2_test.go lnwire: define NodeAnnouncement2 2025-10-02 12:15:16 +02:00
node_announcement_test.go lnwire: add test cases for node alias validation 2019-01-07 12:53:40 -08:00
onion_error.go multi: allow invalid tlv data when decoding payment fail info 2025-11-03 12:54:29 +01:00
onion_error_test.go lnwire: fix test data race 2025-06-20 12:22:59 +02:00
onion_message.go peer: enforce onion message rate limits at ingress 2026-04-15 13:23:50 -07:00
onion_message_test.go peer: enforce onion message rate limits at ingress 2026-04-15 13:23:50 -07:00
onion_msg_payload.go lnwire: add OnionMessagePayload 2026-03-02 15:46:21 +01:00
onion_msg_payload_test.go lnwire: add OnionMessagePayload 2026-03-02 15:46:21 +01:00
opaque_addrs.go lnwire: add new type OpaqueAddrs 2022-05-04 11:06:11 +02:00
open_channel.go lnwire: add new TestMessage interface for property tests 2025-03-20 18:28:07 -07:00
outpoint.go lnwire: add OutPoint type with TLV encoding support 2025-10-01 13:13:32 +02:00
outpoint_test.go lnwire: add OutPoint type with TLV encoding support 2025-10-01 13:13:32 +02:00
partial_sig.go lnwire: validate MuSig2 nonce points on wire decode 2026-03-19 11:11:03 -03:00
ping.go lnwire+peer: ignore no-reply pings 2026-03-26 18:55:13 +08:00
ping_test.go lnwire+peer: ignore no-reply pings 2026-03-26 18:55:13 +08:00
pong.go lnwire: remove stale pong limit error 2026-03-26 18:55:13 +08:00
pure_tlv.go lnwire: introduce PureTLVMessage 2025-09-01 11:04:47 +02:00
pure_tlv_test.go lnwire: introduce PureTLVMessage 2025-09-01 11:04:47 +02:00
query_channel_range.go lnwire: add new TestMessage interface for property tests 2025-03-20 18:28:07 -07:00
query_channel_range_test.go lnwire: add QueryOptions to QueryChannelRange 2023-12-11 09:12:04 +02:00
query_options.go lnwire: add QueryOptions type 2023-12-11 09:12:03 +02:00
query_short_chan_ids.go lnwire: add new TestMessage interface for property tests 2025-03-20 18:28:07 -07:00
query_short_chan_ids_test.go lnwire+discovery: rename ShortChannelIDEncoding to QueryEncoding 2023-12-11 09:12:04 +02:00
README.md scripted-diff: replace ⛰ emoji with $ [skip ci] 2022-10-28 12:06:49 -04:00
reply_channel_range.go lnwire: add new TestMessage interface for property tests 2025-03-20 18:28:07 -07:00
reply_channel_range_test.go lnwire: add timestamps to ReplyChannelRange msg 2023-12-11 09:12:04 +02:00
reply_short_chan_ids_end.go lnwire: add new TestMessage interface for property tests 2025-03-20 18:28:07 -07:00
revoke_and_ack.go multi: fix linter issues 2026-04-13 12:21:42 -07:00
serialized_size_test.go lnwire: add new TestSerializedSize method 2025-03-20 18:28:53 -07:00
short_channel_id.go multi: enforce strict TLV length checks 2025-12-29 19:18:49 -03:00
short_channel_id_test.go multi: enforce strict TLV length checks 2025-12-29 19:18:49 -03:00
shutdown.go lnwallet/chancloser: fix MuSig2 nonce reuse across RBF rounds 2026-03-27 14:04:25 -07:00
shutdown_test.go lnwire: add CustomRecords to shutdown message 2024-09-19 10:18:39 +02:00
signature.go lnwire: add Record() method to lnwire.Sig 2024-02-05 16:30:16 -08:00
signature_test.go lnwire: TestLightningWireProtocol quick check tests for taproot fields 2023-08-22 16:29:49 -07:00
stfu.go lnwire: add new TestMessage interface for property tests 2025-03-20 18:28:07 -07:00
test_message.go multi: fix linter issues 2026-04-13 12:21:42 -07:00
test_utils.go lnwire: validate MuSig2 nonce points on wire decode 2026-03-19 11:11:03 -03:00
timestamp.go lnwire: add Timestamp interface 2026-03-16 11:25:31 +02:00
timestamp_test.go lnwire: add Timestamp interface 2026-03-16 11:25:31 +02:00
timestamps.go lnwire+discovery: rename ShortChannelIDEncoding to QueryEncoding 2023-12-11 09:12:04 +02:00
typed_delivery_addr.go lnwire: add new ChannelType field as TLV record to Open/AcceptChannel 2021-08-30 19:17:21 -07:00
typed_delivery_addr_test.go lnwire: add new ChannelType field as TLV record to Open/AcceptChannel 2021-08-30 19:17:21 -07:00
typed_fee.go multi: enforce strict TLV length checks 2025-12-29 19:18:49 -03:00
typed_fee_test.go multi: enforce strict TLV length checks 2025-12-29 19:18:49 -03:00
typed_lease_expiry.go lnwire: add LeaseExpiry custom record for Open+AcceptChannel 2021-10-19 18:30:17 -07:00
typed_lease_expiry_test.go lnwire: add LeaseExpiry custom record for Open+AcceptChannel 2021-10-19 18:30:17 -07:00
update_add_htlc.go multi: rename experimental endorsement signal to accountable 2026-01-06 09:12:31 -05:00
update_add_htlc_test.go lnwire: add custom records field to type UpdateAddHtlc 2024-09-05 11:18:15 +02:00
update_fail_htlc.go lnwire: add new TestMessage interface for property tests 2025-03-20 18:28:07 -07:00
update_fail_malformed_htlc.go lnwire: add new SerializedSize method to all wire messages 2025-03-20 18:27:52 -07:00
update_fee.go lnwire: add new SerializedSize method to all wire messages 2025-03-20 18:27:52 -07:00
update_fulfill_htlc.go lnwire: add new SerializedSize method to all wire messages 2025-03-20 18:27:52 -07:00
update_fulfill_htlc_test.go lnwire: add custom records field to type UpdateFulfillHtlc 2024-09-05 11:18:15 +02:00
warning.go lnwire: add new SerializedSize method to all wire messages 2025-03-20 18:27:52 -07:00
writer.go lnwire: add encoding/decoding for DNS addr 2025-09-02 17:51:31 +00:00
writer_test.go lnwire: add encoding/decoding for DNS addr 2025-09-02 17:51:31 +00:00

lnwire

Build Status MIT licensed GoDoc

The lnwire package implements the Lightning Network wire protocol.

This package has intentionally been designed so it can be used as a standalone package for any projects needing to interface with lightning peers at the wire protocol level.

Installation and Updating

$  go get -u github.com/lightningnetwork/lnd/lnwire