async def do_tracker_peer_query_test(tracker_params, good_remotes, bad_remotes, blacklist_records=(), connected_remotes=None): tracker = MemoryEth1PeerTracker(**tracker_params) for remote, is_outbound, meta_params in good_remotes: tracker.track_peer_connection(remote, is_outbound, None, *meta_params) for remote, is_outbound, meta_params in bad_remotes: tracker.track_peer_connection(remote, is_outbound, None, *meta_params) # use the same in-memory database blacklist_tracker = SQLiteConnectionTracker(tracker.session) for remote, delta_seconds in blacklist_records: expires_at = datetime.datetime.utcnow() + datetime.timedelta( seconds=delta_seconds) blacklist_tracker._create_record(remote, expires_at, 'test') should_skip = functools.partial(skip_candidate_if_on_list, connected_remotes or set()) candidates = tuple(await tracker.get_peer_candidates(max_candidates=10, should_skip_fn=should_skip)) just_good_remotes = tuple(r[0] for r in good_remotes) just_bad_remotes = tuple(r[0] for r in bad_remotes) assert len(candidates) == len(just_good_remotes) for remote in just_good_remotes: assert remote in candidates for remote in just_bad_remotes: assert remote not in candidates
def test_track_peer_connection_tracks_last_connected(remote, caplog): tracker = MemoryEth1PeerTracker() now = datetime.datetime.utcnow() tracker.track_peer_connection(remote, True, now, *SIMPLE_META) node = tracker._get_remote(remote.id) assert node.last_connected_at == now
def _get_eth1_tracker(cls, boot_info: BootInfo) -> BaseEth1PeerTracker: if not boot_info.args.enable_experimental_eth1_peer_tracking: return NoopEth1PeerTracker() backend = boot_info.args.network_tracking_backend if backend is TrackingBackend.SQLITE3: session = cls._get_database_session(boot_info) # TODO: correctly determine protocols and versions protocols = ('eth', ) protocol_versions = (63, ) # TODO: get genesis_hash return SQLiteEth1PeerTracker( session, network_id=boot_info.trinity_config.network_id, protocols=protocols, protocol_versions=protocol_versions, ) elif backend is TrackingBackend.MEMORY: return MemoryEth1PeerTracker() elif backend is TrackingBackend.DO_NOT_TRACK: return NoopEth1PeerTracker() else: raise Exception(f"INVARIANT: {backend}")
def test_track_peer_connection_metadata(remote, caplog): tracker = MemoryEth1PeerTracker() tracker.track_peer_connection(remote, *TRACK_ARGS) node = tracker._get_remote(remote.id) assert node.genesis_hash == ZERO_HASH_HEX assert node.protocol == 'eth' assert node.protocol_version == 61 assert node.network_id == 1
def test_track_peer_connection_persists(remote, is_outbound): tracker = MemoryEth1PeerTracker() assert not tracker._remote_exists(remote.uri()) tracker.track_peer_connection(remote, is_outbound, *TRACK_ARGS[1:]) assert tracker._remote_exists(remote.uri()) node = tracker._get_remote(remote.uri()) assert node.uri == remote.uri() assert node.is_outbound is is_outbound
def test_track_peer_connection_persists(remote, is_outbound): tracker = MemoryEth1PeerTracker() assert not tracker._remote_exists(remote.id) tracker.track_peer_connection(remote, is_outbound, *TRACK_ARGS[1:]) assert tracker._remote_exists(remote.id) record = tracker._get_remote(remote.id) assert record.node_id == to_hex(remote.id) assert record.enr == repr(remote.enr) assert record.is_outbound is is_outbound
def test_track_peer_connection_updates_last_connected(remote, caplog): tracker = MemoryEth1PeerTracker() now = datetime.datetime.utcnow() tracker.track_peer_connection(remote, True, now, *SIMPLE_META) node = tracker._get_remote(remote.id) assert node.last_connected_at == now later = now + datetime.timedelta(seconds=300) tracker.track_peer_connection(remote, True, later, *SIMPLE_META) updated_node = tracker._get_remote(remote.id) assert updated_node.last_connected_at == later
def test_track_peer_connection_does_not_clobber_existing_record(remote, is_outbound): tracker = MemoryEth1PeerTracker() assert not tracker._remote_exists(remote.uri()) tracker.track_peer_connection(remote, is_outbound, *TRACK_ARGS[1:]) original = tracker._get_remote(remote.uri()) assert original.is_outbound is is_outbound tracker.track_peer_connection(remote, not is_outbound, *TRACK_ARGS[1:]) updated = tracker._get_remote(remote.uri()) assert updated.is_outbound is is_outbound
def test_track_peer_connection_metadata_updates(remote, caplog): tracker = MemoryEth1PeerTracker() tracker.track_peer_connection(remote, *TRACK_ARGS) node = tracker._get_remote(remote.uri()) assert node.genesis_hash == ZERO_HASH_HEX assert node.protocol == 'eth' assert node.protocol_version == 61 assert node.network_id == 1 tracker.track_peer_connection(remote, True, None, ZERO_ONE_HASH, 'les', 60, 2) updated_node = tracker._get_remote(remote.uri()) assert updated_node.genesis_hash == ZERO_ONE_HASH_HEX assert updated_node.protocol == 'les' assert updated_node.protocol_version == 60 assert updated_node.network_id == 2