Start a lightning node on regtest with CLN

LNROOM #2November 22, 2022

In this episode we look at the start_ln and start_nodes commands defined in the file contrib/startup_regtest.sh of the lightning repository and we pick out the things we need to start a Lightning node ourselves.

Transcript with corrections and improvements

Hi guys, welcome to the LN Room, I'm Tony Aldon and today in this episode 2 we are going to start a Lightning node on regtest chain using Core Lightning.

Let's remember what we did last time.

We've created and pay a BOLT11 invoice on Lightning Network running on regtest chain that has one channel funding from one node to another.

To do so we've used the script contrib/startup_regtest.sh provided in the lightning repository.

Specifically we've used the command start_ln to start bitcoind and lightningd for two differents nodes.

Today we're going to look at the start_ln and start_nodes commands and pick out the things we need to start a Lightning node ourselves.

Let's jump into the terminal.

start_ln command in contrib/startup_regtest.sh

We are in the lightning repository:

◉ tony@tony:~/lnroom/lightning:[git»(HEAD detached at v0.12.1)]
$ ls
action.yml     CHANGELOG.md  common     doc         lightningd   pyproject.toml  wallet
bitcoin        channeld      configure  Dockerfile  Makefile     README.md       wire
Cargo.lock     cli           connectd   external    onchaind     so
Cargo.toml     cln-grpc      contrib    gossipd     openingd     test
ccan           cln-rpc       db         hsmd        plugins      tests
ccan_compat.h  closingd      devtools   LICENSE     poetry.lock  tools

Let's have a look to the file contrib/startup_regtest.sh, specifically the command start_ln:

# contrib/startup_regtest.sh

start_ln() {
        # Start bitcoind in the background
        test -f "$PATH_TO_BITCOIN/regtest/bitcoind.pid" || \
                bitcoind -regtest -txindex -fallbackfee=0.00000253 -daemon

        # Wait for it to start.
        while ! bitcoin-cli -regtest ping 2> /tmp/null; do echo "awaiting bitcoind..." && sleep 1; done

        # Kick it out of initialblockdownload if necessary
        if bitcoin-cli -regtest getblockchaininfo | grep -q 'initialblockdownload.*true'; then
                # Modern bitcoind needs createwallet
                echo "Making \"default\" bitcoind wallet."
                bitcoin-cli -regtest createwallet default >/dev/null 2>&1
                bitcoin-cli -regtest generatetoaddress 1 "$(bitcoin-cli -regtest getnewaddress)" > /dev/null
        else
                bitcoin-cli -regtest loadwallet default
        fi
        alias bt-cli='bitcoin-cli -regtest'

        if [ -z "$1" ]; then
                nodes=2
        else
                nodes="$1"
        fi
        start_nodes "$nodes" regtest
        echo "  bt-cli, stop_ln, fund_nodes"
}

As we don't want start_ln to start the nodes, we put in comment the last part of the command:

# contrib/startup_regtest.sh

start_ln() {
  ...

        # if [ -z "$1" ]; then
        #       nodes=2
        # else
        #       nodes="$1"
        # fi
        # start_nodes "$nodes" regtest
        # echo "        bt-cli, stop_ln, fund_nodes"
}

start_nodes command in contrib/startup_regtest.sh

Now we look at the command start_nodes to see how nodes are started in that script:

# contrib/startup_regtest.sh

