Learning-Bitcoin-from-the-C.../18_2__Interlude_Accessing_a_Second_Lightning_Node.md
2020-10-13 13:52:17 -10:00

8.6 KiB

Interlude: Accessing a Second Lightning Node

When you played with Bitcoin you were accessing an existing network, and that made it relatively easy to work with: you just turned on bitcoind and you were immediately interacting with the network. That's now how Lightning works. It's fundamentally a peer-to-peer network, built up from the connections between any two individual nodes. In other words, to interact with the Lightning Network, you'll need to first find a node to connect to.

There are three ways to do so:

Asking for Information on a Node

If someone else already has a Lightning node on the network of your choice, just ask them for their ID.

If they are are running c-lightning, they just need to use the getinfo command:

$ lightning-cli getinfo
lightning-cli: WARNING: default network changing in 2020: please set network=testnet in config!
{
   "id": "02f3d74746934494fa378235e5bc44cfdbb5b8779d839263fb7f9218be032f6f61",
   "alias": "GREENSOURCE",
   "color": "02f3d7",
   ...
}

They can then tell you their id (02f3d74746934494fa378235e5bc44cfdbb5b8779d839263fb7f9218be032f6f61).

Creating a New c-lightning Node

However, for testing purposes, you probably want to have a second node under you own power. The easiest way to do so is to create a second c-lightning node on a new machine, using either Bitcoin Standup, per §2.1 or compiling it by hand, per §18.1.

Once you have your node up, you can run getinfo to retrieve your information, as shown above.

Creating a New LND Node

However, for our examples in the next chapter, we're instead going to create an LND node. This will allow us to demonstrate a bit of the depth of the Lightning ecosystem by showing how similar commands work on the two different platforms.

One way to do so is to run the Bitcoin Standup Scripts again on a new machine, but this time to choose LND, per §2.1.

Another is to compile LND from source code on a machine that you've set up with a Bitcoin node, as follows.

Compiling the LND Source Code

First, you need to download and install Go:

$ wget --progress=bar:force https://dl.google.com/go/"go1.14.4"."linux"-"amd64".tar.gz -O ~standup/"go1.14.4"."linux"-"amd64".tar.gz
$ /bin/tar xzf ~standup/"go1.14.4"."linux"-"amd64".tar.gz -C ~standup
$ sudo mv ~standup/go /usr/local

