Skip to content

Commit 7a9ff9c

Browse files
committed
tools/lightning-downgrade: tool to downgrade (offline) v25.12 to v25.09.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au> Changelog-Added: tools: `lightningd-downgrade` can downgrade your database from v25.12 to v25.09 if something goes wrong.
1 parent 3c89231 commit 7a9ff9c

File tree

5 files changed

+340
-2
lines changed

5 files changed

+340
-2
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ $(info Building version $(VERSION))
99
# Next release.
1010
CLN_NEXT_VERSION := v25.12
1111

12+
# Previous release (for downgrade testing)
13+
CLN_PREV_VERSION := v25.09
14+
1215
# --quiet / -s means quiet, dammit!
1316
ifeq ($(findstring s,$(word 1, $(MAKEFLAGS))),s)
1417
ECHO := :

tests/test_downgrade.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from fixtures import * # noqa: F401,F403
2+
from fixtures import TEST_NETWORK
3+
from utils import (
4+
TIMEOUT # noqa: F401
5+
)
6+
7+
import os
8+
import subprocess
9+
10+
11+
def test_downgrade(node_factory, executor):
12+
l1, l2 = node_factory.line_graph(2, opts={'may_reconnect': True})
13+
14+
# From the binary:
15+
# ERROR_DBVERSION = 1
16+
# ERROR_DBFAIL = 2
17+
ERROR_USAGE = 3
18+
# ERROR_INTERNAL = 99
19+
20+
cmd_line = ["tools/lightning-downgrade",
21+
f"--network={TEST_NETWORK}",
22+
f"--lightning-dir={l1.daemon.lightning_dir}"]
23+
if os.getenv("VALGRIND") == "1":
24+
cmd_line = ['valgrind', '-q', '--error-exitcode=7'] + cmd_line
25+
26+
# No downgrade on live nodes!
27+
retcode = subprocess.call(cmd_line, timeout=TIMEOUT)
28+
assert retcode == ERROR_USAGE
29+
30+
l1.stop()
31+
subprocess.check_call(cmd_line)
32+
33+
# Test with old lightningd if it's available.
34+
old_cln = os.getenv('PREV_LIGHTNINGD')
35+
if old_cln:
36+
current_executable = l1.daemon.executable
37+
l1.daemon.executable = old_cln
38+
39+
l1.start()
40+
41+
# It should connect to l2 no problems, make payment.
42+
l1.connect(l2)
43+
inv = l2.rpc.invoice(1000, 'test_downgrade', 'test_downgrade')
44+
l1.rpc.xpay(inv['bolt11'])
45+
l1.stop()
46+
l1.daemon.executable = current_executable
47+
48+
# Another downgrade is a noop.
49+
assert subprocess.check_output(cmd_line).decode("utf8").startswith("Already compatible with ")
50+
51+
# Should be able to upgrade without any trouble
52+
l1.daemon.opts['database-upgrade'] = True
53+
l1.start()
54+
assert l1.daemon.is_in_log("Updating database from version")
55+
56+
l1.connect(l2)
57+
inv2 = l2.rpc.invoice(1000, 'test_downgrade2', 'test_downgrade2')
58+
l1.rpc.xpay(inv2['bolt11'])

tools/Makefile

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#! /usr/bin/make
2-
TOOLS := tools/lightning-hsmtool
2+
TOOLS := tools/lightning-hsmtool tools/lightning-downgrade
33
TOOLS_SRC := $(TOOLS:=.c)
44
TOOLS_OBJ := $(TOOLS_SRC:.c=.o)
55

@@ -13,8 +13,23 @@ ALL_PROGRAMS += $(TOOLS)
1313
tools/headerversions: $(FORCE) tools/headerversions.o libccan.a
1414
@trap "rm -f $@.tmp.$$$$" EXIT; $(LINK.o) tools/headerversions.o libccan.a $(LOADLIBES) $(LDLIBS) -o $@.tmp.$$$$ && mv -f $@.tmp.$$$$ $@
1515

