def test_mapping_interface(identity_scheme_registry): kv_pairs = { b"id": b"mock", b"key1": b"value1", b"key2": b"value2", } enr = ENR( signature=b"", sequence_number=0, kv_pairs=kv_pairs, identity_scheme_registry=identity_scheme_registry, ) for key, value in kv_pairs.items(): assert key in enr assert enr[key] == value assert enr.get(key) == value not_a_key = b"key3" assert not_a_key not in kv_pairs assert not_a_key not in enr enr.get(not_a_key) is None assert enr.get(not_a_key, b"default") == b"default" assert tuple(enr.keys()) == tuple(kv_pairs.keys()) assert tuple(enr.values()) == tuple(kv_pairs.values()) assert tuple(enr.items()) == tuple(kv_pairs.items()) assert len(enr) == len(kv_pairs) assert tuple(iter(enr)) == tuple(iter(kv_pairs))
def _init(self, enr: ENR) -> None: try: ip = enr[IP_V4_ADDRESS_ENR_KEY] udp_port = enr[UDP_PORT_ENR_KEY] except KeyError: self._address = None else: tcp_port = enr.get(TCP_PORT_ENR_KEY, udp_port) self._address = Address(ip, udp_port, tcp_port) # FIXME: ENRs may use different pubkey formats and this would break, so instead of storing # a PublicKey with a certain format here we should simply use the APIs in the # ENR.identity_scheme for the crypto related operations. self._pubkey = keys.PublicKey.from_compressed_bytes(enr.public_key) self._id = NodeID(keccak(self.pubkey.to_bytes())) self._id_int = big_endian_to_int(self.id) self._enr = enr
def extract_forkid(enr: ENR) -> ForkID: eth_cap = enr.get(b'eth', None) if eth_cap is not None: [forkid] = rlp.sedes.List([ForkID]).deserialize(eth_cap) return forkid return None