diff options
| author | Adam Soltys <[email protected]> | 2024-10-16 15:33:22 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-10-16 15:33:22 -0700 |
| commit | e0ea06df4572c5183c5a4fd1697ca59c2614672c (patch) | |
| tree | bd42b8194aa3485964beaef5ec2f8708e506503a /lib | |
| parent | 4501538630af9d6026fe5661da0386d1b5d65008 (diff) | |
| download | coinos-server-e0ea06df4572c5183c5a4fd1697ca59c2614672c.tar.gz coinos-server-e0ea06df4572c5183c5a4fd1697ca59c2614672c.zip | |
atomic payment reversal
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/db.ts | 37 | ||||
| -rw-r--r-- | lib/locations.ts | 2 | ||||
| -rw-r--r-- | lib/payments.ts | 18 |
3 files changed, 44 insertions, 13 deletions
@@ -1,9 +1,9 @@ import config from "$config"; -import { fail, wait, sleep } from "$lib/utils"; +import { fail, sleep } from "$lib/utils"; import { createClient, defineScript } from "redis"; -import { warn, l, err } from "$lib/logging"; +import { warn, err } from "$lib/logging"; -let SCRIPT = ` +let DEBIT = ` local balanceKey = KEYS[1] local creditKey = KEYS[2] local amount = tonumber(ARGV[1]) @@ -27,15 +27,42 @@ redis.call('decrby', balanceKey, tostring(math.floor(amount + tip + fee + ourfee return ourfee `; +let REVERSE = ` +local paymentKey = KEYS[1] +local balanceKey = KEYS[2] +local creditKey = KEYS[3] +local hashKey = KEYS[4] + +local total = tonumber(ARGV[1]) +local credit = tonumber(ARGV[2]) + +local payment = cjson.decode(redis.call('get', paymentKey)) + +if payment ~= nil then + redis.call('incrby', balanceKey, total) + redis.call('incrby', creditKey, credit) + redis.call('del', paymentKey) + redis.call('del', hashKey) +end + +return paymentKey +`; + let debit = defineScript({ NUMBER_OF_KEYS: 2, - SCRIPT, + SCRIPT: DEBIT, + transformArguments: (...args) => args.map((a) => a.toString()), +}); + +let reverse = defineScript({ + NUMBER_OF_KEYS: 4, + SCRIPT: REVERSE, transformArguments: (...args) => args.map((a) => a.toString()), }); export let db = createClient({ url: config.db, - scripts: { debit }, + scripts: { debit, reverse }, socket: { reconnectStrategy: (retries) => Math.min(retries * 50, 5000), }, diff --git a/lib/locations.ts b/lib/locations.ts index 4122c25a..560d5259 100644 --- a/lib/locations.ts +++ b/lib/locations.ts @@ -8,7 +8,7 @@ export let getLocations = async () => { let previous = await g("locations"); let since = await g("locations:since"); if (!since) since = "2022-09-19T00:00:00Z"; - if (Date.now() - new Date(since).getTime() < 60000) return; + if (Date.now() - new Date(since).getTime() < 60000) return; let locations: Array<any> = await got( `https://api.btcmap.org/v2/elements?updated_since=${since}`, diff --git a/lib/payments.ts b/lib/payments.ts index a073bec7..1f1497ec 100644 --- a/lib/payments.ts +++ b/lib/payments.ts @@ -809,14 +809,18 @@ let finalize = async (r, p) => { }; let reverse = async (p) => { - await db.del(`payment:${p.hash}`); - await db.del(`payment:${p.id}`); - - let total = Math.abs(p.amount); + let total = Math.abs(p.amount) + p.fee; let ourfee = p.ourfee || 0; let credit = Math.round(total * config.fee) - ourfee; - await db.incrBy(`balance:${p.uid}`, total + p.fee + ourfee); - await db.incrBy(`credit:${types.lightning}:${p.uid}`, credit); - await db.lRem(`${p.uid}:payments`, 1, p.id); + let k = await db.reverse( + `payment:${p.id}`, + `balance:${p.uid}`, + `credit:${types.lightning}:${p.uid}`, + `payment:${p.hash}`, + total, + credit, + ); + + warn("reversed", k); }; |