Be sure that the Go version is the most up to date (it's go1.14.4 at the current time), and the platform and architecture are right for your machine. (The above will work for Debian.)

Update your path:

$ export GOPATH=~standup/gocode
$ export PATH="$PATH":/usr/local/go/bin:"$GOPATH"/bin

Then be sure that go works:

$ go version
go version go1.14.4 linux/amd64

You'll also need git and make:

$ sudo apt-get install git
$ apt-get install build-essential
```
You're now ready to retrieve LND. Be sure to get the current verison (currently `v0.11.0-beta.rc4`).
```
$ go get -d github.com/lightningnetwork/lnd
```
And now you can compile:
```
$ cd "$GOPATH"/src/github.com/lightningnetwork/lnd
$ git checkout v0.11.0-beta.rc4
$ make
$ make install
```
This will install to `~/gocode/bin`, which is `$GOPATH/bin`.

And this will put it in global directories:
```
$ sudo cp $GOPATH/bin/lnd $GOPATH/bin/lncli /usr/bin
```
Finally, this will create some standard directories:
```
$ sudo mkdir -p /etc/lnd /var/lib/lnd
$ sudo chown standup -R /var/lib/lnd
```

### Creating an LND Config File

Unlike with c-lightning, you will need to create a default config file for LND.

First, you need to retrieve your rpcuser and rpcpassword. Here's an automated way to do so:
```
$ BITCOINRPC_USER=$(cat ~standup/.bitcoin/bitcoin.conf | grep rpcuser | awk -F = '{print $2}')
$ BITCOINRPC_PASS=$(cat ~standup/.bitcoin/bitcoin.conf | grep rpcpassword | awk -F = '{print $2}')
```

> :warning: **WARNING:** Obviously, never store your RPC password in a shell variable in a production environment.

Second, you need to enable ZMQ on your Bitcoind, if you didn't already in [§15.3](15_3_Receiving_Bitcoind_Notifications_with_C.md).

This requires adding the following to your `~/.bitcoin/bitcoin.conf` file:
```
zmqpubrawblock=tcp://127.0.0.1:28332
zmqpubrawtx=tcp://127.0.0.1:28333
```
You must then restart bitcoin. You can test that it's working as follows:
```
bitcoin-cli getzmqnotifications
[
  {
    "type": "pubrawblock",
    "address": "tcp://127.0.0.1:28332",
    "hwm": 1000
  },
  {
    "type": "pubrawtx",
    "address": "tcp://127.0.0.1:28333",
    "hwm": 1000
  }
]
```
Now you're ready to create a config file:
```
$ mkdir ~/.lnd
$ cat > ~/.lnd/lnd.conf << EOF
[Application Options]
maxlogfiles=3
maxlogfilesize=10
#externalip=1.1.1.1 # change to your public IP address if required.
alias=StandUp
listen=0.0.0.0:9735
debuglevel=debug
[Bitcoin]
bitcoin.active=1
bitcoin.node=bitcoind
bitcoin.testnet=true
[Bitcoind]
bitcoind.rpchost=localhost
bitcoind.rpcuser=$BITCOINRPC_USER
bitcoind.rpcpass=$BITCOINRPC_PASS
bitcoind.zmqpubrawblock=tcp://127.0.0.1:28332
bitcoind.zmqpubrawtx=tcp://127.0.0.1:28333
EOF
```

### Creating an LND Service

Finally, you can create an LND service to automatically run `lnd`:
```
$ cat > ~/lnd.service << EOF
# It is not recommended to modify this file in-place, because it will
# be overwritten during package upgrades. If you want to add further
# options or overwrite existing ones then use
# $ systemctl edit lnd.service
# See "man systemd.service" for details.
# Note that almost all daemon options could be specified in
# /etc/lnd/lnd.conf, except for those explicitly specified as arguments
# in ExecStart=
[Unit]
Description=LND Lightning Network Daemon
Requires=bitcoind.service
After=bitcoind.service
[Service]
ExecStart=/usr/bin/lnd
ExecStop=/usr/bin/lncli --lnddir /var/lib/lnd stop
PIDFile=/run/lnd/lnd.pid
User=standup
Type=simple
KillMode=process
TimeoutStartSec=60
TimeoutStopSec=60
Restart=always
RestartSec=60
[Install]
WantedBy=multi-user.target
EOF
```
You'll then need to install that:
```
$ sudo cp ~/lnd.service /etc/systemd/system
$ sudo systemctl enable lnd
$ sudo systemctl start lnd
```
(Expect this to take a minute the first time.)

### Creating a Wallet

The first time you run LND, you must create a wallet:
```
$ lncli --network=testnet create
```
It will ask you for a password and then ask if you want to enter an existing mnemonic (just hit `n` for the latter one).

You should now have a functioning `lnd`, which you can verify with `getinfo`:
```
$ lncli --network=testnet getinfo
{
    "version": "0.11.0-beta.rc4 commit=v0.11.0-beta.rc4",
    "commit_hash": "fc12656a1a62e5d69430bba6e4feb8cfbaf21542",
    "identity_pubkey": "032a7572dc013b6382cde391d79f292ced27305aa4162ec3906279fc4334602543",
    "alias": "StandUp",
    "color": "#3399ff",
    "num_pending_channels": 0,
    "num_active_channels": 0,
    "num_inactive_channels": 0,
    "num_peers": 2,
    "block_height": 1862848,
    "block_hash": "000000000000000ecb6fd95e1f486283d48683aa3111b6c23144a2056f5a1532",
    "best_header_timestamp": "1602632294",
    "synced_to_chain": true,
    "synced_to_graph": false,
    "testnet": true,
    "chains": [
        {
            "chain": "bitcoin",
            "network": "testnet"
        }
    ],
    "uris": [
    ],
    "features": {
        "0": {
            "name": "data-loss-protect",
            "is_required": true,
            "is_known": true
        },
        "5": {
            "name": "upfront-shutdown-script",
            "is_required": false,
            "is_known": true
        },
        "7": {
            "name": "gossip-queries",
            "is_required": false,
            "is_known": true
        },
        "9": {
            "name": "tlv-onion",
            "is_required": false,
            "is_known": true
        },
        "13": {
            "name": "static-remote-key",
            "is_required": false,
            "is_known": true
        },
        "15": {
            "name": "payment-addr",
            "is_required": false,
            "is_known": true
        },
        "17": {
            "name": "multi-path-payments",
            "is_required": false,
            "is_known": true
        }
    }
}
```
This node's ID is `032a7572dc013b6382cde391d79f292ced27305aa4162ec3906279fc4334602543`.

## Summary: Accessing a Second Lightning Node

You always need two Lightning nodes to form a channel. If you don't have someone else who is testing things out with you, you're going to need to create a second one, either using c-lightning or (as we will in our examples) LND

## What's Next?

Continue "Understanding Your Lightning Setup" with [§18.3: Setting Up_a_Channel](18_3_Setting_Up_a_Channel.md).