aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2019-09-09 23:27:57 -0700
committerAdam Soltys <[email protected]>2019-09-09 23:27:57 -0700
commite2658f928b71c825f044a6a3808349fec5e48560 (patch)
treea882826c5b9ac8d0df8179b57fa64e82f0a15eef
parent6442678745c7d1c3e89e6ce277c73dc5a6cab348 (diff)
downloadcoinos-server-e2658f928b71c825f044a6a3808349fec5e48560.tar.gz
coinos-server-e2658f928b71c825f044a6a3808349fec5e48560.zip
clear out pending payments properly
-rw-r--r--index.js2
-rw-r--r--sockets.js1
-rw-r--r--zmq.js25
3 files changed, 17 insertions, 11 deletions
diff --git a/index.js b/index.js
index 00b0dad1..b21957ed 100644
--- a/index.js
+++ b/index.js
@@ -102,7 +102,7 @@ const authy = new Client({ key: config.authy.key });
attributes: ["hash"]
})).map(p => p.hash);
- require("./zmq")(bc, db, addresses, payments);
+ require("./zmq")(app, bc, db, addresses, payments, emit);
app.post("/register", async (req, res) => {
let err = m => res.status(500).send(m);
diff --git a/sockets.js b/sockets.js
index a26143b8..3f95816b 100644
--- a/sockets.js
+++ b/sockets.js
@@ -1,5 +1,6 @@
import jwt from "jsonwebtoken";
import io from "socket.io";
+import whitelist from "./whitelist";
/* eslint-disable-next-line */
const pick = (O, ...K) => K.reduce((o, k) => ((o[k] = O[k]), o), {});
diff --git a/zmq.js b/zmq.js
index ebb5804d..8e198769 100644
--- a/zmq.js
+++ b/zmq.js
@@ -1,6 +1,7 @@
import reverse from "buffer-reverse";
import zmq from "zeromq";
import config from "./config";
+import Sequelize from "sequelize";
const bitcoin = require("bitcoinjs-lib");
const l = console.log;
@@ -13,7 +14,7 @@ const zmqRawTx = zmq.socket("sub");
zmqRawTx.connect(config.bitcoin.zmqrawtx);
zmqRawTx.subscribe("rawtx");
-module.exports = (bc, db, addresses, payments) => {
+module.exports = (app, bc, db, addresses, payments, emit) => {
zmqRawTx.on("message", (topic, message, sequence) => {
message = message.toString("hex");
@@ -36,10 +37,8 @@ module.exports = (bc, db, addresses, payments) => {
bitcoin.networks[network]
);
} catch (e) {
- l(e);
return;
}
- l(o);
if (Object.keys(addresses).includes(address)) {
payments.push(hash);
@@ -88,6 +87,7 @@ module.exports = (bc, db, addresses, payments) => {
confirmed
});
+ l("tx", message);
emit(user.username, "tx", message);
}
})
@@ -103,22 +103,27 @@ module.exports = (bc, db, addresses, payments) => {
let block = bitcoin.Block.fromHex(message);
l("block", block.getHash().toString("hex"));
- await db.Payment.findAll({
- include: { model: db.User, as: "user" },
- where: { confirmed: false }
- }).map(async p => {
+ const processPayment = async p => {
if ((await bc.getRawTransaction(p.hash, true)).confirmations > 0) {
- let user = p.user;
+ let user = await db.User.findByPk(p.user_id);
+ l(user.pending);
p.confirmed = true;
user.balance += p.amount;
user.pending -= p.amount;
await user.save();
await p.save();
emit(user.username, "user", user);
+ emit(user.username, "block", message);
}
- });
+ };
+
+ await db.Payment.findAll({
+ where: { confirmed: false }
+ }).reduce(async (prev, payment) => {
+ const result = await prev;
+ return [...result, await processPayment(payment)];
+ }, Promise.resolve([]));
- socket.emit("block", message);
break;
}
}