Skip to content

Commit

Permalink
comm: make sure that our version check is reliable
Browse files Browse the repository at this point in the history
Rework the logic of the version check used in the
database migration, and make sure
that it is full functional to avoid confusion
at release time.

Changelog-Fixed: database: Correctly identity official release versions for database upgrade.

Reported-by: @urza
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
  • Loading branch information
vincenzopalazzo authored and rustyrussell committed Jan 17, 2023
1 parent 8d825ef commit 8f94e8b
Showing 5 changed files with 41 additions and 8 deletions.
7 changes: 7 additions & 0 deletions common/test/Makefile
Original file line number Diff line number Diff line change
@@ -90,4 +90,11 @@ common/test/run-bolt12_merkle-json: \
common/base32.o \
common/wireaddr.o


common/test/run-version: \
common/amount.o \
wire/fromwire.o \
wire/towire.o


check-units: $(COMMON_TEST_PROGRAMS:%=unittest/%)
16 changes: 16 additions & 0 deletions common/test/run-version.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "config.h"
#include "../version.c"
#include <common/setup.h>
#include <assert.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
common_setup(argv[0]);

assert(cmp_release_version("v22.11"));
assert(cmp_release_version("v22.11.1"));
assert(cmp_release_version("v22.11.1-6-gdf29990-modded") == false);

common_shutdown();
}
13 changes: 13 additions & 0 deletions common/version.c
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
#include <common/version.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Only common/version.c can safely include this. */
# include "version_gen.h"
@@ -20,3 +21,15 @@ char *version_and_exit(const void *unused UNUSED)
}
exit(0);
}

static bool cmp_release_version(const char *version) {
if (version[0] != 'v')
return false;
return strspn(version+1, ".0123456789") == strlen(version+1);
}

/* Released versions are of form v[year].[month]?(.patch)* */
bool is_released_version(void)
{
return cmp_release_version(version());
}
5 changes: 5 additions & 0 deletions common/version.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#ifndef LIGHTNING_COMMON_VERSION_H
#define LIGHTNING_COMMON_VERSION_H
#include "config.h"
#include <stdbool.h>

char *version_and_exit(const void *unused);
const char *version(void);
/* check if the current version is a release version.
*
* Released versions are of form v[year].[month]?(.patch)* */
bool is_released_version(void);

#define opt_register_version() \
opt_register_early_noarg("--version|-V", version_and_exit, NULL, \
8 changes: 0 additions & 8 deletions wallet/db.c
Original file line number Diff line number Diff line change
@@ -945,14 +945,6 @@ static struct migration dbmigrations[] = {
/* FIXME: Remove payments local_offer_id column! */
};

/* Released versions are of form v{num}[.{num}]* */
static bool is_released_version(void)
{
if (version()[0] != 'v')
return false;
return strcspn(version()+1, ".0123456789") == strlen(version()+1);
}

/**
* db_migrate - Apply all remaining migrations from the current version
*/

0 comments on commit 8f94e8b

Please sign in to comment.