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
- Read the report: write down the expected behaviour and the actual one in a sentence each.
- Reproduce it: run the visible tests and find the failing check that matches the report.
- 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.
- Follow the data through the query or the repository code; compare the rows it returns with the rows the report expects.
- 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 satThe 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.