[bug]: btcd backend: closed channels stay in the graph after a websocket reconnect, so peers ban the node for outdated gossip

Open
#11,517 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go

Research direction

Start with routing/chainview/btcd.go and trace how the chain filter is loaded and restored after an RPC websocket reconnect; the issue also points to rpcclient.reregisterNtfns in infrastructure.go. Use the linked reproduction bundle to confirm the failure after the proxy reconnect. Done means closed channels are removed from the graph after reconnect, and the issue’s question about cleanup of already stale graph edges is addressed.

Written by the indexing model from the issue text.

Description

Background

Main impact: peers ban the node for gossiping outdated channels. A node on
the btcd backend keeps its own closed channels in its graph and keeps
announcing them. Peers on lnd with gossip banning (#9009) reply with
outdated channel announcement. After enough of these, they ban the node for
48h and disconnect it if they share no channel with it. The node cannot fix
this by itself. Each restart and each new peer starts the same cycle again.

Why the channels stay: with bitcoin.node=btcd, a short drop of the
lnd↔btcd websocket makes lnd miss the spends of all channels that exist at
that moment. When one of these channels closes later, lnd lists it in
closedchannels, but its edge stays in the channel graph. An lnd restart does
not remove it.

We found this on a production node that ran on btcd before we moved it to
bitcoind. 126 of its own channels were still in its graph, and 125 of them
were closed on chain. About 60% of all closes from the btcd period stayed in
the graph. After the move to bitcoind, all 52 later closes were removed from
the graph correctly. Peers still banned the node after the move, because the
old stale edges stay in the graph.

Your environment
  • lnd: v0.21.3-beta and v0.21.6-beta (Docker images lightninglabs/lnd)
  • backend: btcd v0.26.0 (built from source)
  • OS: Linux containers (Docker Compose v2)
  • production node: btcd backend, later moved to bitcoind
Steps to reproduce

Repro bundle: https://gist.github.com/bufo24/67812acc91add20c1d1c8cd57f76f5eb

git clone https://gist.github.com/67812acc91add20c1d1c8cd57f76f5eb.git lnd-btcd-repro
cd lnd-btcd-repro && bash run.sh

Set LND_IMAGE=<image> to test another build. Exit code 1 means the bug
reproduced. Requirements: Docker Compose v2, bash, jq.

Topology: alice, bob (lnd) → socat proxy → btcd. A proxy restart drops
all lnd↔btcd websockets and btcd keeps running. (In our tests, a btcd restart
in regtest lost the chain, so we use the proxy.)

  1. Control: open alice→bob, close, mine 6 blocks. The edge is removed
    (GRPH: Block … closed 1 channels).
  2. Open alice→bob. Restart the proxy. All 4 lnd rpcclients log
    Reestablished connection to RPC server. Close the channel, mine 6 blocks.
  3. Restart alice.
Expected behavior

After step 2, the closed channel is removed from alice's graph, the same as
in the control case.

Actual behavior
RESULT[control]: closed channel 122045790748672 was REMOVED from the graph
alice has 4 btcd connections: ... (all through the proxy)
alice logged 4 'Reestablished connection to RPC server' lines
RESULT[after-reconnect]: closed channel 135239930281984 is STILL IN THE GRAPH (closedchannels lists it as closed)
RESULT[after-lnd-restart]: closed channel 135239930281984 is STILL IN THE GRAPH

lnd logs no closed 1 channels for the block with the close. After the
restart, Prune tip for Channel Graph: height=135 is already after the spend,
so the startup sync does not rescan it. Same result on v0.21.3 and v0.21.6,
in 3 of 3 runs.

Cause
  • routing/chainview/btcd.go sends the watched outpoints to btcd with
    LoadTxFilter. It finds spends from the server-filtered
    OnFilteredBlockConnected notifications. It does not set
    OnClientConnected.
  • btcd stores the tx filter per websocket client (rpcwebsocket.go,
    wsClient.filterData). A new connection has no filter, so
    filteredblockconnected has no matched transactions.
  • rpcclient.reregisterNtfns (infrastructure.go) re-sends notifyblocks,
    notifynewtx, notifyspent and notifyreceived, but not loadtxfilter.
  • New channels call UpdateFilter, which adds only their own outpoints. So
    only the channels that exist at the reconnect are lost.
  • The graph builder still moves the prune tip forward for each block, so
    syncGraphWithChain never rescans the missed spends. Zombie pruning skips
    the node's own channels, so nothing else removes them.
Possible fix
  • On reconnect, reload the full chainFilter with LoadTxFilter(reload=true)
    from an OnClientConnected handler, or
  • match each connected block against the local chainFilter instead of
    relying on the server-side filter.

Question: will a fix also clean up the stale channels that already exist,
or only prevent new ones?
A fix in the chain view only prevents new stale
channels. Affected nodes keep the old edges, so they keep gossiping them and
keep getting banned, also after a backend change (as in our case). A cleanup
could, for example, check the node's own graph edges against the
closed-channel records, or against the chain, at startup.

Workaround

lncli abandonchannel --chan_point <txid:idx> --i_know_what_i_am_doing for
each channel that is closed on chain but still in the graph. In the repro
(KEEP=1 ./run.sh), this removes the stale edge.

Dominant language
Go
Stars
8.2k
Forks
2.3k
Avg merge
1d 11h
Merged PRs (30d)
103

Getting set up

  • Ships a Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from lightningnetwork/lnd

All issues in lightningnetwork/lnd

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.