Skip to content

Commit e5d9d69

Browse files
committed
multi: cln configuration and Cachix setup
- Update Cachix commands for integration tests - Add Cachix setup instructions to documentation - Improve logging in waitForClnSynced function - Refactor test data directory creation for better cleanup - Skip LWK-related tests due to intermittent CI failures - Increase timeout duration for tests
1 parent fbcd063 commit e5d9d69

File tree

12 files changed

+242
-89
lines changed

12 files changed

+242
-89
lines changed

.devcontainer/devcontainer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
2-
// README at: https://github.com/devcontainers/templates/tree/main/src/go
2+
// README at: https://github.com/xtruder/nix-devcontainer
33
{
44
"name": "peerswap",
5-
"image": "mcr.microsoft.com/devcontainers/go:dev-1.22",
6-
"features": {
7-
"ghcr.io/devcontainers/features/nix:1": {},
8-
"ghcr.io/devcontainers-contrib/features/direnv:1": {}
9-
},
10-
"postCreateCommand": "echo 'experimental-features = nix-command flakes' | sudo tee -a /etc/nix/nix.conf > /dev/null && nix-env -iA nixpkgs.nixpkgs-fmt && nix-env -iA cachix -f https://cachix.org/api/v1/install && sudo chmod a+w /usr/local/bin/ && GOBIN=/usr/local/bin go install github.com/bufbuild/buf/cmd/buf@v1.50.0", // Configure tool-specific properties.
5+
"image": "ghcr.io/xtruder/nix-devcontainer:v1",
6+
"onCreateCommand": "nix-env -iA cachix -f https://cachix.org/api/v1/install && cachix use peerswap && nix develop --command echo 'Nix environment ready'",
7+
"postCreateCommand": "sudo chmod a+w /usr/local/bin/ && nix develop --command bash -c 'GOBIN=/usr/local/bin go install github.com/bufbuild/buf/cmd/buf@v1.50.0'",
118
"customizations": {
129
"vscode": {
1310
"extensions": [
11+
"arrterian.nix-env-selector",
1412
"jnoortheen.nix-ide",
1513
"mkhl.direnv",
14+
"golang.go",
1615
"bufbuild.vscode-buf"
1716
]
1817
}
1918
},
2019
"containerEnv": {
20+
"USER": "root",
2121
"RUN_INTEGRATION_TESTS": "1",
2222
"hardeningDisable": "all",
2323
"GOROOT": "",
2424
"CACHIX_AUTH_TOKEN": "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJmNTg3ZjViZi00ZWJiLTRhYmQtYmYzOC0xYzFlZGE3ZGE4NTQiLCJzY29wZXMiOiJjYWNoZSJ9.pzmsXqBwHFAExdVFhbdrtN1mpc4h3U3JWnej7PC-NAA"
2525
}
26-
}
26+
}

.envrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
use flake
2+
3+
# Setup Cachix for development
4+
if command -v cachix >/dev/null 2>&1; then
5+
cachix use peerswap
6+
fi

.github/workflows/ci.yml

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,18 @@ jobs:
4545
uses: actions/checkout@v4
4646
with:
4747
fetch-depth: 0
48-
- uses: nixbuild/nix-quick-install-action@v28
49-
- name: Restore and cache Nix store
50-
uses: nix-community/cache-nix-action@v5
48+
49+
- uses: cachix/install-nix-action@v31
50+
- uses: cachix/cachix-action@v16
5151
with:
52-
# restore and save a cache using this key
53-
primary-key: nix-${{ runner.os }}-${{ hashFiles('**/*.nix') }}
54-
# if there's no cache hit, restore a cache by this prefix
55-
restore-prefixes-first-match: nix-${{ runner.os }}-
56-
# collect garbage until Nix store size (in bytes) is at most this number
57-
# before trying to save a new cache
58-
gc-max-store-size-linux: 1073741824
59-
# do purge caches
60-
purge: true
61-
# purge all versions of the cache
62-
purge-prefixes: cache-${{ runner.os }}-
63-
# created more than this number of seconds ago relative to the start of the `Post Restore` phase
64-
purge-created: 0
65-
# except the version with the `primary-key`, if it exists
66-
purge-primary-key: never
67-
- run: nix-shell --run "make test-${{matrix.test-vector}}"
52+
name: peerswap
53+
authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}'
54+
useDaemon: true
55+
# Switch to nix-shell for integration tests instead of nix develop.
56+
# The 'nix develop' command can be unstable in some CI environments,
57+
# causing issues like "clightning-1: Lost connection to the RPC socket."
58+
# While the root cause is unclear, switching to nix-shell provides a more stable alternative for CI.
59+
# For more context, see the issue in this failed job: https://github.com/ElementsProject/peerswap/actions/runs/16064376179/job/45336040866?pr=385
60+
- name: Run integration tests
61+
run: |
62+
nix-shell --run "make test-${{matrix.test-vector}}"

