def rpc_store(self, sender, nodeid, key, value): source = kademlia.node.Node(nodeid, sender[0], sender[1]) self.welcomeIfNewNode(source) self.log.debug("got a store request from %s" % str(sender)) header, payload = default_constant_splitter(value, return_remainder=True) if header == constants.BYTESTRING_IS_URSULA_IFACE_INFO: from nucypher.characters import Ursula stranger_ursula = Ursula.from_bytes(payload, federated_only=self.sourceNode.federated_only) # TODO: Is federated_only the right thing here? if stranger_ursula.interface_is_valid() and key == digest(stranger_ursula.canonical_public_address): self.sourceNode._node_storage[stranger_ursula.checksum_public_address] = stranger_ursula # TODO: 340 return True else: self.log.warning("Got request to store invalid node: {} / {}".format(key, value)) self.illegal_keys_seen.append(key) return False elif header == constants.BYTESTRING_IS_TREASURE_MAP: from nucypher.policy.models import TreasureMap try: treasure_map = TreasureMap.from_bytes(payload) self.log.info("Storing TreasureMap: {} / {}".format(key, value)) self.sourceNode._treasure_maps[treasure_map.public_id()] = value return True except TreasureMap.InvalidSignature: self.log.warning("Got request to store invalid TreasureMap: {} / {}".format(key, value)) self.illegal_keys_seen.append(key) return False else: self.log.info( "Got request to store bad k/v: {} / {}".format(key, value)) return False
def read_node_metadata(filepath: str, federated_only=False) -> Ursula: """Init one ursula from node storage file""" with open(filepath, "r") as seed_file: seed_file.seek(0) node_bytes = binascii.unhexlify(seed_file.read()) node = Ursula.from_bytes(node_bytes, federated_only=federated_only) return node
def get_seed(): teacher_dht_port = 3500 teacher_rest_port = int(teacher_dht_port) + 100 with open("examples-runtime-cruft/node-metadata-{}".format(teacher_rest_port), "r") as f: f.seek(0) teacher_bytes = binascii.unhexlify(f.read()) URSULA = Ursula.from_bytes(teacher_bytes, federated_only=True) print("Will learn from {}".format(URSULA)) return URSULA
def __init__(self, teacher_dht_port: int = 3500, teacher_rest_port: int = 3600, node_meta_dir: str = "../examples/examples-runtime-cruft"): self.teacher_dht_port = teacher_dht_port if teacher_rest_port: self.teacher_rest_port = teacher_rest_port else: self.teacher_rest_port = int(self.teacher_dht_port) + 100 with open( "{}/node-metadata-{}".format(node_meta_dir, self.teacher_rest_port), "r") as f: f.seek(0) teacher_bytes = binascii.unhexlify(f.read()) self.ursula = Ursula.from_bytes(teacher_bytes, federated_only=True)
def check_node_with_cert(node, cert_file): response = requests.get("https://{}/public_information".format( node.rest_url()), verify=cert_file) ursula = Ursula.from_bytes(response.content, federated_only=True) assert ursula == node
root.setLevel(logging.DEBUG) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.INFO) formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch.setFormatter(formatter) root.addHandler(ch) teacher_dht_port = sys.argv[2] teacher_rest_port = int(teacher_dht_port) + 100 with open("examples-runtime-cruft/node-metadata-{}".format(teacher_rest_port), "r") as f: f.seek(0) teacher_bytes = binascii.unhexlify(f.read()) URSULA = Ursula.from_bytes(teacher_bytes, federated_only=True) print("Will learn from {}".format(URSULA)) # network_middleware = SandboxRestMiddleware([URSULA]) ######### # Alice # ######### ALICE = Alice( network_middleware=RestMiddleware(), known_nodes=(URSULA, ), # in lieu of seed nodes federated_only=True, always_be_learning=True) # TODO: 289 # Here are our Policy details.