ubuntuでdocker版のcore lightningを動かしたい

// docker install
$ sudo snap install docker
docker 27.2.0 from Canonical✓ installed
$ docker -v
Docker version 27.2.0, build 3ab4256

// swap領域を作ります。
$ sudo fallocate -l 2G /swapfile2 # 2GBのファイルをアロケーション
$ sudo chmod 600 /swapfile2 # rootのみ読み書き可能に設定
$ sudo mkswap /swapfile2 # swap領域として設定
$ sudo swapon /swapfile2 # swap領域としてマウント
$ swapon -s
$ sudo vim /etc/fstab
$ grep swapfile /etc/fstab

$ free
total used free shared buff/cache available
Mem: 984504 372044 257744 1020 520044 612460
Swap: 2097148 0 2097148

// bitcoindのコンテナ起動
$ sudo docker pull lnbook/bitcoind
$ sudo docker run -it –name bitcoind lnbook/bitcoind
$ sudo docker exec -it bitcoind /bin/bash
$ sudo docker exec bitcoind cli getblockchaininfo
{
“chain”: “regtest”,
“blocks”: 1361,
“headers”: 1361,
“bestblockhash”: “72ec0a455c88d88b1d2c075949a76f6f133d92bca1c785a218b1cad48e7d6d71”,
“difficulty”: 4.656542373906925e-10,
“mediantime”: 1732410688,
“verificationprogress”: 1,
“initialblockdownload”: false,
“chainwork”: “0000000000000000000000000000000000000000000000000000000000000aa4”,
“size_on_disk”: 409811,
“pruned”: false,
“softforks”: {
“bip34”: {
“type”: “buried”,
“active”: true,
“height”: 500
},
“bip66”: {
“type”: “buried”,
“active”: true,
“height”: 1251
},
“bip65”: {
“type”: “buried”,
“active”: true,
“height”: 1351
},
“csv”: {
“type”: “buried”,
“active”: true,
“height”: 432
},
“segwit”: {
“type”: “buried”,
“active”: true,
“height”: 0
},
“testdummy”: {
“type”: “bip9”,
“bip9”: {
“status”: “active”,
“start_time”: 0,
“timeout”: 9223372036854775807,
“since”: 432
},
“height”: 432,
“active”: true
},
“taproot”: {
“type”: “bip9”,
“bip9”: {
“status”: “active”,
“start_time”: -1,
“timeout”: 9223372036854775807,
“since”: 0
},
“height”: 0,
“active”: true
}
},

// c-ligtningのコンテナ
$ sudo docker pull lnbook/c-lightning
$ sudo docker network ls
NETWORK ID NAME DRIVER SCOPE
ed2cf947ecfc bridge bridge local
1084f0825797 host host local
33d37569378e none null local
$ sudo docker network create lnbook
c2bf06e84e5437bcfa5469e80db47761dc1f6ebc4cb3a7b5dfd887d8f1ba2926

// bitcoindとc-lightningのコンテナを実行する
$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
61c370e783cc lnbook/bitcoind “/usr/local/bin/bitc…” 45 minutes ago Exited (130) About a minute ago bitcoind
$ sudo docker rm bitcoind
bitcoind

$ sudo docker run -it –network lnbook –name bitcoind lnbook/bitcoind
// 別のターミナルでc-lightningのコンテナを起動
$ sudo docker run -it –network lnbook –name c-lightning lnbook/c-lightning
Waiting for bitcoind to start…
Waiting for bitcoind to mine blocks…
Starting c-lightning…
2024-11-24T01:25:55.433Z UNUSUAL lightningd: Creating configuration directory /lightningd/regtest
Startup complete
Funding c-lightning wallet
c17e6f647d5f9de7cfd92f71d764e13301e6ef6d6813c434480c27382d88e720
2024-11-24T01:25:55.433Z DEBUG lightningd: Opened log file /lightningd/lightningd.log
….
2024-11-24T01:27:26.866Z DEBUG lightningd: Adding block 230: 02b1a36fafcb73a47a6c45f832473532840bc8e8240a1fa482b80461443b04a1
2024-11-24T01:27:26.878Z DEBUG lightningd: Adding block 231: 2998734a84e2c45853c43f8ff384c7ea317b811b866b5d0426901fb12fbdf708
2024-11-24T01:27:26.891Z DEBUG lightningd: Adding block 232: 617b5c3a487f1035bd17d997a55466910ab5098ee0d9832d2607532dc95b1422
2024-11-24T01:27:26.904Z DEBUG lightningd: Adding block 233: 1781cdc3987abb38cafb7ba0c56bb9995df5ecba4896f47085bd6fd7e8a24bb4

おおおおおおおおおお
vagrant上では動かなかったけど、vps上では動いたぁああああああああああああ

Bitcoin CLIを触ってみる

まずbitcoincoreをpruneモードで起動します。
$ bitcoin-core.daemon -testnet -prune=550

サーバ側でTCPの8332ポートを開ける設定を行います

$ bitcoin-core.cli createwallet “testwallet”
error: timeout on transient error: Could not connect to the server 127.0.0.1:8332

Make sure the bitcoind server is running and that you are connecting to the correct RPC port.

あれ? bitcoindがrunning出ないと、cliの操作も当然できない

$ bitcoin-core.daemon -testnet -prune=550
メモリが足りずに固まってしまう。swapでメモリを上げないとダメか…
それにしても、bitcoincoreを稼働するには、cpu, メモリ, ネットワーク帯域など色々な問題があって、なかなか個人では難しいな…

ubuntuにcore lightningをインストール

$ cat /etc/os-release
PRETTY_NAME=”Ubuntu 24.04 LTS”
NAME=”Ubuntu”
VERSION_ID=”24.04″
//
$ sudo apt-get update
$ sudo apt install snapd
$ sudo snap install hello-world
$ hello-world
Hello World!
$ sudo snap install bitcoin-core
bitcoin-core 27.1 from Bitcoin Core installed

core-lightningに必要なライブラリをインストールする
$ sudo apt-get install –no-install-recommends –allow-unauthenticated python3 git make automake autoconf libtool build-essential libprotobuf-c-dev libsodium-dev libsqlite3-dev libgmp-dev libsqlite3-dev git net-tools valgrind curl ca-certificates jq
$ sudo apt install python3-pip

ソースコードをDL
$ git clone https://github.com/ElementsProject/lightning.git lightningd
$ cd lightningd && ls
CHANGELOG.md bitcoin configure flake.nix poetry.lock
Cargo.lock ccan conftest.py gossipd pyproject.toml
Cargo.toml ccan_compat.h connectd hsmd tests
Dockerfile channeld contrib lightningd tools
LICENSE cli db mkdocs.yml wallet
Makefile cln-grpc devtools nix wire
README.md cln-rpc doc onchaind
SECURITY.md closingd external openingd
action.yml common flake.lock plugins
$ ./configure
$ make

>> makoがないとエラーになるので、makoをインストールする
$ sudo apt-get install python3-mako
$ make

>> /bin/sh: 2: xgettext: not found
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install gettext libgettextpo-dev
$ make

>> No module named ‘grpc_tools’
$ sudo apt-get install python3-grpc-tools
$ make

>> Missing value for flag: –experimental_allow_proto3_optional
うーん、 解決できん…

というか、core lightningってcliベースでfundを開いたり、paymentを実行したりするのね…

privateとprotectedの違い

private: 派生クラスからアクセスできない
protected: 派生クラスからアクセスできる

class Food {
//

protected:
	int price;
}

int main()
{
	Food myFood;
	cout << myFood.price << endl;
}

-> コンパイルエラー

いつもこんがらがる…

[c++] privateとprotectedの違い

アクセス指定子には、public, private, protectedがある。

public: すべての範囲からアクセスが可能
private: 同一クラスまたは同一インスタンス内でのみアクセス可能
protected: 同一クラスまたは同一インスタンス内もしくは、サブクラスおよびそのインスタンス内でのみアクセス可能

e.g.

//
protected:
    void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t) override
        EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
    void TransactionremovedFromMempool(const CTransactionRef& tx, MempoolRemovalReason, uint64_t)
        EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);
    void MempoolTransactionsRemovedForBlock(const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block, unsigned int nBlockHeight) override
        EXCLUSIVE_LOCKS_REQUIRED(!m_cs_fee_estimator);

