diff --git a/client.py b/client.py index ea3ee91..562350c 100644 --- a/client.py +++ b/client.py @@ -14,4 +14,4 @@ def send_message(host, port, message): if msg == "q": break - send_message("localhost", node, {"type": "request", "content": msg}) + send_message("localhost", node, {"type": "client_request", "content": msg}) diff --git a/node.py b/node.py index 28f35ce..d33333d 100644 --- a/node.py +++ b/node.py @@ -179,17 +179,6 @@ def getKeys(self, private_key_path, public_key_path): print("client sending") message_from_node1 = {"type": "client_request", "content": "hello"} + node1.send_message(peer_id="N2", message=message_from_node1) node2.send_message(peer_id="N0", message=message_from_node1) - - time.sleep(5) - message_from_node1 = {"type": "client_request", "content": "testing"} - node1.send_message(peer_id="N1", message=message_from_node1) - - time.sleep(5) - message_from_node1 = {"type": "client_request", "content": "tanguy"} - node2.send_message(peer_id="N0", message=message_from_node1) - - time.sleep(5) - print(node1.consensus_protocol.log) - print(node2.consensus_protocol.log) - print(node3.consensus_protocol.log) \ No newline at end of file + node3.send_message(peer_id="N1", message=message_from_node1) diff --git a/node1.py b/node1.py index 6b692dc..32bcdcc 100644 --- a/node1.py +++ b/node1.py @@ -7,7 +7,7 @@ if __name__ == "__main__": port_node = 5010 - node = Node(node_id="N0", host="localhost", port=port_node, consensus_protocol="pbft") + node = Node(node_id="N0", host="localhost", port=port_node, consensus_protocol="raft") while True: msg = input("Press yes if you are ready to add nodes: ") diff --git a/node2.py b/node2.py index 2e098a7..94676b5 100644 --- a/node2.py +++ b/node2.py @@ -7,7 +7,7 @@ if __name__ == "__main__": port_node = 5011 - node = Node(node_id="N1", host="localhost", port=port_node, consensus_protocol="pbft") + node = Node(node_id="N1", host="localhost", port=port_node, consensus_protocol="raft") while True: msg = input("Press yes if you are ready to add nodes: ") diff --git a/node3.py b/node3.py index 8528edb..756e5e8 100644 --- a/node3.py +++ b/node3.py @@ -7,7 +7,7 @@ if __name__ == "__main__": port_node = 5012 - node = Node(node_id="N2", host="localhost", port=port_node, consensus_protocol="pbft") + node = Node(node_id="N2", host="localhost", port=port_node, consensus_protocol="raft") while True: msg = input("Press yes if you are ready to add nodes: ") diff --git a/protocols/pbft_protocol.py b/protocols/pbft_protocol.py index 6a1d594..6be72eb 100644 --- a/protocols/pbft_protocol.py +++ b/protocols/pbft_protocol.py @@ -17,7 +17,7 @@ def __init__(self, node, blockchain): def handle_message(self, message): message_type = message.get("type") - if message_type == "request": + if message_type == "client_request": self.request(message["content"]) return diff --git a/protocols/raft_protocol.py b/protocols/raft_protocol.py index 1de870c..a0812d9 100644 --- a/protocols/raft_protocol.py +++ b/protocols/raft_protocol.py @@ -79,7 +79,13 @@ def handle_append_entries(self, message): # Process the log entries from the leader if entries: - self.log.extend(entries) + block = Block( + index=entries[-1]["index"], + timestamp=entries[-1]["timestamp"], + data=entries[-1]["data"], + previous_hash=entries[-1]["previous_hash"] + ) + self.blockchain.add_block(block) # Send a response to the leader response = { @@ -95,10 +101,6 @@ def handle_client_request(self, message): if self.state == "leader": # If this node is the leader, process the client request self.process_client_request(message) - else: - if self.leader_id is not None: - print("sent message") - self.node.send_message(self.leader_id, {"type": "client_request", "message": message}) def handle_vote_response(self, message): vote_granted = message.get("vote_granted") @@ -122,14 +124,6 @@ def handle_vote_response(self, message): print(f"Node {self.node_id} has received a majority of votes. Becoming leader.") self.state = "leader" - def request_vote(self, candidate_id, candidate_term): - # Implement RequestVote RPC - pass - - def append_entries(self, leader_id, leader_term, entries): - # Implement AppendEntries RPC - pass - def start_election(self): logging.info("Node %s is starting an election for term %s", self.node_id, self.current_term + 1) @@ -150,27 +144,21 @@ def start_election(self): self.node.broadcast_message(request_vote_message) def process_client_request(self, client_request): - # Assuming client_request is a dictionary representing the client's request - entry = { - "term": self.current_term, - "command": client_request.get("command"), - # Add any other relevant information from the client request - } + previous_blocks = self.blockchain.blocks block = Block( - index=message["index"], - timestamp=message["timestamp"], - data=message["data"], - previous_hash=message["previous_hash"] + index= self.current_term, + timestamp= "1111", + data= client_request.get("content"), + previous_hash= previous_blocks[-1].current_hash ) self.blockchain.add_block(block) - # Replicate the log entry to other nodes append_entries_message = { "type": "append_entries", "leader_id": self.node_id, "leader_term": self.current_term, - "entries": [entry], # Include the new log entry + "entries": [block.to_dict()], # Include the new log entry } # Broadcast the append_entries_message to other nodes diff --git a/test.py b/test.py index dd8bc2a..e3f4085 100644 --- a/test.py +++ b/test.py @@ -10,9 +10,9 @@ port_node_2 = 5011 port_node_3 = 5012 - node1 = Node(node_id="N0", host="localhost", port=port_node_1, consensus_protocol="pbft") - node2 = Node(node_id="N1", host="localhost", port=port_node_2, consensus_protocol="pbft") - node3 = Node(node_id="N2", host="localhost", port=port_node_3, consensus_protocol="pbft") + node1 = Node(node_id="N0", host="localhost", port=port_node_1, consensus_protocol="raft") + node2 = Node(node_id="N1", host="localhost", port=port_node_2, consensus_protocol="raft") + node3 = Node(node_id="N2", host="localhost", port=port_node_3, consensus_protocol="raft") node1.add_peer(peer_id="N1", peer_address=("localhost", port_node_2)) node1.add_peer(peer_id="N2", peer_address=("localhost", port_node_3)) @@ -30,8 +30,10 @@ while True: msg = input() if msg != "q": - message_from_node1 = {"type": "request", "content": msg} + message_from_node1 = {"type": "client_request", "content": msg} + node1.send_message(peer_id="N2", message=message_from_node1) node2.send_message(peer_id="N0", message=message_from_node1) + node3.send_message(peer_id="N1", message=message_from_node1) else: break