1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
| --------------------------- MODULE SynodBasicProtocol ---------------------- \* SynodBasicProtocol.tla EXTENDS Integers, FiniteSets, TLC CONSTANTS PRIESTS, DECREES, BALLOT_IDS VARIABLES priestStatus, ledgers, usedBallotIds, msgs
\* https://github.com/tlaplus/tlaplus/issues/404 SYMM == Permutations(PRIESTS) \union Permutations(DECREES) \* I am uncertain whether or not BALLOT_IDS qualifies as a symmetry set. \* \union Permutations(BALLOT_IDS)
\* This ASSUME statement asserts assumptions being made about the constants. ASSUME /\ BALLOT_IDS \subseteq Nat /\ IsFiniteSet(BALLOT_IDS)
BlankDecree == "BLANK"
GetQuorum(ballotId) == {m.to: m \in {m \in msgs: m.type = "BeginBallot" /\ m.ballotId = ballotId}}
GetDecree(ballotId) == (CHOOSE m \in msgs: m.type = "BeginBallot" /\ m.ballotId = ballotId).decree
Init == /\ priestStatus = [ p \in PRIESTS |-> [ lastTriedBallotId |-> -1, prevVote |-> [priest |-> p, ballotId |-> -1, decree |-> BlankDecree], nextBallotId |-> -1 ] ] /\ ledgers = [p \in PRIESTS |-> [decree |-> BlankDecree]] /\ usedBallotIds = {} /\ msgs = {}
\* Step (1) \* Priest $p$ chooses a new ballot number $b$ \* greater than $\operatorname{lastTried}(p)$, \* sets $\operatorname{lastTried}(p)$ to $b$, \* and sends a $\operatorname{NextBallot}(b)$ message \* to some set of priests. SendNextBallotMsg(p) == \* This set minus operator is defined as follows. \* For any sets S and T, \* S set minus T is the set of all elements in S that are not in T. \E b \in BALLOT_IDS \ usedBallotIds: /\ ledgers[p].decree = BlankDecree \* The following use of CHOOSE is incorrect: \* b == CHOOSE n \in availBallotIds: n > priestStatus[p].lastTriedBallotId \* Because there may be multiple values in availBallotIds that \* satisfy n > priestStatus[p].lastTriedBallotId. \* Instead, EXISTS should be used here. \* The difference between CHOOSE and EXISTS will be explained later. /\ b > priestStatus[p].lastTriedBallotId /\ priestStatus' = [priestStatus EXCEPT ![p].lastTriedBallotId = b] \* The expression above is a shorter way to say \* the same thing as the next expression. \* [ \* priestStatus EXCEPT ![p] = \* [priestStatus[p] EXCEPT !.lastTriedBallotId = b] \* ] /\ usedBallotIds' = usedBallotIds \union {b} /\ msgs' = msgs \union {[ type |-> "NextBallot", ballotId |-> b ]} /\ UNCHANGED <<ledgers>>
\* Step (2) \* Upon receipt of a $\operatorname{NextBallot}(b)$ message from $p$ \* with $b > \operatorname{nextBal}(q)$, \* priest $q$ sets $\operatorname{nextBal}(q)$ to $b$ and \* sends a $\operatorname{LastVote}(b, v)$ message to $p$, \* where $v$ equals $\operatorname{prevVote}(q)$. ReceiveNextBallotMsg(q) == \E b \in BALLOT_IDS: /\ [type |-> "NextBallot", ballotId |-> b] \in msgs /\ b > priestStatus[q].nextBallotId /\ priestStatus' = [priestStatus EXCEPT ![q].nextBallotId = b] \* The expression above is a shorter way to say \* the same thing as the next expression. \* [priestStatus EXCEPT ![q] = [priestStatus[q] EXCEPT !.nextBallotId = b]] /\ msgs' = msgs \union {[ type |-> "LastVote", ballotId |-> b, vote |-> priestStatus[q].prevVote ]} /\ UNCHANGED <<ledgers, usedBallotIds>>
\* Step (3) \* After receiving a $\operatorname{LastVote}(b, v)$ message \* from every priest in some majority set $Q$, \* where $b = \operatorname{lastTried}(p)$, \* priest $p$ initiates a new ballot \* with number $b$, quorum $Q$, and decree $d$, \* where $d$ is chosen to satisfy $\operatorname{B3}$. \* He then sends a $\operatorname{BeginBallot}(b, d)$ message \* to every priest in $Q$. ReceiveLastVoteMsg(p) == \* SUBSET PRIESTS is the set of all subsets of the set PRIESTS. \* Mathematicians call it the powerset of PRIESTS and \* write it \mathcal{P}(Acceptor). \E majority \in SUBSET PRIESTS, dec \in DECREES: \* The LET clause makes these definitions local to the let-in expression. \* The defined identifiers can be used only in the expression. LET b == priestStatus[p].lastTriedBallotId \* https://www.learntla.com/core/operators.html#map-and-filter lastVoteMsgs == { m \in msgs: /\ m.type = "LastVote" /\ m.ballotId = b /\ m.vote.priest \in majority } v == (CHOOSE m \in lastVoteMsgs: \A n \in lastVoteMsgs: m.vote.ballotId >= n.vote.ballotId).vote d == IF v.ballotId = -1 /\ v.decree = BlankDecree THEN dec ELSE v.decree IN /\ {m \in msgs: m.type = "BeginBallot" /\ m.ballotId = b} = {} \* https://www.learntla.com/core/operators.html#map-and-filter /\ {m.vote.priest: m \in lastVoteMsgs} = majority /\ Cardinality(majority) * 2 > Cardinality(PRIESTS) /\ msgs' = msgs \union [ type : {"BeginBallot"}, ballotId : {b}, decree : {d}, to : majority ] /\ UNCHANGED <<priestStatus, ledgers, usedBallotIds>>
\* Step (4) \* Upon receipt of a $\operatorname{BeginBallot}(b, d)$ message \* with $b = \operatorname{nextBal}(q)$, \* priest $q$ casts his vote in ballot number $b$, \* sets $\operatorname{prevVote}(q)$ to this vote, \* and sends a $\operatorname{Voted}(b, q)$ message to $p$. ReceiveBeginBallotMsg(q) == \E d \in DECREES: LET b == priestStatus[q].nextBallotId IN /\ [ type |-> "BeginBallot", ballotId |-> b, decree |-> d, to |-> q ] \in msgs /\ priestStatus' = [ priestStatus EXCEPT ![q].prevVote.priest = q, ![q].prevVote.ballotId = b, ![q].prevVote.decree = d ] \* The expression above is a shorter way to say \* the same thing as the next expression. \* [ \* priestStatus EXCEPT ![q] = \* [ \* priestStatus[q] EXCEPT !.prevVote = \* [priest |-> q, ballotId |-> b, decree |-> d] \* ] \* ] /\ msgs' = msgs \union {[ type |-> "Voted", ballotId |-> b, from |-> q ]} /\ UNCHANGED <<ledgers, usedBallotIds>>
\* Step (5) \* If $p$ has received a $\operatorname{Voted}(b, q)$ message \* from every priest $q$ in $Q$ (the quorum for ballot number $b$), \* where $b = \operatorname{lastTried}(p)$, \* then he writes $d$ (the decree of that ballot) in his ledger and \* sends a $\operatorname{Success}(d)$ message to every priest. ReceiveVotedMsg(p) == LET b == priestStatus[p].lastTriedBallotId votedMsgs == {m \in msgs: m.type = "Voted" /\ m.ballotId = b} IN /\ GetQuorum(b) # {} /\ {m.from: m \in votedMsgs} = GetQuorum(b) /\ ledgers[p].decree = BlankDecree /\ ledgers' = [ledgers EXCEPT ![p] = [decree |-> GetDecree(b)]] /\ msgs' = msgs \union {[ type |-> "Success", ballotId |-> b, decree |-> GetDecree(b) ]} /\ UNCHANGED <<priestStatus, usedBallotIds>>
\* Step (6) \* Upon receiving a $\operatorname{Success}(d)$ message, \* a priest enters decree $d$ in his ledger. ReceiveSuccessMsg(q) == LET successMsgs == {m \in msgs: m.type = "Success"} decrees == {m.decree: m \in successMsgs} IN /\ decrees # {} /\ ledgers[q].decree = BlankDecree /\ ledgers' = \* The Difference Between CHOOSE and EXISTS, and When to Use CHOOSE \* https://lamport.azurewebsites.net/video/video7-script.pdf \* \* x' \in 1..99 \* The formula x' in the set 1..99 allows the value of x in the next \* state to be any of the 99 numbers from 1 to 99. \* \* CHOOSE i \in 1..99: TRUE \* The above expression equals an unspecified integer \* between 1 and 99. We don't know which one. \* It might or might not equal 37; the semantics of TLA+ don't say. \* \* However, there is no nondeterminism in a mathematical expression. \* Any expression always equals itself, including a CHOOSE expression. \* So this CHOOSE expression always equals itself: \* (CHOOSE i \in 1..99: TRUE) = (CHOOSE i \in 1..99: TRUE) \* If this CHOOSE expression equals 37 today, \* it will still equal 37 next week. \* TLC will always get the same number \* when it evaluates this expression. \* But you shouldn't care what number. \* \* x' = CHOOSE i \in 1..99: TRUE \* The formula x' equals this CHOOSE expression allows \* the value of x in the next state to be some particular number \* between 1 and 99 — perhaps 37. \* There's no reason why you'd ever want to write something like this. \* You should write this CHOOSE v \in S: P expression only when \* there's exactly one value v in S satisfying formula P. \* Or when it's part of a larger expression whose value \* doesn't depend on which possible value of v is chosen. \* e.g., (CHOOSE m \in mset: m.bal = maxbal).val \* This CHOOSE expression can allow \* more than one possible choice for m. \* However, in any reachable state of the algorithm, \* all possible choices of m have the same value of m.val. [ledgers EXCEPT ![q] = [decree |-> CHOOSE d \in decrees: TRUE]] /\ UNCHANGED <<priestStatus, usedBallotIds, msgs>>
Next == \E p \in PRIESTS: \/ SendNextBallotMsg(p) \/ ReceiveNextBallotMsg(p) \/ ReceiveLastVoteMsg(p) \/ ReceiveBeginBallotMsg(p) \/ ReceiveVotedMsg(p) \/ ReceiveSuccessMsg(p)
TypeOK == LET validBallotIds == {-1} \union BALLOT_IDS validDecrees == {BlankDecree} \union DECREES IN /\ DOMAIN priestStatus = PRIESTS /\ \A p \in PRIESTS: priestStatus[p] \in [ lastTriedBallotId : validBallotIds, prevVote : [ priest : PRIESTS, ballotId : validBallotIds, decree : validDecrees ], nextBallotId : validBallotIds ] /\ ledgers \in [PRIESTS -> [decree: validDecrees]] /\ usedBallotIds \subseteq BALLOT_IDS /\ msgs \subseteq [type: {"NextBallot"}, ballotId: BALLOT_IDS] \union [type : {"LastVote"}, ballotId : BALLOT_IDS, vote : [priest : PRIESTS, ballotId : validBallotIds, decree : validDecrees]] \union [type : {"BeginBallot"}, ballotId : BALLOT_IDS, decree : DECREES, to : PRIESTS] \union [type : {"Voted"}, ballotId : BALLOT_IDS, from : PRIESTS] \union [type : {"Success"}, ballotId : BALLOT_IDS, decree : DECREES]
B1Consistent == \* $\operatorname{B1}(\mathcal{B})$ \* Each ballot in $\mathcal{B}$ has a unique ballot number. /\ \A p1, p2 \in {p \in PRIESTS: priestStatus[p].lastTriedBallotId # -1}: \/ p1 = p2 \/ priestStatus[p1].lastTriedBallotId # priestStatus[p2].lastTriedBallotId /\ \A m1, m2 \in {m \in msgs: m.type = "BeginBallot"}: \/ m1.decree = m2.decree \/ m1.ballotId # m2.ballotId
B2Consistent == \* $\operatorname{B2}(\mathcal{B})$ \* The quorums of any two ballots in $\mathcal{B}$ \* have at least one priest in common. \A m1, m2 \in {m \in msgs: m.type = "BeginBallot"}: GetQuorum(m1.ballotId) \intersect GetQuorum(m2.ballotId) # {}
B3Consistent == \* $\operatorname{B3}(\mathcal{B})$ \* For every ballot $B$ in $\mathcal{B}$, \* if any priest in $B$'s quorum voted in an earlier ballot in $\mathcal{B}$, \* then the decree of $B$ equals \* the decree of the latest of those earlier ballots. \A beginBallotMsg \in {m \in msgs: m.type = "BeginBallot"}: LET quorum == GetQuorum(beginBallotMsg.ballotId) votedMsgs == { m \in msgs: /\ m.type = "Voted" /\ m.ballotId < beginBallotMsg.ballotId /\ m.from \in quorum } IN \/ votedMsgs = {} \/ LET latestEarlierBallotId == (CHOOSE latestMsg \in votedMsgs: \A m \in votedMsgs: latestMsg.ballotId >= m.ballotId).ballotId latestEarlierDecree == (CHOOSE m \in msgs: /\ m.type = "BeginBallot" /\ m.ballotId = latestEarlierBallotId).decree IN beginBallotMsg.decree = latestEarlierDecree
LemmaConsistent == \* Lemma \* To show that these conditions imply consistency, \* the Paxons first showed that \* $\operatorname{B1}(\mathcal{B})$ – $\operatorname{B3}(\mathcal{B})$ \* imply that, if a ballot $B$ in $\mathcal{B}$ is successful, \* then any later ballot in $\mathcal{B}$ is for the same decree as $B$. \A m1 \in {m \in msgs: m.type = "Success"}: \A m2 \in {m \in msgs: m.type = "BeginBallot" /\ m.ballotId > m1.ballodId}: m2.decree = m1.decree
Theorem1Consistent == \* Theorem 1 \* Any two successful ballots are for the same decree. \A m1, m2 \in {m \in msgs: m.type = "Success"}: m1.decree = m2.decree
LedgerConsistent == \A p1, p2 \in PRIESTS: \/ ledgers[p1].decree = BlankDecree \/ ledgers[p2].decree = BlankDecree \/ ledgers[p1] = ledgers[p2]
Consistent == /\ B1Consistent /\ B2Consistent /\ B3Consistent /\ Theorem1Consistent /\ LedgerConsistent ============================================================================
|