All writing

Engineering & debugging / 5 min read

What Twenty Millisatoshis Taught Me About Tests

A small fee mismatch, an unexpectedly busy queue, and a failed write that changed storage made me examine what my tests actually proved.

I expected 8,600 millisatoshis. The test showed 8,620.

A millisatoshi is one thousandth of a satoshi. The difference was tiny, but it was enough to make me stop. I was introducing realistic fees for dummy hops in blinded Lightning payment paths, and the test’s expected amount no longer matched what the payment actually deducted.

I could think of several possible explanations: rounding, an ordering mistake, a fee applied to the wrong amount. The small size of the discrepancy gave me a direction to investigate. It did not tell me which side of the assertion was wrong.

Over the following months, other failures brought me back to that distinction. Sometimes the expected result assumed too much. Sometimes the fixture described an impossible situation. Once, the machinery simulating a failed write had already changed the stored bytes.

Each case required me to inspect how the test knew what it claimed to know.

The assumption inside an equality

A blinded payment path advertises aggregated fee information. The sender needs enough information to fund the route without receiving a public breakdown of every hidden hop. During execution, the individual hops apply their own relay rules.

Those two views of the payment have to fit together, but exact equality between an aggregate calculation and a sum of deductions needs justification.

When I investigated the January mismatch, I looked at the fee values used by the existing tests. They used finite base fees with zero proportional fees. That simplified the relationship being asserted. Adding nonzero proportional fees put pressure on an assumption that had previously been easy to overlook.

It helped to write down what the test was comparing. One quantity came from the path’s aggregate parameters. The other came from executing the per-hop rules. Their relationship depended on the aggregation and rounding behavior, not simply on whether both were called fees.

My early entry treated that discovery as the breakthrough. Reading the later entries gives a more complete account. In July, another test still needed to account for a twenty-millisatoshi conservative aggregation gap: its expected fee was thirty, while the constructed total was fifty.

These entries describe related work at different stages. They do not establish that every twenty-millisatoshi difference has the same cause. They do show why the first satisfying explanation should remain open to later evidence.

For these tests, I needed an expected result derived from the actual fee model, including its conservative behavior. An equality carried over from a simpler setup was insufficient evidence on its own.

An empty queue was too broad a promise

Later, while working on refreshing static invoices after channel changes, I encountered a different kind of failure.

The intended behavior was specific: a peer update should mark a refresh as pending, and the timer should send the coalesced update later. A test checked that the onion-message queue was empty before that timer tick.

That looked like a convenient way to prove no refresh had been sent early. But the setup could legitimately leave unrelated messages in the queue. An empty queue was a stronger condition than the feature promised.

Simply draining the queue would have hidden the evidence I cared about. An incorrectly early refresh could disappear along with harmless setup traffic.

The more useful helper inspected the pending messages and allowed only the unrelated offer-path requests expected from setup. A static-invoice refresh arriving before the timer still failed the test.

That change made the assertion more precise. It tolerated the activity outside the contract while remaining strict about the behavior under investigation.

The distinction became easier to see once I could state the promise in a sentence: do not send this particular update inline; send it on the timer.

A failure that had already succeeded

In October, a recurrence recovery test rebuilt a node from saved bytes after an injected persistence failure.

The run finished with 173 passing tests and one failure. Following the remaining failure led into the test adapter itself.

The adapter called InMemoryStore::write before checking whether it should inject an error. That call changed the store’s bytes immediately. Awaiting the returned future happened later, but the mutation had already taken place.

The adapter then reported a failed write.

After restart, the node could read bytes that the supposedly failed operation had written. The test had created a different storage condition from the one I intended to exercise.

The correction moved the underlying write after the failure check. I kept the recovery assertions. At the point recorded in that entry, compilation passed after the correction, while a fresh test run was still pending. The adapter flaw had been identified; that did not yet establish a passing recovery result.

This case stayed with me because the return value alone would have looked convincing. Checking saved state after restart made the discrepancy visible.

Give the expected result a reason

These investigations involved different systems and different mistakes. Together, they changed the questions I ask when a test fails.

I start with the behavior the test is supposed to establish. Then I trace how the fixture creates that situation, how the expected result is derived, and what the assertion actually observes.

The fee mismatch required examining the mathematical relationship behind an expected amount. The queue test required narrowing an assertion to the promised timing behavior. The recovery test required checking whether failure injection left storage in the state it claimed to leave behind.

That work can feel slower than changing a number or adjusting a helper. But it leaves something useful after the immediate failure is gone: a clearer reason to trust the next result.

I still like the opening numbers. Eight thousand six hundred and eight thousand six hundred and twenty. Such a small difference, carrying enough information to question a much larger assumption.


Source notes: this article combines Code Chronicle entries 315, 317, 425, 427, and 485. They document separate investigations; the article brings them together around the assumptions in test expectations and test machinery.

Another thread to follow

The Work Between the Milestones

Questions or a different perspective? Write to me.