TASK
Implementation
Implement the RequestVote RPC. Candidates request votes from other nodes. Nodes grant their vote if the candidate's term is current and they have not already voted in this term.
Sample Test Cases
Grant vote to same term candidateTimeout: 5000ms
Input
{"src":"c0","dest":"n1","body":{"type":"init","msg_id":1,"node_id":"n1","node_ids":["n1","n2","n3"]}}
{"src":"c0","dest":"n1","body":{"type":"set_term","msg_id":2,"term":2}}
{"src":"n2","dest":"n1","body":{"type":"request_vote","msg_id":3,"term":2,"candidate_id":"n2","last_log_index":0,"last_log_term":0}}
Expected Output
{"src":"n1","dest":"c0","body":{"type":"init_ok","in_reply_to":1,"msg_id":0}}
{"src":"n1","dest":"c0","body":{"type":"set_term_ok","in_reply_to":2,"msg_id":1}}
{"src":"n1","dest":"n2","body":{"type":"request_vote_ok","in_reply_to":3,"msg_id":2,"term":2,"vote_granted":true}}
Hints
Hint 1▾
Grant vote if candidate term >= current term
Hint 2▾
Only vote once per term
Hint 3▾
Update term if candidate has higher term
OVERVIEW
Theoretical Hub
Vote Granting
A node grants its vote if: the candidate term is at least as recent as its own, and it has not voted for another candidate in this term. This ensures at most one leader per term.
Key Concepts
votingterm comparisonvote granting
main.py
python
1
2
3
4
5
6
#!/usr/bin/env python3
import sys
import json
# TODO: Implement RequestVote RPC handler