start_nodes() {
        if [ -z "$1" ]; then
                node_count=2
        else
                node_count=$1
        fi
        if [ "$node_count" -gt 100 ]; then
                node_count=100
        fi
        if [ -z "$2" ]; then
                network=regtest
        else
                network=$2
        fi

        LN_NODES=$node_count

        for i in $(seq $node_count); do
                socket=$(( 7070 + i * 101))
                mkdir -p "/tmp/l$i-$network"
                # Node config
                cat <<- EOF > "/tmp/l$i-$network/config"
                network=$network
                log-level=debug
                log-file=/tmp/l$i-$network/log
                addr=localhost:$socket
                allow-deprecated-apis=false
                EOF

                # If we've configured to use developer, add dev options
                if $LIGHTNINGD --help | grep -q dev-fast-gossip; then
                        cat <<- EOF >> "/tmp/l$i-$network/config"
                        dev-fast-gossip
                        dev-bitcoind-poll=5
                        experimental-dual-fund
                        experimental-offers
                        funder-policy=match
                        funder-policy-mod=100
                        funder-min-their-funding=10000
                        funder-per-channel-max=100000
                        funder-fuzz-percent=0
                        lease-fee-base-sat=2sat
                        lease-fee-basis=50
                        EOF
                fi


                # Start the lightning nodes
                test -f "/tmp/l$i-$network/lightningd-$network.pid" || \
                        "$LIGHTNINGD" "--lightning-dir=/tmp/l$i-$network" &
                # shellcheck disable=SC2139 disable=SC2086
                alias l$i-cli="$LCLI --lightning-dir=/tmp/l$i-$network"
                # shellcheck disable=SC2139 disable=SC2086
                alias l$i-log="less /tmp/l$i-$network/log"
        done

        # Give a hint.
        echo "Commands: "
        for i in $(seq $node_count); do
                echo "  l$i-cli, l$i-log,"
        done
}

In that command, we see that to start a node we need:

  • a directory in the file system, for instance /tmp/my-regtest/,

  • a config file in that directory (/tmp/my-regtest/config),

  • to run the command:

    $ lightningd --lightning-dir=/tmp/my-regtest/ --daemon

Start Bitcoin on regtest chain

Now that we know what to do, let's source the file contrib/startup_regtest.sh and start Bitcoin on regtest chain by running the following commands:

◉ tony@tony:~/lnroom/lightning:[git»(HEAD detached at v0.12.1)]
$ source contrib/startup_regtest.sh
lightning-cli is /usr/bin/lightning-cli
lightningd is /usr/bin/lightningd
◉ tony@tony:~/lnroom/lightning:[git»(HEAD detached at v0.12.1)]
$ start_ln
Bitcoin Core starting
awaiting bitcoind...
Making "default" bitcoind wallet.

To verify our setup and get information about our Bitcoin chain, we can run the following commands:

◉ tony@tony:~/lnroom/lightning:[git»(HEAD detached at v0.12.1)]
$ bitcoin-cli -regtest -getinfo
Chain: regtest
Blocks: 1
Headers: 1
Verification progress: 100.0000%
Difficulty: 4.656542373906925e-10

Network: in 0, out 0, total 0
Version: 230000
Time offset (s): 0
Proxies: n/a
Min tx relay fee rate (BTC/kvB): 0.00001000

Wallet: default
Keypool size: 4000
Transaction fee rate (-paytxfee) (BTC/kvB): 0.00000000

Balance: 0.00000000

Warnings:

Setup a Lightning node

Let's setup our Lightning node.

First we create the directory /tmp/my-regtest/, move into it,

◉ tony@tony:~/lnroom/lightning:[git»(HEAD detached at v0.12.1)]
$ mkdir -p /tmp/my-regtest/ && cd /tmp/my-regtest/

and we also create the file /tmp/my-regtest/config in which we put the following information:

network=regtest
log-level=debug
log-file=/tmp/my-regtest/log
addr=localhost:7071
allow-deprecated-apis=false

Now we can start our node by running the following command:

◉ tony@tony:/tmp/my-regtest:
$ lightningd --lightning-dir=/tmp/my-regtest/ --daemon

And we can check that it has effectively been started using the sub command getinfo of lightning-cli like this:

$ lightning-cli --lightning-dir=/tmp/my-regtest/ getinfo
{
   "id": "03dec98e211aade19e9d2aef11395b1e7e42f7f3edaba94c3b65cfa227d8681f1c",
   "alias": "IRATEFIRE",
   "color": "03dec9",
   "num_peers": 0,
   "num_pending_channels": 0,
   "num_active_channels": 0,
   "num_inactive_channels": 0,
   "address": [],
   "binding": [
      {
         "type": "ipv4",
         "address": "127.0.0.1",
         "port": 7071
      }
   ],
   "version": "0.12.1",
   "blockheight": 1,
   "network": "regtest",
   "fees_collected_msat": 0,
   "lightning-dir": "/tmp/my-regtest/regtest",
   "our_features": {
      "init": "08a000080269a2",
      "node": "88a000080269a2",
      "channel": "",
      "invoice": "02000000024100"
   }
}

