Question

Currently I'm working on a virtual currency that can do transactions. The problem I have is that I do not want multiple transactions to one user to be executing at the same time.
Here is an example: Tom and Bill both try to send $2 to John at the same time. Instead of executing the transact() function at the same time I would my system to first do complete the transaction for Tom and then to move to Billy and so on.

How could I setup a system like this?

EDIT: Since my code is going to be opensource anyway here is an example of what I have done currently:

//Step 0 - Transaction setup.
    var transaction = {
       "id": hexit(Math.random().toString().replace(".", "") +
           from + to + new Date().getTime() +
           Math.random().toString().replace(".", "")),
           "from": from,
           "to": to,
           "amount": amount,
           "time": new Date().getTime(), 
           "coins": [],
           "message": validator.escape(message)
    };

    //LCF.getuser returns the data for the 'coins' field.
    var from = LCF.getuser(from, ["coins"]);

    //Amount is the amount of coins that the user wants to transact
    if(from.coins.length <= amount) {
       while(transaction.coins.length < amount) {
           collection.update(
               { "username" : from }, 
               { "$pop": { "coins": 1 } } 
           );
       }
       (...)

I need to get the coins updated from $pop and while I could try and get the difference from the latest document and the previously retrieved document if another transaction occurs while it is updating then I can't know that coins that had been $popped from the users wallet.

Was it helpful?

Solution

I tried to use semaphore but I couldn't figure out how I would apply it.
I moved to postgreSQL and that solved all my problems because I can now use TRANSACTIONS (ROLLBACK, COMMIT, etc..).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top