2019-02-08 16:06:34 +01:00
|
|
|
Hacking
|
|
|
|
=======
|
|
|
|
|
2017-08-29 07:15:39 +02:00
|
|
|
Welcome, fellow coder!
|
|
|
|
|
|
|
|
This repository contains a code to run a lightning protocol daemon.
|
|
|
|
It's broken into subdaemons, with the idea being that we can add more
|
|
|
|
layers of separation between different clients and extra barriers to
|
|
|
|
exploits.
|
|
|
|
|
|
|
|
It is designed to implement the lightning protocol as specified in
|
|
|
|
[various BOLTs](https://github.com/lightningnetwork/lightning-rfc).
|
|
|
|
|
2018-03-01 14:58:48 +01:00
|
|
|
|
2017-08-29 07:15:39 +02:00
|
|
|
Getting Started
|
|
|
|
---------------
|
2018-03-15 07:36:31 +01:00
|
|
|
It's in C, to encourage alternate implementations. Patches are welcome!
|
|
|
|
You should read our [Style Guide](STYLE.md).
|
2018-03-01 14:58:48 +01:00
|
|
|
|
2018-09-03 03:08:56 +02:00
|
|
|
To read the code, you should start from
|
2022-04-10 00:41:18 +02:00
|
|
|
[lightningd.c](https://github.com/ElementsProject/lightning/blob/master/lightningd/lightningd.c) and hop your way through
|
2018-09-03 03:08:56 +02:00
|
|
|
the '~' comments at the head of each daemon in the suggested
|
|
|
|
order.
|
2017-08-29 07:15:39 +02:00
|
|
|
|
2018-01-03 06:28:11 +01:00
|
|
|
The Components
|
|
|
|
--------------
|
2017-08-29 07:15:39 +02:00
|
|
|
Here's a list of parts, with notes:
|
|
|
|
|
|
|
|
* ccan - useful routines from http://ccodearchive.net
|
|
|
|
- Use make update-ccan to update it.
|
|
|
|
- Use make update-ccan CCAN_NEW="mod1 mod2..." to add modules
|
2018-01-03 06:28:11 +01:00
|
|
|
- Do not edit this! If you want a wrapper, add one to common/utils.h.
|
2017-08-29 07:15:39 +02:00
|
|
|
|
|
|
|
* bitcoin/ - bitcoin script, signature and transaction routines.
|
|
|
|
- Not a complete set, but enough for our purposes.
|
|
|
|
|
2017-12-07 23:59:39 +01:00
|
|
|
* external/ - external libraries from other sources
|
2018-01-03 06:28:11 +01:00
|
|
|
- libbacktrace - library to provide backtraces when things go wrong.
|
2017-08-29 07:15:39 +02:00
|
|
|
- libsodium - encryption library (should be replaced soon with built-in)
|
|
|
|
- libwally-core - bitcoin helper library
|
|
|
|
- secp256k1 - bitcoin curve encryption library within libwally-core
|
|
|
|
- jsmn - tiny JSON parsing helper
|
|
|
|
|
|
|
|
* tools/ - tools for building
|
|
|
|
- check-bolt.c: check the source code contains correct BOLT quotes
|
|
|
|
(as used by check-source)
|
2019-07-16 01:20:37 +02:00
|
|
|
- generate-wire.py: generates wire marshal/unmarshal-ing
|
2019-07-24 01:54:21 +02:00
|
|
|
routines for subdaemons and BOLT specs.
|
2018-03-01 14:58:48 +01:00
|
|
|
- mockup.sh / update-mocks.sh: tools to generate mock functions for
|
|
|
|
unit tests.
|
2018-01-03 06:28:11 +01:00
|
|
|
|
2018-09-03 03:08:56 +02:00
|
|
|
* tests/ - blackbox tests (mainly)
|
|
|
|
- unit tests are in tests/ subdirectories in each other directory.
|
|
|
|
|
|
|
|
* doc/ - you are here
|
|
|
|
|
2018-01-03 06:28:11 +01:00
|
|
|
* devtools/ - tools for developers
|
2018-09-03 03:08:56 +02:00
|
|
|
- Generally for decoding our formats.
|
2017-08-29 07:15:39 +02:00
|
|
|
|
|
|
|
* contrib/ - python support and other stuff which doesn't belong :)
|
|
|
|
|
2019-07-16 01:20:37 +02:00
|
|
|
* wire/ - basic marshalling/un for messages defined in the BOLTs
|
2017-08-29 07:15:39 +02:00
|
|
|
|
|
|
|
* common/ - routines needed by any two or more of the directories below
|
|
|
|
|
|
|
|
* cli/ - commandline utility to control lightning daemon.
|
|
|
|
|
2018-03-01 14:58:48 +01:00
|
|
|
* lightningd/ - master daemon which controls the subdaemons and passes
|
|
|
|
peer file descriptors between them.
|
2017-08-29 07:15:39 +02:00
|
|
|
|
|
|
|
* wallet/ - database code used by master for tracking what's happening.
|
|
|
|
|
2018-03-01 14:58:48 +01:00
|
|
|
* hsmd/ - daemon which looks after the cryptographic secret, and performs
|
|
|
|
commitment signing.
|
2017-08-29 07:15:39 +02:00
|
|
|
|
2018-09-03 03:08:56 +02:00
|
|
|
* gossipd/ - daemon to maintain routing information and broadcast gossip.
|
|
|
|
|
|
|
|
* connectd/ - daemon to connect to other peers, and receive incoming.
|
2017-08-29 07:15:39 +02:00
|
|
|
|
2018-09-03 03:08:56 +02:00
|
|
|
* openingd/ - daemon to open a channel for a single peer, and chat to
|
|
|
|
a peer which doesn't have any channels/
|
2017-08-29 07:15:39 +02:00
|
|
|
|
2018-03-01 14:58:48 +01:00
|
|
|
* channeld/ - daemon to operate a single peer once channel is operating
|
|
|
|
normally.
|
2017-08-29 07:15:39 +02:00
|
|
|
|
|
|
|
* closingd/ - daemon to handle mutual closing negotiation with a single peer.
|
|
|
|
|
2018-07-20 18:15:02 +02:00
|
|
|
* onchaind/ - daemon to handle a single channel which has had its funding
|
2018-03-01 14:58:48 +01:00
|
|
|
transaction spent.
|
2017-08-29 07:15:39 +02:00
|
|
|
|
2018-01-14 07:00:26 +01:00
|
|
|
Debugging
|
|
|
|
---------
|
|
|
|
|
2022-04-06 07:09:48 +02:00
|
|
|
You can build Core Lightning with DEVELOPER=1 to use dev commands listed in
|
2020-10-22 19:50:52 +02:00
|
|
|
``cli/lightning-cli help``. ``./configure --enable-developer`` will do that.
|
|
|
|
You can log console messages with log_info() in lightningd and status_debug()
|
|
|
|
in other subdaemons.
|
2018-08-19 01:37:43 +02:00
|
|
|
|
2018-01-14 07:00:26 +01:00
|
|
|
You can debug crashing subdaemons with the argument
|
2018-09-04 04:53:32 +02:00
|
|
|
`--dev-debugger=channeld`, where `channeld` is the subdaemon name. It
|
|
|
|
will run `gnome-terminal` by default with a gdb attached to the
|
|
|
|
subdaemon when it starts. You can change the terminal used by setting
|
|
|
|
the `DEBUG_TERM` environment variable, such as `DEBUG_TERM="xterm -e"`
|
|
|
|
or `DEBUG_TERM="konsole -e"`.
|
|
|
|
|
|
|
|
It will also print out (to stderr) the gdb command for manual connection. The
|
|
|
|
subdaemon will be stopped (it sends itself a SIGSTOP); you'll need to
|
|
|
|
`continue` in gdb.
|
2018-01-14 07:00:26 +01:00
|
|
|
|
2018-01-25 19:01:08 +01:00
|
|
|
Database
|
|
|
|
--------
|
|
|
|
|
2022-04-06 07:09:48 +02:00
|
|
|
Core Lightning state is persisted in `lightning-dir`.
|
2018-03-01 14:58:48 +01:00
|
|
|
It is a sqlite database stored in the `lightningd.sqlite3` file, typically
|
2019-12-16 10:17:58 +01:00
|
|
|
under `~/.lightning/<network>/`.
|
2018-03-01 14:58:48 +01:00
|
|
|
You can run queries against this file like so:
|
2018-01-25 19:01:08 +01:00
|
|
|
|
2019-12-16 10:17:58 +01:00
|
|
|
$ sqlite3 ~/.lightning/bitcoin/lightningd.sqlite3 \
|
2018-03-01 15:05:52 +01:00
|
|
|
"SELECT HEX(prev_out_tx), prev_out_index, status FROM outputs"
|
2018-01-25 19:01:08 +01:00
|
|
|
|
|
|
|
Or you can launch into the sqlite3 repl and check things out from there:
|
|
|
|
|
2019-12-16 10:17:58 +01:00
|
|
|
$ sqlite3 ~/.lightning/bitcoin/lightningd.sqlite3
|
2018-01-25 19:01:08 +01:00
|
|
|
SQLite version 3.21.0 2017-10-24 18:55:49
|
|
|
|
Enter ".help" for usage hints.
|
|
|
|
sqlite> .tables
|
|
|
|
channel_configs invoices peers vars
|
|
|
|
channel_htlcs outputs shachain_known version
|
|
|
|
channels payments shachains
|
|
|
|
sqlite> .schema outputs
|
|
|
|
...
|
|
|
|
|
|
|
|
Some data is stored as raw bytes, use `HEX(column)` to pretty print these.
|
|
|
|
|
2018-03-01 14:58:48 +01:00
|
|
|
Make sure that clightning is not running when you query the database,
|
|
|
|
as some queries may lock the database and cause crashes.
|
2018-01-25 19:01:08 +01:00
|
|
|
|
2018-01-28 01:41:31 +01:00
|
|
|
#### Common variables
|
|
|
|
Table `vars` contains global variables used by lightning node.
|
|
|
|
|
2019-12-16 10:17:58 +01:00
|
|
|
$ sqlite3 ~/.lightning/bitcoin/lightningd.sqlite3
|
2018-01-28 01:41:31 +01:00
|
|
|
SQLite version 3.21.0 2017-10-24 18:55:49
|
|
|
|
Enter ".help" for usage hints.
|
|
|
|
sqlite> .headers on
|
|
|
|
sqlite> select * from vars;
|
|
|
|
name|val
|
|
|
|
next_pay_index|2
|
|
|
|
bip32_max_index|4
|
|
|
|
...
|
|
|
|
|
|
|
|
Variables:
|
|
|
|
* `next_pay_index` next resolved invoice counter that will get assigned.
|
|
|
|
* `bip32_max_index` last wallet derivation counter.
|
|
|
|
|
2018-03-01 14:58:48 +01:00
|
|
|
Note: Each time `newaddr` command is called, `bip32_max_index` counter
|
|
|
|
is increased to the last derivation index.
|
|
|
|
Each address generated after `bip32_max_index` is not included as
|
|
|
|
lightning funds.
|
2018-01-28 01:41:31 +01:00
|
|
|
|
|
|
|
|
2020-10-22 19:50:52 +02:00
|
|
|
Build and Development
|
|
|
|
---------------------
|
2022-03-17 04:20:21 +01:00
|
|
|
Install the following dependencies for best results:
|
2018-01-03 06:28:11 +01:00
|
|
|
|
2018-07-20 18:15:02 +02:00
|
|
|
```
|
2021-11-07 17:36:24 +01:00
|
|
|
sudo apt update
|
|
|
|
sudo apt install valgrind cppcheck shellcheck libsecp256k1-dev libpq-dev
|
2018-07-20 18:15:02 +02:00
|
|
|
```
|
2018-01-03 06:28:11 +01:00
|
|
|
|
2022-03-17 04:20:21 +01:00
|
|
|
Re-run `configure` and build using `make`:
|
2018-12-07 07:12:12 +01:00
|
|
|
|
|
|
|
```
|
2019-02-11 23:16:00 +01:00
|
|
|
./configure --enable-developer
|
2020-10-22 19:50:52 +02:00
|
|
|
make -j$(nproc)
|
2018-12-07 07:12:12 +01:00
|
|
|
```
|
|
|
|
|
2020-10-22 19:50:52 +02:00
|
|
|
|
|
|
|
Testing
|
|
|
|
-------
|
2018-07-20 18:15:02 +02:00
|
|
|
Tests are run with: `make check [flags]` where the pertinent flags are:
|
|
|
|
|
|
|
|
```
|
|
|
|
DEVELOPER=[0|1] - developer mode increases test coverage
|
|
|
|
VALGRIND=[0|1] - detects memory leaks during test execution but adds a significant delay
|
|
|
|
PYTEST_PAR=n - runs pytests in parallel
|
|
|
|
```
|
|
|
|
|
|
|
|
A modern desktop can build and run through all the tests in a couple of minutes with:
|
|
|
|
|
2018-09-20 23:37:59 +02:00
|
|
|
make -j12 full-check PYTEST_PAR=24 DEVELOPER=1 VALGRIND=0
|
2018-07-20 18:15:02 +02:00
|
|
|
|
2018-09-04 04:54:32 +02:00
|
|
|
Adjust `-j` and `PYTEST_PAR` accordingly for your hardware.
|
2018-07-20 18:15:02 +02:00
|
|
|
|
2022-04-06 20:44:58 +02:00
|
|
|
There are four kinds of tests:
|
2018-07-20 18:15:02 +02:00
|
|
|
|
|
|
|
* **source tests** - run by `make check-source`, looks for whitespace,
|
2018-01-03 06:28:11 +01:00
|
|
|
header order, and checks formatted quotes from BOLTs if BOLTDIR
|
2018-09-20 23:37:59 +02:00
|
|
|
exists.
|
2018-01-03 06:28:11 +01:00
|
|
|
|
2019-06-20 20:13:14 +02:00
|
|
|
* **unit tests** - standalone programs that can be run individually. You can
|
|
|
|
also run all of the unit tests with `make check-units`.
|
2018-07-20 18:15:02 +02:00
|
|
|
They are `run-*.c` files in test/ subdirectories used to test routines
|
|
|
|
inside C source files.
|
|
|
|
|
|
|
|
You should insert the lines when implementing a unit test:
|
|
|
|
|
|
|
|
`/* AUTOGENERATED MOCKS START */`
|
|
|
|
|
|
|
|
`/* AUTOGENERATED MOCKS END */`
|
|
|
|
|
|
|
|
and `make update-mocks` will automatically generate stub functions which will
|
|
|
|
allow you to link (and conveniently crash if they're called).
|
|
|
|
|
2020-01-05 13:57:47 +01:00
|
|
|
* **blackbox tests** - These tests setup a mini-regtest environment and test
|
2018-07-20 18:15:02 +02:00
|
|
|
lightningd as a whole. They can be run individually:
|
2018-01-03 06:28:11 +01:00
|
|
|
|
2020-03-04 13:56:45 +01:00
|
|
|
`PYTHONPATH=contrib/pylightning:contrib/pyln-client:contrib/pyln-testing:contrib/pyln-proto py.test -v tests/`
|
2018-07-20 18:15:02 +02:00
|
|
|
|
2018-09-04 04:54:32 +02:00
|
|
|
You can also append `-k TESTNAME` to run a single test. Environment variables
|
|
|
|
`DEBUG_SUBD=<subdaemon>` and `TIMEOUT=<seconds>` can be useful for debugging
|
|
|
|
subdaemons on individual tests.
|
2018-01-03 06:28:11 +01:00
|
|
|
|
2019-09-03 13:39:15 +02:00
|
|
|
* **pylightning tests** - will check contrib pylightning for codestyle and run
|
|
|
|
the tests in `contrib/pylightning/tests` afterwards:
|
|
|
|
|
2019-11-28 11:28:05 +01:00
|
|
|
`make check-python`
|
2019-09-03 13:39:15 +02:00
|
|
|
|
2022-04-06 20:56:57 +02:00
|
|
|
Our Github Actions instance (see `.github/workflows/*.yml`) runs all these for each
|
2018-03-01 14:58:48 +01:00
|
|
|
pull request.
|
2018-01-03 06:28:11 +01:00
|
|
|
|
2020-10-22 19:50:52 +02:00
|
|
|
#### Additional Environment Variables
|
|
|
|
|
|
|
|
```
|
|
|
|
TEST_CHECK_DBSTMTS=[0|1] - When running blackbox tests, this will
|
|
|
|
load a plugin that logs all compiled
|
|
|
|
and expanded database statements.
|
|
|
|
Note: Only SQLite3.
|
|
|
|
TEST_DB_PROVIDER=[sqlite3|postgres] - Selects the database to use when running
|
|
|
|
blackbox tests.
|
2021-09-22 06:15:13 +02:00
|
|
|
EXPERIMENTAL_DUAL_FUND=[0|1] - Enable dual-funding tests.
|
2020-10-22 19:50:52 +02:00
|
|
|
```
|
2019-07-16 01:20:37 +02:00
|
|
|
|
2022-02-09 17:10:09 +01:00
|
|
|
#### Troubleshooting
|
|
|
|
|
|
|
|
##### Valgrind complains about code we don't control
|
|
|
|
|
|
|
|
Sometimes `valgrind` will complain about code we do not control
|
|
|
|
ourselves, either because it's in a library we use or it's a false
|
|
|
|
positive. There are generally three ways to address these issues
|
|
|
|
(in descending order of preference):
|
|
|
|
|
|
|
|
1. Add a suppression for the one specific call that is causing the
|
|
|
|
issue. Upon finding an issue `valgrind` is instructed in the
|
|
|
|
testing framework to print filters that'd match the issue. These
|
|
|
|
can be added to the suppressions file under
|
|
|
|
`tests/valgrind-suppressions.txt` in order to explicitly skip
|
|
|
|
reporting these in future. This is preferred over the other
|
|
|
|
solutions since it only disables reporting selectively for things
|
|
|
|
that were manually checked. See the [valgrind docs][vg-supp] for
|
|
|
|
details.
|
|
|
|
2. Add the process that `valgrind` is complaining about to the
|
|
|
|
`--trace-children-skip` argument in `pyln-testing`. This is used
|
|
|
|
in cases of full binaries not being under our control, such as the
|
|
|
|
`python3` interpreter used in tests that run plugins. Do not use
|
|
|
|
this for binaries that are compiled from our code, as it tends to
|
|
|
|
mask real issues.
|
|
|
|
3. Mark the test as skipped if running under `valgrind`. It's mostly
|
|
|
|
used to skip tests that otherwise would take considerably too long
|
|
|
|
to test on CI. We discourage this for suppressions, since it is a
|
|
|
|
very blunt tool.
|
|
|
|
|
|
|
|
[vg-supp]: https://valgrind.org/docs/manual/manual-core.html#manual-core.suppress
|
|
|
|
|
2019-07-16 01:20:37 +02:00
|
|
|
Making BOLT Modifications
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
All of code for marshalling/unmarshalling BOLT protocol messages is generated
|
|
|
|
directly from the spec. These are pegged to the BOLTVERSION, as specified in
|
|
|
|
`Makefile`.
|
|
|
|
|
|
|
|
|
2018-06-05 12:32:50 +02:00
|
|
|
Source code analysis
|
|
|
|
--------------------
|
|
|
|
An updated version of the NCC source code analysis tool is available at
|
|
|
|
|
|
|
|
https://github.com/bitonic-cjp/ncc
|
|
|
|
|
|
|
|
It can be used to analyze the lightningd source code by running
|
|
|
|
`make clean && make ncc`. The output (which is built in parallel with the
|
|
|
|
binaries) is stored in .nccout files. You can browse it, for instance, with
|
|
|
|
a command like `nccnav lightningd/lightningd.nccout`.
|
|
|
|
|
2018-02-03 02:03:52 +01:00
|
|
|
Subtleties
|
|
|
|
----------
|
|
|
|
|
2018-03-01 14:58:48 +01:00
|
|
|
There are a few subtleties you should be aware of as you modify deeper
|
|
|
|
parts of the code:
|
2018-02-03 02:03:52 +01:00
|
|
|
|
2018-07-04 07:30:02 +02:00
|
|
|
* `ccan/structeq`'s STRUCTEQ_DEF will define safe comparison function foo_eq()
|
|
|
|
for struct foo, failing the build if the structure has implied padding.
|
2018-02-03 02:03:52 +01:00
|
|
|
* `command_success`, `command_fail`, and `command_fail_detailed` will free the
|
|
|
|
`cmd` you pass in.
|
|
|
|
This also means that if you `tal`-allocated anything from the `cmd`, they
|
|
|
|
will also get freed at those points and will no longer be accessible
|
|
|
|
afterwards.
|
|
|
|
* When making a structure part of a list, you will instance a
|
|
|
|
`struct list_node`.
|
|
|
|
This has to be the *first* field of the structure, or else `dev-memleak`
|
|
|
|
command will think your structure has leaked.
|
|
|
|
|
2019-04-18 08:27:30 +02:00
|
|
|
|
|
|
|
Protocol Modifications
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
The source tree contains CSV files extracted from the v1.0 BOLT
|
|
|
|
specifications (wire/extracted_peer_wire_csv and
|
2022-04-20 23:15:28 +02:00
|
|
|
wire/extracted_onion_wire_csv). You can regenerate these by
|
|
|
|
first deleting the local copy(if any) at directory .tmp.lightning-rfc,
|
|
|
|
setting `BOLTDIR` and `BOLTVERSION` appropriately, and finally running `make
|
|
|
|
extract-bolt-csv`. By default the bolts will be retrieved from the
|
2022-04-20 23:16:48 +02:00
|
|
|
directory `../bolts` and a recent git version.
|
2019-04-18 08:27:30 +02:00
|
|
|
|
2022-04-20 23:15:28 +02:00
|
|
|
e.g., `make extract-bolt-csv BOLTDIR=../bolts BOLTVERSION=ee76043271f79f45b3392e629fd35e47f1268dc8`
|
2019-04-18 08:27:30 +02:00
|
|
|
|
2018-01-03 06:28:11 +01:00
|
|
|
Further Information
|
|
|
|
-------------------
|
|
|
|
|
2018-03-01 14:58:48 +01:00
|
|
|
Feel free to ask questions on the lightning-dev mailing list, or on
|
2018-03-01 15:05:52 +01:00
|
|
|
`#c-lightning` on IRC, or email me at rusty@rustcorp.com.au.
|
2017-08-29 07:15:39 +02:00
|
|
|
|
|
|
|
Cheers!<br>
|
|
|
|
Rusty.
|