Skip to content

Commit

Permalink
fix: remove onchain lnd dep on lnd service creation
Browse files Browse the repository at this point in the history
  • Loading branch information
dolcalmi committed Nov 15, 2023
1 parent 789d657 commit 1a129ae
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
18 changes: 11 additions & 7 deletions core/api/src/services/lnd/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ export const LndService = (): ILightningService | LightningServiceError => {
const defaultLnd = activeNode.lnd
const defaultPubkey = activeNode.pubkey as Pubkey

const activeOnchainNode = getActiveOnchainLnd()
if (activeOnchainNode instanceof Error) return activeOnchainNode

const defaultOnchainLnd = activeOnchainNode.lnd
const defaultOnchainLnd = () => {
const activeOnchainNode = getActiveOnchainLnd()
if (activeOnchainNode instanceof Error) return activeOnchainNode
return activeOnchainNode.lnd
}

const isLocal = (pubkey: Pubkey): boolean | LightningServiceError =>
getLnds({ type: "offchain" }).some((item) => item.pubkey === pubkey)
Expand Down Expand Up @@ -131,7 +132,7 @@ export const LndService = (): ILightningService | LightningServiceError => {
pubkey?: Pubkey,
): Promise<Satoshis | LightningServiceError> => {
try {
const lndInstance = pubkey ? getLndFromPubkey({ pubkey }) : defaultOnchainLnd
const lndInstance = pubkey ? getLndFromPubkey({ pubkey }) : defaultOnchainLnd()
if (lndInstance instanceof Error) return lndInstance

const { chain_balance } = await getChainBalance({ lnd: lndInstance })
Expand All @@ -145,7 +146,7 @@ export const LndService = (): ILightningService | LightningServiceError => {
pubkey?: Pubkey,
): Promise<Satoshis | LightningServiceError> => {
try {
const lndInstance = pubkey ? getLndFromPubkey({ pubkey }) : defaultOnchainLnd
const lndInstance = pubkey ? getLndFromPubkey({ pubkey }) : defaultOnchainLnd()
if (lndInstance instanceof Error) return lndInstance

const { pending_chain_balance } = await getPendingChainBalance({ lnd: lndInstance })
Expand Down Expand Up @@ -183,8 +184,11 @@ export const LndService = (): ILightningService | LightningServiceError => {
// this is necessary for tests, otherwise `after` may be negative
const after = Math.max(0, blockHeight - scanDepth)

const lnd = defaultOnchainLnd()
if (lnd instanceof Error) return lnd

const txs = await getChainTransactions({
lnd: defaultOnchainLnd,
lnd,
after,
})

Expand Down
4 changes: 2 additions & 2 deletions core/api/test/bats/helpers/ln.bash
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ lnd_cli() {
lnd_start() {
started() {
state="$(lnd_cli state | jq -r '.state')"
is_synced="$(lnd_outside_cli getinfo | jq -r '.synced_to_graph')"
is_synced="$(lnd_cli getinfo | jq -r '.synced_to_graph')"
[[ "$state" == "SERVER_ACTIVE" && "$is_synced" == "true" ]] || exit 1
}

docker start "${COMPOSE_PROJECT_NAME}-lnd1-1"
retry 20 3 started
retry 10 5 started
}

lnd_stop() {
Expand Down
48 changes: 24 additions & 24 deletions core/api/test/bats/invoices.bats
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ setup() {
reset_redis
}

@test "invoices: get invoices for account" {
token_name="$ALICE_TOKEN_NAME"

exec_graphql "$token_name" 'invoices' '{"first": 3}'

invoice_count="$(graphql_output '.data.me.defaultAccount.invoices.edges | length')"
[[ "$invoice_count" -eq "3" ]] || exit 1
}

@test "invoices: get invoices for wallet" {
token_name="$ALICE_TOKEN_NAME"
btc_wallet_name="$token_name.btc_wallet_id"

variables=$(
jq -n \
--arg wallet_id "$(read_value $btc_wallet_name)" \
'{walletId: $wallet_id, first: 2}'
)
exec_graphql "$token_name" 'invoices-by-wallet' "$variables"

invoice_count="$(graphql_output '.data.me.defaultAccount.walletById.invoices.edges | length')"
[[ "$invoice_count" -eq "2" ]] || exit 1
}

@test "invoices: create invoices from alternate node" {
lnd1_pubkey=$(lnd_cli getinfo | jq -r '.identity_pubkey')
lnd2_pubkey=$(lnd2_cli getinfo | jq -r '.identity_pubkey')
Expand Down Expand Up @@ -93,27 +117,3 @@ setup() {
destination_node="$(lnd2_cli decodepayreq $payment_request | jq -r '.destination')"
[[ "${destination_node}" == "${lnd2_pubkey}" ]] || exit 1
}

@test "invoices: get invoices for account" {
token_name="$ALICE_TOKEN_NAME"

exec_graphql "$token_name" 'invoices' '{"first": 3}'

invoice_count="$(graphql_output '.data.me.defaultAccount.invoices.edges | length')"
[[ "$invoice_count" -eq "3" ]] || exit 1
}

@test "invoices: get invoices for wallet" {
token_name="$ALICE_TOKEN_NAME"
btc_wallet_name="$token_name.btc_wallet_id"

variables=$(
jq -n \
--arg wallet_id "$(read_value $btc_wallet_name)" \
'{walletId: $wallet_id, first: 2}'
)
exec_graphql "$token_name" 'invoices-by-wallet' "$variables"

invoice_count="$(graphql_output '.data.me.defaultAccount.walletById.invoices.edges | length')"
[[ "$invoice_count" -eq "2" ]] || exit 1
}

0 comments on commit 1a129ae

Please sign in to comment.