Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
resetius committed Nov 28, 2023
1 parent 887637a commit 9cb5b96
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/raft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,26 +85,26 @@ std::unique_ptr<TResult> TRaft::OnRequestVote(TMessageHolder<TRequestVoteRequest
});
} else if (message->Term == State->CurrentTerm) {
bool accept = false;
if (State->VotedFor == 0) {
accept = true;
} else if (State->VotedFor == message->CandidateId && message->LastLogTerm > State->LogTerm()) {
accept = true;
} else if (State->VotedFor == message->CandidateId && message->LastLogTerm == State->LogTerm() && message->LastLogIndex >= State->Log.size()) {
accept = true;
if (State->VotedFor == 0 || State->VotedFor == message->CandidateId) {
if (message->LastLogTerm > State->LogTerm()) {
accept = true;
} else if (message->LastLogTerm == State->LogTerm() && message->LastLogIndex >= State->Log.size()) {
accept = true;
}
}

auto reply = NewHoldedMessage<TRequestVoteResponse>();
reply->Src = Id;
reply->Dst = message->Src;
reply->Term = message->Term;
reply->Term = State->CurrentTerm;
reply->VoteGranted = accept;

return std::make_unique<TResult>(TResult {
.NextState = std::make_unique<TState>(TState{
.CurrentTerm = message->Term,
.NextState = accept ? std::make_unique<TState>(TState{
.CurrentTerm = State->CurrentTerm,
.VotedFor = message->CandidateId,
.Log = State->Log
}),
}) : nullptr,
.Message = reply,
});
}
Expand Down

0 comments on commit 9cb5b96

Please sign in to comment.