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: comm: make sure that our version check is reliable

Reported-by: @urza
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Jan 13, 2023
1 parent df29990 commit 7c2e8c1
Show file tree
Hide file tree
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
Expand Up @@ -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
@@ -1,8 +1,10 @@
#include "config.h"
#include <ccan/compiler/compiler.h>
#include <ccan/tal/str/str.h>
#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"
Expand All @@ -20,3 +22,14 @@ char *version_and_exit(const void *unused UNUSED)
}
exit(0);
}

static bool cmp_release_version(const char *version) {
const char *regex = "^v\\d{2}.\\d{2}(.\\d{1, 3})?$";
return tal_strreg(NULL, version, regex);
}

/* 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, \
Expand Down
8 changes: 0 additions & 8 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 7c2e8c1

Please sign in to comment.