Run c-lightning in a light and resilient way

Vincenzo Palazzo
5 min readSep 14, 2021

If you are here, means that you are searching for a light method to run a lightning node with c-lightning, and possibly in a reliant way.

Well, I think you are in the right place :-)

Introduction

Yesterday I wrote my first article that talks about the c-lightning plugin system, and how to write a hello word plugin with Kotlin with the clightning4j tools. You may want to read it if you missed it “A day in a c-lightning plugin with Kotlin”

In today's article, we talk about a Kotlin plugin called btcli4j that is a plugin to override the bitcoin backend in c-lightning.

Possibility to run c-lightning node in a light way.

There are different possibilities to run a c-lightning node in a light way, and these are:

  • Use a rest approach, and use some rest API like esplora API. One of the first plugins that bring this concept inside c-lightning it is sauron;
  • Use bitcoin core in pruning mode, this method is partially supported by the c-lightning node;
  • Use proxy tools with the bitcoin core in pruning mode where the proxy pulls from the bitcoin network a missing block.

This article has discussed an approach that merges the first two points, in the btcli4j plugin.

Introduction to btcli4j

How introduced in the previous section, and as you may know, c-lightning has a partial (or maybe lucky) support for the pruning mode. In particular, the method is that you run your c-lightning node in parallel with the bitcoin core in pruning mode, and luckily c-lightning will ask for information on blocks that the bitcoin core has in internal memory.

This usually is difficult because it is challenging to run this procedure without having some error, but at this point, the c-lightning plugin system here enters into the game.

Build a c-lightning plugin that overrides the bitcoin-backed of lightning is what I worked on in the last few months. In particular, my goal was to achieve a plugin that supports the bitcoin pruning mode, but also a remote connection with some rest API like esplora API to use the plugin in different configurations.

What does btcli4j really support?

One of the goals that btcli4j promise is to gives the user the power to run a c-lightning node on any type of system, but also without worry about a crash caused by a network or bitcoin core crash (or bad answer). This is achieved with the redirection of requests to another backend (from bitcoind to rest API) if there is some error, and increasing waiting time in case of network failure or rest API failure.

  • Forwarding the c-lighting request to another bitcoin backend: This is useful when c-lightning is asking a block that was pruning, the request is shared to the rest API.
  • Increasing waiting time, in case of network error: this is useful when the internet connection of the user has some problem, or the rest API have some errors. In this case, the plugin has some rules, it will retry the request 4 times, and in each failure, there is a waiting time equal to (numberOfRetry * baseWaitingTime) where the baseWaitingTime is equal to 60000 milliseconds by default. After four failure the plugin have the permission to die :-)

With these simple rules in the plugin, it is possible to run the c-lightning node in a completely safe way, in pruning mode or fully based on the rest API.

Cool right? Enough! The question is: “It is cool, but how can I use it”?

Well, it is easy, and I will describe it in the next section.

How to run btcli4j plugin

To use the plugin, need to install a JRE (that is the base version to run the java virtual machine on the system), or if you want to compile it you need a JDK, which is the tool to compiler java code.

Compile from the source the plugin (required JDK ≥ 8)

  • Clone the repository with the following command
git clone https://github.com/clightning4j/btcli4j.git && cd btcli4j
  • Run the Gradle script to compile it, and generate all the necessary stuff
./gradlew createRunnableScript -Dorg.gradle.daemon=false --no-build-cache
  • The script will generate a file called btcli4j-gen.sh that looks like the following one
# Script generated from gradle! By clightning4j
#!/bin/bash
/home/vincent/.jdks/adopt-openjdk-14.0.2/bin/java -jar /home/vincent/Github/btcli4j/build/libs/btcli4j-all.jar
  • At this point, you need to link this file to your lightning node, and I suggest using the config file of your c-lightning node, the basic example of a config file is the following one
config c-lightning
network=bitcoin
alias=SOME_ALIAS
rgb=02bf81
# uncomment these propriety if you want to use
# the pruning mode with your bitcoin node
#bitcoin-rpcuser=vincent
#bitcoin-rpcpassword=vincent
#bitcoin-rpcurl=http://127.0.0.1:8332
#btcli4j-pruned=1

plugin=/path/of/your/script/btcli4j/btcli4j-gen.sh
log-level=info
log-file=/tmp/log.log
allow-deprecated-apis=false
  • The last step is to save the config file in your .lightning directory, and run the node with the following command if you want to run c-lightning with only the rest API backend. However, if you want to run it with bitcoin core in pruning mode, you need to uncomment the line in the lightning config (see the previous point) and set your bitcoin core information.
lightningd --disable-plugin bcli

Run the plugin without compiler it (required JRE ≥ 8)

Go to the releases section, and possibly use the last release of the plugin, and download the jar for your JDK version (JRE 8, 11, and 13 are supported).

Save the jar in some place in your system, and create a file called btcli4j.sh that contains the following content

#!/bin/bash
java -jar /path/of/your/jar/btcli4j-jdk*.jar

after that, you need to configure the c-lightning node with the following information

  • you need to link the file that you created to your lightning node, and I suggest using the config file of your c-lightning node, the basic example of a config file is the following one
config c-lightning
network=bitcoin
alias=SOME_ALIAS
rgb=02bf81
# uncomment these propriety if you want to use
# the pruning mode with your bitcoin node
#bitcoin-rpcuser=vincent
#bitcoin-rpcpassword=vincent
#bitcoin-rpcurl=http://127.0.0.1:8332
#btcli4j-pruned=1
plugin=/path/of/your/script/btcli4j/btcli4j.shlog-level=info
log-file=/tmp/log.log
allow-deprecated-apis=false
  • The last step is to save the config file in your .lightning directory, and run the node with the following command if you want to run c-lightning with only the rest API backend. if you want to run it with bitcoin core in pruning mode, you need to uncomment the line in the lightning config (see the previous point) and set your bitcoin core information.
lightningd --disable-plugin bcli

Happy light c-lightning node ran. If you like to share your experience on Twitter, please use the hashtag #clightning4j or ping me @PalazzoVincenzo on Twitter

If you want to contribute, pull requests are welcome, and some of these plugins are waiting to find a maintainer. Please, check out these awesome things.

If you like my Job, please considered supporting me with the following methods

- 3BQ8qbn8hLdmBKEjt1Hj1Z6SiDsnjJurfU
- liberapay.com/vincenzopalazzo
- Github support
- buymeacoffee

P.S: Lightning donation is coming ;-)

If you want open channels with my c-lightning node build on top of Java and Kotlin, here we go https://bruce.bublina.eu.org

You can find me on Github @vincenzopalazzo

--

--

Vincenzo Palazzo

I'm an Open Source software developer, and in my free time, I'm a master's student. My main focus is to build some tools useful for other developers.