Merge pull request #11673 from litbot-9000/flake/neutrino-reorg-debug-logs
What changed, and why it matters
This commit only adds test-only debugging helpers. It creates an in-memory ring buffer that captures the last 10,000 log lines from Neutrino and btcwallet during a specific test suite, and prints them only when a test fails. It also makes two tiny cleanups replacing manual error checks with a standard test helper. There is no change to production code, no user-facing behavior change, and no security fix or vulnerability introduced.
No security action needed. This is a test-infrastructure change. Reviewers may optionally confirm the log ring does not interfere with parallel test isolation, which the commit already addresses by limiting installation to the neutrino backend suite.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces lnwallet/test/debug_log.go and its unit test, plus a call to captureWalletLogs in TestLightningWallet only when targetBackEnd == ‘neutrino’. The log ring is installed once per process via sync.OnceValue, writes to package-wide loggers (neutrino, btcwallet/chain, btcwallet/wallet) at debug/info levels, and dumps the buffer on t.Failed() during t.Cleanup. The remaining diff in test_interface.go is a non-functional refactor from if err != nil { t.Fatalf(…) } to require.NoError. No runtime LND code is modified.
Changed components
lnwallet/test/test_interface.golnwallet/test/debug_log.golnwallet/test/debug_log_test.goInspect captured patch +147 / −6
### lnwallet/test/debug_log.go
@@ -0,0 +1,105 @@
+package lnwallettest
+
+import (
+ "bytes"
+ "sync"
+ "testing"
+
+ "github.com/btcsuite/btclog"
+ "github.com/btcsuite/btcwallet/chain"
+ "github.com/btcsuite/btcwallet/wallet"
+ "github.com/lightninglabs/neutrino"
+)
+
+// numDebugLogLines is the number of most recent log lines kept in memory and
+// printed when the wallet test suite fails. Neutrino logs a few thousand
+// lines while it syncs many blocks, so this is sized to still cover the last
+// test case plus a full wallet sync timeout at the chosen log levels.
+const numDebugLogLines = 10000
+
+// logRing is an io.Writer that keeps the most recent log lines in memory. The
+// btclog backend issues exactly one Write call per log line.
+type logRing struct {
+ mu sync.Mutex
+ lines [][]byte
+ next int
+ full bool
+}
+
+// newLogRing returns a logRing that holds up to size lines.
+func newLogRing(size int) *logRing {
+ return &logRing{
+ lines: make([][]byte, size),
+ }
+}
+
+// Write stores a copy of the given log line, overwriting the oldest one once
+// the ring is full.
+func (r *logRing) Write(p []byte) (int, error) {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+
+ r.lines[r.next] = append([]byte(nil), p...)
+ r.next = (r.next + 1) % len(r.lines)
+ if r.next == 0 {
+ r.full = true
+ }
+
+ return len(p), nil
+}
+
+// String returns the stored log lines from oldest to newest.
+func (r *logRing) String() string {
+ r.mu.Lock()
+ defer r.mu.Unlock()
+
+ var buf bytes.Buffer
+ if r.full {
+ for _, line := range r.lines[r.next:] {
+ buf.Write(line)
+ }
+ }
+ for _, line := range r.lines[:r.next] {
+ buf.Write(line)
+ }
+
+ return buf.String()
+}
+
+// walletLogRing installs the dependency loggers once in the neutrino test
+// process. Replacing them during cleanup can race with shutdown goroutines
+// that still read the package-wide loggers.
+var walletLogRing = sync.OnceValue(func() *logRing {
+ ring := newLogRing(numDebugLogLines)
+ backend := btclog.NewBackend(ring)
+
+ neutrinoLog := backend.Logger("BTCN")
+ neutrinoLog.SetLevel(btclog.LevelDebug)
+ neutrino.UseLogger(neutrinoLog)
+
+ chainLog := backend.Logger("CHIO")
+ chainLog.SetLevel(btclog.LevelDebug)
+ chain.UseLogger(chainLog)
+
+ // The wallet logs every skipped address at debug level, which would
+ // flush everything else out of the ring, so keep it at info.
+ walletLog := backend.Logger("BTWL")
+ walletLog.SetLevel(btclog.LevelInfo)
+ wallet.UseLogger(walletLog)
+
+ return ring
+})
+
+// captureWalletLogs prints recent wallet logs if the neutrino suite fails.
+func captureWalletLogs(t *testing.T) {
+ t.Helper()
+
+ ring := walletLogRing()
+
+ t.Cleanup(func() {
+ if t.Failed() {
+ t.Logf("Last %d neutrino/btcwallet log lines:\n%s",
+ numDebugLogLines, ring.String())
+ }
+ })
+}
### lnwallet/test/debug_log_test.go
@@ -0,0 +1,33 @@
+package lnwallettest
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+// TestLogRing checks that the log ring returns the most recent lines in the
+// order they were written, both before and after it wraps around.
+func TestLogRing(t *testing.T) {
+ t.Parallel()
+
+ ring := newLogRing(3)
+ require.Empty(t, ring.String())
+
+ write := func(i int) {
+ _, err := fmt.Fprintf(ring, "line %d\n", i)
+ require.NoError(t, err)
+ }
+
+ write(1)
+ write(2)
+ require.Equal(t, "line 1\nline 2\n", ring.String())
+
+ write(3)
+ require.Equal(t, "line 1\nline 2\nline 3\n", ring.String())
+
+ write(4)
+ write(5)
+ require.Equal(t, "line 3\nline 4\nline 5\n", ring.String())
+}
### lnwallet/test/test_interface.go
@@ -3236,6 +3236,12 @@ func testSingleFunderExternalFundingTx(miner *rpctest.Harness,
func TestLightningWallet(t *testing.T, targetBackEnd string) {
t.Parallel()
+ // The dependency loggers are process-wide. Limit capture to the single
+ // neutrino suite so the parallel bitcoind suites never replace them.
+ if targetBackEnd == "neutrino" {
+ captureWalletLogs(t)
+ }
+
// Initialize the harness around a btcd node which will serve as our
// dedicated miner to generate blocks, cause re-orgs, etc. We'll set
// up this node with a chain length of 125, so we have plenty of BTC
@@ -3247,9 +3253,8 @@ func TestLightningWallet(t *testing.T, targetBackEnd string) {
// Next mine enough blocks in order for segwit and the CSV package
// soft-fork to activate on RegNet.
numBlocks := netParams.MinerConfirmationWindow * 2
- if _, err := miningNode.Client.Generate(numBlocks); err != nil {
- t.Fatalf("unable to generate blocks: %v", err)
- }
+ _, err := miningNode.Client.Generate(numBlocks)
+ require.NoError(t, err, "unable to generate blocks")
rpcConfig := miningNode.RPCConfig()
@@ -3264,9 +3269,7 @@ func TestLightningWallet(t *testing.T, targetBackEnd string) {
&rpcConfig, netParams, hintCache, hintCache, blockCache,
)
require.NoError(t, err, "unable to create notifier")
- if err := chainNotifier.Start(); err != nil {
- t.Fatalf("unable to start notifier: %v", err)
- }
+ require.NoError(t, chainNotifier.Start(), "unable to start notifier")
for _, walletDriver := range lnwallet.RegisteredWallets() {
for _, backEnd := range walletDriver.BackEnds() {Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.