We've done what we wanted to do today: starting a Lightning node ourselves retrieving information from in the command start_ln of the script contrib/startup_regtest.sh provided in the lightning repository.

Bonus

We can look at the PID of the process corresponding to lightningd:

◉ tony@tony:/tmp/my-regtest:
$ ls
config  lightningd-regtest.pid  log  regtest
◉ tony@tony:/tmp/my-regtest:
$ cat lightningd-regtest.pid
3261
◉ tony@tony:/tmp/my-regtest:
$ ps -ax | grep lightning
 3261 ?        Ss     0:00 lightningd --lightning-dir=/tmp/my-regtest/ --daemon
 3262 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/autoclean
 3263 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/chanbackup
 3264 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/bcli
 3265 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/commando
 3267 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/funder
 3268 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/topology
 3269 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/keysend
 3270 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/offers
 3271 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/pay
 3272 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/txprepare
 3273 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/spenderp
 3274 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/bookkeeper
 3282 pts/1    SL     0:00 /usr/libexec/c-lightning/lightning_hsmd
 3284 pts/1    S      0:00 /usr/libexec/c-lightning/lightning_connectd
 3293 pts/1    S      0:00 /usr/libexec/c-lightning/lightning_gossipd
 5799 pts/1    R+     0:00 grep --color=auto lightning

We also can show the Unix domain socket of that node used for receiving commands which is by default the file lightning-rpc:

◉ tony@tony:/tmp/my-regtest:
$ ls -l regtest/
total 284
-rw-r--r-- 1 tony tony  36864 nov 22 18:34 accounts.sqlite3
-r-------- 1 tony tony     57 nov 22 18:34 emergency.recover
-rw------- 1 tony tony      1 nov 22 18:34 gossip_store
-r-------- 1 tony tony     32 nov 22 18:34 hsm_secret
-rw-r--r-- 1 tony tony 241664 nov 22 18:34 lightningd.sqlite3
srw------- 1 tony tony      0 nov 22 18:34 lightning-rpc

We are done. This is what I wanted to do with you today. I hope you get some value from this video.

See you next time for another episode of the LN Room.

Terminal session

We ran the following commands in this order:

$ ls
$ source contrib/startup_regtest.sh
$ start_ln
$ bitcoin-cli -regtest -getinfo
$ mkdir -p /tmp/my-regtest/
$ cd /tmp/my-regtest/
$ ls
$ cat config
$ lightningd --lightning-dir=/tmp/my-regtest/ --daemon
$ lightning-cli --lightning-dir=/tmp/my-regtest/ getinfo
$ ls
$ cat lightningd-regtest.pid
$ ps -ax | grep lightning
$ ls
$ ls -l regtest/

And below you can read the terminal session (command lines and outputs):

◉ tony@tony:~/lnroom/lightning:[git»(HEAD detached at v0.12.1)]
$ ls
action.yml     CHANGELOG.md  common     doc         lightningd   pyproject.toml  wallet
bitcoin        channeld      configure  Dockerfile  Makefile     README.md       wire
Cargo.lock     cli           connectd   external    onchaind     so
Cargo.toml     cln-grpc      contrib    gossipd     openingd     test
ccan           cln-rpc       db         hsmd        plugins      tests
ccan_compat.h  closingd      devtools   LICENSE     poetry.lock  tools
◉ tony@tony:~/lnroom/lightning:[git»(HEAD detached at v0.12.1)]
$ source contrib/startup_regtest.sh
lightning-cli is /usr/bin/lightning-cli
lightningd is /usr/bin/lightningd
◉ tony@tony:~/lnroom/lightning:[git»(HEAD detached at v0.12.1)]
$ start_ln
Bitcoin Core starting
awaiting bitcoind...
Making "default" bitcoind wallet.
◉ tony@tony:~/lnroom/lightning:[git»(HEAD detached at v0.12.1)]
$ bitcoin-cli -regtest -getinfo
Chain: regtest
Blocks: 1
Headers: 1
Verification progress: 100.0000%
Difficulty: 4.656542373906925e-10

