aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2021-01-13 14:21:47 -0800
committerAdam Soltys <[email protected]>2021-01-13 14:21:47 -0800
commiteb860d78b836d810f7c29d5587e17321adf20bef (patch)
tree437304e1ba62261af52146663a1ffe17339230c2
parent8afa3af7f790bd1d78b2105e7541eed462fbb825 (diff)
downloadcoinos-server-eb860d78b836d810f7c29d5587e17321adf20bef.tar.gz
coinos-server-eb860d78b836d810f7c29d5587e17321adf20bef.zip
add some more transaction locks
-rw-r--r--lib/account.js32
-rw-r--r--routes/send.js58
-rw-r--r--routes/users.js8
3 files changed, 61 insertions, 37 deletions
diff --git a/lib/account.js b/lib/account.js
index 931638e0..5a8c3924 100644
--- a/lib/account.js
+++ b/lib/account.js
@@ -1,16 +1,15 @@
const axios = require("axios");
module.exports = async (asset, user, transaction) => {
- const account = await db.Account.findOne(
- {
- where: {
- user_id: user.id,
- asset,
- pubkey: null
- }
+ const account = await db.Account.findOne({
+ where: {
+ user_id: user.id,
+ asset,
+ pubkey: null
},
- { transaction }
- );
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
if (account) return account;
@@ -28,13 +27,16 @@ module.exports = async (asset, user, transaction) => {
if (assets[asset]) {
({ domain, ticker, precision, name } = assets[asset]);
} else {
- const existing = await db.Account.findOne({
- where: {
- asset
+ const existing = await db.Account.findOne(
+ {
+ where: {
+ asset
+ },
+ order: [["id", "ASC"]],
+ limit: 1
},
- order: [["id", "ASC"]],
- limit: 1,
- }, { transaction });
+ { transaction }
+ );
if (existing) {
({ domain, ticker, precision, name } = existing);
diff --git a/routes/send.js b/routes/send.js
index 790cfff2..c7c3c30f 100644
--- a/routes/send.js
+++ b/routes/send.js
@@ -13,7 +13,15 @@ module.exports = ah(async (req, res, next) => {
try {
await db.transaction(async transaction => {
- let { account } = user;
+ let account = await db.Account.findOne({
+ where: {
+ user_id: user.id,
+ asset: config.liquid.btcasset,
+ pubkey: null
+ },
+ lock: transaction.LOCK.UPDATE,
+ transaction
+ });
if (account.balance < amount) {
throw new Error("Insufficient funds");
@@ -54,14 +62,16 @@ module.exports = ah(async (req, res, next) => {
emit(user.username, "user", user);
if (username) {
- let recipient = await db.User.findOne({
- where: { username },
- include: {
- model: db.Account,
- as: "account"
- }
- },
- { transaction });
+ let recipient = await db.User.findOne(
+ {
+ where: { username },
+ include: {
+ model: db.Account,
+ as: "account"
+ }
+ },
+ { transaction }
+ );
let a2;
let acc = {
@@ -73,9 +83,12 @@ module.exports = ah(async (req, res, next) => {
if (recipient.account.asset === asset && !recipient.account.pubkey)
({ account: a2 } = recipient);
else {
- a2 = await db.Account.findOne({
- where: acc,
- }, { transaction });
+ a2 = await db.Account.findOne(
+ {
+ where: acc
+ },
+ { transaction }
+ );
}
if (a2) {
@@ -87,18 +100,21 @@ module.exports = ah(async (req, res, next) => {
let ticker = asset.substr(0, 3).toUpperCase();
let precision = 8;
- const assets = app.get('assets');
+ const assets = app.get("assets");
if (assets[asset]) {
({ domain, ticker, precision, name } = assets[asset]);
} else {
- const existing = await db.Account.findOne({
- where: {
- asset
+ const existing = await db.Account.findOne(
+ {
+ where: {
+ asset
+ },
+ order: [["id", "ASC"]],
+ limit: 1
},
- order: [["id", "ASC"]],
- limit: 1
- }, { transaction });
+ { transaction }
+ );
if (existing) {
({ domain, ticker, precision, name } = existing);
@@ -108,7 +124,7 @@ module.exports = ah(async (req, res, next) => {
acc = { ...acc, ...{ domain, ticker, precision, name } };
acc.balance = amount;
acc.pending = 0;
- acc.network = 'liquid';
+ acc.network = "liquid";
a2 = await db.Account.create(acc, { transaction });
}
@@ -143,7 +159,7 @@ module.exports = ah(async (req, res, next) => {
"problem sending internal payment",
user.username,
user.balance,
- e.message,
+ e.message
);
return res.status(500).send(e.message);
}
diff --git a/routes/users.js b/routes/users.js
index d6199cb7..e7aec50e 100644
--- a/routes/users.js
+++ b/routes/users.js
@@ -507,6 +507,12 @@ app.post(
} = req.body;
try {
+ let account = await db.Account.findOne({
+ where: { id, user_id: user.id }
+ });
+
+ if (!account || (account.pubkey && !pubkey)) throw new Error("Problem updating account");
+
await db.Account.update(
{
name,
@@ -525,7 +531,7 @@ app.post(
}
);
- const account = await db.Account.findOne({
+ account = await db.Account.findOne({
where: { id }
});