Created
October 17, 2017 21:23
-
-
Save 0xB10C/dfc3423b53946c425308e600984e9438 to your computer and use it in GitHub Desktop.
a playground for bitcoin Core's fee estimation buckets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# last bucket with more than 2^14 sat/byte | |
BUCKETCOUNT = 200 | |
# defines a 5% increase per bucket | |
# https://github.com/bitcoin/bitcoin/commit/e5007bae35ce22036a816505038277d99c84e3f7#diff-8c0941572d1cdf184d1751f7b7f1db4eR109 | |
FEE_SPACING = 1.05 | |
# list of fee buckets | |
BUCKETS = [] | |
# fills the bucket list | |
bucketFillIndex = 0 | |
BUCKETS.append(1) # initial bucket with 1 sat / byte | |
for bucketFillIndex in range(1,BUCKETCOUNT): | |
BUCKETS.append(BUCKETS[bucketFillIndex-1] * FEE_SPACING) # append bucket with 5% more than the previous | |
# binary find bucket by Fee in BUCKETS | |
def findBucketByFee(feerate): | |
posFirst = 0 | |
posLast = len(BUCKETS)-1 | |
if feerate >= BUCKETS[posLast]: | |
return posLast | |
if feerate <= BUCKETS[posFirst]: | |
return posFirst | |
while posFirst<=posLast: | |
posMid = (posFirst+posLast)//2 | |
if BUCKETS[posMid] <= feerate and BUCKETS[posMid+1] > feerate: | |
return posMid | |
if BUCKETS[posMid] > feerate: | |
posLast = posMid - 1 | |
if BUCKETS[posMid] < feerate: | |
posFirst = posMid + 1 | |
# function to manually verify a feerate for a bucket | |
def test(feerate): | |
print str(feerate) + " is in bucket " + str(findBucketByFee(feerate)) | |
# example tests for 1, 1.5, 2 sat/byte | |
test(1) | |
test(1.5) | |
test(2) | |
# print all buckets | |
counter = 0 | |
for bucket in BUCKETS: | |
print str(counter) + " - " + str(bucket) | |
counter+=1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment