Skip to content

Commit 2a042da

Browse files
tbagrel1agustinmista
authored andcommitted
Fixup voteDB
1 parent b250417 commit 2a042da

File tree

1 file changed

+34
-35
lines changed
  • ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Peras/Vote

1 file changed

+34
-35
lines changed

ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Peras/Vote/Aggregation.hs

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,9 @@ onErr :: Either e a -> (e -> e') -> Either e' a
211211
onErr (Left err) f = Left (f err)
212212
onErr (Right val) _ = Right val
213213

214-
data PerasVoteStateCandidateOrWinner blk
215-
= ACandidate (PerasTargetVoteState blk 'Candidate)
214+
data PerasVoteStateCandidateOrWinner blk =
215+
ACandidate (PerasTargetVoteState blk 'Candidate)
216216
| AWinner (PerasTargetVoteState blk 'Winner)
217-
218217
--
219218
-- May fail if the candidate is elected winner but forging the certificate fails.
220219
updateCandidateVoteState ::
@@ -229,7 +228,7 @@ updateCandidateVoteState cfg vote oldState = do
229228
let newVoteTally = updateTargetVoteTally vote (ptvsVoteTally oldState)
230229
voteList = forgetArrivalTime <$> Map.elems (ptvtVotes newVoteTally)
231230
in if voteTallyAboveQuorum cfg newVoteTally
232-
then do
231+
then do
233232
cert <- forgePerasCert cfg (ptvtTarget newVoteTally) voteList
234233
pure $ AWinner (PerasTargetVoteWinner newVoteTally 0 cert)
235234
else
@@ -333,47 +332,47 @@ updatePerasRoundVoteState vote cfg roundState =
333332
(getPerasVoteBlock vote)
334333
prvsCandidateStates
335334
in do
336-
candidateOrWinnerState <-
337-
updateCandidateVoteState cfg vote oldCandidateState
338-
`onErr` (\err -> RoundVoteStateForgingCertError err)
339-
case candidateOrWinnerState of
340-
ACandidate newCandidateState ->
341-
-- Quorum still not reached for this round
342-
let prvsCandidateStates' =
343-
Map.insert
344-
(getPerasVoteBlock vote)
345-
newCandidateState
346-
prvsCandidateStates
347-
in pure $
348-
PerasRoundVoteStateQuorumNotReached
349-
{ prvsRoundNo = prvsRoundNo roundState
350-
, prvsCandidateStates = prvsCandidateStates'
351-
}
352-
AWinner winnerState ->
353-
-- Quorum has been reached for the first time here for this round
354-
let winnerPoint = getPerasVoteBlock winnerState
355-
loserStates = candidateToLoser cfg <$> Map.delete winnerPoint prvsCandidateStates
356-
in pure $
357-
PerasRoundVoteStateQuorumReachedAlready
358-
{ prvsRoundNo = prvsRoundNo roundState
359-
, prvsLoserStates = loserStates
360-
, prvsWinnerState = winnerState
361-
}
335+
candidateOrWinnerState <- updateCandidateVoteState cfg vote oldCandidateState
336+
`onErr` (\err -> RoundVoteStateForgingCertError err)
337+
case candidateOrWinnerState of
338+
ACandidate newCandidateState ->
339+
-- Quorum still not reached for this round
340+
let prvsCandidateStates' =
341+
Map.insert
342+
(getPerasVoteBlock vote)
343+
newCandidateState
344+
prvsCandidateStates
345+
in pure $
346+
PerasRoundVoteStateQuorumNotReached
347+
{ prvsRoundNo = prvsRoundNo roundState
348+
, prvsCandidateStates = prvsCandidateStates'
349+
}
350+
AWinner winnerState ->
351+
-- Quorum has been reached for the first time here for this round
352+
let winnerPoint = getPerasVoteBlock winnerState
353+
loserStates = candidateToLoser cfg <$> Map.delete winnerPoint prvsCandidateStates
354+
in pure $
355+
PerasRoundVoteStateQuorumReachedAlready
356+
{ prvsRoundNo = prvsRoundNo roundState
357+
, prvsLoserStates = loserStates
358+
, prvsWinnerState = winnerState
359+
}
360+
362361
state@PerasRoundVoteStateQuorumReachedAlready{prvsLoserStates, prvsWinnerState} ->
363362
let votePoint = getPerasVoteBlock vote
364363
winnerPoint = getPerasVoteBlock prvsWinnerState
365364

366365
updateMaybeLoser mState =
367366
updateLoserVoteState cfg vote (fromMaybe (freshLoserVoteState (getPerasVoteTarget vote)) mState)
368367
`onErr` (\err -> RoundVoteStateLoserAboveQuorum prvsWinnerState err)
368+
369369
in if votePoint == winnerPoint
370370
then pure $ state{prvsWinnerState = updateWinnerVoteState vote prvsWinnerState}
371371
else do
372-
prvsLoserStates' <-
373-
Map.alterF
374-
(\mState -> Just <$> updateMaybeLoser mState)
375-
votePoint
376-
prvsLoserStates
372+
prvsLoserStates' <- Map.alterF
373+
(\mState -> Just <$> updateMaybeLoser mState)
374+
votePoint
375+
prvsLoserStates
377376
pure $ state{prvsLoserStates = prvsLoserStates'}
378377

379378
-- | Updates the round vote states map with the given vote.

0 commit comments

Comments
 (0)