From 6c42f0d976d7cec3a6c5924c22174e4874b90973 Mon Sep 17 00:00:00 2001 From: Fmar <_@fmar.link> Date: Tue, 5 Mar 2024 11:21:25 +0100 Subject: [PATCH] add max-htlc-fee-rate --- .../workflows/build_max-htlc-fee-rate.yaml | 28 ++++++++ max-htlc-fee-rate/Dockerfile | 16 +++++ max-htlc-fee-rate/README.md | 14 ++++ max-htlc-fee-rate/run.sh | 67 +++++++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 .github/workflows/build_max-htlc-fee-rate.yaml create mode 100644 max-htlc-fee-rate/Dockerfile create mode 100644 max-htlc-fee-rate/README.md create mode 100755 max-htlc-fee-rate/run.sh diff --git a/.github/workflows/build_max-htlc-fee-rate.yaml b/.github/workflows/build_max-htlc-fee-rate.yaml new file mode 100644 index 0000000..b2cbc87 --- /dev/null +++ b/.github/workflows/build_max-htlc-fee-rate.yaml @@ -0,0 +1,28 @@ +name: Build s3-lastmodified-checker +on: + workflow_dispatch: + push: + paths: + - 'max-htlc-fee-rate**' + branches: + - master + tags: + - '*' +jobs: + build: + env: + REGISTRY: ghcr.io + IMAGENAME: max-htlc-fee-rate + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + name: Check out code + - name: Docker build + uses: mr-smithers-excellent/docker-build-push@v5 + with: + image: ${{ env.IMAGENAME }} + registry: ${{ env.REGISTRY }} + directory: s3-lastmodified-checker + dockerfile: s3-lastmodified-checker/Dockerfile + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} diff --git a/max-htlc-fee-rate/Dockerfile b/max-htlc-fee-rate/Dockerfile new file mode 100644 index 0000000..3821d1b --- /dev/null +++ b/max-htlc-fee-rate/Dockerfile @@ -0,0 +1,16 @@ +FROM alpine:latest + +# Update package repository and install bash and jq +RUN apk update && apk add --no-cache \ + bash \ + jq + +RUN apk add --no-cache curl && \ + curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" && \ + chmod +x kubectl && \ + mv kubectl /usr/local/bin/ + +ADD run.sh /scripts/ +RUN chmod 755 /scripts/run.sh + +ENTRYPOINT ["/scripts/run.sh"] diff --git a/max-htlc-fee-rate/README.md b/max-htlc-fee-rate/README.md new file mode 100644 index 0000000..80f94ce --- /dev/null +++ b/max-htlc-fee-rate/README.md @@ -0,0 +1,14 @@ +# max-htlc-fee-rate +Script for setting max_htlc & fee_rate on each channel in a LND node + +## Basic usage + +```sh +$ docker run -e LND_DEPLOYMENT=alby-mainnet-lnd-2 -e MAX_FEE_RATE=2000 -e MAX_HTLC_SIZE_MSAT=15000000000 ghcr.io/getalby/max-htlc-fee-rate +``` + +## Environment variables + +* `LND_DEPLOYMENT` - name of the deployment of LND, e.g. 'alby-mainnet-lnd-2' +* `MAX_FEE_RATE` - max fee rate to apply for each channel, this max will be for channels with full remote liquidity +* `MAX_HTLC_SIZE_MSAT` - max_htlc in msats to be set on each channel diff --git a/max-htlc-fee-rate/run.sh b/max-htlc-fee-rate/run.sh new file mode 100755 index 0000000..d0b4908 --- /dev/null +++ b/max-htlc-fee-rate/run.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +if [ -z "${LND_DEPLOYMENT}" ]; then + echo "LND_DEPLOYMENT environment variable is not set. Please set it before running this script." + exit 1 +fi + +if [ -z "${MAX_FEE_RATE}" ]; then + echo "MAX_FEE_RATE environment variable is not set. Please set it before running this script." + exit 1 +fi + +if [ -z "${MAX_HTLC_SIZE_MSAT}" ]; then + echo "MAX_HTLC_SIZE_MSAT environment variable is not set. Please set it before running this script." + exit 1 +fi + +# Continue with the existing script logic +echo "Using MAX_HTLC_SIZE_MSAT: $MAX_HTLC_SIZE_MSAT" + +# Get the LND Pod +lnd2Pod=$(kubectl get pods -n mainnet | grep $LND_DEPLOYMENT | awk '{print $1}' | grep -v backup) +echo "Using LND Pod: $lnd2Pod" + +# Get our node's public key +ourNodePub=$(kubectl exec $lnd2Pod -n mainnet -c $LND_DEPLOYMENT -- lncli getinfo | jq -r '.identity_pubkey') +echo "Our Node Public Key: $ourNodePub" + +# Get a list of all channels with channel_point and chan_id +channels=$(kubectl exec $lnd2Pod -n mainnet -c $LND_DEPLOYMENT -- lncli listchannels | jq -r '.channels[] | "\(.channel_point) \(.chan_id) \(.capacity) \(.remote_balance) \(.local_constraints.max_pending_amt_msat) \(.peer_alias)"') + +while IFS= read -r line; do + read -r channel_point chan_id capacity remote_balance max_pending_amt_msat peer_alias <<<$(echo $line | awk '{print $1, $2, $3, $4, $5, $6}') + # Since peer_alias can contain spaces, it might be split across multiple fields. Reconstruct it if necessary. + peer_alias=$(echo $line | cut -d' ' -f6-) + + # Fetch channel info using chan_id + chanInfo=$(kubectl exec $lnd2Pod -n mainnet -c $LND_DEPLOYMENT -- lncli getchaninfo --chan_id $chan_id) + + # Determine which node we are (node1 or node2), extract policy details + node1_pub=$(echo "$chanInfo" | jq -r '.node1_pub') + policyPath=".node1_policy" + if [[ "$ourNodePub" != "$node1_pub" ]]; then + policyPath=".node2_policy" + fi + + fee_base_msat=$(echo "$chanInfo" | jq -r "$policyPath.fee_base_msat") + existing_fee_rate_milli_msat=$(echo "$chanInfo" | jq -r "$policyPath.fee_rate_milli_msat") + time_lock_delta=$(echo "$chanInfo" | jq -r "$policyPath.time_lock_delta") + + # Adjusting MAX_HTLC_SIZE_MSAT based on max_pending_amt_msat using bc + adjusted_max_htlc_size_msat=$(echo "if ($MAX_HTLC_SIZE_MSAT > $max_pending_amt_msat) $max_pending_amt_msat else $MAX_HTLC_SIZE_MSAT" | bc) + + # Calculate fee_rate_ppm based on local_balance/capacity ratio + remote_balance_ratio=$((remote_balance *100/ capacity)) + # Adjust fee_rate_ppm calculation as needed based on your desired logic + # Here we set a basic example that scales linearly with the local_balance_ratio + if [ "$existing_fee_rate_milli_msat" -eq 0 ]; then + fee_rate_ppm=0 + else + fee_rate_ppm=$((remote_balance_ratio * $MAX_FEE_RATE / 100 + 10)) + fi + + echo "Updating channel with peer $peer_alias (remote balance ratio of ${remote_balance_ratio}%), setting fee rate -> $fee_rate_ppm ppm , max HTLC -> $adjusted_max_htlc_size_msat" + kubectl exec $lnd2Pod -n mainnet -c $LND_DEPLOYMENT -- lncli updatechanpolicy --chan_point $channel_point --max_htlc_msat $adjusted_max_htlc_size_msat --base_fee_msat $fee_base_msat --fee_rate_ppm $fee_rate_ppm --time_lock_delta $time_lock_delta + +done <<< "$channels"