Bug hunt

Half a Transfer

The Guild's pay function moves coins between vaults. The vaults table has CHECK (coins >= 0), so nobody can go below zero.

The bug report

  • "Bram tried to pay me 50 coins with only 10 in his vault. pay said False, but I got the 50 coins anyway!", Wren
  • "Someone paid from a vault called 'Brahm' (typo). The coins appeared in my vault from nowhere.". Pip

The first UPDATE runs, then the second one fails or matches nothing, and the first change stays.

Assignment

Fix pay(conn, payer, payee, coins) so it is all or nothing:

  1. On success, both vaults change, the change is committed, and pay returns True.
  2. If the payer can't afford it, or either vault doesn't exist, nothing changes and pay returns False.

Use a with conn: block and check rowcount.