forked from microsoft/CCF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Consistency.tla
361 lines (302 loc) · 13.6 KB
/
Consistency.tla
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
DO NOT MODIFY THIS FILE!!! MODIFY THE INDIVIDUAL MODULES FROM WHICH THIS IS MANUALLY STICHED TOGETHER!!!
---- MODULE Consistency ----
EXTENDS Naturals, Sequences
----------------------------------------------------------------------
\* Sequences.tla
\* tla-web doesn't have a builtin for the standard module's Sequences!SelectSeq.
\* This is why its definition is included under a new name here. TLC will suffer
\* a slowdown because it's module override won't be active for MySelectSeq.
MySelectSeq(s, Test(_)) ==
LET F[i \in 0..Len(s)] ==
IF i = 0 THEN << >>
ELSE IF Test(s[i]) THEN Append(F[i-1], s[i])
ELSE F[i-1]
IN F[Len(s)]
----------------------------------------------------------------------
\* SequencesExt.tla
\* The following five operators are copied from the CommunityModule's SequencesExt,
\* because tla-web does not have a module system that can load SequencesExt.
Max(S) == CHOOSE x \in S : \A y \in S : x >= y
Last(s) == s[Len(s)]
SelectLastInSeq(seq, Test(_)) ==
LET I == { i \in 1..Len(seq) : Test(seq[i]) }
IN IF I # {} THEN Max(I) ELSE 0
IsPrefix(s, t) ==
Len(s) <= Len(t) /\ SubSeq(s, 1, Len(s)) = SubSeq(t, 1, Len(s))
Contains(s, e) ==
\E i \in 1..Len(s) : s[i] = e
----------------------------------------------------------------------
\* ExteranlHistory.tla
RwTxRequest == "RwTxRequest"
RwTxResponse == "RwTxResponse"
RoTxRequest == "RoTxRequest"
RoTxResponse == "RoTxResponse"
TxStatusReceived == "TxStatusReceived"
CommittedStatus == "CommittedStatus"
InvalidStatus == "InvalidStatus"
TxStatuses == {
CommittedStatus,
InvalidStatus
}
FirstBranch == 1
Views == Nat
\* Sequence numbers start at 1, 0 is used a null value
SeqNums == Nat
\* TxIDs consist of a view and sequence number and thus start at (1,1)
TxIDs == Views \X SeqNums
\* This models uses a dummy application where read-write transactions
\* append an integer to a list and then reads the list
\* Read-only transactions simply read the list
Txs == Nat
VARIABLES history
HistoryTypeOK ==
\A i \in DOMAIN history:
\/ /\ history[i].type \in {RwTxRequest, RoTxRequest}
/\ history[i].tx \in Txs
\/ /\ history[i].type \in {RwTxResponse, RoTxResponse}
/\ history[i].tx \in Txs
/\ history[i].observed \in Seq(Txs)
/\ history[i].tx_id \in TxIDs
\/ /\ history[i].type = TxStatusReceived
/\ history[i].tx_id \in TxIDs
/\ history[i].status \in TxStatuses
CommittedEventIndexes ==
{i \in DOMAIN history:
/\ history[i].type = TxStatusReceived
/\ history[i].status = CommittedStatus
}
\* Transaction IDs which received committed status messages
CommittedTxIDs ==
{history[i].tx_id: i \in CommittedEventIndexes}
CommitSeqNum ==
Max({i[2]: i \in CommittedTxIDs} \cup {0})
RwTxResponseCommittedEventIndexes ==
{x \in DOMAIN history :
/\ history[x].type = RwTxResponse
/\ history[x].tx_id \in CommittedTxIDs}
\* Note these index are the events where the transaction was responded to
RoTxResponseCommittedEventIndexes ==
{x \in DOMAIN history :
/\ history[x].type = RoTxResponse
/\ history[x].tx_id \in CommittedTxIDs}
RoTxRequestCommittedEventIndexes ==
{x \in DOMAIN history :
/\ history[x].type = RoTxRequest
/\ \E y \in RoTxResponseCommittedEventIndexes:
history[y].tx = history[x].tx}
----------------------------------------------------------------------
\* ExternalHistoryInvars.tla
AllCommittedObservedRoInv ==
\A i \in RwTxResponseCommittedEventIndexes :
\A j \in RoTxRequestCommittedEventIndexes :
\A k \in RoTxResponseCommittedEventIndexes :
/\ history[k].tx = history[j].tx
/\ i < j
=> Contains(history[k].observed, history[i].tx)
----------------------------------------------------------------------
\* SingleNode.tla
\* Abstract ledgers that contains only client transactions (no signatures)
\* Indexed by view, each ledger is the ledger associated with leader of that view
\* In practice, the ledger of every CCF node is one of these or a prefix for one of these
\* This could be switched to a tree which can represent forks more elegantly
VARIABLES ledgerBranches
LedgerTypeOK ==
\A view \in DOMAIN ledgerBranches:
\A seqnum \in DOMAIN ledgerBranches[view]:
\* Each ledger entry is tuple containing a view and tx
\* The ledger entry index is the sequence number
/\ ledgerBranches[view][seqnum].view \in Views
/\ "tx" \in DOMAIN ledgerBranches[view][seqnum] => ledgerBranches[view][seqnum].tx \in Txs
\* In this abstract version of CCF's consensus layer, each ledger is append-only
LedgersMonoProp ==
[][\A view \in DOMAIN ledgerBranches: IsPrefix(ledgerBranches[view], ledgerBranches'[view])]_ledgerBranches
VARIABLE action
vars == << history, ledgerBranches, action >>
TypeOK ==
/\ HistoryTypeOK
/\ LedgerTypeOK
Init ==
/\ history = <<>>
/\ ledgerBranches = [ x \in 1..FirstBranch |-> <<>>]
/\ action = "Init"
IndexOfLastRequested ==
SelectLastInSeq(history, LAMBDA e : e.type \in {RwTxRequest, RoTxRequest})
NextRequestId ==
IF IndexOfLastRequested = 0 THEN 0 ELSE history[IndexOfLastRequested].tx+1
\* Submit new read-write transaction
\* This could be extended to add a notion of session and then check for session consistency
RwTxRequestAction ==
/\ history' = Append(
history,
[type |-> RwTxRequest, tx |-> NextRequestId]
)
/\ UNCHANGED ledgerBranches
/\ action' = "RwTxRequest"
\* Execute a read-write transaction
RwTxExecuteAction(i) ==
\* RwTxExecuteAction ==
\* /\ \E i \in DOMAIN history :
/\ history[i].type = RwTxRequest
\* Check transaction has not already been added to a ledger
/\ \A view \in DOMAIN ledgerBranches:
\A seqnum \in DOMAIN ledgerBranches[view]:
"tx" \in DOMAIN ledgerBranches[view][seqnum]
=> history[i].tx /= ledgerBranches[view][seqnum].tx
\* Note that a transaction can be added to any ledger, simulating the fact
\* that it can be picked up by the current leader or any former leader
/\ \E view \in FirstBranch..Len(ledgerBranches):
ledgerBranches' = [ledgerBranches EXCEPT ![view] =
Append(@,[view |-> view, tx |-> history[i].tx])]
/\ UNCHANGED history
/\ action' = "RwTxExecute"
LedgerBranchTxOnly(branch) ==
LET SubBranch == MySelectSeq(branch, LAMBDA e : "tx" \in DOMAIN e)
IN [i \in DOMAIN SubBranch |-> SubBranch[i].tx]
\* Response to a read-write transaction
RwTxResponseAction(i) ==
\* RwTxResponseAction ==
\* /\ \E i \in DOMAIN history :
\* Check request has been received and executed but not yet responded to
/\ history[i].type = RwTxRequest
/\ {j \in DOMAIN history:
/\ j > i
/\ history[j].type = RwTxResponse
/\ history[j].tx = history[i].tx} = {}
/\ \E view \in FirstBranch..Len(ledgerBranches):
/\ \E seqnum \in DOMAIN ledgerBranches[view]:
/\ "tx" \in DOMAIN ledgerBranches[view][seqnum]
/\ history[i].tx = ledgerBranches[view][seqnum].tx
/\ history' = Append(
history,[
type |-> RwTxResponse,
tx |-> history[i].tx,
observed |-> LedgerBranchTxOnly(SubSeq(ledgerBranches[view],1,seqnum)),
tx_id |-> <<ledgerBranches[view][seqnum].view, seqnum>>] )
/\ UNCHANGED ledgerBranches
/\ action' = "RwTxResponse"
\* Sending a committed status message
\* Note that a request could only be committed if it's in the highest view's ledger
StatusCommittedResponseAction(i) ==
\* StatusCommittedResponseAction ==
\* /\ \E i \in DOMAIN history :
/\ history[i].type = RwTxResponse
/\ Len(Last(ledgerBranches)) >= history[i].tx_id[2]
/\ Last(ledgerBranches)[history[i].tx_id[2]].view = history[i].tx_id[1]
\* There is no future InvalidStatus that's incompatible with this commit
\* This is to accomodate StatusInvalidResponseAction making future commits invalid,
\* and is an unnecessary complication for model checking. It does facilitate trace
\* validation though, by allowing immediate processing of Invalids without
\* needing to wait for the commit history knowledge to catch up.
/\ \lnot \E j \in DOMAIN history:
/\ history[j].type = TxStatusReceived
/\ history[j].status = InvalidStatus
/\ history[j].tx_id[1] = history[i].tx_id[1]
/\ history[j].tx_id[2] <= history[i].tx_id[2]
\* Reply
/\ history' = Append(
history,[
type |-> TxStatusReceived,
tx_id |-> history[i].tx_id,
status |-> CommittedStatus]
)
/\ UNCHANGED ledgerBranches
/\ action' = "StatusCommittedResponse"
\* Append a transaction to the ledger which does not impact the state we are considering
AppendOtherTxnAction ==
/\ \E view \in FirstBranch..Len(ledgerBranches):
ledgerBranches' = [ledgerBranches EXCEPT ![view] =
Append(@,[view |-> view])]
/\ UNCHANGED history
/\ action' = "AppendOtherTxn"
----------------------------------------------------------------------
\* SingleNodeReads.tla
\* Submit new read-only transaction
RoTxRequestAction ==
/\ history' = Append(
history,
[type |-> RoTxRequest, tx |-> NextRequestId]
)
/\ UNCHANGED ledgerBranches
/\ action' = "RoTxRequest"
\* Response to a read-only transaction request
\* Assumes read-only transactions are always forwarded
\* TODO: Separate execution and response
RoTxResponseAction(i) ==
\* RoTxResponseAction ==
\* /\ \E i \in DOMAIN history :
\* Check request has been received but not yet responded to
/\ history[i].type = RoTxRequest
/\ {j \in DOMAIN history:
/\ j > i
/\ history[j].type = RoTxResponse
/\ history[j].tx = history[i].tx} = {}
/\ \E view \in FirstBranch..Len(ledgerBranches):
/\ Len(ledgerBranches[view]) > 0
/\ history' = Append(
history,[
type |-> RoTxResponse,
tx |-> history[i].tx,
observed |-> LedgerBranchTxOnly(ledgerBranches[view]),
tx_id |-> <<ledgerBranches[view][Len(ledgerBranches[view])].view, Len(ledgerBranches[view])>>] )
/\ UNCHANGED ledgerBranches
/\ action' = "RoTxResponse"
----------------------------------------------------------------------
\* MultiNode.tla
\* The set of views where the corresponding terms have all committed log entries
ViewWithAllCommitted ==
{view \in DOMAIN ledgerBranches:
/\ Len(ledgerBranches[view]) >= CommitSeqNum
/\ \/ CommitSeqNum = 0
\/ <<ledgerBranches[view][CommitSeqNum].view, CommitSeqNum>> \in CommittedTxIDs }
\* Simulates leader election by rolling back some number of uncommitted transactions and updating view
TruncateLedgerAction ==
/\ \E view \in ViewWithAllCommitted:
/\ \E i \in CommitSeqNum..Len(ledgerBranches[view]) :
/\ ledgerBranches' = Append(ledgerBranches, SubSeq(ledgerBranches[view], 1, i))
/\ UNCHANGED history
/\ action' = "TruncateLedger (CheckQuorum)"
\* Sends status invalid message
StatusInvalidResponseAction(i) ==
\* StatusInvalidResponseAction ==
\* /\ \E i \in DOMAIN history :
/\ history[i].type = RwTxResponse
\* either commit has passed seqnum but committed another transaction
/\ \/ /\ CommitSeqNum >= history[i].tx_id[2]
/\ Len(ledgerBranches[Len(ledgerBranches)]) >= history[i].tx_id[2]
/\ ledgerBranches[Len(ledgerBranches)][history[i].tx_id[2]].view # history[i].tx_id[1]
\* or commit hasn't reached seqnum but never will as current view is higher
\/ /\ CommitSeqNum > 0
/\ CommitSeqNum < history[i].tx_id[2]
/\ ledgerBranches[Len(ledgerBranches)][CommitSeqNum].view > history[i].tx_id[1]
\* or commit hasn't reached there,... but can never reach there
\* note that this effectively allows StatusInvalidResponseAction to "declare" future transactions
\* invalid, and requires a corresponding change in SingleNode::StatusCommittedResponseAction to
\* constrain future commits. This combined change is unnecessary for model checking, but greatly
\* simplifies trace validation.
\/ /\ history[i].tx_id[1] = Len(ledgerBranches)
/\ history[i].tx_id[2] > CommitSeqNum
\* Reply
/\ history' = Append(
history,[
type |-> TxStatusReceived,
tx_id |-> history[i].tx_id,
status |-> InvalidStatus]
)
/\ UNCHANGED ledgerBranches
/\ action' = "StatusInvalidResponse"
----------------------------------------------------------------------
\* A CCF service with a single node will never have a view change
\* so the log will never be rolled back and thus transaction IDs cannot be invalid
Next ==
\/ TruncateLedgerAction
\/ AppendOtherTxnAction
\/ RwTxRequestAction
\/ RoTxRequestAction
\/ \E i \in DOMAIN history :
\/ RwTxExecuteAction(i)
\/ RwTxResponseAction(i)
\/ RoTxResponseAction(i)
\/ StatusCommittedResponseAction(i)
\/ StatusInvalidResponseAction(i)
Spec == Init /\ [][Next]_vars
====