5

I'm following this guide to build a stellar exchange. https://www.stellar.org/developers/guides/exchange.html

It's nothing too serious, I'm mostly just doing it to have some fun and learn more about stellar because I love the technology. I have deposits working great, but I have a question about submitting withdrawals to the stellar network.

According to the guide, when a user submits a withdrawal to my exchange, I deduct the amount from their balance and create a row in my database to indicate a withdrawal of state 'pending'. Then, I have a background job running that will query for all withdrawals that are pending and create a stellar transaction for them and submit it to the network. Finally, mark the database row as ‘done’.

My problem is that what happens if I successfully submit the transaction to the stellar network but something happens (network error, server crashes, etc etc) and the database never updates the row to be ‘done’. When the server restarts, the background job would see the same row as being ‘pending’ still and resubmit the transaction, effectively double withdrawing from my account.

How do most exchanges guard against this?

3

I have no idea what exchanges do, but I'll happily riff some starting points.

One solution might be to differentiate between pending transactions you retry at startup, versus during normal operation. If it's the first sweep of pending transactions since application startup, you might check Stellar's operation for account against the expected operations to determine if there is an unprocessed delta.

A different, complementary solution could be to record the next sequence id against the pending transactions in your DB. In this way, you will know if the transaction has been submitted or not simply by checking the pending sequence against the account's current sequence counter.

  • Agree with @Synesso. In general, that's a non-trivial problem because you cannot guarantee the "execute only once" behavior when dealing with third-party systems. But in this particular case you can store account sequence in the DB just before submitting a transaction and compare with current sequence after service reboot. – Orbit Lens Mar 5 '18 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.