|
| 1 | +import pytest |
| 2 | +from random import ( |
| 3 | + randint, |
| 4 | +) |
| 5 | + |
| 6 | +from web3._utils.request import ( |
| 7 | + _async_session_cache, |
| 8 | +) |
| 9 | +from web3.beacon import ( |
| 10 | + AsyncBeacon, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +def _assert_valid_response(response): |
| 15 | + # assert valid response according to Beacon API spec |
| 16 | + assert isinstance(response, dict) |
| 17 | + assert "data" in response |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def async_beacon(): |
| 22 | + return AsyncBeacon(base_url="http://127.0.0.1:5052") |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(autouse=True) |
| 26 | +def _cleanup(): |
| 27 | + yield |
| 28 | + _async_session_cache.clear() |
| 29 | + |
| 30 | + |
| 31 | +# Beacon endpoint tests: |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.asyncio |
| 35 | +async def test_async_cl_beacon_get_genesis(async_beacon): |
| 36 | + response = await async_beacon.get_genesis() |
| 37 | + _assert_valid_response(response) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.asyncio |
| 41 | +async def test_async_cl_beacon_get_hash_root(async_beacon): |
| 42 | + response = await async_beacon.get_hash_root() |
| 43 | + _assert_valid_response(response) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.asyncio |
| 47 | +async def test_async_cl_beacon_get_fork_data(async_beacon): |
| 48 | + response = await async_beacon.get_fork_data() |
| 49 | + _assert_valid_response(response) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.asyncio |
| 53 | +async def test_async_cl_beacon_get_finality_checkpoint(async_beacon): |
| 54 | + response = await async_beacon.get_finality_checkpoint() |
| 55 | + _assert_valid_response(response) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.asyncio |
| 59 | +async def test_async_cl_beacon_get_validators(async_beacon): |
| 60 | + response = await async_beacon.get_validators() |
| 61 | + _assert_valid_response(response) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.asyncio |
| 65 | +async def test_async_cl_beacon_get_validator(async_beacon): |
| 66 | + validators_response = await async_beacon.get_validators() |
| 67 | + _assert_valid_response(validators_response) |
| 68 | + |
| 69 | + validators = validators_response["data"] |
| 70 | + random_validator = validators[randint(0, len(validators))] |
| 71 | + random_validator_pubkey = random_validator["validator"]["pubkey"] |
| 72 | + |
| 73 | + response = await async_beacon.get_validator(random_validator_pubkey) |
| 74 | + _assert_valid_response(response) |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.asyncio |
| 78 | +async def test_async_cl_beacon_get_validator_balances(async_beacon): |
| 79 | + response = await async_beacon.get_validator_balances() |
| 80 | + _assert_valid_response(response) |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.asyncio |
| 84 | +async def test_async_cl_beacon_get_epoch_committees(async_beacon): |
| 85 | + response = await async_beacon.get_epoch_committees() |
| 86 | + _assert_valid_response(response) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.asyncio |
| 90 | +async def test_async_cl_beacon_get_block_headers(async_beacon): |
| 91 | + response = await async_beacon.get_block_headers() |
| 92 | + _assert_valid_response(response) |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.asyncio |
| 96 | +async def test_async_cl_beacon_get_block_header(async_beacon): |
| 97 | + response = await async_beacon.get_block_header("head") |
| 98 | + _assert_valid_response(response) |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.asyncio |
| 102 | +async def test_async_cl_beacon_get_block(async_beacon): |
| 103 | + response = await async_beacon.get_block("head") |
| 104 | + _assert_valid_response(response) |
| 105 | + |
| 106 | + |
| 107 | +@pytest.mark.asyncio |
| 108 | +async def test_async_cl_beacon_get_block_root(async_beacon): |
| 109 | + response = await async_beacon.get_block_root("head") |
| 110 | + _assert_valid_response(response) |
| 111 | + |
| 112 | + |
| 113 | +@pytest.mark.asyncio |
| 114 | +async def test_async_cl_beacon_get_block_attestations(async_beacon): |
| 115 | + response = await async_beacon.get_block_attestations("head") |
| 116 | + _assert_valid_response(response) |
| 117 | + |
| 118 | + |
| 119 | +@pytest.mark.asyncio |
| 120 | +async def test_async_cl_beacon_get_attestations(async_beacon): |
| 121 | + response = await async_beacon.get_attestations() |
| 122 | + _assert_valid_response(response) |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.asyncio |
| 126 | +async def test_async_cl_beacon_get_attester_slashings(async_beacon): |
| 127 | + response = await async_beacon.get_attester_slashings() |
| 128 | + _assert_valid_response(response) |
| 129 | + |
| 130 | + |
| 131 | +@pytest.mark.asyncio |
| 132 | +async def test_async_cl_beacon_get_proposer_slashings(async_beacon): |
| 133 | + response = await async_beacon.get_proposer_slashings() |
| 134 | + _assert_valid_response(response) |
| 135 | + |
| 136 | + |
| 137 | +@pytest.mark.asyncio |
| 138 | +async def test_async_cl_beacon_get_voluntary_exits(async_beacon): |
| 139 | + response = await async_beacon.get_voluntary_exits() |
| 140 | + _assert_valid_response(response) |
| 141 | + |
| 142 | + |
| 143 | +# Config endpoint tests: |
| 144 | + |
| 145 | + |
| 146 | +@pytest.mark.asyncio |
| 147 | +async def test_async_cl_config_get_fork_schedule(async_beacon): |
| 148 | + response = await async_beacon.get_fork_schedule() |
| 149 | + _assert_valid_response(response) |
| 150 | + |
| 151 | + |
| 152 | +@pytest.mark.asyncio |
| 153 | +async def test_async_cl_config_get_spec(async_beacon): |
| 154 | + response = await async_beacon.get_spec() |
| 155 | + _assert_valid_response(response) |
| 156 | + |
| 157 | + |
| 158 | +@pytest.mark.asyncio |
| 159 | +async def test_async_cl_config_get_deposit_contract(async_beacon): |
| 160 | + response = await async_beacon.get_deposit_contract() |
| 161 | + _assert_valid_response(response) |
| 162 | + |
| 163 | + |
| 164 | +# Debug endpoint test_asyncs: |
| 165 | + |
| 166 | + |
| 167 | +@pytest.mark.asyncio |
| 168 | +async def test_async_cl_debug_get_beacon_state(async_beacon): |
| 169 | + response = await async_beacon.get_beacon_state() |
| 170 | + _assert_valid_response(response) |
| 171 | + |
| 172 | + |
| 173 | +@pytest.mark.asyncio |
| 174 | +async def test_async_cl_debug_get_beacon_heads(async_beacon): |
| 175 | + response = await async_beacon.get_beacon_heads() |
| 176 | + _assert_valid_response(response) |
| 177 | + |
| 178 | + |
| 179 | +# Node endpoint test_asyncs: |
| 180 | + |
| 181 | + |
| 182 | +@pytest.mark.asyncio |
| 183 | +async def test_async_cl_node_get_node_identity(async_beacon): |
| 184 | + response = await async_beacon.get_node_identity() |
| 185 | + _assert_valid_response(response) |
| 186 | + |
| 187 | + |
| 188 | +@pytest.mark.asyncio |
| 189 | +async def test_async_cl_node_get_peers(async_beacon): |
| 190 | + response = await async_beacon.get_peers() |
| 191 | + _assert_valid_response(response) |
| 192 | + |
| 193 | + |
| 194 | +@pytest.mark.asyncio |
| 195 | +async def test_async_cl_node_get_peer(async_beacon): |
| 196 | + response = await async_beacon.get_peer("") |
| 197 | + _assert_valid_response(response) |
| 198 | + |
| 199 | + |
| 200 | +@pytest.mark.asyncio |
| 201 | +async def test_async_cl_node_get_health(async_beacon): |
| 202 | + response = await async_beacon.get_health() |
| 203 | + assert response <= 206 |
| 204 | + |
| 205 | + |
| 206 | +@pytest.mark.asyncio |
| 207 | +async def test_async_cl_node_get_version(async_beacon): |
| 208 | + response = await async_beacon.get_version() |
| 209 | + _assert_valid_response(response) |
| 210 | + |
| 211 | + |
| 212 | +@pytest.mark.asyncio |
| 213 | +async def test_async_cl_node_get_syncing(async_beacon): |
| 214 | + response = await async_beacon.get_syncing() |
| 215 | + _assert_valid_response(response) |
0 commit comments