Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spec/keeper: Adds Skip threshold and threshold emission tracking #30

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 48 additions & 30 deletions Specs/Quint/voteBookkeeper.qnt
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ module voteBookkeeper {
total: Weight,
// includes nil
valuesWeights: ValueWeights,
valuesAddresses: Value -> Set[Address]
votesAddresses: Set[Address]
}

type RoundVotes = {
height: Height,
round: Round,
prevotes: VoteCount,
precommits: VoteCount,
emittedThresholds: Set[Threshold],
votesAddressesWeights: Address -> Weight
}

type Threshold = {
Expand All @@ -57,42 +59,38 @@ module voteBookkeeper {

type Bookkeeper = {
height: Height,
totalWeight: int,
totalWeight: Weight,
rounds: Round -> RoundVotes
}

// Internal functions

pure def newVoteCount(total: Weight): VoteCount = {
{total: total, valuesWeights: Map(), valuesAddresses: Map(),}
{total: total, valuesWeights: Map(), votesAddresses: Set()}
}

pure def isQuorum(weight: int, total: int): bool = {
3 * weight > 2 * total
}

pure def addVote(voteCount: VoteCount, vote: Vote, weight: Weight): {voteCount: VoteCount, threshold: Threshold} = {
val value = vote.value
val total = voteCount.total
val isDuplicate = vote.address.in(voteCount.valuesAddresses.getOrElse(value, Set()))
val newWeight = if (isDuplicate) voteCount.valuesWeights.getOrElse(value, 0)
else voteCount.valuesWeights.getOrElse(value, 0) + weight
val updatedVoteCount = voteCount.with("valuesWeights", voteCount.valuesWeights.mapSafeSet(value, newWeight))
val sumWeight = updatedVoteCount.valuesWeights.keys().fold(0, (sum, v) => sum + updatedVoteCount.valuesWeights.get(v))
val threshold = if (value != "nil" and isQuorum(newWeight, total)) {name: "Value", value: value}
else if (value == "nil" and isQuorum(newWeight, total)) {name: "Nil", value: "null"}
else if (isQuorum(sumWeight, total)) {name: "Any", value: "null"}
else {name: "Unreached", value: "null"}
{voteCount: updatedVoteCount, threshold: threshold}
pure def isSkip(weight: int, total: int): bool = {
3 * weight > total
}

pure def checkThresholdCount(voteCount: VoteCount, threshold: Threshold): bool = {
val total = voteCount.total
pure def addVote(voteCount: VoteCount, vote: Vote, weight: Weight): VoteCount = {
//val total = voteCount.total
if (vote.address.in(voteCount.votesAddresses)) voteCount
else val newWeight = voteCount.valuesWeights.getOrElse(vote.value, 0) + weight
voteCount.with("valuesWeights", voteCount.valuesWeights.mapSafeSet(vote.value, newWeight))
}

pure def computeThreshold(voteCount: VoteCount, value: Value, totalWeight: Weight): Threshold = {
val weight = voteCount.valuesWeights.get(value)
val sumWeight = voteCount.valuesWeights.keys().fold(0, (sum, v) => sum + voteCount.valuesWeights.get(v))
if (threshold.name == "Value") isQuorum(voteCount.valuesWeights.getOrElse(threshold.value, 0), total)
else if (threshold.name == "Nil") isQuorum(voteCount.valuesWeights.getOrElse("nil", 0), total)
else if (threshold.name == "Any") isQuorum(sumWeight, total)
else false
if (value != "nil" and isQuorum(weight, totalWeight)) {name: "Value", value: value}
else if (value == "nil" and isQuorum(weight, totalWeight)) {name: "Nil", value: "null"}
else if (isQuorum(sumWeight, totalWeight)) {name: "Any", value: "null"}
else {name: "Unreached", value: "null"}
}

pure def toEvent(round: Round, voteType: str, threshold: Threshold): ExecutorEvent = {
Expand All @@ -110,22 +108,42 @@ module voteBookkeeper {
pure def applyVote(keeper: Bookkeeper, vote: Vote, weight: int): {bookkeper: Bookkeeper, event: ExecutorEvent} = {
val height = keeper.height
val total = keeper.totalWeight
val roundVotes = keeper.rounds.getOrElse(vote.round, {height: height, round: vote.round, prevotes: newVoteCount(total), precommits: newVoteCount(total)})
val resultAddVote = if (vote.typ == "Prevote") roundVotes.prevotes.addVote(vote, weight)
else roundVotes.precommits.addVote(vote, weight)
val updatedRoundVotes = if (vote.typ == "Prevote") roundVotes.with("prevotes", resultAddVote.voteCount)
else roundVotes.with("precommits", resultAddVote.voteCount)
val roundVotes = keeper.rounds.getOrElse(vote.round, {height: height,
round: vote.round,
prevotes: newVoteCount(total),
precommits: newVoteCount(total),
emittedThresholds: Set(),
votesAddressesWeights: Map()})
val updatedVoteCount = if (vote.typ == "Prevote") roundVotes.prevotes.addVote(vote, weight)
else roundVotes.precommits.addVote(vote, weight)
val threshold = computeThreshold(updatedVoteCount, vote.value, total)
val updatedVotesAddressesWeights = if (roundVotes.votesAddressesWeights.has(vote.address)) roundVotes.votesAddressesWeights
else roundVotes.votesAddressesWeights.mapSafeSet(vote.address, weight)
val sumSkip = updatedVotesAddressesWeights.keys().fold(0, (sum, v) => sum + updatedVotesAddressesWeights.get(v))
val skipThreshold = {name: "Skip", value: "null"}
val finalThreshold = if (not(threshold.in(roundVotes.emittedThresholds))) threshold
else if (isSkip(sumSkip, total) and not(skipThreshold.in(roundVotes.emittedThresholds))) {name: "Skip", value: "null"}
else {name: "Unreached", value: "null"}

val updatedRoundVotes = if (vote.typ == "Prevote") roundVotes.with("prevotes", updatedVoteCount)
else roundVotes.with("precommits", updatedVoteCount)
.with("votesAddressesWeights", updatedVotesAddressesWeights)
.with("emittedThresholds", roundVotes.emittedThresholds.setAdd(finalThreshold))
val updatedBookkeeper = keeper.with("rounds", keeper.rounds.mapSafeSet(vote.round, updatedRoundVotes))
{bookkeper: updatedBookkeeper, event: toEvent(vote.round, vote.typ, resultAddVote.threshold)}
{bookkeper: updatedBookkeeper, event: toEvent(vote.round, vote.typ, finalThreshold)}
}

pure def checkThreshold(keeper: Bookkeeper, round: Round, voteType: str, threshold: Threshold): bool = {
if (keeper.rounds.has(round)) {
val roundVotes = keeper.rounds.get(round)
val voteCount = if (voteType == "Prevote") roundVotes.prevotes
else roundVotes.precommits
checkThresholdCount(voteCount, threshold)
val total = voteCount.total
val sumWeight = voteCount.valuesWeights.keys().fold(0, (sum, v) => sum + voteCount.valuesWeights.get(v))
if (threshold.name == "Value") isQuorum(voteCount.valuesWeights.getOrElse(threshold.value, 0), total)
else if (threshold.name == "Nil") isQuorum(voteCount.valuesWeights.getOrElse("nil", 0), total)
else if (threshold.name == "Any") isQuorum(sumWeight, total)
else false
} else false
}

}