refactor: ♻️ forward queries and cleanup

This commit is contained in:
AP 2020-05-21 09:14:56 +02:00
parent caefd02234
commit feb036b29a
16 changed files with 110 additions and 143 deletions

15
api/helpers/async.ts Normal file
View file

@ -0,0 +1,15 @@
import { getErrorMsg } from './helpers';
import { logger } from './logger';
export const to = promise => {
return promise
.then(data => data)
.catch(err => {
logger.error('%o', err);
throw new Error(getErrorMsg(err));
});
};
export const toWithError = promise => {
return promise.then(data => [data, undefined]).catch(err => [undefined, err]);
};

View file

@ -7,7 +7,6 @@ import {
CLIENT_ACCOUNT,
} from 'src/context/AccountContext';
import { ContextType } from 'api/types/apiTypes';
import { logger } from './logger';
const { serverRuntimeConfig } = getConfig();
const { nodeEnv } = serverRuntimeConfig;
@ -85,14 +84,3 @@ export const getErrorMsg = (error: any[] | string): string => {
return 'Error';
};
export const to = promise => {
return promise
.then(data => {
return data;
})
.catch(err => {
logger.error('%o', err);
throw new Error(getErrorMsg(err));
});
};

View file

@ -0,0 +1,52 @@
import { getNode, getChannel } from 'ln-service';
import { logger } from 'api/helpers/logger';
import { toWithError } from 'api/helpers/async';
const errorNode = {
alias: 'Partner node not found',
color: '#000000',
};
export const getNodeFromChannel = async (
id: string,
publicKey: string,
lnd
) => {
const [channelInfo, channelError] = await toWithError(
getChannel({
lnd,
id,
})
);
if (channelError) {
logger.verbose(`Error getting channel with id ${id}: %o`, channelError);
return errorNode;
}
const partnerPublicKey =
channelInfo.policies[0].public_key !== publicKey
? channelInfo.policies[0].public_key
: channelInfo.policies[1].public_key;
const [nodeInfo, nodeError] = await toWithError(
getNode({
lnd,
is_omitting_channels: true,
public_key: partnerPublicKey,
})
);
if (nodeError) {
logger.verbose(
`Error getting node with public key ${partnerPublicKey}: %o`,
nodeError
);
return errorNode;
}
return {
alias: nodeInfo.alias,
color: nodeInfo.color,
};
};

View file

@ -7,8 +7,9 @@ import {
} from 'ln-service';
import { GraphQLString, GraphQLNonNull, GraphQLInt } from 'graphql';
import { ContextType } from 'api/types/apiTypes';
import { to } from 'api/helpers/async';
import { requestLimiter } from '../../../helpers/rateLimiter';
import { getAuthLnd, to, getCorrectAuth } from '../../../helpers/helpers';
import { getAuthLnd, getCorrectAuth } from '../../../helpers/helpers';
import { defaultParams } from '../../../helpers/defaultProps';
import { createCustomRecords } from '../../../helpers/customRecords';

View file

@ -43,8 +43,7 @@ export const recoverFunds = {
});
return true;
} catch (error) {
params.logger &&
logger.error('Error recovering funds from channels: %o', error);
logger.error('Error recovering funds from channels: %o', error);
throw new Error(getErrorMsg(error));
}
},

View file

@ -57,8 +57,7 @@ export const getPendingChainBalance = {
});
return pendingValue.pending_chain_balance;
} catch (error) {
params.logger &&
logger.error('Error getting pending chain balance: %o', error);
logger.error('Error getting pending chain balance: %o', error);
throw new Error(getErrorMsg(error));
}
},

View file

@ -47,8 +47,7 @@ export const getChainTransactions = {
).reverse();
return transactions;
} catch (error) {
params.logger &&
logger.error('Error getting chain transactions: %o', error);
logger.error('Error getting chain transactions: %o', error);
throw new Error(getErrorMsg(error));
}
},

View file

@ -70,8 +70,8 @@ export const getChannels = {
return {
...channel,
time_offline: channel.time_offline / 1000,
time_online: channel.time_online / 1000,
time_offline: Math.round((channel.time_online || 0) / 1000),
time_online: Math.round((channel.time_online || 0) / 1000),
partner_node_info: {
...nodeInfo,
},

View file

@ -67,8 +67,7 @@ export const getPendingChannels = {
});
return channels;
} catch (error) {
params.logger &&
logger.error('Error getting pending channels: %o', error);
logger.error('Error getting pending channels: %o', error);
throw new Error(getErrorMsg(error));
}
},

