use basic coordinates
This commit is contained in:
parent
16e90ebe4e
commit
9c1e980bd3
1 changed files with 64 additions and 93 deletions
|
|
@ -2,7 +2,6 @@
|
||||||
* @format
|
* @format
|
||||||
*/
|
*/
|
||||||
// @ts-check
|
// @ts-check
|
||||||
const { performance } = require('perf_hooks')
|
|
||||||
const logger = require('winston')
|
const logger = require('winston')
|
||||||
const isFinite = require('lodash/isFinite')
|
const isFinite = require('lodash/isFinite')
|
||||||
const isNumber = require('lodash/isNumber')
|
const isNumber = require('lodash/isNumber')
|
||||||
|
|
@ -14,7 +13,11 @@ const {
|
||||||
} = Common
|
} = Common
|
||||||
|
|
||||||
const LightningServices = require('../../../../utils/lightningServices')
|
const LightningServices = require('../../../../utils/lightningServices')
|
||||||
|
const {
|
||||||
|
addInvoice,
|
||||||
|
myLNDPub
|
||||||
|
} = require('../../../../utils/lightningServices/v2')
|
||||||
|
const { writeCoordinate } = require('../../../coordinates')
|
||||||
const Key = require('../key')
|
const Key = require('../key')
|
||||||
const Utils = require('../utils')
|
const Utils = require('../utils')
|
||||||
|
|
||||||
|
|
@ -56,28 +59,6 @@ const ordersProcessed = new Set()
|
||||||
|
|
||||||
let currentOrderAddr = ''
|
let currentOrderAddr = ''
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {InvoiceRequest} invoiceReq
|
|
||||||
* @returns {Promise<InvoiceResponse>}
|
|
||||||
*/
|
|
||||||
const _addInvoice = invoiceReq =>
|
|
||||||
new Promise((resolve, rej) => {
|
|
||||||
const {
|
|
||||||
services: { lightning }
|
|
||||||
} = LightningServices
|
|
||||||
|
|
||||||
lightning.addInvoice(invoiceReq, (
|
|
||||||
/** @type {any} */ error,
|
|
||||||
/** @type {InvoiceResponse} */ response
|
|
||||||
) => {
|
|
||||||
if (error) {
|
|
||||||
rej(error)
|
|
||||||
} else {
|
|
||||||
resolve(response)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} addr
|
* @param {string} addr
|
||||||
* @param {ISEA} SEA
|
* @param {ISEA} SEA
|
||||||
|
|
@ -104,8 +85,6 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const listenerStartTime = performance.now()
|
|
||||||
|
|
||||||
ordersProcessed.add(orderID)
|
ordersProcessed.add(orderID)
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
|
|
@ -114,8 +93,6 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
||||||
)} -- addr: ${addr}`
|
)} -- addr: ${addr}`
|
||||||
)
|
)
|
||||||
|
|
||||||
const orderAnswerStartTime = performance.now()
|
|
||||||
|
|
||||||
const alreadyAnswered = await getUser()
|
const alreadyAnswered = await getUser()
|
||||||
.get(Key.ORDER_TO_RESPONSE)
|
.get(Key.ORDER_TO_RESPONSE)
|
||||||
.get(orderID)
|
.get(orderID)
|
||||||
|
|
@ -126,12 +103,6 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const orderAnswerEndTime = performance.now() - orderAnswerStartTime
|
|
||||||
|
|
||||||
logger.info(`[PERF] Order Already Answered: ${orderAnswerEndTime}ms`)
|
|
||||||
|
|
||||||
const decryptStartTime = performance.now()
|
|
||||||
|
|
||||||
const senderEpub = await Utils.pubToEpub(order.from)
|
const senderEpub = await Utils.pubToEpub(order.from)
|
||||||
const secret = await SEA.secret(senderEpub, getUser()._.sea)
|
const secret = await SEA.secret(senderEpub, getUser()._.sea)
|
||||||
|
|
||||||
|
|
@ -140,10 +111,6 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
||||||
SEA.decrypt(order.memo, secret)
|
SEA.decrypt(order.memo, secret)
|
||||||
])
|
])
|
||||||
|
|
||||||
const decryptEndTime = performance.now() - decryptStartTime
|
|
||||||
|
|
||||||
logger.info(`[PERF] Decrypt invoice info: ${decryptEndTime}ms`)
|
|
||||||
|
|
||||||
const amount = Number(decryptedAmount)
|
const amount = Number(decryptedAmount)
|
||||||
|
|
||||||
if (!isNumber(amount)) {
|
if (!isNumber(amount)) {
|
||||||
|
|
@ -175,26 +142,19 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
||||||
`onOrders() -> Will now create an invoice : ${JSON.stringify(invoiceReq)}`
|
`onOrders() -> Will now create an invoice : ${JSON.stringify(invoiceReq)}`
|
||||||
)
|
)
|
||||||
|
|
||||||
const invoiceStartTime = performance.now()
|
const invoice = await addInvoice(
|
||||||
|
invoiceReq.value,
|
||||||
const invoice = await _addInvoice(invoiceReq)
|
invoiceReq.memo,
|
||||||
|
true,
|
||||||
const invoiceEndTime = performance.now() - invoiceStartTime
|
invoiceReq.expiry
|
||||||
|
)
|
||||||
logger.info(`[PERF] LND Invoice created in ${invoiceEndTime}ms`)
|
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
'onOrders() -> Successfully created the invoice, will now encrypt it'
|
'onOrders() -> Successfully created the invoice, will now encrypt it'
|
||||||
)
|
)
|
||||||
|
|
||||||
const invoiceEncryptStartTime = performance.now()
|
|
||||||
|
|
||||||
const encInvoice = await SEA.encrypt(invoice.payment_request, secret)
|
const encInvoice = await SEA.encrypt(invoice.payment_request, secret)
|
||||||
|
|
||||||
const invoiceEncryptEndTime = performance.now() - invoiceEncryptStartTime
|
|
||||||
|
|
||||||
logger.info(`[PERF] Invoice encrypted in ${invoiceEncryptEndTime}ms`)
|
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
`onOrders() -> Will now place the encrypted invoice in order to response usergraph: ${addr}`
|
`onOrders() -> Will now place the encrypted invoice in order to response usergraph: ${addr}`
|
||||||
)
|
)
|
||||||
|
|
@ -205,9 +165,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
||||||
type: 'invoice'
|
type: 'invoice'
|
||||||
}
|
}
|
||||||
|
|
||||||
const invoicePutStartTime = performance.now()
|
await /** @type {Promise<void>} */ (new Promise((res, rej) => {
|
||||||
|
|
||||||
await new Promise((res, rej) => {
|
|
||||||
getUser()
|
getUser()
|
||||||
.get(Key.ORDER_TO_RESPONSE)
|
.get(Key.ORDER_TO_RESPONSE)
|
||||||
.get(orderID)
|
.get(orderID)
|
||||||
|
|
@ -223,9 +181,7 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
||||||
res()
|
res()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
}))
|
||||||
|
|
||||||
const invoicePutEndTime = performance.now() - invoicePutStartTime
|
|
||||||
|
|
||||||
// invoices should be settled right away so we can rely on this single
|
// invoices should be settled right away so we can rely on this single
|
||||||
// subscription instead of life-long all invoices subscription
|
// subscription instead of life-long all invoices subscription
|
||||||
|
|
@ -234,24 +190,46 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
||||||
if (!Common.isPopulatedString(postID)) {
|
if (!Common.isPopulatedString(postID)) {
|
||||||
throw new TypeError(`postID not a a populated string`)
|
throw new TypeError(`postID not a a populated string`)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
const { r_hash } = invoice
|
|
||||||
|
|
||||||
// A post tip order lifecycle is short enough that we can do it like this.
|
// A post tip order lifecycle is short enough that we can do it like this.
|
||||||
const stream = LightningServices.invoices.subscribeSingleInvoice({
|
const stream = LightningServices.invoices.subscribeSingleInvoice({
|
||||||
r_hash
|
r_hash: invoice.r_hash
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/** @type {Common.Coordinate} */
|
||||||
|
const coord = {
|
||||||
|
amount,
|
||||||
|
id: invoice.r_hash.toString(),
|
||||||
|
inbound: true,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
type: 'invoice',
|
||||||
|
invoiceMemo: memo,
|
||||||
|
fromGunPub: order.from,
|
||||||
|
toGunPub: getUser()._.sea.pub,
|
||||||
|
toLndPub: await myLNDPub()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (order.targetType === 'post') {
|
||||||
|
coord.type = 'tip'
|
||||||
|
} else {
|
||||||
|
coord.type = 'spontaneousPayment'
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {Common.InvoiceWhenListed} invoice
|
* @param {Common.InvoiceWhenListed} invoice
|
||||||
*/
|
*/
|
||||||
const onData = invoice => {
|
const onData = invoice => {
|
||||||
if (invoice.settled) {
|
if (invoice.settled) {
|
||||||
|
if (order.targetType === 'post') {
|
||||||
getUser()
|
getUser()
|
||||||
.get('postToTipCount')
|
.get('postToTipCount')
|
||||||
.get(postID)
|
// CAST: Checked above.
|
||||||
|
.get(/** @type {string} */ (order.postID))
|
||||||
.set(null) // each item in the set is a tip
|
.set(null) // each item in the set is a tip
|
||||||
|
}
|
||||||
|
|
||||||
|
writeCoordinate(invoice.r_hash.toString(), coord)
|
||||||
stream.off()
|
stream.off()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -259,21 +237,14 @@ const listenerForAddr = (addr, SEA) => async (order, orderID) => {
|
||||||
stream.on('data', onData)
|
stream.on('data', onData)
|
||||||
|
|
||||||
stream.on('status', (/** @type {any} */ status) => {
|
stream.on('status', (/** @type {any} */ status) => {
|
||||||
logger.info(`Post tip, post: ${postID}, invoice status:`, status)
|
logger.info(`Post tip, post: ${order.postID}, invoice status:`, status)
|
||||||
})
|
})
|
||||||
stream.on('end', () => {
|
stream.on('end', () => {
|
||||||
logger.warn(`Post tip, post: ${postID}, invoice stream ended`)
|
logger.warn(`Post tip, post: ${order.postID}, invoice stream ended`)
|
||||||
})
|
})
|
||||||
stream.on('error', (/** @type {any} */ e) => {
|
stream.on('error', (/** @type {any} */ e) => {
|
||||||
logger.warn(`Post tip, post: ${postID}, error:`, e)
|
logger.warn(`Post tip, post: ${order.postID}, error:`, e)
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
logger.info(`[PERF] Added invoice to GunDB in ${invoicePutEndTime}ms`)
|
|
||||||
|
|
||||||
const listenerEndTime = performance.now() - listenerStartTime
|
|
||||||
|
|
||||||
logger.info(`[PERF] Invoice generation completed in ${listenerEndTime}ms`)
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(
|
logger.error(
|
||||||
`error inside onOrders, orderAddr: ${addr}, orderID: ${orderID}, order: ${JSON.stringify(
|
`error inside onOrders, orderAddr: ${addr}, orderID: ${orderID}, order: ${JSON.stringify(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue