From dec12454289461e6851b3b7328b5a80f56ce4a98 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 22 Aug 2014 23:32:42 -0700 Subject: Log txids and prevent duplicates --- app.js | 4 ++-- public/js/calculator.coffee | 19 +++++++++++++------ routes/transactions.js | 41 ++++++++++++++++++++++++++++++----------- src/app.coffee | 4 ++-- src/routes/transactions.coffee | 36 +++++++++++++++++++++++++++--------- views/users/show.html | 26 ++++++++++++-------------- 6 files changed, 86 insertions(+), 44 deletions(-) diff --git a/app.js b/app.js index c739c325..48bd8092 100644 --- a/app.js +++ b/app.js @@ -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 @@
- - + +
@@ -32,18 +32,16 @@ +
-
- -
-

Please send 0.0 to

-
-

-
- - Payment Received. Thank you! -