-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbft_locking.qnt
More file actions
358 lines (317 loc) · 12 KB
/
bft_locking.qnt
File metadata and controls
358 lines (317 loc) · 12 KB
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
// BFT with Locking specification in Quint
//
// Extends BFT with multiple rounds and locking.
// Demonstrates why locking is needed to maintain safety across rounds.
//
// To run:
// quint run bft_locking.qnt
// quint test bft_locking.qnt
module bft_locking {
// ==========================================================================
// TYPES
// ==========================================================================
type Node = str
type Value = str
type Round = int
type Status =
| Correct
| Faulty
// A lock records what value a node is locked on and since which round
type Lock = { value: Value, round: Round }
// ==========================================================================
// CONSTANTS
// ==========================================================================
pure val N1: Node = "n1"
pure val N2: Node = "n2"
pure val N3: Node = "n3"
pure val N4: Node = "n4"
pure val ALL_NODES: Set[Node] = Set(N1, N2, N3, N4)
pure val N: int = 4
pure val F: int = 1
pure val QUORUM: int = 2 * F + 1 // = 3
pure val A: Value = "A"
pure val B: Value = "B"
pure val NO_VALUE: Value = ""
pure val ALL_VALUES: Set[Value] = Set(A, B)
pure val NO_LOCK: Lock = { value: NO_VALUE, round: -1 }
// ==========================================================================
// STATE
// ==========================================================================
// Which nodes are faulty (fixed for a run)
var status: Node -> Status
// Current round
var round: Round
// Leader for each round (deterministic rotation for simplicity)
// In practice this would be round % n or similar
var leader: Round -> Node
// What each node is locked on (NO_LOCK if not locked)
var locked: Node -> Lock
// Votes cast this round: node -> value voted for (NO_VALUE if not voted)
var votes: Node -> Value
// Values that have been committed (across all rounds)
var committed: Set[Value]
// ==========================================================================
// HELPERS
// ==========================================================================
pure def voteCount(allVotes: Node -> Value, value: Value): int =
ALL_NODES.filter(n => allVotes.get(n) == value).size()
pure def hasQuorum(allVotes: Node -> Value, value: Value): bool =
voteCount(allVotes, value) >= QUORUM
pure def currentLeader(r: Round, leaders: Round -> Node): Node =
if (leaders.keys().contains(r)) leaders.get(r) else N1
pure def isLocked(node: Node, locks: Node -> Lock): bool =
locks.get(node) != NO_LOCK
pure def hasVoted(node: Node, allVotes: Node -> Value): bool =
allVotes.get(node) != NO_VALUE
// ==========================================================================
// INITIALIZATION
// ==========================================================================
action init = all {
nondet faultyNodes = ALL_NODES.powerset().filter(s => s.size() == F).oneOf()
status' = ALL_NODES.mapBy(n =>
if (faultyNodes.contains(n)) Faulty else Correct
),
round' = 0,
// Simple leader rotation: round 0 -> N1, round 1 -> N2, etc.
leader' = Map(0 -> N1, 1 -> N2, 2 -> N3, 3 -> N4),
locked' = ALL_NODES.mapBy(_ => NO_LOCK),
votes' = ALL_NODES.mapBy(_ => NO_VALUE),
committed' = Set(),
}
// ==========================================================================
// ACTIONS
// ==========================================================================
/// Correct node votes for a value
/// - If locked, must vote for locked value
/// - If not locked, can vote for leader's proposal
/// - When a quorum forms, ALL correct nodes who voted for that value lock on it
action correctVote(node: Node, value: Value): bool = all {
status.get(node) == Correct,
not(hasVoted(node, votes)),
// If locked, must vote for locked value
if (isLocked(node, locked))
locked.get(node).value == value
else
true,
votes' = votes.set(node, value),
// Check if this vote causes a commit
val newVotes = votes.set(node, value)
if (hasQuorum(newVotes, value)) all {
committed' = committed.union(Set(value)),
// All correct nodes who voted for this value lock on it
locked' = ALL_NODES.mapBy(n =>
val currentLock = locked.get(n)
if (status.get(n) == Correct and newVotes.get(n) == value and currentLock.round < round)
{ value: value, round: round }
else
currentLock
),
} else all {
committed' = committed,
locked' = locked,
},
status' = status,
round' = round,
leader' = leader,
}
/// Faulty node votes for any value (ignores locks)
/// When quorum forms, correct nodes who voted for that value still lock
action faultyVote(node: Node, value: Value): bool = all {
status.get(node) == Faulty,
votes' = votes.set(node, value),
val newVotes = votes.set(node, value)
if (hasQuorum(newVotes, value)) all {
committed' = committed.union(Set(value)),
// All correct nodes who voted for this value lock on it
locked' = ALL_NODES.mapBy(n =>
val currentLock = locked.get(n)
if (status.get(n) == Correct and newVotes.get(n) == value and currentLock.round < round)
{ value: value, round: round }
else
currentLock
),
} else all {
committed' = committed,
locked' = locked,
},
status' = status,
round' = round,
leader' = leader,
}
/// Node observes quorum for a value and locks on it
action observeQuorum(node: Node, value: Value): bool = all {
status.get(node) == Correct,
hasQuorum(votes, value),
// Lock on this value at current round (or keep higher lock)
val currentLock = locked.get(node)
if (currentLock.round < round)
locked' = locked.set(node, { value: value, round: round })
else
locked' = locked,
status' = status,
round' = round,
leader' = leader,
votes' = votes,
committed' = committed,
}
/// Advance to next round (e.g., leader timeout)
/// Resets votes but preserves locks
action nextRound: bool = all {
round' = round + 1,
votes' = ALL_NODES.mapBy(_ => NO_VALUE), // clear votes for new round
status' = status,
leader' = leader,
locked' = locked, // locks persist!
committed' = committed,
}
// ==========================================================================
// STATE MACHINE
// ==========================================================================
action step = any {
nondet node = ALL_NODES.oneOf()
nondet value = ALL_VALUES.oneOf()
correctVote(node, value),
nondet node = ALL_NODES.oneOf()
nondet value = ALL_VALUES.oneOf()
faultyVote(node, value),
nondet node = ALL_NODES.oneOf()
nondet value = ALL_VALUES.oneOf()
observeQuorum(node, value),
nextRound,
}
// ==========================================================================
// SAFETY PROPERTIES
// ==========================================================================
/// Agreement: at most one value commits (across all rounds)
val agreement: bool =
committed.size() <= 1
/// Combined safety
val safety: bool = agreement
// ==========================================================================
// TESTS
// ==========================================================================
/// Test: value commits in round 0, lock prevents conflicting commit in round 1
run lockPreventsConflictTest = {
all {
status' = Map(N1 -> Faulty, N2 -> Correct, N3 -> Correct, N4 -> Correct),
round' = 0,
leader' = Map(0 -> N1, 1 -> N2, 2 -> N3, 3 -> N4),
locked' = ALL_NODES.mapBy(_ => NO_LOCK),
votes' = ALL_NODES.mapBy(_ => NO_VALUE),
committed' = Set(),
}
// Round 0: A gets quorum, nodes automatically lock
.then(correctVote(N2, A))
.then(correctVote(N3, A))
.then(correctVote(N4, A))
.then(all {
assert(committed.contains(A)),
// Nodes who voted for A are now locked
assert(locked.get(N2).value == A),
assert(locked.get(N3).value == A),
assert(locked.get(N4).value == A),
status' = status, round' = round, leader' = leader,
locked' = locked, votes' = votes, committed' = committed,
})
// Move to round 1
.then(nextRound)
.then(all {
assert(round == 1),
// Locks persist across rounds
assert(locked.get(N2).value == A),
assert(locked.get(N3).value == A),
status' = status, round' = round, leader' = leader,
locked' = locked, votes' = votes, committed' = committed,
})
// Round 1: faulty node tries to get B committed
// But locked nodes can only vote A
.then(faultyVote(N1, B))
.then(correctVote(N2, A)) // locked, must vote A
.then(correctVote(N3, A)) // locked, must vote A
.then(all {
// B has only 1 vote (N1)
// A has 2 votes (N2, N3) - not quorum yet but B can't reach quorum
assert(not(committed.contains(B))),
assert(agreement),
status' = status, round' = round, leader' = leader,
locked' = locked, votes' = votes, committed' = committed,
})
}
/// Test: locking prevents agreement violation
/// Even with faulty node and unlocked node voting B, locked nodes prevent quorum
run whyLockingMattersTest = {
all {
status' = Map(N1 -> Faulty, N2 -> Correct, N3 -> Correct, N4 -> Correct),
round' = 0,
leader' = Map(0 -> N1, 1 -> N2, 2 -> N3, 3 -> N4),
locked' = ALL_NODES.mapBy(_ => NO_LOCK),
votes' = ALL_NODES.mapBy(_ => NO_VALUE),
committed' = Set(),
}
// Round 0: A gets quorum and commits, N2/N3 automatically lock
.then(correctVote(N2, A))
.then(correctVote(N3, A))
.then(faultyVote(N1, A))
.then(all {
assert(committed.contains(A)),
// N2, N3 are now locked on A
assert(locked.get(N2).value == A),
assert(locked.get(N3).value == A),
status' = status, round' = round, leader' = leader,
locked' = locked, votes' = votes, committed' = committed,
})
.then(nextRound)
// Round 1: Even if faulty node votes B and N4 votes B,
// locked nodes N2, N3 vote A, so B can't reach quorum
.then(faultyVote(N1, B))
.then(correctVote(N4, B)) // N4 wasn't locked
.then(correctVote(N2, A)) // locked on A
.then(all {
// B has: N1, N4 (2 votes)
// Can't reach quorum because N2, N3 are locked on A
assert(not(committed.contains(B))),
assert(agreement),
status' = status, round' = round, leader' = leader,
locked' = locked, votes' = votes, committed' = committed,
})
}
/// Test: node that missed round 0 can still vote in round 1
run latecomerTest = {
all {
status' = Map(N1 -> Faulty, N2 -> Correct, N3 -> Correct, N4 -> Correct),
round' = 0,
leader' = Map(0 -> N1, 1 -> N2, 2 -> N3, 3 -> N4),
locked' = ALL_NODES.mapBy(_ => NO_LOCK),
votes' = ALL_NODES.mapBy(_ => NO_VALUE),
committed' = Set(),
}
// Round 0: only N2, N3 vote A (N4 is slow/offline)
// N1 (faulty) completes the quorum
.then(correctVote(N2, A))
.then(correctVote(N3, A))
.then(faultyVote(N1, A))
.then(all {
assert(committed.contains(A)), // A commits with N1, N2, N3
// N2, N3 automatically locked when quorum formed
assert(locked.get(N2).value == A),
assert(locked.get(N3).value == A),
// N4 didn't vote, so not locked
assert(locked.get(N4) == NO_LOCK),
status' = status, round' = round, leader' = leader,
locked' = locked, votes' = votes, committed' = committed,
})
.then(nextRound)
// Round 1: N4 joins, but since it's not locked it could vote B
// However, with N2, N3 locked on A, B can't get quorum
.then(correctVote(N4, B)) // N4 not locked, votes B
.then(faultyVote(N1, B)) // faulty helps B
.then(correctVote(N2, A)) // locked
.then(all {
// B has: N1, N4 (2 votes) - not quorum
assert(not(committed.contains(B))),
assert(agreement),
status' = status, round' = round, leader' = leader,
locked' = locked, votes' = votes, committed' = committed,
})
}
}