Browse Source

mempool fees: increase estimate by max precision of histogram

related: #4551
3.2.x
SomberNight 7 years ago
parent
commit
b8ab36546d
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 19
      electrum/simple_config.py
  2. 36
      electrum/tests/test_simple_config.py

19
electrum/simple_config.py

@ -5,6 +5,7 @@ import os
import stat import stat
from decimal import Decimal from decimal import Decimal
from typing import Union from typing import Union
from numbers import Real
from copy import deepcopy from copy import deepcopy
@ -310,7 +311,11 @@ class SimpleConfig(PrintError):
fee = int(fee) fee = int(fee)
return 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 depth = 0
for fee, s in self.mempool_fees: for fee, s in self.mempool_fees:
depth += s depth += s
@ -320,10 +325,16 @@ class SimpleConfig(PrintError):
return 0 return 0
return depth return depth
@impose_hard_limits_on_fee
def depth_to_fee(self, slider_pos) -> int: def depth_to_fee(self, slider_pos) -> int:
"""Returns fee in sat/kbyte.""" """Returns fee in sat/kbyte."""
target = self.depth_target(slider_pos) 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 depth = 0
for fee, s in self.mempool_fees: for fee, s in self.mempool_fees:
depth += s depth += s
@ -331,6 +342,10 @@ class SimpleConfig(PrintError):
break break
else: else:
return 0 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 return fee * 1000
def depth_target(self, slider_pos): def depth_target(self, slider_pos):

36
electrum/tests/test_simple_config.py

@ -110,6 +110,42 @@ class Test_SimpleConfig(SequentialTestCase):
result.pop('config_version', None) result.pop('config_version', None)
self.assertEqual({"something": "a"}, result) 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): class TestUserConfig(SequentialTestCase):

Loading…
Cancel
Save