16+
$(TOOLS): libcommon.a
17+
1618
tools/headerversions.o: ccan/config.h
17-
tools/lightning-hsmtool: tools/lightning-hsmtool.o libcommon.a
19+
tools/lightning-hsmtool: tools/lightning-hsmtool.o
20+
tools/lightning-downgrade.o: CFLAGS:=$(CFLAGS) -DCLN_PREV_VERSION=$(CLN_PREV_VERSION)
21+
22+
tools/lightning-downgrade: \
23+
db/exec.o \
24+
db/bindings.o \
25+
db/utils.o \
26+
tools/lightning-downgrade.o \
27+
wallet/migrations.o \
28+
$(DB_OBJS) \
29+
$(WALLET_DB_QUERIES:.c=.o) \
30+
tools/lightning-downgrade.o
31+
32+
update-mocks: $(tools/lightning-downgrade.c:%=update-mocks/%.c)
1833

1934
clean: tools-clean
2035

tools/lightning-downgrade.c

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
/* Tool for limited downgrade of an offline node */
2+
#include "config.h"
3+
#include <bitcoin/chainparams.h>
4+
#include <ccan/array_size/array_size.h>
5+
#include <ccan/err/err.h>
6+
#include <ccan/opt/opt.h>
7+
#include <ccan/tal/path/path.h>
8+
#include <ccan/tal/str/str.h>
9+
#include <common/configdir.h>
10+
#include <common/utils.h>
11+
#include <db/bindings.h>
12+
#include <db/common.h>
13+
#include <db/exec.h>
14+
#include <db/utils.h>
15+
#include <stdio.h>
16+
#include <unistd.h>
17+
#include <wallet/migrations.h>
18+
19+
#define ERROR_DBVERSION 1
20+
#define ERROR_DBFAIL 2
21+
#define ERROR_USAGE 3
22+
#define ERROR_INTERNAL 99
23+
24+
#define PREV_VERSION stringify(CLN_PREV_VERSION)
25+
26+
struct db_version {
27+
const char *name;
28+
size_t db_height;
29+
bool gossip_store_compatible;
30+
};
31+
32+
static const struct db_version db_versions[] = {
33+
{ "v25.09", 276, false },
34+
/* When we implement v25.12 downgrade: { "v25.12", 280, ???}, */
35+
};
36+
37+
static const struct db_version *version_db(const char *version)
38+
{
39+
for (size_t i = 0; i < ARRAY_SIZE(db_versions); i++) {
40+
if (streq(db_versions[i].name, version))
41+
return &db_versions[i];
42+
}
43+
errx(ERROR_INTERNAL, "Unknown version %s", version);
44+
}
45+
46+
static void db_error(void *unused, bool fatal, const char *fmt, va_list ap)
47+
{
48+
vfprintf(stderr, fmt, ap);
49+
fprintf(stderr, "\n");
50+
if (fatal)
51+
exit(ERROR_DBFAIL);
52+
}
53+
54+
/* The standard opt_log_stderr_exit exits with status 1 */
55+
static void opt_log_stderr_exit_usage(const char *fmt, ...)
56+
{
57+
va_list ap;
58+
59+
va_start(ap, fmt);
60+
vfprintf(stderr, fmt, ap);
61+
fprintf(stderr, "\n");
62+
va_end(ap);
63+
exit(ERROR_USAGE);
64+
}
65+
66+
int main(int argc, char *argv[])
67+
{
68+
char *config_filename, *base_dir, *net_dir, *rpc_filename, *wallet_dsn = NULL;
69+
size_t current, prev_version_height, num_migrations;
70+
struct db *db;
71+
const struct db_migration *migrations;
72+
struct db_stmt *stmt;
73+
74+
setup_locale();
75+
err_set_progname(argv[0]);
76+
77+
minimal_config_opts(tmpctx, argc, argv, &config_filename, &base_dir,
78+
&net_dir, &rpc_filename);
79+
opt_register_early_arg("--wallet", opt_set_talstr, NULL,
80+
&wallet_dsn,
81+
"Location of the wallet database.");
82+
opt_register_noarg("--help|-h", opt_usage_and_exit,
83+
"A tool to downgrade an offline Core Lightning Node to " PREV_VERSION,
84+
"Print this message.");
85+
opt_early_parse(argc, argv, opt_log_stderr_exit_usage);
86+
opt_parse(&argc, argv, opt_log_stderr_exit_usage);
87+
88+
if (argc != 1)
89+
opt_usage_exit_fail("No arguments expected");
90+
91+
if (!wallet_dsn)
92+
wallet_dsn = tal_fmt(tmpctx, "sqlite3://%s/lightningd.sqlite3", net_dir);
93+
94+
if (path_is_file(path_join(tmpctx, base_dir,
95+
tal_fmt(tmpctx, "lightningd-%s.pid",
96+
chainparams->network_name)))) {
97+
errx(ERROR_USAGE,
98+
"Lightningd PID file exists, aborting: lightningd must not be running");
99+
}
100+
101+
migrations = get_db_migrations(&num_migrations);
102+
prev_version_height = version_db(PREV_VERSION)->db_height;
103+
104+
/* Open db, check it's the expected version */
105+
db = db_open(tmpctx, wallet_dsn, false, false, db_error, NULL);
106+
db->report_changes_fn = NULL;
107+
108+
db_begin_transaction(db);
109+
db->data_version = db_data_version_get(db);
110+
current = db_get_version(db);
111+
112+
if (current < prev_version_height)
113+
errx(ERROR_DBVERSION, "Database version %zu already less than %zu expected for %s",
114+
current, prev_version_height, PREV_VERSION);
115+
if (current == prev_version_height) {
116+
printf("Already compatible with %s\n", PREV_VERSION);
117+
exit(0);
118+
}
119+
if (current >= num_migrations)
120+
errx(ERROR_DBVERSION, "Unknown database version %zu: I only know up to %zu (%s)",
121+
current, num_migrations, stringify(CLN_NEXT_VERSION));
122+
123+
/* current version is the last migration we did. */
124+
while (current > prev_version_height) {
125+
if (migrations[current].revertsql) {
126+
stmt = db_prepare_v2(db, migrations[current].revertsql);
127+
db_exec_prepared_v2(stmt);
128+
tal_free(stmt);
129+
}
130+
if (migrations[current].revertfn) {
131+
const char *error = migrations[current].revertfn(tmpctx, db);
132+
if (error)
133+
errx(ERROR_DBFAIL, "Downgrade failed: %s", error);
134+
}
135+
current--;
136+
}
137+
138+
/* Finally update the version number in the version table */
139+
stmt = db_prepare_v2(db, SQL("UPDATE version SET version=?;"));
140+
db_bind_int(stmt, current);
141+
db_exec_prepared_v2(stmt);
142+
tal_free(stmt);
143+
144+
printf("Downgrade to %s succeeded. Committing.\n", PREV_VERSION);
145+
db_commit_transaction(db);
146+
tal_free(db);
147+
148+
if (!version_db(PREV_VERSION)->gossip_store_compatible) {
149+
printf("Deleting incompatible gossip_store\n");
150+
unlink(path_join(tmpctx, net_dir, "gossip_store"));
151+
}
152+
}
153+
154+
/*** We don't actually perform migrations, so these are stubs which abort. ***/
155+
/* Remake with `make update-mocks` or `make update-mocks/tools/lightning-downgrade.c` */
156+
157+
/* AUTOGENERATED MOCKS START */
158+
/* Generated stub for fillin_missing_channel_blockheights */
159+
void fillin_missing_channel_blockheights(struct lightningd *ld UNNEEDED,
160+
struct db *db UNNEEDED)
161+
{ fprintf(stderr, "fillin_missing_channel_blockheights called!\n"); abort(); }
162+
/* Generated stub for fillin_missing_channel_id */
163+
void fillin_missing_channel_id(struct lightningd *ld UNNEEDED, struct db *db UNNEEDED)
164+
{ fprintf(stderr, "fillin_missing_channel_id called!\n"); abort(); }
165+
/* Generated stub for fillin_missing_lease_satoshi */
166+
void fillin_missing_lease_satoshi(struct lightningd *ld UNNEEDED,
167+
struct db *db UNNEEDED)
168+
{ fprintf(stderr, "fillin_missing_lease_satoshi called!\n"); abort(); }
169+
/* Generated stub for fillin_missing_local_basepoints */
170+
void fillin_missing_local_basepoints(struct lightningd *ld UNNEEDED,
171+
struct db *db UNNEEDED)
172+
{ fprintf(stderr, "fillin_missing_local_basepoints called!\n"); abort(); }
173+
/* Generated stub for fillin_missing_scriptpubkeys */
174+
void fillin_missing_scriptpubkeys(struct lightningd *ld UNNEEDED, struct db *db UNNEEDED)
175+
{ fprintf(stderr, "fillin_missing_scriptpubkeys called!\n"); abort(); }
176+
/* Generated stub for insert_addrtype_to_addresses */
177+
void insert_addrtype_to_addresses(struct lightningd *ld UNNEEDED,
178+
struct db *db UNNEEDED)
179+
{ fprintf(stderr, "insert_addrtype_to_addresses called!\n"); abort(); }
180+
/* Generated stub for migrate_channels_scids_as_integers */
181+
void migrate_channels_scids_as_integers(struct lightningd *ld UNNEEDED,
182+
struct db *db UNNEEDED)
183+
{ fprintf(stderr, "migrate_channels_scids_as_integers called!\n"); abort(); }
184+
/* Generated stub for migrate_convert_old_channel_keyidx */
185+
void migrate_convert_old_channel_keyidx(struct lightningd *ld UNNEEDED,
186+
struct db *db UNNEEDED)
187+
{ fprintf(stderr, "migrate_convert_old_channel_keyidx called!\n"); abort(); }
188+
/* Generated stub for migrate_datastore_commando_runes */
189+
void migrate_datastore_commando_runes(struct lightningd *ld UNNEEDED, struct db *db UNNEEDED)
190+
{ fprintf(stderr, "migrate_datastore_commando_runes called!\n"); abort(); }
191+
/* Generated stub for migrate_fail_pending_payments_without_htlcs */
192+
void migrate_fail_pending_payments_without_htlcs(struct lightningd *ld UNNEEDED,
193+
struct db *db UNNEEDED)
194+
{ fprintf(stderr, "migrate_fail_pending_payments_without_htlcs called!\n"); abort(); }
195+
/* Generated stub for migrate_fill_in_channel_type */
196+
void migrate_fill_in_channel_type(struct lightningd *ld UNNEEDED,
197+
struct db *db UNNEEDED)
198+
{ fprintf(stderr, "migrate_fill_in_channel_type called!\n"); abort(); }
199+
/* Generated stub for migrate_forwards_add_rowid */
200+
void migrate_forwards_add_rowid(struct lightningd *ld UNNEEDED,
201+
struct db *db UNNEEDED)
202+
{ fprintf(stderr, "migrate_forwards_add_rowid called!\n"); abort(); }
203+
/* Generated stub for migrate_from_account_db */
204+
void migrate_from_account_db(struct lightningd *ld UNNEEDED, struct db *db UNNEEDED)
205+
{ fprintf(stderr, "migrate_from_account_db called!\n"); abort(); }
206+
/* Generated stub for migrate_inflight_last_tx_to_psbt */
207+
void migrate_inflight_last_tx_to_psbt(struct lightningd *ld UNNEEDED, struct db *db UNNEEDED)
208+
{ fprintf(stderr, "migrate_inflight_last_tx_to_psbt called!\n"); abort(); }
209+
/* Generated stub for migrate_initialize_alias_local */
210+
void migrate_initialize_alias_local(struct lightningd *ld UNNEEDED,
211+
struct db *db UNNEEDED)
212+
{ fprintf(stderr, "migrate_initialize_alias_local called!\n"); abort(); }
213+
/* Generated stub for migrate_initialize_channel_htlcs_wait_indexes_and_fixup_forwards */
214+
void migrate_initialize_channel_htlcs_wait_indexes_and_fixup_forwards(struct lightningd *ld UNNEEDED,
215+
struct db *db UNNEEDED)
216+
{ fprintf(stderr, "migrate_initialize_channel_htlcs_wait_indexes_and_fixup_forwards called!\n"); abort(); }
217+
/* Generated stub for migrate_initialize_forwards_wait_indexes */
218+
void migrate_initialize_forwards_wait_indexes(struct lightningd *ld UNNEEDED,
219+
struct db *db UNNEEDED)
220+
{ fprintf(stderr, "migrate_initialize_forwards_wait_indexes called!\n"); abort(); }
221+
/* Generated stub for migrate_initialize_invoice_wait_indexes */
222+
void migrate_initialize_invoice_wait_indexes(struct lightningd *ld UNNEEDED,
223+
struct db *db UNNEEDED)
224+
{ fprintf(stderr, "migrate_initialize_invoice_wait_indexes called!\n"); abort(); }
225+
/* Generated stub for migrate_initialize_payment_wait_indexes */
226+
void migrate_initialize_payment_wait_indexes(struct lightningd *ld UNNEEDED,
227+
struct db *db UNNEEDED)
228+
{ fprintf(stderr, "migrate_initialize_payment_wait_indexes called!\n"); abort(); }
229+
/* Generated stub for migrate_invalid_last_tx_psbts */
230+
void migrate_invalid_last_tx_psbts(struct lightningd *ld UNNEEDED,
231+
struct db *db UNNEEDED)
232+
{ fprintf(stderr, "migrate_invalid_last_tx_psbts called!\n"); abort(); }
233+
/* Generated stub for migrate_invoice_created_index_var */
234+
void migrate_invoice_created_index_var(struct lightningd *ld UNNEEDED,
235+
struct db *db UNNEEDED)
236+
{ fprintf(stderr, "migrate_invoice_created_index_var called!\n"); abort(); }
237+
/* Generated stub for migrate_last_tx_to_psbt */
238+
void migrate_last_tx_to_psbt(struct lightningd *ld UNNEEDED, struct db *db UNNEEDED)
239+
{ fprintf(stderr, "migrate_last_tx_to_psbt called!\n"); abort(); }
240+
/* Generated stub for migrate_normalize_invstr */
241+
void migrate_normalize_invstr(struct lightningd *ld UNNEEDED,
242+
struct db *db UNNEEDED)
243+
{ fprintf(stderr, "migrate_normalize_invstr called!\n"); abort(); }
244+
/* Generated stub for migrate_our_funding */
245+
void migrate_our_funding(struct lightningd *ld UNNEEDED, struct db *db UNNEEDED)
246+
{ fprintf(stderr, "migrate_our_funding called!\n"); abort(); }
247+
/* Generated stub for migrate_payments_scids_as_integers */
248+
void migrate_payments_scids_as_integers(struct lightningd *ld UNNEEDED,
249+
struct db *db UNNEEDED)
250+
{ fprintf(stderr, "migrate_payments_scids_as_integers called!\n"); abort(); }
251+
/* Generated stub for migrate_pr2342_feerate_per_channel */
252+
void migrate_pr2342_feerate_per_channel(struct lightningd *ld UNNEEDED, struct db *db UNNEEDED)
253+
{ fprintf(stderr, "migrate_pr2342_feerate_per_channel called!\n"); abort(); }
254+
/* Generated stub for migrate_remove_chain_moves_duplicates */
255+
void migrate_remove_chain_moves_duplicates(struct lightningd *ld UNNEEDED, struct db *db UNNEEDED)
256+
{ fprintf(stderr, "migrate_remove_chain_moves_duplicates called!\n"); abort(); }
257+
/* Generated stub for migrate_runes_idfix */
258+
void migrate_runes_idfix(struct lightningd *ld UNNEEDED, struct db *db UNNEEDED)
259+
{ fprintf(stderr, "migrate_runes_idfix called!\n"); abort(); }
260+
/* AUTOGENERATED MOCKS END */

wallet/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ WALLET_SQL_FILES := \
4141
wallet/test/run-wallet.c \
4242
wallet/test/run-chain_moves_duplicate-detect.c \
4343
wallet/test/run-migrate_remove_chain_moves_duplicates.c \
44+
tools/lightning-downgrade.c \
45+
4446

4547
wallet/statements_gettextgen.po: $(WALLET_SQL_FILES) $(FORCE)
4648
@if $(call SHA256STAMP_CHANGED); then \

0 commit comments

Comments
 (0)