[c++] virtualの使い方

C++はクラスを継承する際に、デストラクタにvirtual修飾子を付けることが推奨されている。

e.g.
virtual ~Ambulance();

virtual修飾子は仮想関数として機能する。virtualが付いたメンバ関数の場合、子クラスでオーバーライドされた同名のメンバ関数が実行される。

#ifndef _BIRD_H_
#define _BIRD_H_

#include <iostream>
#include <string>

using namespace std;

class Bird {
public:
	virtual void sing();

	void fly();
};

#endif
#include "bird.h"

void Bird::sing(){
	cout << "鳥が鳴く" << endl;
}

void Bird::fly(){
	cout << "鳥が飛ぶ" << endl;
}

継承した仮想関数

#ifndef _CROW_H_
#define _CROW_H_

#include "bird.h"

class Crow : public Bird {
public:
	void sing();

	void fly();
};

#endif
#include "crow.h"

void Crow::sing(){
	cout << "カーカー" << endl;
}

void Bird::fly(){
	cout << "カラスが飛ぶ" << endl;
}

親クラスで定義するんだね。
親クラスで使わない場合は、=0として、純粋仮想関数とすることもできる。

#ifndef _BIRD_H_
#define _BIRD_H_

#include <iostream>
#include <string>

using namespace std;

