Improve channel allocation and write basic tests for it

This commit is contained in:
2022-08-12 22:27:49 +01:00
parent 6306231098
commit 065fe94cbd
4 changed files with 143 additions and 12 deletions

View File

@@ -61,7 +61,7 @@ def getChanFree(net, new):
return (chanfree, chanlimits.pop())
def emptyChanAllocate(net, flist, relay, new):
def emptyChanAllocate(net, flist, new):
chanfree = getChanFree(net, new)
if not chanfree:
return
@@ -102,20 +102,21 @@ def emptyChanAllocate(net, flist, relay, new):
flist = flist[:sum_free]
debug(f"emptyChanAllocate() flist truncated to {sum_free}, length nis now {len(flist)}")
trace(f"emptyChanAllocate() best effort allocation: {flist}")
newlist = list(flist)
for i in chanfree[0].keys():
for x in range(chanfree[0][i]):
if not len(flist):
if not len(newlist):
break
if i in allocated.keys():
allocated[i].append(flist.pop())
allocated[i].append(newlist.pop())
else:
allocated[i] = [flist.pop()]
allocated[i] = [newlist.pop()]
return allocated
def populateChans(net, clist, relay, new):
def populateChans(net, clist, new):
# divided = array_split(clist, relay)
allocated = emptyChanAllocate(net, clist, relay, new)
allocated = emptyChanAllocate(net, clist, new)
if not allocated:
return
for i in allocated.keys():
@@ -148,7 +149,7 @@ def minifyChans(net, listinfo):
return listinfo
def keepChannels(net, listinfo, mean, sigrelay, relay):
def keepChannels(net, listinfo, mean, sigrelay, relay, chanlimit):
listinfo = minifyChans(net, listinfo)
if not listinfo:
return
@@ -159,23 +160,27 @@ def keepChannels(net, listinfo, mean, sigrelay, relay):
if not sigrelay <= main.config["ChanKeep"]["MaxRelay"]:
error("Network %s is too big to cover: %i relays required" % (net, sigrelay))
return
num_instances = len(getActiveRelays(net))
max_chans = chanlimit * num_instances
if coverAll:
needed = relay - len(getActiveRelays(net))
debug(f"keepChannels() coverAll asking to provision {needed} relays for {net} relay:{relay}")
newNums = modules.provision.provisionMultipleRelays(net, needed)
flist = [i[0] for i in listinfo]
populateChans(net, flist, relay, newNums)
chosen = sorted(flist, reverse=True, key=lambda x: x[1])[:max_chans]
populateChans(net, chosen, newNums)
else:
needed = sigrelay - len(getActiveRelays(net))
debug(f"keepChannels() NOT coverAll asking to provision {needed} relays for {net} sigrelay:{sigrelay}")
newNums = modules.provision.provisionMultipleRelays(net, needed)
siglist = [i[0] for i in listinfo if int(i[1]) > mean]
populateChans(net, siglist, sigrelay, newNums)
chosen = sorted(siglist, reverse=True, key=lambda x: x[1])[:max_chans]
populateChans(net, chosen, newNums)
notifyJoin(net)
def joinSingle(net, channel):
eca = emptyChanAllocate(net, [channel], None, [])
eca = emptyChanAllocate(net, [channel], [])
if not eca:
return False
if not len(eca.keys()) == 1:
@@ -272,8 +277,22 @@ def _initialList(net, num, listinfo, chanlimit):
p.execute()
debug("List parsing completed on %s" % net)
keepChannels(net, listinfo, mean, sigrelay, relay)
keepChannels(net, listinfo, mean, sigrelay, relay, chanlimit)
# return (listinfo, mean, sigrelay, relay)
def initialList(net, num, listinfo, chanlimit):
deferToThread(_initialList, net, num, deepcopy(listinfo), chanlimit)
def chankeep_handler(net, num, listinfo, chanlimit):
"""
Handle a channel keep request.
:param net:
:param num:
:param listinfo:
:param chanlimit:
:return:
"""
listinfo, mean, sigrelay, relay = _initialList(net, num, listinfo, chanlimit)