Skip to content

Commit

Permalink
Add status reqresp test
Browse files Browse the repository at this point in the history
  • Loading branch information
lsankar4033 committed Aug 18, 2020
1 parent 45f1b48 commit 058c342
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 34 deletions.
74 changes: 40 additions & 34 deletions tests/reqresp/status.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,51 @@
from eth2spec.utils.ssz.ssz_typing import (
Bytes4, Bytes32, Container, uint64
)
from pyrum import SubprocessConn, Rumor
from eth2spec.utils.ssz.ssz_typing import Container
from eth2spec.phase0.spec import Slot, Root, Epoch, ForkDigest
from sclients import connect_rumor
import trio

from ..utils import *
from ..utils import parse_chunk_response, with_rumor, enr_to_fork_digest


class Request(Container):
version: Bytes4
finalized_root: Bytes32
finalized_epoch: uint64
head_root: Bytes32
head_slot: uint64
class Status(Container):
fork_digest: ForkDigest
finalized_root: Root
finalized_epoch: Epoch
head_root: Root
head_slot: Slot


async def test_status(enr, beacon_state_path):
async with SubprocessConn(cmd='rumor bare') as conn:
async with trio.open_nursery() as nursery:
rumor = Rumor(conn, nursery)
peer_id = await connect_rumor(rumor, enr)
@with_rumor
async def run(rumor, args):
peer_id = await connect_rumor(rumor, args['client'], args['enr'])

req = Request(head_slot=0).encode_bytes().hex()
resp = await rumor.rpc.status.req.raw(peer_id, req, raw=True, compression='snappy')
resp_data = parse_response(resp)
logs = []
return_code = 0

if resp_data is not None:
resp_status = Request.decode_bytes(bytes.fromhex(resp_data))
fork_digest = enr_to_fork_digest(args['enr'])
req_data = Status(fork_digest=fork_digest).encode_bytes().hex()
req_call = rumor.rpc.status.req.raw(peer_id, req_data, raw=True, compression='snappy')
await req_call
await req_call.next()
resp = await req_call.next()

compare_containers(
Request(
version=resp_status.version,
finalized_root='0x0000000000000000000000000000000000000000000000000000000000000000',
head_root='0xef64a1b94652cd9070baa4f9c0e8b1ce624bdb071b77b51b1a54b8babb1a5cd2',
),
resp_status
)
(resp_data, l) = parse_chunk_response(resp)
logs.extend(l)
if resp_data is None:
return_code = 1

nursery.cancel_scope.cancel()
else:
resp_status = Status.decode_bytes(bytes.fromhex(resp_data))

if __name__ == '__main__':
args = parse_args('--enr', '--beacon_state_path')
trio.run(test_status, args.enr, args.beacon_state_path)
# TODO: make these dependent on beacon_state
expected_status = Status(
fork_digest=fork_digest,
finalized_root='0x0000000000000000000000000000000000000000000000000000000000000000',
finalized_epoch=0,
head_root='0x2227ce1b4e15c6320d493998fab783190a50e71d1075af73ce4e9ccc0dc84bca',
head_slot=0
)

if resp_status != expected_status:
logs.append(f'response error: expected {expected_status}, got {resp_status}')
return_code = 1

return (return_code, logs)
7 changes: 7 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ def parse_chunk_response(resp):
return (None, [f"request error: 'data' field not in response"])

return (resp['data'], [])

def enr_to_fork_digest(enr):
eth2 = enr.eth2
eth2 = eth2[2:] if eth2.startswith('0x') else eth2

fork_digest_hex = eth2[:8]
return bytes.fromhex(fork_digest_hex[:8])

0 comments on commit 058c342

Please sign in to comment.