cmd/peerswap-plugin/main.go

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -519,21 +519,46 @@ func setPanicLogger() (func() error, error) {
519519
}
520520

521521
// waitForClnSynced waits until cln is synced to the blockchain and the network.
522-
// This call is blocking.
522+
// This call is blocking and includes detailed observability logging.
523523
func waitForClnSynced(cln *glightning.Lightning, tick time.Duration) error {
524524
ticker := time.NewTicker(tick)
525525
defer ticker.Stop()
526+
cln.SetTimeout(10)
526527

527-
for {
528-
select {
529-
case <-ticker.C:
530-
info, err := cln.GetInfo()
531-
if err != nil {
532-
return err
533-
}
534-
if info.IsBitcoindSync() && info.IsLightningdSync() {
535-
return nil
536-
}
528+
log.Infof("Waiting for Core Lightning to sync (checking every %v)", tick)
529+
startTime := time.Now()
530+
531+
for range ticker.C {
532+
info, err := cln.GetInfo()
533+
if err != nil {
534+
log.Infof("Failed to get node info while waiting for sync: %v", err)
535+
return fmt.Errorf("failed to check sync status: %w", err)
536+
}
537+
538+
bitcoindSynced := info.IsBitcoindSync()
539+
lightningdSynced := info.IsLightningdSync()
540+
elapsed := time.Since(startTime)
541+
542+
log.Infof("Node info - ID: %s, Alias: %s, Version: %s",
543+
info.Id, info.Alias, info.Version)
544+
log.Infof("Block info - Height: %d, Peers: %d",
545+
info.Blockheight, info.PeerCount)
546+
log.Infof("Network: %s, Fees collected: %s",
547+
info.Network, info.FeesCollected)
548+
log.Infof("Sync status: bitcoind=%t, lightningd=%t (elapsed: %v)",
549+
bitcoindSynced, lightningdSynced, elapsed)
550+
551+
if bitcoindSynced && lightningdSynced {
552+
log.Infof("Core Lightning is now fully synced (took %v)", elapsed)
553+
return nil
554+
}
555+
556+
if !bitcoindSynced {
557+
log.Infof("Still waiting for bitcoind to sync...")
558+
}
559+
if !lightningdSynced {
560+
log.Infof("Still waiting for lightningd to sync...")
537561
}
538562
}
563+
return fmt.Errorf("timed out waiting for Core Lightning to sync after %v", time.Since(startTime))
539564
}

docs/setup-nix.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Nix Development Environment & Cachix Setup
2+
3+
This document explains how to set up and maintain the Nix development environment for PeerSwap, including Cachix binary cache configuration.
4+
5+
## Overview
6+
7+
PeerSwap uses Nix flakes for reproducible development environments and Cachix for fast binary caching. This setup provides:
8+
- Deterministic build environments across all platforms
9+
- Fast CI/CD builds through binary caching
10+
- Consistent developer experience
11+
12+
## Quick Start for Developers
13+
14+
### Prerequisites
15+
16+
1. **Install Nix** (if not already installed):
17+
```bash
18+
# Multi-user installation (recommended)
19+
curl -L https://nixos.org/nix/install | sh -s -- --daemon
20+
21+
# Enable flakes
22+
mkdir -p ~/.config/nix
23+
echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf
24+
```
25+
2. **Install direnv** (optional but recommended):
26+
```bash
27+
# On macOS
28+
brew install direnv
29+
30+
# On Linux
31+
curl -sfL https://direnv.net/install.sh | bash
32+
33+
# Add to shell profile
34+
echo 'eval "$(direnv hook bash)"' >> ~/.bashrc # or ~/.zshrc
35+
```
36+
37+
### Development Environment
38+
```bash
39+
git clone https://github.com/YusukeShimizu/peerswap.git
40+
cd peerswap
41+
42+
# With direnv (automatic)
43+
direnv allow
44+
45+
# Without direnv (manual)
46+
nix develop
47+
```
48+
### Binary Cache (Cachix)
49+
50+
The project automatically uses the `peerswap` Cachix cache for faster builds. If you don't have Cachix installed:
51+
52+
```bash
53+
# Install Cachix
54+
nix profile install nixpkgs#cachix
55+
56+
# Use the cache (done automatically by .envrc)
57+
cachix use peerswap
58+
```
59+
60+
## Maintainer Setup Guide
61+
62+
### 1. Cachix Cache Creation
63+
64+
1. **Create Cachix account**:
65+
- Visit https://app.cachix.org/
66+
- Sign up with GitHub account
67+
- Verify email address
68+
69+
2. **Access existing cache**:
70+
- The cache `peerswap` already exists at https://app.cachix.org/cache/peerswap
71+
- Ensure you have admin access to this cache
72+
- If you don't have access, contact the cache owner
73+
74+
### 2. Authentication Token
75+
76+
1. **Generate token**:
77+
- Go to cache dashboard → `peerswap` (https://app.cachix.org/cache/peerswap)
78+
- Navigate to "Auth tokens" tab
79+
- Click "Generate token"
80+
- Select permissions: **Push** (required for CI)
81+
- Copy the generated token
82+
83+
2. **Add GitHub repository secret**:
84+
- Go to GitHub repository: Settings → Secrets and variables → Actions
85+
- Click "New repository secret"
86+
- Name: `CACHIX_AUTH_TOKEN`
87+
- Value: Paste the token from step 1
88+
- Click "Add secret"
89+
90+
### 3. Local Cache Push
91+
92+
To push from local development to the cache:
93+
94+
```bash
95+
# Create a profile and push to cache
96+
nix develop --profile dev-profile
97+
cachix push peerswap dev-profile
98+
```
99+
100+
## How It Works
101+
102+
### Local Development
103+
104+
The `.envrc` file automatically configures Cachix:
105+
106+
```bash
107+
use flake
108+
109+
# Setup Cachix for development
110+
if command -v cachix >/dev/null 2>&1; then
111+
cachix use peerswap
112+
fi
113+
```
114+
115+
## References
116+
117+
- [Nix Flakes](https://nixos.wiki/wiki/Flakes)
118+
- [Cachix Documentation](https://docs.cachix.org/)
119+
- [GitHub Actions + Nix](https://nix.dev/guides/recipes/continuous-integration-github-actions)

flake.lock

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
inputs = {
3-
nixpkgs.url = "github:NixOS/nixpkgs";
3+
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
44
flake-utils.url = "github:numtide/flake-utils";
55
# blockstream-electrs: init at 0.4.1 #299761
66
# https://github.com/NixOS/nixpkgs/pull/299761/commits/680d27ad847801af781e0a99e4b87ed73965c69a
@@ -37,19 +37,19 @@
3737
{
3838
devShells.default = mkShell {
3939
buildInputs = [
40-
go_1_23
41-
gotools
42-
blockstream-electrs
43-
bitcoind
44-
elementsd
45-
clightning
46-
lnd
47-
lwk
40+
go_1_23 # Go 1.23.10 (updated from nixpkgs-unstable)
41+
gotools # Go development tools
42+
blockstream-electrs # Electrum Rust Server 0.4.1
43+
bitcoind # Bitcoin Core daemon v29.0.0 (updated from v28.0.0)
44+
elementsd # Elements daemon v23.2.4 (Liquid)
45+
clightning # Core Lightning v25.05 (updated from v24.11)
46+
lnd # Lightning Network Daemon 0.19.1-beta (updated from 0.18.3-beta)
47+
lwk # Liquid Wallet Kit 0.8.0
4848
];
4949
# Cannot run the debugger without this
5050
# see https://github.com/go-delve/delve/issues/3085
5151
hardeningDisable = [ "all" ];
5252
};
5353
}
5454
);
55-
}
55+
}

test/config_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func Test_ClnConfig(t *testing.T) {
2525

2626
_, filename, _, _ := runtime.Caller(0)
2727
pathToPlugin := filepath.Join(filename, "..", "..", "out", "test-builds", "peerswap")
28-
testDir := t.TempDir()
28+
testDir := makeTestDataDir(t)
2929

3030
// Start bitcoin node
3131
bitcoind, err := testframework.NewBitcoinNode(testDir, 1)
@@ -78,7 +78,7 @@ func Test_ClnPluginConfigFile(t *testing.T) {
7878

7979
_, filename, _, _ := runtime.Caller(0)
8080
pathToPlugin := filepath.Join(filename, "..", "..", "out", "test-builds", "peerswap")
81-
testDir := t.TempDir()
81+
testDir := makeTestDataDir(t)
8282

8383
// Start bitcoin node
8484
bitcoind, err := testframework.NewBitcoinNode(testDir, 1)
@@ -132,7 +132,7 @@ func Test_ClnPluginConfigFile_DoesNotExist(t *testing.T) {
132132

133133
_, filename, _, _ := runtime.Caller(0)
134134
pathToPlugin := filepath.Join(filename, "..", "..", "out", "test-builds", "peerswap")
135-
testDir := t.TempDir()
135+
testDir := makeTestDataDir(t)
136136

137137
// Start bitcoin node
138138
bitcoind, err := testframework.NewBitcoinNode(testDir, 1)
@@ -177,7 +177,7 @@ func Test_ClnPluginConfig_ElementsAuthCookie(t *testing.T) {
177177

178178
_, filename, _, _ := runtime.Caller(0)
179179
pathToPlugin := filepath.Join(filename, "..", "..", "out", "test-builds", "peerswap")
180-
testDir := t.TempDir()
180+
testDir := makeTestDataDir(t)
181181

182182
// Start bitcoin node
183183
bitcoind, err := testframework.NewBitcoinNode(testDir, 1)
@@ -281,7 +281,7 @@ func Test_ClnPluginConfig_DisableLiquid(t *testing.T) {
281281

282282
_, filename, _, _ := runtime.Caller(0)
283283
pathToPlugin := filepath.Join(filename, "..", "..", "out", "test-builds", "peerswap")
284-
testDir := t.TempDir()
284+
testDir := makeTestDataDir(t)
285285

286286
// Start bitcoin node
287287
bitcoind, err := testframework.NewBitcoinNode(testDir, 1)

test/grpc_retry_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func Test_GrpcRetryRequest(t *testing.T) {
2424
t.Parallel()
2525

2626
// Setup bitcoind and lnd.
27-
tmpDir := t.TempDir()
27+
tmpDir := makeTestDataDir(t)
2828
bitcoind, err := testframework.NewBitcoinNode(tmpDir, 1)
2929
if err != nil {
3030
t.Fatalf("Can not create bitcoin node: %v", err)
@@ -93,7 +93,7 @@ func Test_GrpcReconnectStream(t *testing.T) {
9393
t.Parallel()
9494

9595
// Setup bitcoind and lnd.
96-
tmpDir := t.TempDir()
96+
tmpDir := makeTestDataDir(t)
9797
bitcoind, err := testframework.NewBitcoinNode(tmpDir, 1)
9898
if err != nil {
9999
t.Fatalf("Can not create bitcoin node: %v", err)

0 commit comments

Comments
 (0)