def node_message(self, node, data):
        from node.unl import get_unl_nodes, get_as_node_type

        if str(data) == "sendmefullledger":
            self.send_full_chain(node)
        print("Data Type: " + str(type(data)) + "\n")

        if str(data) == "sendmefullnodelist":
            self.send_full_node_list(node)
        print("Data Type: " + str(type(data)) + "\n")

        try:
            from node.unl import node_is_unl
            if data["fullledger"] == 1 and node_is_unl(
                    node.id) and Ecdsa.verify(
                        "fullledger" + data["byte"],
                        Signature.fromBase64(data["signature"]),
                        PublicKey.fromPem(node.id)):
                print("getting chain")
                self.get_full_chain(data["byte"])
        except:
            pass

        try:
            from node.unl import node_is_unl
            if data["fullnodelist"] == 1 and node_is_unl(
                    node.id) and Ecdsa.verify(
                        "fullnodelist" + data["byte"],
                        Signature.fromBase64(data["signature"]),
                        PublicKey.fromPem(node.id)):
                print("getting node list")
                self.get_full_node_list(data["byte"])
        except:
            pass

        try:
            if data["transactionrequest"] == 1:
                self.get_transaction(data, node)
        except Exception as e:
            print(e)
            pass
        try:
            if data["transactionresponse"] == 1:
                self.get_transaction_response(data, node)
        except Exception as e:
            print(e)
            pass

        print("node_message from " + node.id + ": " + str(data))
Example #2
0
    def run(self):

        while not self.terminate_flag.is_set():
            try:
                connection, client_address = self.sock.accept()

                connected_node_id = connection.recv(4096).decode('utf-8')
                connection.send(self.id.encode('utf-8'))
                if node_is_unl(connected_node_id):
                    thread_client = self.create_the_new_connection(
                        connection, connected_node_id, client_address[0],
                        client_address[1])
                    thread_client.start()

                    self.nodes_inbound.append(thread_client)

                    self.inbound_node_connected(thread_client)
                else:
                    dprint(
                        "Node System: Could not connect with node because node is not unl node."
                    )

            except socket.timeout:
                pass

            except Exception as e:
                raise e

            time.sleep(0.01)

        print("Node System: Node stopping...")
        for t in self.nodes_inbound:
            t.stop()

        for t in self.nodes_outbound:
            t.stop()

        time.sleep(1)

        for t in self.nodes_inbound:
            t.join()

        for t in self.nodes_outbound:
            t.join()

        self.sock.settimeout(None)
        self.sock.close()
        print("Node System: Node stopped")
Example #3
0
    def get_candidate_block_hash(self, data, node):

        dprint("Getting the candidate block hash")

        from node.unl import node_is_unl
        if node_is_unl(node.id) and GetBlock(
        ).sequance_number == data["sequance_number"]:
            dprint("is unl")

            if Ecdsa.verify(
                    "myblockhash" + data["hash"] +
                    str(data["sequance_number"]),
                    Signature.fromBase64(data["signature"]),
                    PublicKey.fromPem(node.id)):
                dprint("ecdsa true")
                data["sender"] = node.id

                node.candidate_block_hash = data
Example #4
0
    def connect_to_node(self, host, port):

        if host == self.host and port == self.port:
            print(
                "Node System: connect_to_node: Cannot connect with yourself!!")
            return False

        # Check if node is already connected with this node!
        for node in self.nodes_outbound:
            if node.host == host and node.port == port:
                print(
                    "Node System: connect_to_node: Already connected with this node."
                )
                return True

        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            dprint("Node System: connecting to %s port %s" % (host, port))
            sock.connect((host, port))

            # Basic information exchange (not secure) of the id's of the nodes!
            sock.send(
                self.id.encode('utf-8'))  # Send my id to the connected node!
            connected_node_id = sock.recv(4096).decode(
                'utf-8')  # When a node is connected, it sends it id!

            if node_is_unl(connected_node_id):
                thread_client = self.create_the_new_connection(
                    sock, connected_node_id, host, port)
                thread_client.start()

                self.nodes_outbound.append(thread_client)
                self.outbound_node_connected(thread_client)
            else:
                dprint(
                    "Node System: Could not connect with node because node is not unl node."
                )
        except Exception as e:
            dprint(
                "Node System: TcpServer.connect_to_node: Could not connect with node. ("
                + str(e) + ")")