View file

@ -1,8 +1,9 @@
import { GraphQLString, GraphQLBoolean } from 'graphql';
import { getInvoices, verifyMessage } from 'ln-service';
import { ContextType } from 'api/types/apiTypes';
import { to } from 'api/helpers/async';
import { requestLimiter } from '../../../helpers/rateLimiter';
import { getAuthLnd, to, getCorrectAuth } from '../../../helpers/helpers';
import { getAuthLnd, getCorrectAuth } from '../../../helpers/helpers';
import { defaultParams } from '../../../helpers/defaultProps';
import { decodeMessage } from '../../../helpers/customRecords';
import { GetMessagesType } from '../../types/QueryType';

View file

@ -1,7 +1,8 @@
import { getWalletVersion } from 'ln-service';
import { ContextType } from 'api/types/apiTypes';
import { to } from 'api/helpers/async';
import { requestLimiter } from '../../../helpers/rateLimiter';
import { getAuthLnd, to, getCorrectAuth } from '../../../helpers/helpers';
import { getAuthLnd, getCorrectAuth } from '../../../helpers/helpers';
import { defaultParams } from '../../../helpers/defaultProps';
import { WalletInfoType } from '../../types/QueryType';

View file

@ -32,8 +32,7 @@ export const getCountries = {
}
throw new Error('Problem getting HodlHodl countries.');
} catch (error) {
params.logger &&
logger.error('Error getting HodlHodl countries: %o', error);
logger.error('Error getting HodlHodl countries: %o', error);
throw new Error('Problem getting HodlHodl countries.');
}
},

View file

@ -32,8 +32,7 @@ export const getCurrencies = {
}
throw new Error('Problem getting HodlHodl currencies.');
} catch (error) {
params.logger &&
logger.error('Error getting HodlHodl currencies: %o', error);
logger.error('Error getting HodlHodl currencies: %o', error);
throw new Error('Problem getting HodlHodl currencies.');
}
},

View file

