Skip to content

Commit

Permalink
Merge pull request #2290 from niteshbalusu11/cln-rest-performance-imp…
Browse files Browse the repository at this point in the history
…rovements

CLNRest: performance improvements using sql query
  • Loading branch information
kaloudis authored Jul 18, 2024
2 parents 641f6c8 + a6e2612 commit 8c395f4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 44 deletions.
3 changes: 2 additions & 1 deletion backends/CLNRest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ export default class CLNRest {
return this.postRequest('/v1/withdraw', request);
};
getMyNodeInfo = () => this.postRequest('/v1/getinfo');
getInvoices = () => this.postRequest('/v1/listinvoices');
getInvoices = () =>
this.postRequest('/v1/listinvoices', { limit: 150, index: 'created' });
createInvoice = (data: any) =>
this.postRequest('/v1/invoice', {
description: data.memo,
Expand Down
78 changes: 35 additions & 43 deletions backends/CoreLightningRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,41 +131,34 @@ export const listPeers = async (data: any) => {

// Get all chain transactions from your core-lightnig node
export const getChainTransactions = async () => {
const sqlQuery =
`SELECT * FROM bkpr_accountevents WHERE (tag='deposit' OR tag='to_them' OR tag='channel_open' OR tag='channel_close') ORDER BY timestamp DESC LIMIT 150`.trim();

const results = await Promise.allSettled([
api.postRequest('/v1/bkpr-listaccountevents'),
api.postRequest('/v1/sql', { query: sqlQuery.trim() }),
api.postRequest('/v1/listtransactions'),
api.postRequest('/v1/getinfo')
]);

const [walletTxsResult, listTxsResult, getinfoResult] = results;
const [sqlResult, listTxsResult, getinfoResult] = results;

// If getinfo fails, return blank txs
if (getinfoResult.status !== 'fulfilled') {
return { transactions: [] };
}

const getinfo = getinfoResult.value;

const allTxs =
walletTxsResult.status === 'fulfilled'
? walletTxsResult.value.events.filter(
(tx: any) => tx.type !== 'onchain_fee'
)
: { events: [] };

const walletTxs =
walletTxsResult.status === 'fulfilled'
? walletTxsResult.value.events.filter(
(tx: any) => tx.tag === 'deposit' && tx.account === 'wallet'
)
: { events: [] };

const externalTxs =
walletTxsResult.status === 'fulfilled'
? walletTxsResult.value.events.filter(
(tx: any) => tx.tag === 'deposit' && tx.account === 'external'
)
: { events: [] };
sqlResult.status === 'fulfilled' ? sqlResult.value : { rows: [] };

const walletTxs = allTxs.rows.filter(
(tx: any) =>
!!tx[3] && tx[3] === 'deposit' && !!tx[1] && tx[1] === 'wallet'
);

const externalTxs = allTxs.rows.filter(
(tx: any) =>
!!tx[3] && tx[3] === 'deposit' && !!tx[1] && tx[1] === 'external'
);

const listTxs =
listTxsResult.status === 'fulfilled'
Expand All @@ -175,17 +168,17 @@ export const getChainTransactions = async () => {
const transactions = listTxs.transactions
.map((tx: any) => {
const withdrawal = externalTxs.find((n: any) => {
const txid = n.outpoint ? n.outpoint.split(':')[0] : null;
const txid = n[8] ? n[8].split(':')[0] : null;

// Check if the deposit is associated with a channel open or close
const isChannelChange = allTxs.find((c: any) => {
if (!c.outpoint) {
const isChannelChange = allTxs.rows.find((c: any) => {
if (!c[8]) {
return undefined;
}

return (
c.outpoint.split(':')[0] === tx.hash &&
(c.tag === 'channel_open' || c.tag === 'channel_close')
c[8].split(':')[0] === tx.hash &&
(c[3] === 'channel_open' || c[3] === 'channel_close')
);
});

Expand All @@ -197,16 +190,17 @@ export const getChainTransactions = async () => {
});

const deposit = walletTxs.find((n: any) => {
const txid = n.outpoint ? n.outpoint.split(':')[0] : null;
const txid = n[8] ? n[8].split(':')[0] : null;

// Check if the deposit is associated with a channel open or close
const isChannelChange = allTxs.find((c: any) => {
if (!c.outpoint) {
const isChannelChange = allTxs.rows.find((c: any) => {
if (!c[8]) {
return undefined;
}

return (
c.outpoint.split(':')[0] === tx.hash &&
(c.tag === 'channel_open' || c.tag === 'channel_close')
c[8].split(':')[0] === tx.hash &&
(c[3] === 'channel_open' || c[3] === 'channel_close')
);
});

Expand All @@ -219,23 +213,21 @@ export const getChainTransactions = async () => {

if (withdrawal) {
return {
amount: -Math.abs(withdrawal.credit_msat) / 1000,
block_height: withdrawal.blockheight,
num_confirmations:
getinfo.blockheight - withdrawal.blockheight,
time_stamp: withdrawal.timestamp,
amount: -Math.abs(withdrawal[4]) / 1000,
block_height: withdrawal[9],
num_confirmations: getinfo.blockheight - withdrawal[9],
time_stamp: withdrawal[7],
txid: tx.hash,
note: 'on-chain withdrawal'
};
}

if (deposit) {
return {
amount: deposit.credit_msat / 1000,
block_height: deposit.blockheight,
num_confirmations:
getinfo.blockheight - deposit.blockheight,
time_stamp: deposit.timestamp,
amount: deposit[4] / 1000,
block_height: deposit[9],
num_confirmations: getinfo.blockheight - deposit[9],
time_stamp: deposit[7],
txid: tx.hash,
note: 'on-chain deposit'
};
Expand Down

0 comments on commit 8c395f4

Please sign in to comment.