Example #5
0
    def get_candidate_block(self, data, node):

        dprint("Getting the candidate block")
        from node.unl import node_is_unl
        if node_is_unl(node.id) and GetBlock(
        ).sequance_number == data["sequance_number"]:
            dprint("is unl")

            signature_list = []
            for element in data["transaction"]:
                signature_list.append(element["signature"])

            dprint("signature_list: " + str(signature_list))

            merkle_root_of_signature_list = MerkleTree(
                signature_list).getRootHash(
                ) if len(signature_list) != 0 else "0"

            dprint("signatureverify: " + str(
                Ecdsa.verify("myblock" + merkle_root_of_signature_list,
                             Signature.fromBase64(data["signature"]),
                             PublicKey.fromPem(node.id))))
            dprint("publickey from pem: " + str(node.id))

            dprint("merkleroot: " + merkle_root_of_signature_list)

            if Ecdsa.verify(
                    "myblock" + merkle_root_of_signature_list +
                    str(data["sequance_number"]),
                    Signature.fromBase64(data["signature"]),
                    PublicKey.fromPem(node.id)):
                dprint("ecdsa true")

                temp_tx = []

                for element in data["transaction"]:
                    temp_tx.append(Transaction.load_json(element))

                data["transaction"] = temp_tx

                node.candidate_block = data
    def get_transaction_response(self, data, node):
        #burada bu mesaj gönderen adamın bizim istediğimiz node ve pub key olup olmadığına bakacağız. ayrıca eğer unl listemizdeki bir adamdan evet oyu aldıysak o oyu hayıra çeviremeyiz
        from ledger.ledger_main import get_ledger
        dprint("Getting the transactions response")
        system = get_ledger()
        from node.unl import node_is_unl
        if node_is_unl(node.id):
            for tx in system.validating_list:

                if node.id == data["fromUser"] and Ecdsa.verify(
                        data["response"] + str(data["transaction_signature"]),
                        Signature.fromBase64(data["signature"]),
                        PublicKey.fromPem(data["fromUser"])):

                    if data["transaction_signature"] == tx.signature:
                        if data["response"] == "TRUE":
                            tx.valid.append({"data": data, "node": node.id})
                            system.Verificate_Pending_Trans()
                        elif data["response"] == "FALSE":
                            tx.invalid.append({"data": data, "node": node.id})
                            system.Verificate_Pending_Trans()
Example #7
0
    def message_from_node(self, node, data):
        from node.unl import get_unl_nodes, get_as_node_type

        if str(data) == "sendmefullblock":
            self.send_full_chain(node)
        print("Data Type: " + str(type(data)) + "\n")

        if str(data) == "sendmefullnodelist":
            self.send_full_node_list(node)
        print("Data Type: " + str(type(data)) + "\n")

        try:
            from node.unl import node_is_unl
            if data["fullblock"] == 1 and node_is_unl(
                    node.id) and Ecdsa.verify(
                        "fullblock" + data["byte"],
                        Signature.fromBase64(data["signature"]),
                        PublicKey.fromPem(node.id)):
                print("getting chain")
                self.get_full_chain(data, node)
        except Exception as e:
            print(e)

        try:
            from node.unl import node_is_unl
            if data["fullaccounts"] == 1 and node_is_unl(
                    node.id) and Ecdsa.verify(
                        "fullaccounts" + data["byte"],
                        Signature.fromBase64(data["signature"]),
                        PublicKey.fromPem(node.id)):
                print("getting chain")
                self.get_full_accounts(data, node)
        except Exception as e:
            print(e)

        try:
            from node.unl import node_is_unl
            if data["fullblockshash"] == 1 and node_is_unl(
                    node.id) and Ecdsa.verify(
                        "fullblockshash" + data["byte"],
                        Signature.fromBase64(data["signature"]),
                        PublicKey.fromPem(node.id)):
                self.get_full_blockshash(data, node)
        except Exception as e:
            print(e)

        try:
            from node.unl import node_is_unl
            if data["fullnodelist"] == 1 and node_is_unl(
                    node.id) and Ecdsa.verify(
                        "fullnodelist" + data["byte"],
                        Signature.fromBase64(data["signature"]),
                        PublicKey.fromPem(node.id)):
                print("getting node list")
                self.get_full_node_list(data["byte"])
        except Exception as e:
            print(e)

        try:
            if data["transactionrequest"] == 1:
                self.get_transaction(data, node)
        except Exception as e:
            print(e)

        try:
            if data["action"] == "myblock":
                self.get_candidate_block(data, node)
        except Exception as e:
            print(e)

        try:
            if data["action"] == "myblockhash":
                self.get_candidate_block_hash(data, node)
        except Exception as e:
            print(e)

        print("message_from_node from " + node.id + ": " + str(data))