share the lock for transactions via external file. Do all possible checks before acquiring the lock

This commit is contained in:
Ulf Gebhardt 2022-12-15 13:05:21 +01:00
parent f741986b2b
commit c2c7e34539
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9

View File

@ -36,10 +36,7 @@ import { BalanceResolver } from './BalanceResolver'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from './const/const'
import { findUserByEmail } from './UserResolver'
import { Semaphore } from 'await-semaphore'
const CONCURRENT_TRANSACTIONS = 1
const LOCK_TRANSACTIONS = new Semaphore(CONCURRENT_TRANSACTIONS)
import { TRANSACTIONS_LOCK } from '@/util/TRANSACTIONS_LOCK'
export const executeTransaction = async (
amount: Decimal,
@ -52,24 +49,25 @@ export const executeTransaction = async (
`executeTransaction(amount=${amount}, memo=${memo}, sender=${sender}, recipient=${recipient})...`,
)
if (sender.id === recipient.id) {
logger.error(`Sender and Recipient are the same.`)
throw new Error('Sender and Recipient are the same.')
}
if (memo.length > MEMO_MAX_CHARS) {
logger.error(`memo text is too long: memo.length=${memo.length} > ${MEMO_MAX_CHARS}`)
throw new Error(`memo text is too long (${MEMO_MAX_CHARS} characters maximum)`)
}
if (memo.length < MEMO_MIN_CHARS) {
logger.error(`memo text is too short: memo.length=${memo.length} < ${MEMO_MIN_CHARS}`)
throw new Error(`memo text is too short (${MEMO_MIN_CHARS} characters minimum)`)
}
// acquire lock
const releaseLock = await LOCK_TRANSACTIONS.acquire()
const releaseLock = await TRANSACTIONS_LOCK.acquire()
try {
if (sender.id === recipient.id) {
logger.error(`Sender and Recipient are the same.`)
throw new Error('Sender and Recipient are the same.')
}
if (memo.length > MEMO_MAX_CHARS) {
logger.error(`memo text is too long: memo.length=${memo.length} > ${MEMO_MAX_CHARS}`)
throw new Error(`memo text is too long (${MEMO_MAX_CHARS} characters maximum)`)
}
if (memo.length < MEMO_MIN_CHARS) {
logger.error(`memo text is too short: memo.length=${memo.length} < ${MEMO_MIN_CHARS}`)
throw new Error(`memo text is too short (${MEMO_MIN_CHARS} characters minimum)`)
}
// validate amount
const receivedCallDate = new Date()
const sendBalance = await calculateBalance(