I am writing a system that uses the Bitcoin Core JSON-RPC API.
Use case: user wants to withdraw some BTC to his BTC address.
My application handles this request by the following logic:
- Check if user balance is OK.
- Call Bitcoin API method 'sendfrom'. In return I get the transaction ID.
- Store the 'Wihdraw' informations with the transaction ID in the database.
Now I want to make this error-safe, for example let's suppose that between points 2. and 3. the server crashes. Th 'Withdraw' info hasn't been saved to the DB. Next time user wants to withdraw some BTC it will allow they.
How do I make this error-safe? I have read that you can store a 'comment' in the Bitcoin Core, so an idea was to first create a unique comment, and store it in database with the withdraw address and amount, and set it status to UNDONE. Then submit this transaction to the Bitcoin Network and then set the status to DONE. In this way I can check if there are any UNDONE transactions in the DB and if so, verify if in my Bitcoin Core wallet there are no transactions with given comment. If there are not such transaction try to resubmit them. The problem with this approach is, that to get a transaction with a given message is to loop through all transactions.
Is there any better way to make the Bitcoin-Transaction transaction-safe?
EDIT: Best would be not to store any transaction info in database, but use the Bitcoin Core database itself. Maybe this way? But only way is to store transaction details in comment. Maybe use it to store deserialized objects?