teamsempo/SempoBlockchain

Transfer account balance updates need guaranteed idempotency

Closed this issue · 0 comments

Describe the bug
In some (unidentified cases), transfer.resolve_as_completed is called more than once.

This method is what is used to update the balances of the sender and recipient account, which is currently done by incrementing/decrementing. While an exception SHOULD be thrown if it's called more than once, this doesn't always happen, meaning the balances get updated incorrectly.

We haven't identified when this occurs because the error rate is about 1 in 20k transfers and we've only had a few cases of it.

Suggested fix: We should swap the update balance function from being a simple "increment/decrement" to recalculating balances as sum(transfers in) - sum(transfers out)** so that it can be called as much as we want without the balance being incorrect. This may be best as an sql query to keep it efficient

**approved transfers only

To Reproduce
Can't reproduce :(

Expected behavior
Balances should always represent the credit transfer history