From 4a5fa4d64ef3d3cddabbd44ecda546a85abf8981 Mon Sep 17 00:00:00 2001 From: Michael Schmoock Date: Fri, 2 Dec 2022 14:01:12 +0100 Subject: [PATCH] drain: rpcversion check for new format --- drain/clnutils.py | 23 +++++++++++++++++++++++ drain/drain.py | 15 +++++---------- drain/requirements.txt | 1 - 3 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 drain/clnutils.py diff --git a/drain/clnutils.py b/drain/clnutils.py new file mode 100644 index 00000000..68161cad --- /dev/null +++ b/drain/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/drain/drain.py b/drain/drain.py index 1052b704..2f6800ce 100755 --- a/drain/drain.py +++ b/drain/drain.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 +from clnutils import cln_parse_rpcversion from pyln.client import Plugin, Millisatoshi, RpcError from utils import get_ours, wait_ours import re -import semver import time import uuid @@ -23,7 +23,7 @@ HTLC_FEE_PAT = re.compile("^.* HTLC fee: ([0-9]+sat).*$") # The route msat helpers are needed because older versions of cln # had different msat/msatoshi fields with different types Millisatoshi/int def route_set_msat(r, msat): - if plugin.rpcversion.major == 0 and plugin.rpcversion.minor < 12: + if plugin.rpcversion[0] == 0 and plugin.rpcversion[1] < 12: r[plugin.msatfield] = msat.millisatoshis r['amount_msat'] = Millisatoshi(msat) else: @@ -488,20 +488,15 @@ def setbalance(plugin, scid: str, percentage: float = 50, chunks: int = 0, retry @plugin.init() def init(options, configuration, plugin): + # do all the stuff that needs to be done just once ... plugin.getinfo = plugin.rpc.getinfo() + plugin.rpcversion = cln_parse_rpcversion(plugin.getinfo.get('version')) plugin.configs = plugin.rpc.listconfigs() plugin.cltv_final = plugin.configs.get('cltv-final') - # parse semver string to determine RPC version - # strip leading 'v' although semver should ignore it, but it doesn't. - rpcversion = plugin.getinfo.get('version') - if rpcversion.startswith('v'): - rpcversion = rpcversion[1:] - plugin.rpcversion = semver.VersionInfo.parse(rpcversion) - # use getroute amount_msat/msatoshi field depending on version plugin.msatfield = 'amount_msat' - if plugin.rpcversion.major == 0 and plugin.rpcversion.minor < 12: + if plugin.rpcversion[0] == 0 and plugin.rpcversion[1] < 12: plugin.msatfield = 'msatoshi' plugin.log("Plugin drain.py initialized") diff --git a/drain/requirements.txt b/drain/requirements.txt index 38ec7a50..7ebb30eb 100644 --- a/drain/requirements.txt +++ b/drain/requirements.txt @@ -1,2 +1 @@ pyln-client>=0.12 -semver==2.*