|
| 1 | +import pytest |
| 2 | + |
| 3 | +from async_substrate_interface.async_substrate import ( |
| 4 | + DiskCachedAsyncSubstrateInterface, |
| 5 | + AsyncSubstrateInterface, |
| 6 | +) |
| 7 | +from async_substrate_interface.sync_substrate import SubstrateInterface |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.asyncio |
| 11 | +async def test_disk_cache(): |
| 12 | + entrypoint = "wss://entrypoint-finney.opentensor.ai:443" |
| 13 | + async with DiskCachedAsyncSubstrateInterface(entrypoint) as disk_cached_substrate: |
| 14 | + current_block = await disk_cached_substrate.get_block_number(None) |
| 15 | + block_hash = await disk_cached_substrate.get_block_hash(current_block) |
| 16 | + parent_block_hash = await disk_cached_substrate.get_parent_block_hash( |
| 17 | + block_hash |
| 18 | + ) |
| 19 | + block_runtime_info = await disk_cached_substrate.get_block_runtime_info( |
| 20 | + block_hash |
| 21 | + ) |
| 22 | + block_runtime_version_for = ( |
| 23 | + await disk_cached_substrate.get_block_runtime_version_for(block_hash) |
| 24 | + ) |
| 25 | + block_hash_from_cache = await disk_cached_substrate.get_block_hash( |
| 26 | + current_block |
| 27 | + ) |
| 28 | + parent_block_hash_from_cache = ( |
| 29 | + await disk_cached_substrate.get_parent_block_hash(block_hash_from_cache) |
| 30 | + ) |
| 31 | + block_runtime_info_from_cache = ( |
| 32 | + await disk_cached_substrate.get_block_runtime_info(block_hash_from_cache) |
| 33 | + ) |
| 34 | + block_runtime_version_from_cache = ( |
| 35 | + await disk_cached_substrate.get_block_runtime_version_for( |
| 36 | + block_hash_from_cache |
| 37 | + ) |
| 38 | + ) |
| 39 | + assert block_hash == block_hash_from_cache |
| 40 | + assert parent_block_hash == parent_block_hash_from_cache |
| 41 | + assert block_runtime_info == block_runtime_info_from_cache |
| 42 | + assert block_runtime_version_for == block_runtime_version_from_cache |
| 43 | + async with AsyncSubstrateInterface(entrypoint) as non_cache_substrate: |
| 44 | + block_hash_non_cache = await non_cache_substrate.get_block_hash(current_block) |
| 45 | + parent_block_hash_non_cache = await non_cache_substrate.get_parent_block_hash( |
| 46 | + block_hash_non_cache |
| 47 | + ) |
| 48 | + block_runtime_info_non_cache = await non_cache_substrate.get_block_runtime_info( |
| 49 | + block_hash_non_cache |
| 50 | + ) |
| 51 | + block_runtime_version_for_non_cache = ( |
| 52 | + await non_cache_substrate.get_block_runtime_version_for( |
| 53 | + block_hash_non_cache |
| 54 | + ) |
| 55 | + ) |
| 56 | + assert block_hash == block_hash_non_cache |
| 57 | + assert parent_block_hash == parent_block_hash_non_cache |
| 58 | + assert block_runtime_info == block_runtime_info_non_cache |
| 59 | + assert block_runtime_version_for == block_runtime_version_for_non_cache |
| 60 | + with SubstrateInterface(entrypoint) as sync_substrate: |
| 61 | + block_hash_sync = sync_substrate.get_block_hash(current_block) |
| 62 | + parent_block_hash_sync = sync_substrate.get_parent_block_hash( |
| 63 | + block_hash_non_cache |
| 64 | + ) |
| 65 | + block_runtime_info_sync = sync_substrate.get_block_runtime_info( |
| 66 | + block_hash_non_cache |
| 67 | + ) |
| 68 | + block_runtime_version_for_sync = sync_substrate.get_block_runtime_version_for( |
| 69 | + block_hash_non_cache |
| 70 | + ) |
| 71 | + assert block_hash == block_hash_sync |
| 72 | + assert parent_block_hash == parent_block_hash_sync |
| 73 | + assert block_runtime_info == block_runtime_info_sync |
| 74 | + assert block_runtime_version_for == block_runtime_version_for_sync |
0 commit comments