Skip to content

Commit

Permalink
test: remove legacy public-ln-receive.bats
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayush170-Future committed Dec 9, 2023
1 parent f5f8d5d commit 11d3803
Show file tree
Hide file tree
Showing 9 changed files with 345 additions and 366 deletions.
4 changes: 3 additions & 1 deletion bats/core/api/public-ln-receive.bats
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ load "../../helpers/wallet.bash"
load "../../helpers/subscriber.bash"

setup_file() {
lnds_init

create_user 'alice'
user_update_username 'alice'
fund_user_onchain 'alice' 'btc_wallet'
Expand Down Expand Up @@ -333,4 +335,4 @@ usd_amount=50
exec_graphql 'anon' 'ln-usd-invoice-btc-denominated-create-on-behalf-of-recipient' "$variables"
error_msg="$(graphql_output '.data.lnUsdInvoiceBtcDenominatedCreateOnBehalfOfRecipient.errors[0].message')"
[[ "$error_msg" == "A valid satoshi amount is required" ]] || exit 1
}
}
6 changes: 6 additions & 0 deletions bats/gql/account-default-wallet.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
query accountDefaultWallet($username: Username!, $walletCurrency: WalletCurrency) {
accountDefaultWallet(username: $username, walletCurrency: $walletCurrency) {
id
walletCurrency
}
}
7 changes: 7 additions & 0 deletions bats/gql/account-update-default-wallet-id.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mutation accountUpdateDefaultWalletId($input: AccountUpdateDefaultWalletIdInput!) {
accountUpdateDefaultWalletId(input: $input) {
account {
defaultWalletId
}
}
}
13 changes: 13 additions & 0 deletions bats/gql/ln-invoice-create-on-behalf-of-recipient.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mutation lnInvoiceCreateOnBehalfOfRecipient(
$input: LnInvoiceCreateOnBehalfOfRecipientInput!
) {
lnInvoiceCreateOnBehalfOfRecipient(input: $input) {
invoice {
paymentRequest
paymentHash
}
errors {
message
}
}
}
13 changes: 13 additions & 0 deletions bats/gql/ln-no-amount-invoice-create-on-behalf-of-recipient.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mutation lnNoAmountInvoiceCreateOnBehalfOfRecipient(
$input: LnNoAmountInvoiceCreateOnBehalfOfRecipientInput!
) {
lnNoAmountInvoiceCreateOnBehalfOfRecipient(input: $input) {
invoice {
paymentRequest
paymentHash
}
errors {
message
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mutation lnUsdInvoiceBtcDenominatedCreateOnBehalfOfRecipient(
$input: LnUsdInvoiceBtcDenominatedCreateOnBehalfOfRecipientInput!
) {
lnUsdInvoiceBtcDenominatedCreateOnBehalfOfRecipient(input: $input) {
invoice {
paymentRequest
paymentHash
}
errors {
message
}
}
}
13 changes: 13 additions & 0 deletions bats/gql/ln-usd-invoice-create-on-behalf-of-recipient.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mutation lnUsdInvoiceCreateOnBehalfOfRecipient(
$input: LnUsdInvoiceCreateOnBehalfOfRecipientInput!
) {
lnUsdInvoiceCreateOnBehalfOfRecipient(input: $input) {
invoice {
paymentRequest
paymentHash
}
errors {
message
}
}
}
277 changes: 277 additions & 0 deletions bats/helpers/ln.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,41 @@ CURRENT_FILE=${BASH_SOURCE:-bats/helpers/.}
source "$(dirname "$CURRENT_FILE")/_common.bash"
source "$(dirname "$CURRENT_FILE")/cli.bash"

LND_FUNDING_TOKEN_NAME="lnd_funding"
LND_FUNDING_PHONE="+16505554351"

LNDS_REST_LOG=".e2e-lnds-rest.log"

mempool_not_empty() {
local txid="$(bitcoin_cli getrawmempool | jq -r ".[0]")"
[[ "$txid" != "null" ]] || exit 1
}

run_with_lnd() {
local func_name="$1"
shift # This will shift away the function name, so $1 becomes the next argument

if [[ "$func_name" == "lnd_cli" ]]; then
lnd_cli "$@"
elif [[ "$func_name" == "lnd2_cli" ]]; then
lnd2_cli "$@"
elif [[ "$func_name" == "lnd_outside_cli" ]]; then
lnd_outside_cli "$@"
elif [[ "$func_name" == "lnd_outside_2_cli" ]]; then
lnd_outside_2_cli "$@"
else
echo "Invalid function name passed!" && return 1
fi
}

lnd2_cli() {
docker exec "${COMPOSE_PROJECT_NAME}-lnd2-1" \
lncli \
--macaroonpath /root/.lnd/admin.macaroon \
--tlscertpath /root/.lnd/tls.cert \
$@
}

create_new_lnd_onchain_address() {
local wallet_name=$1
local wallet_id=$(read_value $wallet_name)
Expand Down Expand Up @@ -31,3 +66,245 @@ create_new_lnd_onchain_address() {

echo $address
}

run_with_lnd() {
local func_name="$1"
shift # This will shift away the function name, so $1 becomes the next argument

if [[ "$func_name" == "lnd_cli" ]]; then
lnd_cli "$@"
elif [[ "$func_name" == "lnd2_cli" ]]; then
lnd2_cli "$@"
elif [[ "$func_name" == "lnd_outside_cli" ]]; then
lnd_outside_cli "$@"
elif [[ "$func_name" == "lnd_outside_2_cli" ]]; then
lnd_outside_2_cli "$@"
else
echo "Invalid function name passed!" && return 1
fi
}

close_partner_initiated_channels_with_external() {
close_channels_with_external() {
lnd_cli_value="$1"
lnd1_pubkey=$(lnd_cli getinfo | jq -r '.identity_pubkey')
lnd2_pubkey=$(lnd2_cli getinfo | jq -r '.identity_pubkey')

partner_initiated_external_channel_filter='
.channels[]?
| select(.initiator != true)
| select(.remote_pubkey != $lnd1_pubkey)
| select(.remote_pubkey != $lnd2_pubkey)
| .channel_point
'

run_with_lnd "$lnd_cli_value" listchannels \
| jq -r \
--arg lnd1_pubkey "$lnd1_pubkey" \
--arg lnd2_pubkey "$lnd2_pubkey" \
"$partner_initiated_external_channel_filter" \
| while read -r channel_point; do
funding_txid="${channel_point%%:*}"
run_with_lnd "$lnd_cli_value" closechannel "$funding_txid"
done
}

close_channels_with_external lnd_cli
close_channels_with_external lnd2_cli
close_channels_with_external lnd_outside_cli
close_channels_with_external lnd_outside_2_cli
}

lnd_outside_cli() {
docker exec "${COMPOSE_PROJECT_NAME}-lnd-outside-1-1" \
lncli \
--macaroonpath /root/.lnd/admin.macaroon \
--tlscertpath /root/.lnd/tls.cert \
$@
}

lnd_outside_2_cli() {
docker exec "${COMPOSE_PROJECT_NAME}-lnd-outside-2-1" \
lncli \
--macaroonpath /root/.lnd/admin.macaroon \
--tlscertpath /root/.lnd/tls.cert \
$@
}

fund_wallet_from_lightning() {
local token_name=$1
local wallet_id_name=$2
local amount=$3

variables=$(
jq -n \
--arg wallet_id "$(read_value $wallet_id_name)" \
'{input: {walletId: $wallet_id}}'
)
exec_graphql "$token_name" 'ln-no-amount-invoice-create' "$variables"
invoice="$(graphql_output '.data.lnNoAmountInvoiceCreate.invoice')"

payment_request="$(echo $invoice | jq -r '.paymentRequest')"
[[ "${payment_request}" != "null" ]]
payment_hash="$(echo $invoice | jq -r '.paymentHash')"
[[ "${payment_hash}" != "null" ]]

lnd_outside_cli payinvoice -f \
--pay_req "$payment_request" \
--amt "$amount"

retry 15 1 check_for_ln_initiated_settled "$token_name" "$payment_hash"
}

check_for_ln_initiated_status() {
local expected_status=$1
local token_name=$2
local payment_hash=$3
local first=${4:-"2"}

variables=$(
jq -n \
--argjson first "$first" \
'{"first": $first}'
)
exec_graphql "$token_name" 'transactions' "$variables"

status="$(get_from_transaction_by_ln_hash_and_status $payment_hash $expected_status '.status')"
[[ "${status}" == "${expected_status}" ]] || return 1
}

check_for_ln_initiated_settled() {
check_for_ln_initiated_status "SUCCESS" "$@"
}

check_for_ln_initiated_pending() {
check_for_ln_initiated_status "PENDING" "$@"
}

get_from_transaction_by_ln_hash_and_status() {
payment_hash="$1"
expected_status="$2"
property_query="$3"

jq_query='
.data.me.defaultAccount.transactions.edges[]
| select(.node.initiationVia.paymentHash == $payment_hash)
| select(.node.status == $expected_status)
.node'

echo $output \
| jq -r \
--arg payment_hash "$payment_hash" \
--arg expected_status "$expected_status" \
"$jq_query" \
| jq -r "$property_query" \
| head -n 1
}

check_for_ln_update() {
payment_hash=$1

retry 10 1 \
grep "Data.*LnUpdate.*$payment_hash" .e2e-subscriber.log \
| awk '{print $2}' \
| jq -r --arg hash "$payment_hash" 'select(.data.myUpdates.update.paymentHash == $hash)'

paid_status=$( \
grep 'Data.*LnUpdate' .e2e-subscriber.log \
| awk '{print $2}' \
| jq -r --arg hash "$payment_hash" 'select(.data.myUpdates.update.paymentHash == $hash) .data.myUpdates.update.status'
)

[[ "$paid_status" == "PAID" ]] || exit 1
}

num_txns_for_hash() {
token_name="$1"
payment_hash="$2"

first=20
txn_variables=$(
jq -n \
--argjson first "$first" \
'{"first": $first}'
)
exec_graphql "$token_name" 'transactions' "$txn_variables" > /dev/null

jq_query='
[
.data.me.defaultAccount.transactions.edges[]
| select(.node.initiationVia.paymentHash == $payment_hash)
]
| length
'
echo $output \
| jq -r \
--arg payment_hash "$payment_hash" \
"$jq_query"
}

lnds_init() {
# Clean up any existing channels
close_partner_initiated_channels_with_external || true

# Mine onchain balance
local amount="1"
local address="$(lnd_outside_cli newaddress p2wkh | jq -r '.address')"
local local_amount="10000000"
local push_amount="5000000"
bitcoin_cli sendtoaddress "$address" "$amount"
bitcoin_cli -generate 3

no_pending_channels() {
pending_channel="$(lnd_outside_cli pendingchannels | jq -r '.pending_open_channels[0]')"
if [[ "$pending_channel" != "null" ]]; then
bitcoin_cli -generate 3
exit 1
fi
}

synced_to_graph() {
is_synced="$(lnd_outside_cli getinfo | jq -r '.synced_to_graph')"
[[ "$is_synced" == "true" ]] || exit 1
}

# Open channel from lndoutside1 -> lnd1
pubkey="$(lnd_cli getinfo | jq -r '.identity_pubkey')"
endpoint="${COMPOSE_PROJECT_NAME}-lnd1-1:9735"
lnd_outside_cli connect "${pubkey}@${endpoint}" || true
retry 10 1 synced_to_graph
lnd_outside_cli openchannel \
--node_key "$pubkey" \
--local_amt "$local_amount"

retry 10 1 mempool_not_empty
retry 10 1 no_pending_channels

# Open channel with push from lndoutside1 -> lndoutside2
pubkey="$(lnd_outside_2_cli getinfo | jq -r '.identity_pubkey')"
endpoint="${COMPOSE_PROJECT_NAME}-lnd-outside-2-1:9735"
lnd_outside_cli connect "${pubkey}@${endpoint}" || true
retry 10 1 synced_to_graph
lnd_outside_cli openchannel \
--node_key "$pubkey" \
--local_amt "$local_amount" \
--push_amt "$push_amount"

retry 10 1 mempool_not_empty
retry 10 1 no_pending_channels

# FIXME: we may need some check on the graph or something else
# NB: I get randomly a "no route" error otherwise
sleep 10

# Fund lnd1 node with push_amount via funding user
login_user \
"$LND_FUNDING_TOKEN_NAME" \
"$LND_FUNDING_PHONE" \
"$CODE"

fund_wallet_from_lightning \
"$LND_FUNDING_TOKEN_NAME" \
"$LND_FUNDING_TOKEN_NAME.btc_wallet_id" \
"$push_amount"
}
Loading

0 comments on commit 11d3803

Please sign in to comment.