Skip to content

Instantly share code, notes, and snippets.

@mgritter
Created July 27, 2018 21:56
Show Gist options
  • Save mgritter/b4543a6edbebc1a8cf003b9733ba957d to your computer and use it in GitHub Desktop.
Save mgritter/b4543a6edbebc1a8cf003b9733ba957d to your computer and use it in GitHub Desktop.
Find an interval within the Steem blockchain
#!/usr/bin/python3
from steem import Steem
from pprint import pprint
import sys
startTime = "2018-05-01T00:00:00"
endTime = "2018-05-31T23:59:59"
if len( sys.argv ) > 1:
startTime = sys.argv[1]
if len( sys.argv ) > 2:
endTime = sys.argv[2]
s = Steem()
lastBlock = s.head_block_number
upperBound = lastBlock
lowerBound = 1
midpoint = None
while lowerBound + 1 < upperBound:
probe = ( upperBound + lowerBound) // 2
b = s.get_block_header( probe )
print( "block", probe, b['timestamp'] )
if b['timestamp'] < startTime:
lowerBound = probe
elif b['timestamp'] > endTime:
upperBound = probe
else:
midpoint = probe
break
tooLarge = midpoint
while lowerBound + 1 < tooLarge:
probe = (lowerBound + tooLarge) // 2
b = s.get_block_header( probe )
print( "block", probe, b['timestamp'] )
if b['timestamp'] <= startTime:
lowerBound = probe
else:
tooLarge = probe
tooSmall = midpoint
while tooSmall + 1 < upperBound:
probe = ( tooSmall + upperBound ) // 2
b = s.get_block_header( probe )
print( "block", probe, b['timestamp'] )
if b['timestamp'] <= endTime:
tooSmall = probe
else:
upperBound = probe
firstBlock = s.get_block_header( lowerBound )
lastBlock = s.get_block_header( upperBound - 1 )
print( "First block:", lowerBound, firstBlock['timestamp'] )
print( "Last block:", upperBound - 1, lastBlock['timestamp'] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment