42 lines
893 B
Rust
42 lines
893 B
Rust
use serde::{Serialize, Deserialize};
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
|
|
pub enum NodeState {
|
|
Follower,
|
|
Candidate,
|
|
Leader,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
pub struct LogEntry {
|
|
pub term: u64,
|
|
pub index: u64,
|
|
pub command: Vec<u8>,
|
|
}
|
|
|
|
pub struct RaftState {
|
|
pub node_id: String,
|
|
pub current_term: u64,
|
|
pub voted_for: Option<String>,
|
|
pub vote_count: u64,
|
|
pub current_state: NodeState,
|
|
pub log: Vec<LogEntry>,
|
|
pub commit_index: u64,
|
|
pub last_applied: u64,
|
|
}
|
|
|
|
impl RaftState {
|
|
pub fn new(node_id: String) -> Self {
|
|
Self {
|
|
node_id,
|
|
current_term: 0,
|
|
voted_for: None,
|
|
vote_count: 0,
|
|
current_state: NodeState::Follower,
|
|
log: Vec::new(),
|
|
commit_index: 0,
|
|
last_applied: 0,
|
|
}
|
|
}
|
|
}
|