diff options
| author | Adam Soltys <[email protected]> | 2014-08-22 23:32:42 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2014-08-22 23:32:42 -0700 |
| commit | dec12454289461e6851b3b7328b5a80f56ce4a98 (patch) | |
| tree | 394b18ad2fe0d875a689e8acee4d918397845300 | |
| parent | 11390ded587c17ca06535c0826f2d2da94417d93 (diff) | |
| download | coinos-server-dec12454289461e6851b3b7328b5a80f56ce4a98.tar.gz coinos-server-dec12454289461e6851b3b7328b5a80f56ce4a98.zip | |
Log txids and prevent duplicates
| -rw-r--r-- | app.js | 4 | ||||
| -rw-r--r-- | public/js/calculator.coffee | 19 | ||||
| -rw-r--r-- | routes/transactions.js | 41 | ||||
| -rw-r--r-- | src/app.coffee | 4 | ||||
| -rw-r--r-- | src/routes/transactions.coffee | 36 | ||||
| -rw-r--r-- | views/users/show.html | 26 |
6 files changed, 86 insertions, 44 deletions
@@ -128,13 +128,13 @@ app.get('/:user/transactions.json', authorize, transactions.json); - app.post('/:user/transactions', authorize, transactions.create); + app.post('/:user/transactions', transactions.create); app.get('/:user/report', cache, authorize, transactions.index); app.get('/:user.json', authorize, users.json); - app.get('/:user', cache, authorize, users.show); + app.get('/:user', cache, users.show); app.use(require('connect-assets')({ src: 'public' diff --git a/public/js/calculator.coffee b/public/js/calculator.coffee index c90954ab..e1e97cef 100644 --- a/public/js/calculator.coffee +++ b/public/js/calculator.coffee @@ -118,15 +118,18 @@ setupSocket = -> g.blockchain.onmessage = (e) -> results = eval('(' + e.data + ')') - from_address = '' - received = 0 + amount_received = 0 + txid = results.x.hash + + return if txid == g.last + g.last = txid $.each(results.x.out, (i, v) -> if (v.addr == g.address) - received += v.value / 100000000 + amount_received += v.value / 100000000 ) - if $('#received').is(":hidden") and g.amount_requested <= received + if $('#received').is(":hidden") and g.amount_requested <= amount_received $('#amount').blur() $('#payment').hide() $('#received').fadeIn('slow') @@ -134,9 +137,9 @@ setupSocket = -> if g.user $.post("/#{g.user}/transactions", - address: from_address, + txid: txid, date: moment().format("YYYY-MM-DD HH:mm:ss"), - received: received, + received: amount_received, exchange: g.exchange ) @@ -158,6 +161,10 @@ setupSocket = -> g.btcd.onmessage = (e) -> data = JSON.parse(e.data) + txid = data.result.txid + return if txid == g.last + g.last = txid + amount_received = 0 if data.result diff --git a/routes/transactions.js b/routes/transactions.js index 25d6fc14..83ec441f 100644 --- a/routes/transactions.js +++ b/routes/transactions.js @@ -11,29 +11,48 @@ 'transactions': [] }; return db.lrange("" + user + ":transactions", 0, -1, function(err, transactions) { - var i, process; - process = function(err, t) { + var cb, i, txid; + cb = function(err, t) { + var txid; r.transactions.push(t); if (i >= transactions.length) { res.write(JSON.stringify(r)); return res.end(); } else { - return db.hgetall("" + user + ":transactions:" + transactions[i++], process); + txid = transactions[i++]; + if (parseInt(txid)) { + txid = user + ":transactions:" + txid; + } + return db.hgetall(txid, cb); } }; i = 0; - return db.hgetall("" + user + ":transactions:" + transactions[i++], process); + txid = transactions[i++]; + if (parseInt(txid)) { + txid = user + ":transactions:" + txid; + } + return db.hgetall(txid, cb); }); }, create: function(req, res) { - var user; + var finish, user; user = req.params.user; - return db.incr('transactions', function(err, id) { - return db.hmset("" + user + ":transactions:" + id, req.body, function() { - return db.rpush("" + user + ":transactions", id, function() { - res.write(JSON.stringify(req.body)); - return res.end(); - }); + finish = function() { + res.write(JSON.stringify(req.body)); + return res.end(); + }; + db.watch(req.body.txid); + return db.exists(req.body.txid, function(err, result) { + var multi; + if (result) { + finish(); + return; + } + multi = db.multi(); + multi.hmset(req.body.txid, req.body); + multi.rpush("" + user + ":transactions", req.body.txid); + return multi.exec(function(err, replies) { + return finish(); }); }); }, diff --git a/src/app.coffee b/src/app.coffee index 361a9a56..66ce32a5 100644 --- a/src/app.coffee +++ b/src/app.coffee @@ -73,11 +73,11 @@ app.get('/:user/edit', cache, authorize, users.edit) app.post('/:user', authorize, users.update) app.get('/:user/transactions.json', authorize, transactions.json) -app.post('/:user/transactions', authorize, transactions.create) +app.post('/:user/transactions', transactions.create) app.get('/:user/report', cache, authorize, transactions.index) app.get('/:user.json', authorize, users.json) -app.get('/:user', cache, authorize, users.show) +app.get('/:user', cache, users.show) app.use(require('connect-assets')(src: 'public')) diff --git a/src/routes/transactions.coffee b/src/routes/transactions.coffee index b4653362..06636a15 100644 --- a/src/routes/transactions.coffee +++ b/src/routes/transactions.coffee @@ -6,27 +6,45 @@ module.exports = r = 'transactions': [] db.lrange("#{user}:transactions", 0, -1, (err, transactions) -> - process = (err, t) -> + cb = (err, t) -> r.transactions.push t if i >= transactions.length res.write(JSON.stringify(r)) res.end() else - db.hgetall("#{user}:transactions:#{transactions[i++]}", process) + txid = transactions[i++] + if parseInt(txid) + txid = user + ":transactions:" + txid + + db.hgetall(txid, cb) i = 0 - db.hgetall("#{user}:transactions:#{transactions[i++]}", process) + + txid = transactions[i++] + if parseInt(txid) + txid = user + ":transactions:" + txid + + db.hgetall(txid, cb) ) create: (req, res) -> user = req.params.user - db.incr('transactions', (err, id) -> - db.hmset("#{user}:transactions:#{id}", req.body, -> - db.rpush("#{user}:transactions", id, -> - res.write(JSON.stringify(req.body)) - res.end() - ) + finish = -> + res.write(JSON.stringify(req.body)) + res.end() + + db.watch(req.body.txid) + db.exists(req.body.txid, (err, result) -> + if result + finish() + return + + multi = db.multi() + multi.hmset(req.body.txid, req.body) + multi.rpush("#{user}:transactions", req.body.txid) + multi.exec((err, replies) -> + finish() ) ) diff --git a/views/users/show.html b/views/users/show.html index 328353b6..cdcdb2a3 100644 --- a/views/users/show.html +++ b/views/users/show.html @@ -10,8 +10,8 @@ <form role="form"> <div class="form-group"> - <label for="exchange">Exchange Rate:</label> - <input type="text" id="exchange" value="0" disabled="disabled" class="form-control" /> + <label for="exchange">Rate:</label> + <input type="text" id="exchange" value="0" disabled class="form-control" /> <span id="symbol"></span> </div> @@ -32,18 +32,16 @@ </div> </div> </div> + </form> - <br /> - - <div id="payment"> - <p>Please send <strong id="total">0.0</strong> <span id="unit"></span> to</p> - <a href="/{{user}}/report"><div id="qr"></div></a> - <p id="address"></p> - </div> - - <span id="received" class="btn btn-success">Payment Received. Thank you!</span> - <audio id="chaching" src="/cash_register.wav" hidden="true" enablejavascript="true"> + <div id="payment"> + <p>Please send <strong id="total">0.0</strong> <span id="unit"></span> to</p> + <a href="/{{user}}/report"><div id="qr"></div></a> + <p id="address"></p> </div> -</form> -<span id="error" class="btn btn-danger" style="display: none"></span> + <span id="received" class="btn btn-success">Payment Received. Thank you!</span> + <audio id="chaching" src="/cash_register.wav" hidden="true" enablejavascript="true"> + <span id="error" class="btn btn-danger" style="display: none"></span> +</div> + |
