tmempool fees: increase estimate by max precision of histogram - electrum - Electrum Bitcoin wallet HTML git clone https://git.parazyd.org/electrum DIR Log DIR Files DIR Refs DIR Submodules --- DIR commit b8ab36546dad3b0774222b1f2e0bf61628c0d490 DIR parent f8ee20322551575c3f969d4b00dfb3c441ac1b3f HTML Author: SomberNight <somber.night@protonmail.com> Date: Sat, 14 Jul 2018 18:45:02 +0200 mempool fees: increase estimate by max precision of histogram related: #4551 Diffstat: M electrum/simple_config.py | 19 +++++++++++++++++-- M electrum/tests/test_simple_config.… | 36 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) --- DIR diff --git a/electrum/simple_config.py b/electrum/simple_config.py t@@ -5,6 +5,7 @@ import os import stat from decimal import Decimal from typing import Union +from numbers import Real from copy import deepcopy t@@ -310,7 +311,11 @@ class SimpleConfig(PrintError): fee = int(fee) return fee - def fee_to_depth(self, target_fee): + def fee_to_depth(self, target_fee: Real) -> int: + """For a given sat/vbyte fee, returns an estimate of how deep + it would be in the current mempool in vbytes. + Pessimistic == overestimates the depth. + """ depth = 0 for fee, s in self.mempool_fees: depth += s t@@ -320,10 +325,16 @@ class SimpleConfig(PrintError): return 0 return depth - @impose_hard_limits_on_fee def depth_to_fee(self, slider_pos) -> int: """Returns fee in sat/kbyte.""" target = self.depth_target(slider_pos) + return self.depth_target_to_fee(target) + + @impose_hard_limits_on_fee + def depth_target_to_fee(self, target: int) -> int: + """Returns fee in sat/kbyte. + target: desired mempool depth in sat/vbyte + """ depth = 0 for fee, s in self.mempool_fees: depth += s t@@ -331,6 +342,10 @@ class SimpleConfig(PrintError): break else: return 0 + # add one sat/byte as currently that is + # the max precision of the histogram + fee += 1 + # convert to sat/kbyte return fee * 1000 def depth_target(self, slider_pos): DIR diff --git a/electrum/tests/test_simple_config.py b/electrum/tests/test_simple_config.py t@@ -110,6 +110,42 @@ class Test_SimpleConfig(SequentialTestCase): result.pop('config_version', None) self.assertEqual({"something": "a"}, result) + def test_depth_target_to_fee(self): + config = SimpleConfig({}) + config.mempool_fees = [[49, 100110], [10, 121301], [6, 153731], [5, 125872], [1, 36488810]] + self.assertEqual( 2 * 1000, config.depth_target_to_fee(1000000)) + self.assertEqual( 6 * 1000, config.depth_target_to_fee( 500000)) + self.assertEqual( 7 * 1000, config.depth_target_to_fee( 250000)) + self.assertEqual(11 * 1000, config.depth_target_to_fee( 200000)) + self.assertEqual(50 * 1000, config.depth_target_to_fee( 100000)) + config.mempool_fees = [] + self.assertEqual( 1 * 1000, config.depth_target_to_fee(10 ** 5)) + self.assertEqual( 1 * 1000, config.depth_target_to_fee(10 ** 6)) + self.assertEqual( 1 * 1000, config.depth_target_to_fee(10 ** 7)) + config.mempool_fees = [[1, 36488810]] + self.assertEqual( 2 * 1000, config.depth_target_to_fee(10 ** 5)) + self.assertEqual( 2 * 1000, config.depth_target_to_fee(10 ** 6)) + self.assertEqual( 2 * 1000, config.depth_target_to_fee(10 ** 7)) + self.assertEqual( 1 * 1000, config.depth_target_to_fee(10 ** 8)) + config.mempool_fees = [[5, 125872], [1, 36488810]] + self.assertEqual( 6 * 1000, config.depth_target_to_fee(10 ** 5)) + self.assertEqual( 2 * 1000, config.depth_target_to_fee(10 ** 6)) + self.assertEqual( 2 * 1000, config.depth_target_to_fee(10 ** 7)) + self.assertEqual( 1 * 1000, config.depth_target_to_fee(10 ** 8)) + + def test_fee_to_depth(self): + config = SimpleConfig({}) + config.mempool_fees = [[49, 100000], [10, 120000], [6, 150000], [5, 125000], [1, 36000000]] + self.assertEqual(100000, config.fee_to_depth(500)) + self.assertEqual(100000, config.fee_to_depth(50)) + self.assertEqual(100000, config.fee_to_depth(49)) + self.assertEqual(220000, config.fee_to_depth(48)) + self.assertEqual(220000, config.fee_to_depth(10)) + self.assertEqual(370000, config.fee_to_depth(9)) + self.assertEqual(370000, config.fee_to_depth(6.5)) + self.assertEqual(370000, config.fee_to_depth(6)) + self.assertEqual(495000, config.fee_to_depth(5.5)) + class TestUserConfig(SequentialTestCase):