Satoshis, Not Doubles

Medium

· Database

Incident

SEV-2

BUG-1SAT

Priority: High · reported by reconciliation

Time to fix: 20 min

Failing checks on the shipped build

  • the classic drift case is exact

    expected 29000000 but got 28999999

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the data through the query or the repository code; compare the rows it returns with the rows the report expects.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Inspired by

Crypto exchanges

Inspired by the precision bugs that plague crypto exchanges: parse "0.29" of a coin through a double, multiply by 10^8, truncate — and you just destroyed one satoshi. Multiply that by a million trades a day.

AmountParser.java converts a decimal string into satoshis (8 decimal places). It must use integer math end to end.

Code map

class AmountParser {
    // Parses a decimal coin amount (max 8 dp) into satoshis.
    static long parseToSatoshis(String amount) {
        double value = Double.parseDouble(amount);
        return (long) (value * 100000000);
    }
}

Sign in to edit the file, run the tests and submit a fix

A free account — no card, no trial clock. You come straight back here.

Satoshis, Not Doubles — Java Bug Hunt

Inspired by the precision bugs that plague crypto exchanges: parse "0.29" of a coin through a double, multiply by 10^8, truncate — and you just destroyed…

  • Language: Java
  • Layer: Database
  • Difficulty: Medium
  • Concepts: Floating Point, Money
  • Modelled on: Crypto exchanges
  • Visible tests: the classic drift case is exact; whole coins are exact; full 8-decimal precision survives
  • Reward: 50 XP for a complete fix

Incident

  • Severity: SEV-2 (priority: High)
  • Ticket: BUG-1SAT
  • Reported by: reconciliation
  • Time to fix: 20 minutes, the target for a medium incident

Failing checks on the shipped build

  • the classic drift case is exact — expected 29000000 but got 28999999

How to work this incident

  1. Read the report: write down the expected behaviour and the actual one in a sentence each.
  2. Reproduce it: run the visible tests and find the failing check that matches the report.
  3. Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
  4. Follow the data through the query or the repository code; compare the rows it returns with the rows the report expects.
  5. Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.

Briefing

Inspired by the precision bugs that plague crypto exchanges: parse "0.29" of a coin through a double, multiply by 10^8, truncate — and you just destroyed one satoshi. Multiply that by a million trades a day.

AmountParser.java converts a decimal string into satoshis (8 decimal places). It must use integer math end to end.

Bug report

BUG-1SAT · Priority: High · Reported by: reconciliation

parseToSatoshis(amount) — up to 8 decimal places:

  • "0.29" -> 29000000
  • "1" -> 100000000
  • "123.45678901"-> 12345678901

Observed: "0.29" parses to 28999999. The books never balance.

Logs

[ledger] daily reconciliation drift: -412 sat

The code as shipped

AmountParser.java (editable)

class AmountParser {
    // Parses a decimal coin amount (max 8 dp) into satoshis.
    static long parseToSatoshis(String amount) {
        double value = Double.parseDouble(amount);
        return (long) (value * 100000000);
    }
}

Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More Java bug hunts.