Skip to content

Commit

Permalink
v0.1 raft protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanguyvans committed Dec 15, 2023
1 parent 5f038c8 commit d5aac6e
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 47 deletions.
2 changes: 1 addition & 1 deletion client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
15 changes: 2 additions & 13 deletions node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
node3.send_message(peer_id="N1", message=message_from_node1)
2 changes: 1 addition & 1 deletion node1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: ")
Expand Down
2 changes: 1 addition & 1 deletion node2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: ")
Expand Down
2 changes: 1 addition & 1 deletion node3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: ")
Expand Down
2 changes: 1 addition & 1 deletion protocols/pbft_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
38 changes: 13 additions & 25 deletions protocols/raft_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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")
Expand All @@ -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)

Expand All @@ -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
Expand Down
10 changes: 6 additions & 4 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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

Expand Down

0 comments on commit d5aac6e

Please sign in to comment.