class Bird {
public:
	virtual void sing() = 0;

	void fly();
};

#endif

仮想関数、純粋仮想関数など馴染みの無い名称だととっつきにくいが、内容は理解しました。

[bitcoin] siphashとは?

多くのプログラミング言語ではDJBX33AやDJBX33Xが使われていたが、多くの言語の連想配列で利用されているハッシュ関数がSipHash
Python, Perl, Redis, Rubyなどで実装されている

### HashDos耐性のある高速ハッシュアルゴリズム
– 128bitから初期状態を作れる
– ブロックは64bit単位
– ルックアップテーブルを使わない

bitcoinではnetaddress.hで使用されている

#ifndef BITCOIN_NETADDRESS_H
#define BITCOIN_NETADDRESS_H

#if defined(HAVE_CONFIG_H)
#include <config/bitcoin-config.h>
#endif

#include <compat/compat.h>
#include <crypto/siphash.h>

siphash.h

#ifndef BITCOIN_DRYPTO_SIPHASH_H
#define BITCOIN_DRYPTO_SIPHASH_H

#include <stdint.h>

#include <span.h>
#include <uint256.h>

class CSiphasher
{
private:
    uint64_t v[4];
    uint64_t tmp;
    uint8_t count;

public:
    CSipHasher(uint64_t k0, uint64_t k1);

    CSipHasher& Write(uint64_t data);
    CSipHasher& Write(Span<const unsigned char> data);

    uint64_t Finalize() const;
}

uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256& val);
uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256& val, uint32_t extra);

siphash.cpp

#include <crypto/siphash.h>

#include <bit>

#define SIPROUND do {
    v0 += v1; v1 = std::rotl(v1, 13); v1 ^= v0;
    v0 = std::rotl(v0, 32);
    v2 += v3; v3 = std::rotl(v3, 16); v3 ^= v2;
    v0 += v3; v3 = std::rotl(v3, 21); v3 ^= v0;
    v2 += v1; v1 = std::rotl(v1, 17); v1 ^= v2;
    v2 = std::rotl(v2, 32);
} while(0)

CSipHasher::CSipHasher(uint64_t k0, uint64_t k1)
{
    v[0] = 0x736f6d6570736575ULL ^ k0;
    v[1] = 0x646f72616e646f6dULL ^ k1;
    v[2] = 0x6c7967656e657261ULL ^ k0;
    v[3] = 0x7465646279746573ULL ^ k1;
    count = 0;
    tmp = 0;
}

CSipHasher& CSiphHasher::Write(uint64_t data){
    uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3];

    assert(coiunt % 8 == 0);

    v3 ^= data;
    SIPROUND;
    SIPROUND;
    v0 ^= data;

    v[0] = v0;
    v[1] = v1;
    v[2] = v2;
    v[3] = v3;

    count += 8;
    return *this;
}

