How would you do Binary Search to Locate a Block given a TimeStamp (for Any Blockchain)?

in #blog3 days ago (edited)

Yesterday, I mentioned using binary search to locate a block number. It is definitely doable. However, sometimes it might be quicker to use the SDS API, since this information can be retrieved by a simple query against the database.

That said, because the steemd API is publicly available, we don’t have to rely on external services. Instead, we can write a small function that performs a binary search directly against the blockchain.

For example, consider the following Python skeleton:

# Call steemd API to get the timestamp of a given block number.
def get_timestamp_from_block(block_num):
    pass

# Query steemd API to return the latest block number and its timestamp.
# Example return: (block_num, timestamp)
def get_latest_block_and_timestamp():
    pass

# Binary search to locate the block number that is closest to the target timestamp.
def get_block_from_timestamp(target_ts):
    # Step 1: Get latest block and its timestamp
    latest_block, latest_ts = get_latest_block_and_timestamp()
    
    # Step 2: Define search boundaries
    left, right = 1, latest_block
    result = None
    
    # Step 3: Binary search loop
    while left <= right:
        mid = (left + right) // 2
        mid_ts = get_timestamp_from_block(mid)
        
        if mid_ts < target_ts:
            left = mid + 1
            result = mid
        elif mid_ts > target_ts:
            right = mid - 1
        else:
            return mid  # exact match
    
    return result

How This Works

  1. Fetch timestamps from blocks
    By calling the steemd API, we can easily get the timestamp associated with any block.

  2. Find the latest block
    This gives us an upper bound for the binary search.

  3. Binary search for the target timestamp
    Instead of scanning blocks linearly (which would be painfully slow), we halve the search space at each step. This reduces the complexity to about O(log N) where N is the current block height. This is the beauty of Binary Search which is rated 1 of the top 10 algorithms that are invented by human so far.

Why This Matters

If you want to map real-world events (with timestamps) to their corresponding block numbers, this approach is efficient and doesn’t require any special database access — just the public RPC node.

Reblog to post: How would you do Binary Search to Locate a Block given a TimeStamp (for Any Blockchain)?

Steem to the Moon🚀!

Support me, thank you!

Why you should vote me? My contributions
Please vote me as a witness or set me as a proxy via https://steemitwallet.com/~witnesses

image.png

Sort:  

@justyy, this is a fantastic post! I love how you break down the process of using binary search on the blockchain to pinpoint block numbers based on timestamps. The Python code snippet is super helpful, and the explanation is clear and concise, even for those who aren't hardcore developers.

The efficiency gains you highlight are significant – moving from a potentially slow linear scan to O(log N) complexity is a huge win. This is exactly the kind of practical, insightful content that makes the Steemit blockchain so valuable!

Thanks for sharing your knowledge and providing a great tool for anyone looking to connect real-world events to the blockchain. I'm sure this will be useful to a lot of Steemians! Keep up the great work!