drain: rpcversion check for new format

This commit is contained in:
Michael Schmoock
2022-12-02 14:01:12 +01:00
parent 656068ba09
commit 4a5fa4d64e
3 changed files with 28 additions and 11 deletions

23
drain/clnutils.py Normal file
View File

@@ -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('.')))

View File

@@ -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")

View File

@@ -1,2 +1 @@
pyln-client>=0.12
semver==2.*