From bc2d604639b190f593cf4b5f7d11f5a542782e3b Mon Sep 17 00:00:00 2001 From: Adam Gibson Date: Wed, 2 Jun 2021 16:10:05 +0100 Subject: [PATCH] Prevent absurdly low minsizes in yg-pe Fixes #889. Prior to this commit, it was possible for the yg-privacyenhanced.py script to go into an infinite loop of attempting to halve the txfee setting, in case the user selected (a) reloffer, (b) an absurdly low minsize like 1 sat, and (c) a txfee of zero. This occurred partially due to the randomization of minsizes included in the yg-pe algorithm, which could lead to a pathological zero value for minsize. After this commit the minsize is automatically bumped to the `DUST_THRESHOLD` value if it falls below it, preventing the possibility of a nonsensical value. Additionally, the loop which halves the txfee is no longer allowed to continue beyond 20 iterations, as an extra sanity check. --- scripts/yg-privacyenhanced.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/scripts/yg-privacyenhanced.py b/scripts/yg-privacyenhanced.py index f4760e8..43984c0 100755 --- a/scripts/yg-privacyenhanced.py +++ b/scripts/yg-privacyenhanced.py @@ -2,8 +2,10 @@ from future.utils import iteritems import random +import sys -from jmbase import get_log, jmprint +from jmbase import get_log, jmprint, EXIT_ARGERROR +from jmbitcoin import amount_to_str from jmclient import YieldGeneratorBasic, ygmain, jm_single # This is a maker for the purposes of generating a yield from held bitcoins @@ -66,6 +68,10 @@ class YieldGeneratorPrivacyEnhanced(YieldGeneratorBasic): self.txfee * (1 + float(self.txfee_factor)))) randomize_minsize = int(random.uniform(self.minsize * (1 - float(self.size_factor)), self.minsize * (1 + float(self.size_factor)))) + if randomize_minsize < jm_single().DUST_THRESHOLD: + jlog.warn("Minsize was randomized to below dust; resetting to dust " + "threshold: " + amount_to_str(jm_single().DUST_THRESHOLD)) + randomize_minsize = jm_single().DUST_THRESHOLD possible_maxsize = mix_balance[max_mix] - max(jm_single().DUST_THRESHOLD, randomize_txfee) randomize_maxsize = int(random.uniform(possible_maxsize * (1 - float(self.size_factor)), possible_maxsize)) @@ -87,14 +93,17 @@ class YieldGeneratorPrivacyEnhanced(YieldGeneratorBasic): 'cjfee': str(randomize_cjfee)} # sanity check - assert order['minsize'] >= 0 - assert order['maxsize'] > 0 + assert order['minsize'] >= jm_single().DUST_THRESHOLD assert order['minsize'] <= order['maxsize'] if order['ordertype'] in ['swreloffer', 'sw0reloffer']: - while order['txfee'] >= (float(order['cjfee']) * order['minsize']): + for i in range(20): + if order['txfee'] < (float(order['cjfee']) * order['minsize']): + break order['txfee'] = int(order['txfee'] / 2) - jlog.info('Warning: too high txfee to be profitable, halfing it to: ' + str(order['txfee'])) - + jlog.info('Warning: too high txfee to be profitable, halving it to: ' + str(order['txfee'])) + else: + jlog.error("Tx fee reduction algorithm failed. Quitting.") + sys.exit(EXIT_ARGERROR) return [order]