def test_fromraw_toraw(self): peer = Peer.from_signed_raw(rawpeer) rendered_peer = peer.signed_raw() from_rendered_peer = Peer.from_signed_raw(rendered_peer) self.assertEqual(from_rendered_peer.currency, "beta_brousouf") self.assertEqual(from_rendered_peer.pubkey, "HsLShAtzXTVxeUtQd7yi5Z5Zh4zNvbu8sTEZ53nfKcqY") self.assertEqual(str(from_rendered_peer.blockUID), "8-1922C324ABC4AF7EF7656734A31F5197888DDD52") self.assertEqual(len(from_rendered_peer.endpoints), 3) self.assertTrue(type(from_rendered_peer.endpoints[0]) is BMAEndpoint) self.assertTrue(type(from_rendered_peer.endpoints[1]) is BMAEndpoint) self.assertTrue(type(from_rendered_peer.endpoints[2]) is UnknownEndpoint) self.assertEqual(from_rendered_peer.endpoints[0].server, "some.dns.name") self.assertEqual(from_rendered_peer.endpoints[0].ipv4, "88.77.66.55") self.assertEqual(from_rendered_peer.endpoints[0].ipv6, "2001:0db8:0000:85a3:0000:0000:ac1f") self.assertEqual(from_rendered_peer.endpoints[0].port, 9001) self.assertEqual(from_rendered_peer.endpoints[1].server, "some.dns.name") self.assertEqual(from_rendered_peer.endpoints[1].ipv4, "88.77.66.55") self.assertEqual(from_rendered_peer.endpoints[1].ipv6, "2001:0db8:0000:85a3:0000:0000:ac1f") self.assertEqual(from_rendered_peer.endpoints[1].port, 9002) self.assertEqual(from_rendered_peer.signatures[0], "dkaXIiCYUJtCg8Feh/BKvPYf4uFH9CJ/zY6J4MlA9BsjmcMe4YAblvNt/gJy31b1aGq3ue3h14mLMCu84rraDg==") self.assertEqual(rawpeer, from_rendered_peer.signed_raw())
def test_fromraw(self): peer = Peer.from_signed_raw(rawpeer) self.assertEqual(peer.currency, "beta_brousouf") self.assertEqual(peer.pubkey, "HsLShAtzXTVxeUtQd7yi5Z5Zh4zNvbu8sTEZ53nfKcqY") self.assertEqual(str(peer.blockUID), "8-1922C324ABC4AF7EF7656734A31F5197888DDD52") self.assertEqual(len(peer.endpoints), 4) self.assertIsInstance(peer.endpoints[0], BMAEndpoint) self.assertIsInstance(peer.endpoints[1], BMAEndpoint) self.assertIsInstance(peer.endpoints[2], WS2PEndpoint) self.assertIsInstance(peer.endpoints[3], UnknownEndpoint) self.assertEqual(peer.endpoints[0].server, "some.dns.name") self.assertEqual(peer.endpoints[0].ipv4, "88.77.66.55") self.assertEqual(peer.endpoints[0].ipv6, "2001:42d0:52:a00::648") self.assertEqual(peer.endpoints[0].port, 9001) self.assertEqual(peer.endpoints[1].server, "some.dns.name") self.assertEqual(peer.endpoints[1].ipv4, "88.77.66.55") self.assertEqual(peer.endpoints[1].ipv6, "2001:42d0:52:a00::648") self.assertEqual(peer.endpoints[1].port, 9002) self.assertEqual(peer.endpoints[2].server, "g1-test.duniter.org") self.assertEqual(peer.endpoints[2].ws2pid, "d2edcb92") self.assertEqual(peer.endpoints[2].port, 20902) self.assertEqual( peer.signatures[0], "dkaXIiCYUJtCg8Feh/BKvPYf4uFH9CJ/zY6J4MlA9BsjmcMe4YAblvNt/gJy31b1aGq3ue3h14mLMCu84rraDg==" )
async def from_address(cls, currency, address, port, session): """ Factory method to get a node from a given address :param str currency: The node currency. None if we don't know\ the currency it should have, for example if its the first one we add :param str address: The node address :param int port: The node port :return: A new node :rtype: sakia.core.net.Node """ peer_data = await bma.network.Peering(ConnectionHandler(address, port) ).get(session) peer = Peer.from_signed_raw("{0}{1}\n".format(peer_data['raw'], peer_data['signature'])) if currency is not None: if peer.currency != currency: raise InvalidNodeCurrency(peer.currency, currency) node = cls(peer, "", peer.pubkey, None, Node.ONLINE, time.time(), { 'root': "", 'leaves': [] }, "", "", 0, session) logging.debug("Node from address : {:}".format(str(node))) return node
def setUp(self): self.setUpQuamash() QLocale.setDefault(QLocale("en_GB")) self.identities_registry = IdentitiesRegistry() self.application = Application(self.qapplication, self.lp, self.identities_registry) self.application.preferences['notifications'] = False self.peer = Peer.from_signed_raw("""Version: 2 Type: Peer Currency: meta_brouzouf PublicKey: 8Fi1VSTbjkXguwThF4v2ZxC5whK7pwG2vcGTkPUPjPGU Block: 48698-000005E0F228038E4DDD4F6CA4ACB01EC88FBAF8 Endpoints: BASIC_MERKLED_API duniter.inso.ovh 80 82o1sNCh1bLpUXU6nacbK48HBcA9Eu2sPkL1/3c2GtDPxBUZd2U2sb7DxwJ54n6ce9G0Oy7nd1hCxN3fS0oADw== """) self.node = Node(self.peer, "", "HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk", None, Node.ONLINE, time.time(), {}, "duniter", "0.12.0", 0, Mock("aiohttp.ClientSession")) self.network = Network.create(self.node) self.bma_access = BmaAccess.create(self.network) self.community = Community("test_currency", self.network, self.bma_access)
async def from_address(cls, currency, secured, address, port, user_parameters): """ Factory method to get a node from a given address :param str currency: The node currency. None if we don't know\ the currency it should have, for example if its the first one we add :param bool secured: True if the node uses https :param str address: The node address :param int port: The node port :return: A new node :rtype: sakia.core.net.Node """ http_scheme = "https" if secured else "http" ws_scheme = "ws" if secured else "wss" session = aiohttp.ClientSession() peer_data = await bma.network.peering(ConnectionHandler(http_scheme, ws_scheme, address, port, "", proxy=user_parameters.proxy(), session=session)) peer = Peer.from_signed_raw("{0}{1}\n".format(peer_data['raw'], peer_data['signature'])) if currency and peer.currency != currency: raise InvalidNodeCurrency(currency, peer.currency) node = Node(peer.currency, peer.pubkey, peer.endpoints, peer.blockUID) logging.getLogger('sakia').debug("Node from address : {:}".format(str(node))) return cls(node, user_parameters, session=session)
async def from_address(cls, currency, address, port, session): """ Factory method to get a node from a given address :param str currency: The node currency. None if we don't know\ the currency it should have, for example if its the first one we add :param str address: The node address :param int port: The node port :return: A new node :rtype: sakia.core.net.Node """ peer_data = await bma.network.Peering(ConnectionHandler(address, port)).get(session) peer = Peer.from_signed_raw("{0}{1}\n".format(peer_data['raw'], peer_data['signature'])) if currency is not None: if peer.currency != currency: raise InvalidNodeCurrency(peer.currency, currency) node = cls(peer, "", peer.pubkey, None, Node.ONLINE, time.time(), {'root': "", 'leaves': []}, "", "", 0, session) logging.debug("Node from address : {:}".format(str(node))) return node
async def from_address(cls, currency, secured, address, port, user_parameters): """ Factory method to get a node from a given address :param str currency: The node currency. None if we don't know\ the currency it should have, for example if its the first one we add :param bool secured: True if the node uses https :param str address: The node address :param int port: The node port :return: A new node :rtype: sakia.core.net.Node """ http_scheme = "https" if secured else "http" ws_scheme = "ws" if secured else "wss" session = aiohttp.ClientSession() peer_data = await bma.network.peering(ConnectionHandler(http_scheme, ws_scheme, address, port, "", proxy=user_parameters.proxy(), session=session)) peer = Peer.from_signed_raw("{0}{1}\n".format(peer_data['raw'], peer_data['signature'])) if currency and peer.currency != currency: raise InvalidNodeCurrency(currency, peer.currency) node = Node(peer.currency, peer.pubkey, peer.endpoints, peer.blockUID, last_state_change=time.time()) logging.getLogger('sakia').debug("Node from address : {:}".format(str(node))) return cls(node, user_parameters, session=session)
async def refresh_informations(self): """ Refresh basic information (pubkey and currency) """ conn_handler = self.endpoint.conn_handler() try: peering_data = await bma.network.Peering(conn_handler).get( self._session) node_pubkey = peering_data["pubkey"] node_currency = peering_data["currency"] self.state = Node.ONLINE if peering_data['raw'] != self.peer.raw(): peer = Peer.from_signed_raw("{0}{1}\n".format( peering_data['raw'], peering_data['signature'])) if peer.blockUID.number > peer.blockUID.number: self.peer = Peer.from_signed_raw("{0}{1}\n".format( peering_data['raw'], peering_data['signature'])) if node_pubkey != self.pubkey: self._pubkey = node_pubkey self.identity_changed.emit() if node_currency != self.currency: self.state = Node.CORRUPTED logging.debug("Change : new state corrupted") self.changed.emit() except errors.DuniterError as e: if e.ucode == errors.PEER_NOT_FOUND: logging.debug("Error in peering reply : {0}".format(str(e))) self.state = Node.OFFLINE self.changed.emit() except (ClientError, gaierror, TimeoutError, DisconnectedError, ValueError) as e: logging.debug("{0} : {1}".format( type(e).__name__, self.pubkey[:5])) self.state = Node.OFFLINE except (MalformedDocumentError, jsonschema.ValidationError) as e: logging.debug(str(e)) logging.debug("Validation error : {0}".format(self.pubkey[:5])) self.state = Node.CORRUPTED
def refresh_peer_data(self, peer_data): if "raw" in peer_data: try: str_doc = "{0}{1}\n".format(peer_data['raw'], peer_data['signature']) peer_doc = Peer.from_signed_raw(str_doc) self.neighbour_found.emit(peer_doc) except MalformedDocumentError as e: self._logger.debug(str(e)) else: self._logger.debug("Incorrect leaf reply")
def refresh_peer_data(self, peer_data): if "raw" in peer_data: try: str_doc = "{0}{1}\n".format(peer_data['raw'], peer_data['signature']) peer_doc = Peer.from_signed_raw(str_doc) self.neighbour_found.emit(peer_doc) except MalformedDocumentError as e: logging.debug(str(e)) else: logging.debug("Incorrect leaf reply")
async def refresh_informations(self): """ Refresh basic information (pubkey and currency) """ conn_handler = self.endpoint.conn_handler() try: peering_data = await bma.network.Peering(conn_handler).get(self._session) node_pubkey = peering_data["pubkey"] node_currency = peering_data["currency"] self.state = Node.ONLINE if peering_data['raw'] != self.peer.raw(): peer = Peer.from_signed_raw("{0}{1}\n".format(peering_data['raw'], peering_data['signature'])) if peer.blockUID.number > peer.blockUID.number: self.peer = Peer.from_signed_raw("{0}{1}\n".format(peering_data['raw'], peering_data['signature'])) if node_pubkey != self.pubkey: self._pubkey = node_pubkey self.identity_changed.emit() if node_currency != self.currency: self.state = Node.CORRUPTED logging.debug("Change : new state corrupted") self.changed.emit() except errors.DuniterError as e: if e.ucode == errors.PEER_NOT_FOUND: logging.debug("Error in peering reply : {0}".format(str(e))) self.state = Node.OFFLINE self.changed.emit() except (ClientError, gaierror, TimeoutError, DisconnectedError, ValueError) as e: logging.debug("{0} : {1}".format(type(e).__name__, self.pubkey[:5])) self.state = Node.OFFLINE except (MalformedDocumentError, jsonschema.ValidationError) as e: logging.debug(str(e)) logging.debug("Validation error : {0}".format(self.pubkey[:5])) self.state = Node.CORRUPTED
def test_peer_signature(self): signed_raw = """Version: 2 Type: Peer Currency: test_net PublicKey: 8Fi1VSTbjkXguwThF4v2ZxC5whK7pwG2vcGTkPUPjPGU Block: 2086-00005D6FC6E22FB308D8815A565A01C66FFB7DC761D616DE0698F6322565F1D6 Endpoints: BASIC_MERKLED_API testnet.duniter.inso.ovh 80 4aQ/sfqFAFUeYkkLdC2OfgXqTBjCIcMptpR/GIlGqbe4aFVJcy9NEVAFx7sHiLuAb+VNnec3XHHC+xOk3MLzDA== """ peer = Peer.from_signed_raw(signed_raw) pubkey = "8Fi1VSTbjkXguwThF4v2ZxC5whK7pwG2vcGTkPUPjPGU" verifying_key = VerifyingKey(pubkey) self.assertTrue(verifying_key.verify_document(peer))
def test_peer_signature(self): signed_raw = """Version: 2 Type: Peer Currency: test_net PublicKey: 8Fi1VSTbjkXguwThF4v2ZxC5whK7pwG2vcGTkPUPjPGU Block: 2086-00005D6FC6E22FB308D8815A565A01C66FFB7DC761D616DE0698F6322565F1D6 Endpoints: BASIC_MERKLED_API testnet.duniter.inso.ovh 80 4aQ/sfqFAFUeYkkLdC2OfgXqTBjCIcMptpR/GIlGqbe4aFVJcy9NEVAFx7sHiLuAb+VNnec3XHHC+xOk3MLzDA== """"" peer = Peer.from_signed_raw(signed_raw) pubkey = "8Fi1VSTbjkXguwThF4v2ZxC5whK7pwG2vcGTkPUPjPGU" verifying_key = VerifyingKey(pubkey) self.assertTrue(verifying_key.verify_document(peer))
def setUp(self): self.setUpQuamash() QLocale.setDefault(QLocale("en_GB")) self.identities_registry = IdentitiesRegistry() self.application = Application(self.qapplication, self.lp, self.identities_registry) self.application.preferences['notifications'] = False self.peer = Peer.from_signed_raw("""Version: 2 Type: Peer Currency: meta_brouzouf PublicKey: 8Fi1VSTbjkXguwThF4v2ZxC5whK7pwG2vcGTkPUPjPGU Block: 48698-000005E0F228038E4DDD4F6CA4ACB01EC88FBAF8 Endpoints: BASIC_MERKLED_API duniter.inso.ovh 80 82o1sNCh1bLpUXU6nacbK48HBcA9Eu2sPkL1/3c2GtDPxBUZd2U2sb7DxwJ54n6ce9G0Oy7nd1hCxN3fS0oADw== """) self.node = Node(self.peer, "", "HnFcSms8jzwngtVomTTnzudZx7SHUQY8sVE1y8yBmULk", None, Node.ONLINE, time.time(), {}, "duniter", "0.12.0", 0, Mock("aiohttp.ClientSession")) self.network = Network.create(self.node) self.bma_access = BmaAccess.create(self.network) self.community = Community("test_currency", self.network, self.bma_access)
def test_incorrect(self): peer = Peer.from_signed_raw(test_weird_ipv6_peer) rendered_peer = peer.signed_raw() self.assertEqual(test_weird_ipv6_peer, rendered_peer) Peer.from_signed_raw(rendered_peer)
def from_json(cls, currency, data, file_version, session): """ Loads a node from json data :param str currency: the currency of the community :param dict data: the json data of the node :param NormalizedVersion file_version: the version of the file :return: A new node :rtype: Node """ endpoints = [] uid = "" pubkey = "" software = "" version = "" fork_window = 0 block = None last_change = time.time() state = Node.OFFLINE if 'uid' in data: uid = data['uid'] if 'pubkey' in data: pubkey = data['pubkey'] if 'last_change' in data: last_change = data['last_change'] if 'block' in data: block = data['block'] if 'state' in data: state = data['state'] if 'software' in data: software = data['software'] if 'version' in data: version = data['version'] if 'fork_window' in data: fork_window = data['fork_window'] if parse_version("0.11") <= file_version < parse_version("0.12dev1"): for endpoint_data in data['endpoints']: endpoints.append(Endpoint.from_inline(endpoint_data)) if currency in data: currency = data['currency'] peer = Peer(2, currency, pubkey, BlockUID(0, Block.Empty_Hash), endpoints, "SOMEFAKESIGNATURE") else: peer = Peer.from_signed_raw(data['peer']) node = cls(peer, uid, pubkey, block, state, last_change, { 'root': "", 'leaves': [] }, software, version, fork_window, session) logging.debug("Node from json : {:}".format(str(node))) return node
def test_incorrect(self): peer = Peer.from_signed_raw(test_weird_ipv6_peer) rendered_peer = peer.signed_raw() from_rendered_peer = Peer.from_signed_raw(rendered_peer)
def from_json(cls, currency, data, file_version, session): """ Loads a node from json data :param str currency: the currency of the community :param dict data: the json data of the node :param NormalizedVersion file_version: the version of the file :return: A new node :rtype: Node """ endpoints = [] uid = "" pubkey = "" software = "" version = "" fork_window = 0 block = None last_change = time.time() state = Node.OFFLINE if 'uid' in data: uid = data['uid'] if 'pubkey' in data: pubkey = data['pubkey'] if 'last_change' in data: last_change = data['last_change'] if 'block' in data: block = data['block'] if 'state' in data: state = data['state'] if 'software' in data: software = data['software'] if 'version' in data: version = data['version'] if 'fork_window' in data: fork_window = data['fork_window'] if parse_version("0.11") <= file_version < parse_version("0.12dev1") : for endpoint_data in data['endpoints']: endpoints.append(Endpoint.from_inline(endpoint_data)) if currency in data: currency = data['currency'] peer = Peer(2, currency, pubkey, BlockUID(0, Block.Empty_Hash), endpoints, "SOMEFAKESIGNATURE") else: peer = Peer.from_signed_raw(data['peer']) node = cls(peer, uid, pubkey, block, state, last_change, {'root': "", 'leaves': []}, software, version, fork_window, session) logging.debug("Node from json : {:}".format(str(node))) return node