|
| 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 */ |
0 commit comments