@ -1,14 +1,10 @@
import { GraphQLString } from 'graphql';
import {
getForwards as getLnForwards,
getNode,
getChannel,
getWalletInfo,
} from 'ln-service';
import { getForwards as getLnForwards, getWalletInfo } from 'ln-service';
import { subHours, subDays } from 'date-fns';
import { sortBy } from 'underscore';
import { ContextType } from 'api/types/apiTypes';
import { logger } from '../../../helpers/logger';
import { getNodeFromChannel } from 'api/schemas/helpers/getNodeFromChannel';
import { logger } from 'api/helpers/logger';
import { requestLimiter } from '../../../helpers/rateLimiter';
import {
getAuthLnd,
@ -19,15 +15,6 @@ import { defaultParams } from '../../../helpers/defaultProps';
import { countArray, countRoutes } from './Helpers';
import { ForwardCompleteProps } from './ForwardReport.interface';
interface NodeProps {
alias: string;
color: string;
}
interface ChannelsProps {
policies: { public_key: string }[];
}
export const getForwardChannelsReport = {
type: GraphQLString,
args: {
@ -53,47 +40,19 @@ export const getForwardChannelsReport = {
startDate = subHours(endDate, 24);
}
const getNodeAlias = async (id: string, publicKey: string) => {
let channelInfo: ChannelsProps;
try {
channelInfo = await getChannel({
lnd,
id,
});
} catch (error) {
if (error[1] == 'FullChannelDetailsNotFound') {
return {
alias: 'Edge Zombie or not found',
color: '#000000',
};
}
logger.error('Error getting channel / node information: %o', error);
throw new Error(getErrorMsg(error));
}
const partnerPublicKey =
channelInfo.policies[0].public_key !== publicKey
? channelInfo.policies[0].public_key
: channelInfo.policies[1].public_key;
const nodeInfo: NodeProps = await getNode({
lnd,
is_omitting_channels: true,
public_key: partnerPublicKey,
});
return {
alias: nodeInfo.alias,
color: nodeInfo.color,
};
};
const getRouteAlias = (array: any[], publicKey: string) =>
Promise.all(
array.map(async channel => {
const nodeAliasIn = await getNodeAlias(channel.in, publicKey);
const nodeAliasOut = await getNodeAlias(channel.out, publicKey);
const nodeAliasIn = await getNodeFromChannel(
channel.in,
publicKey,
lnd
);
const nodeAliasOut = await getNodeFromChannel(
channel.out,
publicKey,
lnd
);
return {
aliasIn: nodeAliasIn.alias,
@ -108,7 +67,11 @@ export const getForwardChannelsReport = {
const getAlias = (array: any[], publicKey: string) =>
Promise.all(
array.map(async channel => {
const nodeAlias = await getNodeAlias(channel.name, publicKey);
const nodeAlias = await getNodeFromChannel(
channel.name,
publicKey,
lnd
);
return {
alias: nodeAlias.alias,
color: nodeAlias.color,
@ -166,8 +129,7 @@ export const getForwardChannelsReport = {
.slice(0, 10);
return JSON.stringify(sortedOutCount);
} catch (error) {
params.logger &&
logger.error('Error getting forward channel report: %o', error);
logger.error('Error getting forward channel report: %o', error);
throw new Error(getErrorMsg(error));
}
},

View file

@ -1,13 +1,9 @@
import { GraphQLString } from 'graphql';
import {
getForwards as getLnForwards,
getChannel,
getNode,
getWalletInfo,
} from 'ln-service';
import { getForwards as getLnForwards, getWalletInfo } from 'ln-service';
import { sortBy } from 'underscore';
import { subHours, subDays, subMonths, subYears } from 'date-fns';
import { ContextType } from 'api/types/apiTypes';
import { getNodeFromChannel } from 'api/schemas/helpers/getNodeFromChannel';
import { logger } from '../../../helpers/logger';
import { requestLimiter } from '../../../helpers/rateLimiter';
import {
@ -19,15 +15,6 @@ import { ForwardCompleteProps } from '../report/ForwardReport.interface';
import { defaultParams } from '../../../helpers/defaultProps';
import { GetForwardType } from '../../types/QueryType';
interface NodeProps {
alias: string;
color: string;
}
interface ChannelsProps {
policies: { public_key: string }[];
}
export const getForwards = {
type: GetForwardType,
args: {
@ -61,52 +48,18 @@ export const getForwards = {
lnd,
});
const getNodeAlias = async (id: string, publicKey: string) => {
let channelInfo: ChannelsProps;
try {
channelInfo = await getChannel({
lnd,
id,
});
} catch (error) {
if (error[1] == 'FullChannelDetailsNotFound') {
return {
alias: 'Edge Zombie or not found',
color: '#000000',
};
}
logger.error('Error getting channel / node information: %o', error);
throw new Error(getErrorMsg(error));
}
const partnerPublicKey =
channelInfo.policies[0].public_key !== publicKey
? channelInfo.policies[0].public_key
: channelInfo.policies[1].public_key;
const nodeInfo: NodeProps = await getNode({
lnd,
is_omitting_channels: true,
public_key: partnerPublicKey,
});
return {
alias: nodeInfo.alias,
color: nodeInfo.color,
};
};
const getAlias = (array: any[], publicKey: string) =>
Promise.all(
array.map(async forward => {
const inNodeAlias = await getNodeAlias(
const inNodeAlias = await getNodeFromChannel(
forward.incoming_channel,
publicKey
publicKey,
lnd
);
const outNodeAlias = await getNodeAlias(
const outNodeAlias = await getNodeFromChannel(
forward.outgoing_channel,
publicKey
publicKey,
lnd
);
return {
incoming_alias: inNodeAlias.alias,

View file

@ -53,7 +53,7 @@ const apolloServer = new ApolloServer({
jwt.verify(req.cookies.SSOAuth, secret);
ssoVerified = true;
} catch (error) {
logger.verbose('SSO authentication cookie failed');
logger.silly('SSO authentication cookie failed');
}
}
@ -75,7 +75,7 @@ const apolloServer = new ApolloServer({
macaroon: accountMacaroon,
};
} catch (error) {
logger.verbose('Account authentication cookie failed');
logger.silly('Account authentication cookie failed');
}
}