-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Percentile function to handle out of range percentiles #983
Changes from 4 commits
ae718ba
9ec88d8
952bfb5
cab6a5b
ce5f8c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
) | ||
|
||
from web3.exceptions import ( | ||
InsufficientData, | ||
ValidationError, | ||
) | ||
from web3.utils.math import ( | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My instinct would be Also, is it definitely impossible for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose, but it could also trend down with min. Also maybe it doesn't matter on a private network? I don't feel strongly about it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't you also include transactions from the last few blocks when there is not enough data available? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's already looking across 120 blocks by default, but this is grouped by miner. So a few scenarios could cause a small data set:
|
||
yield MinerData( | ||
miner, | ||
len(set(block_hashes)), | ||
min(gas_prices), | ||
percentile(gas_prices, percentile=15)) | ||
price_percentile) | ||
|
||
|
||
@to_tuple | ||
|
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)) | ||
if values in [None, tuple(), []] or len(values) < 1: | ||
raise InsufficientData("Expected a sequence of at least 1 integers, got {0}".format(values)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
if percentile is None: | ||
raise ValueError("Expected a percentile choice, got {0}".format(percentile)) | ||
|
||
|
@@ -11,6 +16,8 @@ def percentile(values=None, percentile=None): | |
rank = len(values) * percentile / 100 | ||
if rank > 0: | ||
index = rank - 1 | ||
if index < 0: | ||
return sorted_values[0] | ||
else: | ||
index = rank | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not reviewing these numbers, just trusting that you double-checked them. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be a good idea to at least once check how accurate the target times are when compared to actual times on ropsten or even mainnet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Also, as a quicker test, we could compare against https://ethgasstation.info/.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooh yeah. Then I could compare to main net without spending any gas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The estimates are comparable to ethgasstation.info, but I noticed that with longer acceptable wait times, our estimates are a bit lower. I ran some tests by sending a transaction back and forth on on mainnet using our estimate. I tried raising the percentile. Given the results, I am going to bump up the percentile to 20%, since it yielded the best results.
15% percentile
20% percentile
25%
40%
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solid 👍