Network: in 0, out 0, total 0
Version: 230000
Time offset (s): 0
Proxies: n/a
Min tx relay fee rate (BTC/kvB): 0.00001000

Wallet: default
Keypool size: 4000
Transaction fee rate (-paytxfee) (BTC/kvB): 0.00000000

Balance: 0.00000000

Warnings:
◉ tony@tony:~/lnroom/lightning:[git»(HEAD detached at v0.12.1)]
$ mkdir -p /tmp/my-regtest/
◉ tony@tony:~/lnroom/lightning:[git»(HEAD detached at v0.12.1)]
$ cd /tmp/my-regtest/
◉ tony@tony:/tmp/my-regtest:
$ ls
config
◉ tony@tony:/tmp/my-regtest:
$ cat config
network=regtest
log-level=debug
log-file=/tmp/my-regtest/log
addr=localhost:7071
allow-deprecated-apis=false
◉ tony@tony:/tmp/my-regtest:
$ lightningd --lightning-dir=/tmp/my-regtest/ --daemon
◉ tony@tony:/tmp/my-regtest:
$ lightning-cli --lightning-dir=/tmp/my-regtest/ getinfo
{
   "id": "03dec98e211aade19e9d2aef11395b1e7e42f7f3edaba94c3b65cfa227d8681f1c",
   "alias": "IRATEFIRE",
   "color": "03dec9",
   "num_peers": 0,
   "num_pending_channels": 0,
   "num_active_channels": 0,
   "num_inactive_channels": 0,
   "address": [],
   "binding": [
      {
         "type": "ipv4",
         "address": "127.0.0.1",
         "port": 7071
      }
   ],
   "version": "0.12.1",
   "blockheight": 1,
   "network": "regtest",
   "fees_collected_msat": 0,
   "lightning-dir": "/tmp/my-regtest/regtest",
   "our_features": {
      "init": "08a000080269a2",
      "node": "88a000080269a2",
      "channel": "",
      "invoice": "02000000024100"
   }
}
◉ tony@tony:/tmp/my-regtest:
$ ls
config  lightningd-regtest.pid  log  regtest
◉ tony@tony:/tmp/my-regtest:
$ cat lightningd-regtest.pid
3261
◉ tony@tony:/tmp/my-regtest:
$ ps -ax | grep lightning
 3261 ?        Ss     0:00 lightningd --lightning-dir=/tmp/my-regtest/ --daemon
 3262 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/autoclean
 3263 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/chanbackup
 3264 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/bcli
 3265 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/commando
 3267 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/funder
 3268 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/topology
 3269 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/keysend
 3270 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/offers
 3271 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/pay
 3272 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/txprepare
 3273 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/spenderp
 3274 pts/1    S      0:00 /usr/libexec/c-lightning/plugins/bookkeeper
 3282 pts/1    SL     0:00 /usr/libexec/c-lightning/lightning_hsmd
 3284 pts/1    S      0:00 /usr/libexec/c-lightning/lightning_connectd
 3293 pts/1    S      0:00 /usr/libexec/c-lightning/lightning_gossipd
 5799 pts/1    R+     0:00 grep --color=auto lightning
◉ tony@tony:/tmp/my-regtest:
$ ls
config  lightningd-regtest.pid  log  regtest
◉ tony@tony:/tmp/my-regtest:
$ ls -l regtest/
total 284
-rw-r--r-- 1 tony tony  36864 nov 22 18:34 accounts.sqlite3
-r-------- 1 tony tony     57 nov 22 18:34 emergency.recover
-rw------- 1 tony tony      1 nov 22 18:34 gossip_store
-r-------- 1 tony tony     32 nov 22 18:34 hsm_secret
-rw-r--r-- 1 tony tony 241664 nov 22 18:34 lightningd.sqlite3
srw------- 1 tony tony      0 nov 22 18:34 lightning-rpc

Resources