Skip to content

Commit

Permalink
Update time based strategy to use min() with < 3 data points
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanjw committed Jul 31, 2018
1 parent e00740e commit ae718ba
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
5 changes: 4 additions & 1 deletion tests/core/utilities/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
strategies as st,
)

from web3.exceptions import (
InsufficientData,
)
from web3.utils.math import (
percentile,
)
Expand All @@ -30,7 +33,7 @@ def test_percentiles_out_of_one_hundred(p, expected):


def test_percentiles_with_single_values():
with pytest.raises(ValueError):
with pytest.raises(InsufficientData):
percentile([0], 1)


Expand Down
8 changes: 8 additions & 0 deletions web3/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,11 @@ class NoABIEventsFound(AttributeError):
Raised when an ABI doesn't contain any events.
"""
pass


class InsufficientData(Exception):
"""
Raised when there are insufficient data points to
complete a calculation
"""
pass
7 changes: 6 additions & 1 deletion web3/gas_strategies/time_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
)

from web3.exceptions import (
InsufficientData,
ValidationError,
)
from web3.utils.math import (
Expand Down Expand Up @@ -60,11 +61,15 @@ def _aggregate_miner_data(raw_data):

for miner, miner_data in data_by_miner.items():
_, block_hashes, gas_prices = map(set, zip(*miner_data))
try:
price_percentile = percentile(gas_prices, percentile=15)
except InsufficientData:
price_percentile = min(gas_prices)
yield MinerData(
miner,
len(set(block_hashes)),
min(gas_prices),
percentile(gas_prices, percentile=15))
price_percentile)


@to_tuple
Expand Down
7 changes: 6 additions & 1 deletion web3/utils/math.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from web3.exceptions import (
InsufficientData,
)


def percentile(values=None, percentile=None):
"""Calculates a simplified weighted average percentile
"""
if values in [None, tuple(), []] or len(values) < 3:
raise ValueError("Expected a sequence of at least 3 integers, got {0}".format(values))
raise InsufficientData("Expected a sequence of at least 3 integers, got {0}".format(values))
if percentile is None:
raise ValueError("Expected a percentile choice, got {0}".format(percentile))

Expand Down

0 comments on commit ae718ba

Please sign in to comment.