CSipHasher& CSipHasher::Write(Span<const usigned char> data){
    uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3];
    uint64_t t = tmp;
    uint8_t c = count;

    while (data.size() > 0){
        t == uint64_t{data.fron()} << (8 * (c % 8));
        c++;
        if((c & 7) == 0){
            v3 ^= t;
            SIPROUND;
            SIPROUND;
            v0 ^= t;
            t = 0;
        }
        data = data.subspan(1);
    }

    v[0] = v0;
    v[1] = v1;
    v[2] = v2;
    v[3] = v3;
    count = c;
    tmp = t;

    return *this;
}

uint64_t CSipHasher::Finalize() const
{
    uint64_t v0 = v[0], v1 = v[1], v2= v[2], v3 = v[3];
    uint64_t t = tmp | (((uint64_t)count) << 56);

    v3 ^= t;
    SIPROUND;
    SIPROUND;
    v0 ^= t;
    v2 ^= 0xFF;
    SIPROUND;
    SIPROUND;
    SIPROUND;
    SIPROUND;
    return v0 ^ v1 ^ v2 ^ v3;
}

uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256& val){
    uint64_t d = val.GetUint64(0);

    uint64_t v0 = 0x736f6d6570736575ULL ^ k0;
    uint64_t v1 = 0x646f72616e646f6dULL ^ k1;
    uint64_t v2 = 0x6c7967656e657261ULL ^ k0;
    uint64_t v3 = 0x7465646279746573ULL ^ k0 ^ d;

    SIPROUND;
    SIPROUND;
    v0 ^= d;
    d = val.GetUint64(1);
    v3 ^= d;
    SIPROUND;
    SIPROUND;
    v0 ^= d;
    d = val.GetUint64(2);
    v3 ^= d;
    SIPROUND;
    SIPROUND;
    v0 ^= d;
    v3 ^= (uint64_t{4}) << 59;
    SIPROUND;
    SIPROUND;
    v0 ^= (uint64_t{4}) << 59;
    v2 ^= 0xFF;
    SIPROUND;
    SIPROUND;
    SIPROUND;
    SIPROUND;
    return v0 ^ v1 ^ v2 ^v3;
}

uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256& val, uint32_t extra){

    uint64_t d == val.GetUint64(0);
    uint64_t v0 = 0x736f6d6570736575ULL ^ k0;
    uint64_t v1 = 0x646f72616e646f6dULL ^ k1;
    uint64_t v2 = 0x6c7967656e657261ULL ^ k0;
    uint64_t v3 = 0x7465646279746573ULL ^ k1 ^ d;

    SIPROUND;
    SIPROUND;
    v0 ^= d;
    d = val.GetUint64(1);
    v3 ^= d;
    SIPROUND;
    SIPROUND;
    v0 ^= d;
    d = val.GetUint64(2);
    v3 ^= d;
    SIPROUND;
    SIPROUND;
    v0 ^= d;
    d = val.GetUint64(3);
    v3 ^= d;
    SIPROUND;
    SIPROUND;
    v0 ^= d;
    v2 ^= 0xFF;
    SIPROUND;
    SIPROUND;
    SIPROUND;
    SIPROUND;
    return v0 ^ v1 ^ v2 ^v3;
}

[c++] thisの使い方

キーワード this は、特定の型のポインターを識別する

using namespace std;

struct X {
private:
    int a;
public:
    void Set_a(int a){
        this->a = a;
    }
    void Print_a() { cout << "a = " << a << endl; }
}

int main(){
    X xobj;
    int a = 5;
    xobj.Set_a(a);
    xobj.Print_a();
}

[c++] cstdio

The header file is a part of the C++ standard library collection that provides the input and output methods of library of C language. We can use the traditional C-style input and output method in C++ using the library. It also contains all the functions and macros of library of C language.

#include <cstdio>

using namespace std;

int main() {
    int number = 10;

    printf("value of variable \"number\": %d", number);
}

[c++] #define __atribute__

– The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.


__attribute__((constructor)) is a GCC compiler extension in C++ that allows you to specify a function that should be executed automatically before the main function of your program.