diff --git a/feeadjuster/clnutils.py b/feeadjuster/clnutils.py new file mode 100644 index 0000000..68161ca --- /dev/null +++ b/feeadjuster/clnutils.py @@ -0,0 +1,23 @@ +import re + + +def cln_parse_rpcversion(string): + """ + Parse cln version string to determine RPC version. + + cln switched from 'semver' alike `major.minor.sub[rcX][-mod]` + to ubuntu style with version 22.11 `yy.mm[.patch][-mod]` + make sure we can read all of them for (the next 80 years). + """ + rpcversion = string + if rpcversion.startswith('v'): # strip leading 'v' + rpcversion = rpcversion[1:] + if rpcversion.find('-') != -1: # strip mods + rpcversion = rpcversion[:rpcversion.find('-')] + if re.search('.*(rc[\\d]*)$', rpcversion): # strip release candidates + rpcversion = rpcversion[:rpcversion.find('rc')] + if rpcversion.count('.') == 1: # imply patch version 0 if not given + rpcversion = rpcversion + '.0' + + # split and convert numeric string parts to actual integers + return list(map(int, rpcversion.split('.'))) diff --git a/feeadjuster/feeadjuster.py b/feeadjuster/feeadjuster.py index 355c065..72c5677 100755 --- a/feeadjuster/feeadjuster.py +++ b/feeadjuster/feeadjuster.py @@ -1,9 +1,9 @@ #!/usr/bin/env python3 import random -import semver import statistics import time import math +from clnutils import cln_parse_rpcversion from pyln.client import Plugin, Millisatoshi, RpcError from threading import Lock @@ -213,7 +213,7 @@ def forward_event(plugin: Plugin, forward_event: dict, **kwargs): out_scid = forward_event["out_channel"] maybe_add_new_balances(plugin, [in_scid, out_scid]) - if plugin.rpcversion.major == 0 and plugin.rpcversion.minor < 12: + if plugin.rpcversion[0] == 0 and plugin.rpcversion[1] < 12: plugin.adj_balances[in_scid]["our"] += int(forward_event["in_msatoshi"]) plugin.adj_balances[out_scid]["our"] -= int(forward_event["out_msatoshi"]) else: @@ -287,14 +287,9 @@ def feeadjuster_toggle(plugin: Plugin, value: bool = None): @plugin.init() def init(options: dict, configuration: dict, plugin: Plugin, **kwargs): - # parse semver string to determine RPC version - # strip leading 'v' although semver should ignore it, but it doesn't. + # do all the stuff that needs to be done just once ... plugin.getinfo = plugin.rpc.getinfo() - rpcversion = plugin.getinfo.get('version') - if rpcversion.startswith('v'): - rpcversion = rpcversion[1:] - plugin.rpcversion = semver.VersionInfo.parse(rpcversion) - + plugin.rpcversion = cln_parse_rpcversion(plugin.getinfo.get('version')) plugin.our_node_id = plugin.getinfo["id"] plugin.deactivate_fuzz = options.get("feeadjuster-deactivate-fuzz") plugin.forward_event_subscription = not options.get("feeadjuster-deactivate-fee-update") diff --git a/feeadjuster/requirements.txt b/feeadjuster/requirements.txt index 38ec7a5..7ebb30e 100644 --- a/feeadjuster/requirements.txt +++ b/feeadjuster/requirements.txt @@ -1,2 +1 @@ pyln-client>=0.12 -semver==2.* diff --git a/feeadjuster/test_clnutils.py b/feeadjuster/test_clnutils.py new file mode 100644 index 0000000..002e5c7 --- /dev/null +++ b/feeadjuster/test_clnutils.py @@ -0,0 +1,43 @@ +from clnutils import cln_parse_rpcversion + + +def test_rpcversion(): + foo = cln_parse_rpcversion("0.11.2") + assert(foo[0] == 0) + assert(foo[1] == 11) + assert(foo[2] == 2) + + foo = cln_parse_rpcversion("0.11.2rc2-modded") + assert(foo[0] == 0) + assert(foo[1] == 11) + assert(foo[2] == 2) + + foo = cln_parse_rpcversion("22.11") + assert(foo[0] == 22) + assert(foo[1] == 11) + assert(foo[2] == 0) + + foo = cln_parse_rpcversion("22.11rc1") + assert(foo[0] == 22) + assert(foo[1] == 11) + assert(foo[2] == 0) + + foo = cln_parse_rpcversion("22.11rc1-modded") + assert(foo[0] == 22) + assert(foo[1] == 11) + assert(foo[2] == 0) + + foo = cln_parse_rpcversion("22.11-modded") + assert(foo[0] == 22) + assert(foo[1] == 11) + assert(foo[2] == 0) + + foo = cln_parse_rpcversion("22.11.0") + assert(foo[0] == 22) + assert(foo[1] == 11) + assert(foo[2] == 0) + + foo = cln_parse_rpcversion("22.11.1") + assert(foo[0] == 22) + assert(foo[1] == 11) + assert(foo[2] == 1)