Last active
May 14, 2022 23:07
-
-
Save normanlmfung/1f58d9718c0630a602117185e13deee7 to your computer and use it in GitHub Desktop.
block chain cutoff scanner - unit tests
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
import unittest | |
import datetime | |
from typing import List | |
from src.gizmo.blockchain_gizmo import search_block_with_cutoff_ts | |
class BlockChainGizmoTests(unittest.TestCase): | |
def test_search_block_with_cutoff_ts(self): | |
class DummyBlock: | |
def __init__(self, _number : int, _timestamp : int) -> None: | |
self.number = _number | |
self.timestamp = _timestamp | |
def _generate_dummy_blocks(how_many : int, target_cutoff_ts : int) -> List[DummyBlock]: | |
half = int(how_many / 2) | |
block_ts : int = target_cutoff_ts - half | |
dummy_blocks = [] | |
for i in range(half): | |
random_block = DummyBlock(i, block_ts) | |
block_ts = block_ts + 1 | |
dummy_blocks.append(random_block) | |
return dummy_blocks | |
cutoff = datetime.datetime(2022, 5, 1) | |
cutoff_ts = cutoff.timestamp() | |
dummy_blocks = _generate_dummy_blocks(how_many=1000, target_cutoff_ts=cutoff_ts) | |
def _get_dummy_block(block_number : int): | |
random_block = dummy_blocks.pop() | |
return random_block | |
end_block = dummy_blocks[-1].number | |
''' | |
If you want to use it for real: | |
start_block = scanner.search_block_with_cutoff_ts(end_block=end_block, cutoff_ts=cutoff_ts, search_step_size=10000, get_block=web3.eth.getBlock) | |
''' | |
start_block = search_block_with_cutoff_ts(end_block=end_block, cutoff_ts=cutoff_ts, search_step_size=30, get_block=_get_dummy_block) | |
# Dummies from [0 - 999], the mid block from _generate_dummy_blocks is 498th | |
assert(start_block==498) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment