-
Notifications
You must be signed in to change notification settings - Fork 200
/
BPConProof.tla
2127 lines (2009 loc) · 99.8 KB
/
BPConProof.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---------------------------- MODULE BPConProof ------------------------------
(***************************************************************************)
(* This module specifies a Byzantine Paxos algorithm--a version of Paxos *)
(* in which failed acceptors and leaders can be malicious. It is an *)
(* abstraction and generalization of the Castro-Liskov algorithm in *)
(* *)
(* author = "Miguel Castro and Barbara Liskov", *)
(* title = "Practical byzantine fault tolerance and proactive *)
(* recovery", *)
(* journal = ACM Transactions on Computer Systems, *)
(* volume = 20, *)
(* number = 4, *)
(* year = 2002, *)
(* pages = "398--461" *)
(***************************************************************************)
EXTENDS Integers, FiniteSets, FiniteSetTheorems, TLAPS
----------------------------------------------------------------------------
(***************************************************************************)
(* The sets Value and Ballot are the same as in the Voting and *)
(* PConProof specs. *)
(***************************************************************************)
CONSTANT Value
Ballot == Nat
(***************************************************************************)
(* As in module PConProof, we define None to be an unspecified value that *)
(* is not an element of Value. *)
(***************************************************************************)
None == CHOOSE v : v \notin Value
-----------------------------------------------------------------------------
(***************************************************************************)
(* We pretend that which acceptors are good and which are malicious is *)
(* specified in advance. Of course, the algorithm executed by the good *)
(* acceptors makes no use of which acceptors are which. Hence, we can *)
(* think of the sets of good and malicious acceptors as "prophecy *)
(* constants" that are used only for showing that the algorithm implements *)
(* the PCon algorithm. *)
(* *)
(* We can assume that a maximal set of acceptors are bad, since a bad *)
(* acceptor is allowed to do anything--including acting like a good one. *)
(* *)
(* The basic idea is that the good acceptors try to execute the Paxos *)
(* consensus algorithm, while the bad acceptors may try to prevent them. *)
(* *)
(* We do not distinguish between faulty and non-faulty leaders. Safety *)
(* must be preserved even if all leaders are malicious, so we allow any *)
(* leader to send any syntactically correct message at any time. (In an *)
(* implementation, syntactically incorrect messages are simply ignored by *)
(* non-faulty acceptors and have no effect.) Assumptions about leader *)
(* behavior are required only for liveness. *)
(***************************************************************************)
CONSTANTS Acceptor, \* The set of good (non-faulty) acceptors.
FakeAcceptor, \* The set of possibly malicious (faulty) acceptors.
ByzQuorum,
(***************************************************************)
(* A Byzantine quorum is set of acceptors that includes a *)
(* quorum of good ones. In the case that there are 2f+1 good *)
(* acceptors and f bad ones, a Byzantine quorum is any set of *)
(* 2f+1 acceptors. *)
(***************************************************************)
WeakQuorum
(***************************************************************)
(* A weak quorum is a set of acceptors that includes at least *)
(* one good one. If there are f bad acceptors, then a weak *)
(* quorum is any set of f+1 acceptors. *)
(***************************************************************)
(***************************************************************************)
(* We define ByzAcceptor to be the set of all real or fake acceptors. *)
(***************************************************************************)
ByzAcceptor == Acceptor \cup FakeAcceptor
(***************************************************************************)
(* As in the Paxos consensus algorithm, we assume that the set of ballot *)
(* numbers and -1 is disjoint from the set of all (real and fake) *)
(* acceptors. *)
(***************************************************************************)
ASSUME BallotAssump == (Ballot \cup {-1}) \cap ByzAcceptor = {}
(***************************************************************************)
(* The following are the assumptions about acceptors and quorums that are *)
(* needed to ensure safety of our algorithm. *)
(***************************************************************************)
ASSUME BQA ==
/\ Acceptor \cap FakeAcceptor = {}
/\ \A Q \in ByzQuorum : Q \subseteq ByzAcceptor
/\ \A Q1, Q2 \in ByzQuorum : Q1 \cap Q2 \cap Acceptor # {}
/\ \A Q \in WeakQuorum : /\ Q \subseteq ByzAcceptor
/\ Q \cap Acceptor # {}
(***************************************************************************)
(* The following assumption is not needed for safety, but it will be *)
(* needed to ensure liveness. *)
(***************************************************************************)
ASSUME BQLA ==
/\ \E Q \in ByzQuorum : Q \subseteq Acceptor
/\ \E Q \in WeakQuorum : Q \subseteq Acceptor
-----------------------------------------------------------------------------
(***************************************************************************)
(* We now define the set BMessage of all possible messages. *)
(***************************************************************************)
1aMessage == [type : {"1a"}, bal : Ballot]
(*************************************************************************)
(* Type 1a messages are the same as in module PConProof. *)
(*************************************************************************)
1bMessage ==
(*************************************************************************)
(* A 1b message serves the same function as a 1b message in ordinary *)
(* Paxos, where the mbal and mval components correspond to the mbal and *)
(* mval components in the 1b messages of PConProof. The m2av component *)
(* is set containing all records with val and bal components equal to *)
(* the corresponding of components of a 2av message that the acceptor *)
(* has sent, except containing for each val only the record *)
(* corresponding to the 2av message with the highest bal component. *)
(*************************************************************************)
[type : {"1b"}, bal : Ballot,
mbal : Ballot \cup {-1}, mval : Value \cup {None},
m2av : SUBSET [val : Value, bal : Ballot],
acc : ByzAcceptor]
1cMessage ==
(*************************************************************************)
(* Type 1c messages are the same as in PConProof. *)
(*************************************************************************)
[type : {"1c"}, bal : Ballot, val : Value]
2avMessage ==
(*************************************************************************)
(* When an acceptor receives a 1c message, it relays that message's *)
(* contents to the other acceptors in a 2av message. It does this only *)
(* for the first 1c message it receives for that ballot; it can receive *)
(* a second 1c message only if the leader is malicious, in which case it *)
(* ignores that second 1c message. *)
(*************************************************************************)
[type : {"2av"}, bal : Ballot, val : Value, acc : ByzAcceptor]
2bMessage == [type : {"2b"}, acc : ByzAcceptor, bal : Ballot, val : Value]
(*************************************************************************)
(* 2b messages are the same as in ordinary Paxos. *)
(*************************************************************************)
BMessage ==
1aMessage \cup 1bMessage \cup 1cMessage \cup 2avMessage \cup 2bMessage
(***************************************************************************)
(* We will need the following simple fact about these sets of messages. *)
(***************************************************************************)
LEMMA BMessageLemma ==
\A m \in BMessage :
/\ (m \in 1aMessage) <=> (m.type = "1a")
/\ (m \in 1bMessage) <=> (m.type = "1b")
/\ (m \in 1cMessage) <=> (m.type = "1c")
/\ (m \in 2avMessage) <=> (m.type = "2av")
/\ (m \in 2bMessage) <=> (m.type = "2b")
<1>1. /\ \A m \in 1aMessage : m.type = "1a"
/\ \A m \in 1bMessage : m.type = "1b"
/\ \A m \in 1cMessage : m.type = "1c"
/\ \A m \in 2avMessage : m.type = "2av"
/\ \A m \in 2bMessage : m.type = "2b"
BY DEF 1aMessage, 1bMessage, 1cMessage, 2avMessage, 2bMessage
<1>2. QED
BY <1>1 DEF BMessage
-----------------------------------------------------------------------------
(****************************************************************************
We now give the algorithm. The basic idea is that the set Acceptor of
real acceptors emulate an execution of the PCon algorithm with
Acceptor as its set of acceptors. Of course, they must do that
without knowing which of the other processes in ByzAcceptor are real
acceptors and which are fake acceptors. In addition, they don't know
whether a leader is behaving according to the PCon algorithm or if it
is malicious.
The main idea of the algorithm is that, before performing an action of
the PCon algorithm, a good acceptor determines that this action is
actually enabled in that algorithm. Since an action is enabled by the
receipt of one or more messages, the acceptor has to determine that
the enabling messages are legal PCon messages. Because algorithm PCon
allows a 1a message to be sent at any time, the only acceptor action
whose enabling messages must be checked is the Phase2b action. It is
enabled iff the appropriate 1c message and 2a message are legal. The
1c message is legal iff the leader has received the necessary 1b
messages. The acceptor therefore maintains a set of 1b messages that
it knows have been sent, and checks that those 1b messages enable the
sending of the 1c message.
A 2a message is legal in the PCon algorithm iff (i) the corresponding
1c message is legal and (ii) it is the only 2a message that the leader
sends. In the BPCon algorithm, there are no explicit 2a messages.
They are implicitly sent by the acceptors when they send enough 2av
messages.
We leave unspecified how an acceptor discovers what 1b messages have
been sent. In the Castro-Liskov algorithm, this is done by having
acceptors relay messages sent by other acceptors. An acceptor knows
that a 1b message has been sent if it receives it directly or else
receives a copy from a weak Byzantine quorum of acceptors. A
(non-malicious) leader must determine what 1b messages acceptors know
about so it chooses a value so that a quorum of acceptors will act on
its Phase1c message and cause that value to be chosen. However, this
is necessary only for liveness, so we ignore this for now.
In other implementations of our algorithm, the leader sends along with
the 1c message a proof that the necessary 1b messages have been sent.
The easiest way to do this is to have acceptors digitally sign their 1b
messages, so a copy of the message proves that it has been sent (by the
acceptor indicated in the message's acc field). The necessary proofs
can also be constructed using only message authenticators (like the
ones used in the Castro-Liskov algorithm); how this is done is
described elsewhere.
In the abstract algorithm presented here, which we call
BPCon, we do not specify how acceptors learn what 1b
messages have been sent. We simply introduce a variable knowsSent such
that knowsSent[a] represents the set of 1b messages that (good)
acceptor a knows have been sent, and have an action that
nondeterministically adds sent 1b messages to this set.
--algorithm BPCon {
(**************************************************************************
The variables:
maxBal[a] = Highest ballot in which acceptor a has participated.
maxVBal[a] = Highest ballot in which acceptor a has cast a vote
(sent a 2b message); or -1 if it hasn't cast a vote.
maxVVal[a] = Value acceptor a has voted for in ballot maxVBal[a],
or None if maxVBal[a] = -1.
2avSent[a] = A set of records in [val : Value, bal : Ballot]
describing the 2av messages that a has sent. A
record is added to this set, and any element with
the same val field (and lower bal field) removed
when a sends a 2av message.
knownSent[a] = The set of 1b messages that acceptor a knows have
been sent.
bmsgs = The set of all messages that have been sent. See the
discussion of the msgs variable in module PConProof
to understand our modeling of message passing.
**************************************************************************)
variables maxBal = [a \in Acceptor |-> -1],
maxVBal = [a \in Acceptor |-> -1] ,
maxVVal = [a \in Acceptor |-> None] ,
2avSent = [a \in Acceptor |-> {}],
knowsSent = [a \in Acceptor |-> {}],
bmsgs = {}
define {
sentMsgs(type, bal) == {m \in bmsgs: m.type = type /\ m.bal = bal}
KnowsSafeAt(ac, b, v) ==
(*********************************************************************)
(* True for an acceptor ac, ballot b, and value v iff the set of 1b *)
(* messages in knowsSent[ac] implies that value v is safe at ballot *)
(* b in the PaxosConsensus algorithm being emulated by the good *)
(* acceptors. To understand the definition, see the definition of *)
(* ShowsSafeAt in module PConProof and recall (a) the meaning of the *)
(* mCBal and mCVal fields of a 1b message and (b) that the set of *)
(* real acceptors in a ByzQuorum forms a quorum of the *)
(* PaxosConsensus algorithm. *)
(*********************************************************************)
LET S == {m \in knowsSent[ac] : m.bal = b}
IN \/ \E BQ \in ByzQuorum :
\A a \in BQ : \E m \in S : /\ m.acc = a
/\ m.mbal = -1
\/ \E c \in 0..(b-1):
/\ \E BQ \in ByzQuorum :
\A a \in BQ : \E m \in S : /\ m.acc = a
/\ m.mbal =< c
/\ (m.mbal = c) => (m.mval = v)
/\ \E WQ \in WeakQuorum :
\A a \in WQ :
\E m \in S : /\ m.acc = a
/\ \E r \in m.m2av : /\ r.bal >= c
/\ r.val = v
}
(*************************************************************************)
(* We now describe the processes' actions as macros. *)
(* *)
(* The following two macros send a message and a set of messages, *)
(* respectively. These macros are so simple that they're hardly worth *)
(* introducing, but they do make the processes a little easier to read. *)
(*************************************************************************)
macro SendMessage(m) { bmsgs := bmsgs \cup {m} }
macro SendSetOfMessages(S) { bmsgs := bmsgs \cup S }
(*************************************************************************)
(* As in the Paxos consensus algorithm, a ballot `self' leader (good or *)
(* malicious) can execute a Phase1a ation at any time. *)
(*************************************************************************)
macro Phase1a() { SendMessage([type |-> "1a", bal |-> self]) }
(*************************************************************************)
(* The acceptor's Phase1b ation is similar to that of the PaxosConsensus *)
(* algorithm. *)
(*************************************************************************)
macro Phase1b(b) {
when (b > maxBal[self]) /\ (sentMsgs("1a", b) # {}) ;
maxBal[self] := b ;
SendMessage([type |-> "1b", bal |-> b, acc |-> self, m2av |-> 2avSent[self],
mbal |-> maxVBal[self], mval |-> maxVVal[self]])
}
(*************************************************************************)
(* A good ballot `self' leader can send a phase 1c message for value v *)
(* if it knows that the messages in knowsSent[a] for a Quorum of (good) *)
(* acceptors imply that they know that v is safe at ballot `self', and *)
(* that they can convince any other acceptor that the appropriate 1b *)
(* messages have been sent to that it will also know that v is safe at *)
(* ballot `self'. *)
(* *)
(* A malicious ballot `self' leader can send any phase 1c messages it *)
(* wants (including one that a good leader could send). We prove safety *)
(* with a Phase1c ation that allows a leader to be malicious. To prove *)
(* liveness, we will have to assume a good leader that sends only *)
(* correct 1c messages. *)
(* *)
(* As in the PaxosConsensus algorithm, we allow a Phase1c action to send *)
(* a set of Phase1c messages. (This is not done in the Castro-Liskov *)
(* algorithm, but seems natural in light of the PaxosConsensus *)
(* algorithm.) *)
(*************************************************************************)
macro Phase1c() {
with (S \in SUBSET [type : {"1c"}, bal : {self}, val : Value]) {
SendSetOfMessages(S) }
}
(*************************************************************************)
(* If acceptor `self' receives a ballot b phase 1c message with value v, *)
(* it relays v in a phase 2av message if *)
(* *)
(* - it has not already sent a 2av message in this or a later *)
(* ballot and *)
(* *)
(* - the messages in knowsSent[self] show it that v is safe at b in *)
(* the non-Byzantine Paxos consensus algorithm being emulated. *)
(*************************************************************************)
macro Phase2av(b) {
when /\ maxBal[self] =< b
/\ \A r \in 2avSent[self] : r.bal < b ;
\* We could just as well have used r.bal # b in this condition.
with (m \in {ms \in sentMsgs("1c", b) : KnowsSafeAt(self, b, ms.val)}) {
SendMessage([type |-> "2av", bal |-> b, val |-> m.val, acc |-> self]) ;
2avSent[self] := {r \in 2avSent[self] : r.val # m.val}
\cup {[val |-> m.val, bal |-> b]}
} ;
maxBal[self] := b ;
}
(*************************************************************************)
(* Acceptor `self' can send a phase 2b message with value v if it has *)
(* received phase 2av messages from a Byzantine quorum, which implies *)
(* that a quorum of good acceptors assert that this is the first 1c *)
(* message sent by the leader and that the leader was allowed to send *)
(* that message. It sets maxBal[self], maxVBal[self], and maxVVal[self] *)
(* as in the non-Byzantine algorithm. *)
(*************************************************************************)
macro Phase2b(b) {
when maxBal[self] =< b ;
with (v \in {vv \in Value :
\E Q \in ByzQuorum :
\A aa \in Q :
\E m \in sentMsgs("2av", b) : /\ m.val = vv
/\ m.acc = aa} ) {
SendMessage([type |-> "2b", acc |-> self, bal |-> b, val |-> v]) ;
maxVVal[self] := v ;
} ;
maxBal[self] := b ;
maxVBal[self] := b
}
(*************************************************************************)
(* At any time, an acceptor can learn that some set of 1b messages were *)
(* sent (but only if they atually were sent). *)
(*************************************************************************)
macro LearnsSent(b) {
with (S \in SUBSET sentMsgs("1b", b)) {
knowsSent[self] := knowsSent[self] \cup S
}
}
(*************************************************************************)
(* A malicious acceptor `self' can send any acceptor message indicating *)
(* that it is from itself. Since a malicious acceptor could allow other *)
(* malicious processes to forge its messages, this action could *)
(* represent the sending of the message by any malicious process. *)
(*************************************************************************)
macro FakingAcceptor() {
with ( m \in { mm \in 1bMessage \cup 2avMessage \cup 2bMessage :
mm.acc = self} ) {
SendMessage(m)
}
}
(*************************************************************************)
(* We combine these individual actions into a complete algorithm in the *)
(* usual way, with separate process declarations for the acceptor, *)
(* leader, and fake acceptor processes. *)
(*************************************************************************)
process (acceptor \in Acceptor) {
acc: while (TRUE) {
with (b \in Ballot) {either Phase1b(b) or Phase2av(b)
or Phase2b(b) or LearnsSent(b)}
}
}
process (leader \in Ballot) {
ldr: while (TRUE) {
either Phase1a() or Phase1c()
}
}
process (facceptor \in FakeAcceptor) {
facc : while (TRUE) { FakingAcceptor() }
}
}
Below is the TLA+ translation, as produced by the translator. (Some
blank lines have been removed.)
**************************************************************************)
\* BEGIN TRANSLATION
VARIABLES maxBal, maxVBal, maxVVal, 2avSent, knowsSent, bmsgs
(* define statement *)
sentMsgs(type, bal) == {m \in bmsgs: m.type = type /\ m.bal = bal}
KnowsSafeAt(ac, b, v) ==
LET S == {m \in knowsSent[ac] : m.bal = b}
IN \/ \E BQ \in ByzQuorum :
\A a \in BQ : \E m \in S : /\ m.acc = a
/\ m.mbal = -1
\/ \E c \in 0..(b-1):
/\ \E BQ \in ByzQuorum :
\A a \in BQ : \E m \in S : /\ m.acc = a
/\ m.mbal =< c
/\ (m.mbal = c) => (m.mval = v)
/\ \E WQ \in WeakQuorum :
\A a \in WQ :
\E m \in S : /\ m.acc = a
/\ \E r \in m.m2av : /\ r.bal >= c
/\ r.val = v
vars == << maxBal, maxVBal, maxVVal, 2avSent, knowsSent, bmsgs >>
ProcSet == (Acceptor) \cup (Ballot) \cup (FakeAcceptor)
Init == (* Global variables *)
/\ maxBal = [a \in Acceptor |-> -1]
/\ maxVBal = [a \in Acceptor |-> -1]
/\ maxVVal = [a \in Acceptor |-> None]
/\ 2avSent = [a \in Acceptor |-> {}]
/\ knowsSent = [a \in Acceptor |-> {}]
/\ bmsgs = {}
acceptor(self) == \E b \in Ballot:
\/ /\ (b > maxBal[self]) /\ (sentMsgs("1a", b) # {})
/\ maxBal' = [maxBal EXCEPT ![self] = b]
/\ bmsgs' = (bmsgs \cup {([type |-> "1b", bal |-> b, acc |-> self, m2av |-> 2avSent[self],
mbal |-> maxVBal[self], mval |-> maxVVal[self]])})
/\ UNCHANGED <<maxVBal, maxVVal, 2avSent, knowsSent>>
\/ /\ /\ maxBal[self] =< b
/\ \A r \in 2avSent[self] : r.bal < b
/\ \E m \in {ms \in sentMsgs("1c", b) : KnowsSafeAt(self, b, ms.val)}:
/\ bmsgs' = (bmsgs \cup {([type |-> "2av", bal |-> b, val |-> m.val, acc |-> self])})
/\ 2avSent' = [2avSent EXCEPT ![self] = {r \in 2avSent[self] : r.val # m.val}
\cup {[val |-> m.val, bal |-> b]}]
/\ maxBal' = [maxBal EXCEPT ![self] = b]
/\ UNCHANGED <<maxVBal, maxVVal, knowsSent>>
\/ /\ maxBal[self] =< b
/\ \E v \in {vv \in Value :
\E Q \in ByzQuorum :
\A aa \in Q :
\E m \in sentMsgs("2av", b) : /\ m.val = vv
/\ m.acc = aa}:
/\ bmsgs' = (bmsgs \cup {([type |-> "2b", acc |-> self, bal |-> b, val |-> v])})
/\ maxVVal' = [maxVVal EXCEPT ![self] = v]
/\ maxBal' = [maxBal EXCEPT ![self] = b]
/\ maxVBal' = [maxVBal EXCEPT ![self] = b]
/\ UNCHANGED <<2avSent, knowsSent>>
\/ /\ \E S \in SUBSET sentMsgs("1b", b):
knowsSent' = [knowsSent EXCEPT ![self] = knowsSent[self] \cup S]
/\ UNCHANGED <<maxBal, maxVBal, maxVVal, 2avSent, bmsgs>>
leader(self) == /\ \/ /\ bmsgs' = (bmsgs \cup {([type |-> "1a", bal |-> self])})
\/ /\ \E S \in SUBSET [type : {"1c"}, bal : {self}, val : Value]:
bmsgs' = (bmsgs \cup S)
/\ UNCHANGED << maxBal, maxVBal, maxVVal, 2avSent, knowsSent >>
facceptor(self) == /\ \E m \in { mm \in 1bMessage \cup 2avMessage \cup 2bMessage :
mm.acc = self}:
bmsgs' = (bmsgs \cup {m})
/\ UNCHANGED << maxBal, maxVBal, maxVVal, 2avSent,
knowsSent >>
Next == (\E self \in Acceptor: acceptor(self))
\/ (\E self \in Ballot: leader(self))
\/ (\E self \in FakeAcceptor: facceptor(self))
Spec == Init /\ [][Next]_vars
\* END TRANSLATION
-----------------------------------------------------------------------------
(***************************************************************************)
(* As in module PConProof, we now rewrite the next-state relation in a *)
(* form more convenient for writing proofs. *)
(***************************************************************************)
Phase1b(self, b) ==
/\ (b > maxBal[self]) /\ (sentMsgs("1a", b) # {})
/\ maxBal' = [maxBal EXCEPT ![self] = b]
/\ bmsgs' = bmsgs \cup {[type |-> "1b", bal |-> b, acc |-> self,
m2av |-> 2avSent[self],
mbal |-> maxVBal[self], mval |-> maxVVal[self]]}
/\ UNCHANGED <<maxVBal, maxVVal, 2avSent, knowsSent>>
Phase2av(self, b) ==
/\ maxBal[self] =< b
/\ \A r \in 2avSent[self] : r.bal < b
/\ \E m \in {ms \in sentMsgs("1c", b) : KnowsSafeAt(self, b, ms.val)}:
/\ bmsgs' = bmsgs \cup
{[type |-> "2av", bal |-> b, val |-> m.val, acc |-> self]}
/\ 2avSent' = [2avSent EXCEPT
![self] = {r \in 2avSent[self] : r.val # m.val}
\cup {[val |-> m.val, bal |-> b]}]
/\ maxBal' = [maxBal EXCEPT ![self] = b]
/\ UNCHANGED <<maxVBal, maxVVal, knowsSent>>
Phase2b(self, b) ==
/\ maxBal[self] =< b
/\ \E v \in {vv \in Value :
\E Q \in ByzQuorum :
\A a \in Q :
\E m \in sentMsgs("2av", b) : /\ m.val = vv
/\ m.acc = a }:
/\ bmsgs' = (bmsgs \cup
{[type |-> "2b", acc |-> self, bal |-> b, val |-> v]})
/\ maxVVal' = [maxVVal EXCEPT ![self] = v]
/\ maxBal' = [maxBal EXCEPT ![self] = b]
/\ maxVBal' = [maxVBal EXCEPT ![self] = b]
/\ UNCHANGED <<2avSent, knowsSent>>
LearnsSent(self, b) ==
/\ \E S \in SUBSET sentMsgs("1b", b):
knowsSent' = [knowsSent EXCEPT ![self] = knowsSent[self] \cup S]
/\ UNCHANGED <<maxBal, maxVBal, maxVVal, 2avSent, bmsgs>>
Phase1a(self) ==
/\ bmsgs' = (bmsgs \cup {[type |-> "1a", bal |-> self]})
/\ UNCHANGED << maxBal, maxVBal, maxVVal, 2avSent, knowsSent >>
Phase1c(self) ==
/\ \E S \in SUBSET [type : {"1c"}, bal : {self}, val : Value]:
bmsgs' = (bmsgs \cup S)
/\ UNCHANGED << maxBal, maxVBal, maxVVal, 2avSent, knowsSent >>
FakingAcceptor(self) ==
/\ \E m \in { mm \in 1bMessage \cup 2avMessage \cup 2bMessage : mm.acc = self} :
bmsgs' = (bmsgs \cup {m})
/\ UNCHANGED << maxBal, maxVBal, maxVVal, 2avSent, knowsSent >>
-----------------------------------------------------------------------------
(***************************************************************************)
(* The following lemma describes how the next-state relation Next can be *)
(* written in terms of the actions defined above. *)
(***************************************************************************)
LEMMA NextDef ==
Next <=> \/ \E self \in Acceptor :
\E b \in Ballot : \/ Phase1b(self, b)
\/ Phase2av(self, b)
\/ Phase2b(self,b)
\/ LearnsSent(self, b)
\/ \E self \in Ballot : \/ Phase1a(self)
\/ Phase1c(self)
\/ \E self \in FakeAcceptor : FakingAcceptor(self)
<1>1. \A self : acceptor(self) <=> NextDef!2!1!(self)
BY DEF acceptor, Phase1b, Phase2av, Phase2b, LearnsSent
<1>2. \A self : leader(self) <=> NextDef!2!2!(self)
BY DEF leader, Phase1a, Phase1c
<1>3. \A self : facceptor(self) <=> NextDef!2!3!(self)
BY DEF facceptor, FakingAcceptor
<1>4. QED
BY <1>1, <1>2, <1>3, Zenon
DEF Next, acceptor, leader, facceptor
-----------------------------------------------------------------------------
(***************************************************************************)
(* THE REFINEMENT MAPPING *)
(***************************************************************************)
(***************************************************************************)
(* We define a quorum to be the set of acceptors in a Byzantine quorum. *)
(* The quorum assumption QA of module PConProof, which we here call *)
(* QuorumTheorem, follows easily from the definition and assumption BQA. *)
(***************************************************************************)
Quorum == {S \cap Acceptor : S \in ByzQuorum}
THEOREM QuorumTheorem ==
/\ \A Q1, Q2 \in Quorum : Q1 \cap Q2 # {}
/\ \A Q \in Quorum : Q \subseteq Acceptor
BY BQA DEF Quorum
(***************************************************************************)
(* We now define refinement mapping under which our algorithm implements *)
(* the algorithm of module PConProof. First, we define the set msgs that *)
(* implements the variable of the same name in PConProof. There are two *)
(* non-obvious parts of the definition. *)
(* *)
(* 1. The 1c messages in msgs should just be the ones that are *)
(* legal--that is, messages whose value is safe at the indicated ballot. *)
(* The obvious way to define legality is in terms of 1b messages that have *)
(* been sent. However, this has the effect that sending a 1b message can *)
(* add both that 1b message and one or more 1c messages to msgs. Proving *)
(* implementation under this refinement mapping would require adding a *)
(* stuttering variable. Instead, we define the 1c message to be legal if *)
(* the set of 1b messages that some acceptor knows were sent confirms its *)
(* legality. Thus, those 1c messages are added to msgs by the LearnsSent *)
(* ation, which has no other effect on the refinement mapping. *)
(* *)
(* 2. A 2a message is added to msgs when a quorum of acceptors have *)
(* reacted to it by sending a 2av message. *)
(***************************************************************************)
msgsOfType(t) == {m \in bmsgs : m.type = t }
acceptorMsgsOfType(t) == {m \in msgsOfType(t) : m.acc \in Acceptor}
1bRestrict(m) == [type |-> "1b", acc |-> m.acc, bal |-> m.bal,
mbal |-> m.mbal, mval |-> m.mval]
1bmsgs == { 1bRestrict(m) : m \in acceptorMsgsOfType("1b") }
1cmsgs == {m \in msgsOfType("1c") :
\E a \in Acceptor : KnowsSafeAt(a, m.bal, m.val)}
2amsgs == {m \in [type : {"2a"}, bal : Ballot, val : Value] :
\E Q \in Quorum :
\A a \in Q :
\E m2av \in acceptorMsgsOfType("2av") :
/\ m2av.acc = a
/\ m2av.bal = m.bal
/\ m2av.val = m.val }
msgs == msgsOfType("1a") \cup 1bmsgs \cup 1cmsgs \cup 2amsgs
\cup acceptorMsgsOfType("2b")
(***************************************************************************)
(* We now define PmaxBal, the state function with which we instantiate the *)
(* variable maxBal of PConProof. The reason we don't just instantiate it *)
(* with the variable maxBal is that maxBal[a] can change when acceptor `a' *)
(* performs a Phase2av ation, which does not correspond to any acceptor *)
(* action of the PCon algorithm. We want PmaxBal[a] to change only *)
(* when `a' performs a Phase1b or Phase2b ation--that is, when it sends a *)
(* 1b or 2b message. Thus, we define PmaxBal[a] to be the largest bal *)
(* field of all 1b and 2b messages sent by `a'. *)
(* *)
(* To define PmaxBal, we need to define an operator MaxBallot so that *)
(* MaxBallot(S) is the largest element of S if S is non-empty a finite set *)
(* consisting of ballot numbers and possibly the value -1. *)
(***************************************************************************)
MaxBallot(S) ==
IF S = {} THEN -1
ELSE CHOOSE mb \in S : \A x \in S : mb >= x
(***************************************************************************)
(* To prove that the CHOOSE in this definition actually does choose a *)
(* maximum of S when S is nonempty, we need the following fact. *)
(***************************************************************************)
LEMMA FiniteSetHasMax ==
\A S \in SUBSET Int :
IsFiniteSet(S) /\ (S # {}) => \E max \in S : \A x \in S : max >= x
<1>. DEFINE P(S) == S \subseteq Int /\ S # {} =>
\E max \in S : \A x \in S : max >= x
<1>1. P({})
OBVIOUS
<1>2. ASSUME NEW T, NEW x, P(T)
PROVE P(T \cup {x})
BY <1>2
<1>3. \A S : IsFiniteSet(S) => P(S)
<2>. HIDE DEF P
<2>. QED BY <1>1, <1>2, FS_Induction, IsaM("blast")
<1>. QED BY <1>3, Zenon
(***************************************************************************)
(* Our proofs use this property of MaxBallot. *)
(***************************************************************************)
THEOREM MaxBallotProp ==
ASSUME NEW S \in SUBSET (Ballot \cup {-1}),
IsFiniteSet(S)
PROVE IF S = {} THEN MaxBallot(S) = -1
ELSE /\ MaxBallot(S) \in S
/\ \A x \in S : MaxBallot(S) >= x
<1>1. CASE S = {}
BY <1>1 DEF MaxBallot
<1>2. CASE S # {}
<2>. PICK mb \in S : \A x \in S : mb >= x
BY <1>2, FiniteSetHasMax DEF Ballot
<2>. QED BY <1>2 DEF MaxBallot
<1>. QED BY <1>1, <1>2
(***************************************************************************)
(* We now prove a couple of lemmas about MaxBallot. *)
(***************************************************************************)
LEMMA MaxBallotLemma1 ==
ASSUME NEW S \in SUBSET (Ballot \cup {-1}),
IsFiniteSet(S),
NEW y \in S, \A x \in S : y >= x
PROVE y = MaxBallot(S)
<1>1. /\ MaxBallot(S) \in S
/\ MaxBallot(S) >= y
BY MaxBallotProp
<1>2 /\ y \in Ballot \cup {-1}
/\ y >= MaxBallot(S)
BY MaxBallotProp
<1>3. MaxBallot(S) \in Int /\ y \in Int
BY <1>1, <1>2, Isa DEF Ballot
<1>. QED BY <1>1, <1>2, <1>3
LEMMA MaxBallotLemma2 ==
ASSUME NEW S \in SUBSET (Ballot \cup {-1}),
NEW T \in SUBSET (Ballot \cup {-1}),
IsFiniteSet(S), IsFiniteSet(T)
PROVE MaxBallot(S \cup T) = IF MaxBallot(S) >= MaxBallot(T)
THEN MaxBallot(S) ELSE MaxBallot(T)
<1>1. /\ MaxBallot(S) \in Ballot \cup {-1}
/\ MaxBallot(T) \in Ballot \cup {-1}
BY MaxBallotProp
<1>. S \cup T \subseteq Int
BY DEF Ballot
<1>2. CASE MaxBallot(S) >= MaxBallot(T)
<2>. SUFFICES ASSUME T # {}
PROVE MaxBallot(S \cup T) = MaxBallot(S)
BY <1>2, Zenon
<2>1. /\ MaxBallot(T) \in T
/\ \A x \in T : MaxBallot(T) >= x
BY MaxBallotProp
<2>2. CASE S = {}
<3>1. MaxBallot(S) = -1
BY <2>2 DEF MaxBallot
<3>2. MaxBallot(T) = -1
BY <3>1, <1>2, <1>1 DEF Ballot
<3>. QED BY <2>2, <3>1, <3>2, <2>1, MaxBallotLemma1, FS_Union
<2>3. CASE S # {}
<3>1. /\ MaxBallot(S) \in S
/\ \A x \in S : MaxBallot(S) >= x
BY <2>3, MaxBallotProp
<3>2. /\ MaxBallot(S) \in S \cup T
/\ \A x \in S \cup T : MaxBallot(S) >= x
BY <3>1, <2>1, <1>2
<3>. QED BY <3>2, MaxBallotLemma1, FS_Union, Zenon
<2>. QED BY <2>2, <2>3
<1>3. CASE ~(MaxBallot(S) >= MaxBallot(T))
<2>. SUFFICES ASSUME S # {}
PROVE MaxBallot(S \cup T) = MaxBallot(T)
BY <1>3
<2>1. /\ MaxBallot(S) \in S
/\ \A x \in S : MaxBallot(S) >= x
BY MaxBallotProp
<2>2. /\ MaxBallot(S) < MaxBallot(T)
/\ MaxBallot(T) # -1
BY <1>3, <1>1 DEF Ballot
<2>3. /\ MaxBallot(T) \in T
/\ \A x \in T : MaxBallot(T) >= x
BY <2>2, MaxBallotProp
<2>4. /\ MaxBallot(T) \in S \cup T
/\ \A x \in S \cup T : MaxBallot(T) >= x
BY <2>3, <2>2, <2>1
<2>. QED BY <2>4, MaxBallotLemma1, FS_Union, Zenon
<1>. QED BY <1>2, <1>3
(***************************************************************************)
(* We finally come to our definition of PmaxBal, the state function *)
(* substituted for variable maxBal of module PConProof by our refinement *)
(* mapping. We also prove a couple of lemmas about PmaxBal. *)
(***************************************************************************)
1bOr2bMsgs == {m \in bmsgs : m.type \in {"1b", "2b"}}
PmaxBal == [a \in Acceptor |->
MaxBallot({m.bal : m \in {ma \in 1bOr2bMsgs :
ma.acc = a}})]
LEMMA PmaxBalLemma1 ==
ASSUME NEW m ,
bmsgs' = bmsgs \cup {m},
m.type # "1b" /\ m.type # "2b"
PROVE PmaxBal' = PmaxBal
BY Zenon DEF PmaxBal, 1bOr2bMsgs
LEMMA PmaxBalLemma2 ==
ASSUME NEW m,
bmsgs' = bmsgs \cup {m},
NEW a \in Acceptor,
m.acc # a
PROVE PmaxBal'[a] = PmaxBal[a]
BY DEF PmaxBal, 1bOr2bMsgs
(***************************************************************************)
(* Finally, we define the refinement mapping. As before, for any operator *)
(* op defined in module PConProof, the following INSTANCE statement *)
(* defines P!op to be the operator obtained from op by the indicated *)
(* substitutions, along with the implicit substitutions *)
(* *)
(* Acceptor <- Acceptor, *)
(* Quorum <- Quorum *)
(* Value <- Value *)
(* maxVBal <- maxVBal *)
(* maxVVal <- maxVVal *)
(* msgs <- msgs *)
(***************************************************************************)
P == INSTANCE PConProof WITH maxBal <- PmaxBal
-----------------------------------------------------------------------------
(***************************************************************************)
(* We now define the inductive invariant Inv used in our proof. It is *)
(* defined to be the conjunction of a number of separate invariants that *)
(* we define first, starting with the ever-present type-correctness *)
(* invariant. *)
(***************************************************************************)
TypeOK == /\ maxBal \in [Acceptor -> Ballot \cup {-1}]
/\ 2avSent \in [Acceptor -> SUBSET [val : Value, bal : Ballot]]
/\ maxVBal \in [Acceptor -> Ballot \cup {-1}]
/\ maxVVal \in [Acceptor -> Value \cup {None}]
/\ knowsSent \in [Acceptor -> SUBSET 1bMessage]
/\ bmsgs \subseteq BMessage
(***************************************************************************)
(* To use the definition of PmaxBal, we need to know that the set of 1b *)
(* and 2b messages in bmsgs is finite. This is asserted by the following *)
(* invariant. Note that the set bmsgs is not necessarily finite because *)
(* we allow a Phase1c action to send an infinite number of 1c messages. *)
(***************************************************************************)
bmsgsFinite == IsFiniteSet(1bOr2bMsgs)
(***************************************************************************)
(* The following lemma is used to prove the invariance of bmsgsFinite. *)
(***************************************************************************)
LEMMA FiniteMsgsLemma ==
ASSUME NEW m, bmsgsFinite, bmsgs' = bmsgs \cup {m}
PROVE bmsgsFinite'
BY FS_AddElement DEF bmsgsFinite, 1bOr2bMsgs
(***************************************************************************)
(* Invariant 1bInv1 asserts that if (good) acceptor `a' has mCBal[a] # -1, *)
(* then there is a 1c message for ballot mCBal[a] and value mCVal[a] in *)
(* the emulated execution of algorithm PCon. *)
(***************************************************************************)
1bInv1 == \A m \in bmsgs :
/\ m.type = "1b"
/\ m.acc \in Acceptor
=> \A r \in m.m2av :
[type |-> "1c", bal |-> r.bal, val |-> r.val] \in msgs
(***************************************************************************)
(* Invariant 1bInv2 asserts that an acceptor sends at most one 1b message *)
(* for any ballot. *)
(***************************************************************************)
1bInv2 == \A m1, m2 \in bmsgs :
/\ m1.type = "1b"
/\ m2.type = "1b"
/\ m1.acc \in Acceptor
/\ m1.acc = m2.acc
/\ m1.bal = m2.bal
=> m1 = m2
(***************************************************************************)
(* Invariant 2avInv1 asserts that an acceptor sends at most one 2av *)
(* message in any ballot. *)
(***************************************************************************)
2avInv1 == \A m1, m2 \in bmsgs :
/\ m1.type = "2av"
/\ m2.type = "2av"
/\ m1.acc \in Acceptor
/\ m1.acc = m2.acc
/\ m1.bal = m2.bal
=> m1 = m2
(***************************************************************************)
(* Invariant 2avInv2 follows easily from the meaning (and setting) of *)
(* 2avSent. *)
(***************************************************************************)
2avInv2 == \A m \in bmsgs :
/\ m.type = "2av"
/\ m.acc \in Acceptor
=> \E r \in 2avSent[m.acc] : /\ r.val = m.val
/\ r.bal >= m.bal
(***************************************************************************)
(* Invariant 2avInv3 asserts that an acceptor sends a 2av message only if *)
(* the required 1c message exists in the emulated execution of *)
(* algorithm PConf. *)
(***************************************************************************)
2avInv3 == \A m \in bmsgs :
/\ m.type = "2av"
/\ m.acc \in Acceptor
=> [type |-> "1c", bal |-> m.bal, val |-> m.val] \in msgs
(***************************************************************************)
(* Invariant maxBalInv is a simple consequence of the fact that an *)
(* acceptor `a' sets maxBal[a] to b whenever it sends a 1b, 2av, or 2b *)
(* message in ballot b. *)
(***************************************************************************)
maxBalInv == \A m \in bmsgs :
/\ m.type \in {"1b", "2av", "2b"}
/\ m.acc \in Acceptor
=> m.bal =< maxBal[m.acc]
(***************************************************************************)
(* Invariant accInv asserts some simple relations between the variables *)
(* local to an acceptor, as well as the fact that acceptor `a' sets *)
(* maxCBal[a] to b and maxCVal[a] to v only if there is a ballot-b 1c *)
(* message for value c in the simulated execution of the PCon *)
(* algorithm. *)
(***************************************************************************)
accInv == \A a \in Acceptor :
\A r \in 2avSent[a] :
/\ r.bal =< maxBal[a]
/\ [type |-> "1c", bal |-> r.bal, val |-> r.val] \in msgs
(***************************************************************************)
(* Invariant knowsSentInv simply asserts that for any acceptor `a', *)
(* knowsSent[a] is a set of 1b messages that have actually been sent. *)
(***************************************************************************)
knowsSentInv == \A a \in Acceptor : knowsSent[a] \subseteq msgsOfType("1b")
Inv ==
TypeOK /\ bmsgsFinite /\ 1bInv1 /\ 1bInv2 /\ maxBalInv /\ 2avInv1 /\ 2avInv2
/\ 2avInv3 /\ accInv /\ knowsSentInv
-----------------------------------------------------------------------------
(***************************************************************************)
(* We now prove some simple lemmas that are useful for reasoning about *)
(* PmaxBal. *)
(***************************************************************************)
LEMMA PMaxBalLemma3 ==
ASSUME TypeOK,
bmsgsFinite,
NEW a \in Acceptor
PROVE LET S == {m.bal : m \in {ma \in bmsgs :
/\ ma.type \in {"1b", "2b"}
/\ ma.acc = a}}
IN /\ IsFiniteSet(S)
/\ S \in SUBSET Ballot
<1> DEFINE T == {ma \in bmsgs : /\ ma.type \in {"1b", "2b"}
/\ ma.acc = a}
S == {m.bal : m \in T}
<1>1. IsFiniteSet(S)
<2>1. IsFiniteSet(T)
BY FS_Subset DEF bmsgsFinite, 1bOr2bMsgs
<2>. QED
BY <2>1, FS_Image, Isa
<1>. QED BY <1>1, BMessageLemma DEF 1bMessage, 2bMessage, TypeOK
LEMMA PmaxBalLemma4 ==
ASSUME TypeOK,
maxBalInv,
bmsgsFinite,
NEW a \in Acceptor
PROVE PmaxBal[a] =< maxBal[a]
<1> DEFINE SM == {ma \in bmsgs : /\ ma.type \in {"1b", "2b"}
/\ ma.acc = a}
S == {ma.bal : ma \in SM}
<1>1. PmaxBal[a] = MaxBallot(S)
BY DEF PmaxBal, 1bOr2bMsgs
<1>2. /\ IsFiniteSet(S)
/\ S \in SUBSET Ballot
BY PMaxBalLemma3
<1>3. \A b \in S : b =< maxBal[a]
BY DEF maxBalInv
<1>4. CASE S = {}
<2>1. PmaxBal[a] = -1
BY <1>2, <1>1, <1>4, MaxBallotProp
<2>. QED
BY <2>1 DEF Ballot, TypeOK
<1>5. CASE S # {}
<2>1. MaxBallot(S) \in S
BY <1>2, <1>5, MaxBallotProp, Zenon
<2>2. QED
BY <1>1, <1>3, <2>1
<1>6. QED
BY <1>4, <1>5
LEMMA PmaxBalLemma5 ==
ASSUME TypeOK, bmsgsFinite, NEW a \in Acceptor
PROVE PmaxBal[a] \in Ballot \cup {-1}
BY PMaxBalLemma3, MaxBallotProp DEF PmaxBal, 1bOr2bMsgs
-----------------------------------------------------------------------------
(***************************************************************************)
(* Now comes a bunch of useful lemmas. *)
(***************************************************************************)
(***************************************************************************)
(* We first prove that P!NextDef is a valid theorem and give it the name *)
(* PNextDef. This requires proving that the assumptions of module *)