-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathreplica.qnt
More file actions
1730 lines (1612 loc) · 75.7 KB
/
Copy pathreplica.qnt
File metadata and controls
1730 lines (1612 loc) · 75.7 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
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
// -*- mode: Bluespec; -*-
module replica {
import types.* from "./types"
import defs.* from "./defs"
import option.* from "./option"
// Weights of all replicas.
const WEIGHTS: ReplicaId -> Weight
// The keys of the replicas, normally, one unique key per replica.
// For modeling byzantine behaviour via twin replicas use the same keys.
const REPLICA_KEYS: ReplicaId -> ReplicaKey
// The number of all replicas.
const N: int
// Byzantine replica number threshold
const F: int
// Identities of the correct replicas.
const CORRECT: Set[ReplicaId]
// Identities of the faulty replicas.
const FAULTY: Set[ReplicaId]
// All replicas.
pure val REPLICAS = CORRECT.union(FAULTY)
// Check that threshold assumptions hold.
assume FIVE_F = (N >= 5 * F + 1 and F >= 0)
assume REPLICA_SIZE = (N == REPLICAS.size() and F == FAULTY.size())
// The sum of all validator weights.
pure val TOTAL_WEIGHT: Weight = WEIGHTS.keys().fold(0, (s, id) => s + WEIGHTS.get(id));
// The maximum weight of faulty replicas.
// We want 5*FAULTY_WEIGHT + 1 = TOTAL_WEIGHT
pure val FAULTY_WEIGHT: Weight = (TOTAL_WEIGHT - 1) / 5;
// The weight threshold needed to form a quorum certificate.
pure val QUORUM_WEIGHT: Weight = TOTAL_WEIGHT - FAULTY_WEIGHT;
// The weight threshold needed to trigger a reproposal.
pure val SUBQUORUM_WEIGHT: Weight = TOTAL_WEIGHT - 3 * FAULTY_WEIGHT;
// produce replica's signature, just its key for specification purposes
pure def sig_of_id(id: ReplicaId): ReplicaKey = REPLICA_KEYS.get(id)
// The set of all views.
const VIEWS: Set[ViewNumber]
// the set of all blocks
const VALID_BLOCKS: Set[Block]
const INVALID_BLOCKS: Set[Block]
pure val ALL_BLOCKS = VALID_BLOCKS.union(INVALID_BLOCKS)
assume DISJOINT_BLOCKS = (VALID_BLOCKS.intersect(INVALID_BLOCKS) == Set())
var replica_state: ReplicaId -> ReplicaState
// the leader function from the view to the replicas identities
var leader: ViewNumber -> ReplicaKey
// an internal variable to handle timeouts
var replica_view: ReplicaId -> ViewNumber
// the current view of the proposer (only reading the replica state)
var proposer_view: ReplicaId -> ViewNumber
// the set of all Timeout messages sent in the past
var msgs_signed_timeout: Set[SignedTimeoutVote]
// the set of all SignedCommitVote messages sent in the past
var msgs_signed_commit: Set[SignedCommitVote]
// the set of all NewView messages sent in the past
var msgs_new_view: Set[NewView]
// the set of all Proposal messages sent in the past
var msgs_proposal: Set[Proposal]
// vote storage for each correct replica
var store_signed_timeout: ReplicaId -> Set[SignedTimeoutVote]
var store_signed_commit: ReplicaId -> Set[SignedCommitVote]
// justifications stored by a replica
var ghost_justifications: (ReplicaId, ViewNumber) -> Justification
// save the last executed action
var ghost_step: StepKind
val ALL_FAULTY_SIGNED_COMMIT_VOTES =
tuples(VIEWS, FAULTY, ALL_BLOCKS, VIEWS)
.map( ((v, r, b, n)) =>
{
vote:{
view: v, block_number: n, block_hash: b
},
sig: sig_of_id(r)
})
// This is a simple deterministic version that initializes
// everything to the "genesis" state.
// In general, we should start in an arbitrary replica_state that
// satisfies certain conditions.
// TODO: in the future, we will run the system in an intermediate state:
// https://github.com/dnkolegov/chonkybft-spec/issues/23
action init = {
// non-deterministically choose the leader function
nondet ldr = VIEWS.setOfMaps(REPLICAS.map(id => sig_of_id(id))).oneOf()
init_view_1_with_leader(ldr)
}
action init_view_0_with_leader(ldr: ViewNumber -> ReplicaKey): bool = all {
replica_state' = CORRECT.mapBy(id => {
view: 0,
high_vote: COMMIT_VOTE_NONE,
high_commit_qc: HIGH_COMMIT_QC_NONE,
high_timeout_qc: HIGH_TIMEOUT_QC_NONE,
phase: PhaseTimeout,
cached_proposals: Set(),
committed_blocks: [],
}),
replica_view' = CORRECT.mapBy(id => 0),
proposer_view' = CORRECT.mapBy(id => 0),
// all correct replicas send SignedTimeoutVote as their first step
msgs_signed_timeout' = CORRECT.map(id => {
vote: { view: 0, high_vote: COMMIT_VOTE_NONE, high_commit_qc_view: None },
high_commit_qc: HIGH_COMMIT_QC_NONE,
sig: sig_of_id(id)
}),
msgs_signed_commit' = Set(),
msgs_new_view' = Set(),
msgs_proposal' = Set(),
store_signed_timeout' = CORRECT.mapBy(id => Set()),
store_signed_commit' = CORRECT.mapBy(id => Set()),
ghost_justifications' = Map(),
leader' = ldr,
ghost_step' = InitStep,
}
// start from a bootstrapped state in view 1
action init_view_1_with_leader(ldr: ViewNumber -> ReplicaKey): bool = {
// An initial quorum certificate.
// We use all the correct + faulty replicas. Otherwise, when a quorum
// of correct replicas is impossible, e.g., the actual number of faults is above F,
// we cannot reach this state.
pure val init_timeout_qc: TimeoutQC = {
votes: REPLICAS.map(id => sig_of_id(id)).mapBy(_ => {
view: 0,
high_vote: COMMIT_VOTE_NONE,
high_commit_qc_view: VIEW_NUMBER_NONE
}),
agg_sig: REPLICAS.map(id => sig_of_id(id)),
high_commit_qc: HIGH_COMMIT_QC_NONE,
ghost_view: 0,
}
pure val timeout_votes0 = REPLICAS.map(id => sig_of_id(id)).map(sig => {
sig: sig,
vote: { view: 0, high_vote: COMMIT_VOTE_NONE, high_commit_qc_view: VIEW_NUMBER_NONE },
high_commit_qc: HIGH_COMMIT_QC_NONE,
})
all {
replica_state' = CORRECT.mapBy(id => {
view: 1,
high_vote: COMMIT_VOTE_NONE,
high_commit_qc: HIGH_COMMIT_QC_NONE,
high_timeout_qc: Some(init_timeout_qc),
phase: PhasePrepare,
cached_proposals: Set(),
committed_blocks: [],
}),
replica_view' = CORRECT.mapBy(id => 0),
proposer_view' = CORRECT.mapBy(id => 0),
// all replicas have sent TimeoutVote in view 0
msgs_signed_timeout' = timeout_votes0,
msgs_new_view' = CORRECT.map(id => {
{ sig: sig_of_id(id), justification: Timeout(init_timeout_qc) }
}),
msgs_signed_commit' = Set(),
msgs_proposal' = Set(),
store_signed_timeout' = CORRECT.mapBy(id => {
// the leader stores all timeout votes, the others receive NewView
if (sig_of_id(id) == ldr.get(0)) timeout_votes0 else Set()
}),
store_signed_commit' = CORRECT.mapBy(id => Set()),
// all correct correct replicas have Timeout(init_timeout_qc)
ghost_justifications' =
tuples(CORRECT, Set(1)).mapBy(((id, view)) => Timeout(init_timeout_qc)),
leader' = ldr,
ghost_step' = InitStep,
}
}
// a single system step, including correct replicas & proposers, and faulty replicas & proposers
action step = {
any {
correct_step,
all {
faulty_step,
unchanged_replica,
leader' = leader,
}
}
}
// a single step + the fetcher algorithm
action step_with_fetcher = {
any {
step,
all {
nondet id = oneOf(CORRECT)
fetcher_step(id),
leader' = leader,
},
}
}
// same as step but correct replicas never timeout
action step_no_timeout = {
any {
nondet id = oneOf(CORRECT)
any {
all {
replica_view' = replica_view.set(id, replica_state.get(id).view),
replica_step_no_timeout(id),
leader' = leader,
},
all {
// Non-deterministically choose the next block, use it only for the case of None below.
nondet new_block = oneOf(VALID_BLOCKS)
proposer_step(id, new_block),
replica_view' = replica_view,
leader' = leader,
},
},
all {
faulty_step,
unchanged_replica,
leader' = leader,
}
}
}
// same as step but no faulty replicas (all FAULTY replicas are halted)
action correct_step = {
nondet id = oneOf(CORRECT)
any {
all {
replica_view' = replica_view.set(id, replica_state.get(id).view),
replica_step(id),
leader' = leader,
},
all {
// Non-deterministically choose the next block, use it only for the case of None below.
nondet new_block = oneOf(VALID_BLOCKS)
proposer_step(id, new_block),
replica_view' = replica_view,
leader' = leader,
},
}
}
// a single step by multiple Byzantine replicas
action faulty_step: bool = all {
FAULTY != Set(),
all {
nondet senders = FAULTY.powerset().oneOf()
nondet commit_view = VIEWS.oneOf()
nondet block_hash = ALL_BLOCKS.oneOf()
nondet block_number = VIEWS.oneOf()
val signed_commits = senders.map(s => {
vote:{
view: commit_view, block_number: block_number, block_hash: block_hash
},
sig: s
})
msgs_signed_commit' = msgs_signed_commit.union(signed_commits),
},
all {
// non-deterministically produce a timeout vote
nondet msg_view = VIEWS.oneOf()
nondet sig = oneOf(FAULTY)
nondet commit_vote: SignedCommitVote = oneOf(msgs_signed_commit.union(ALL_FAULTY_SIGNED_COMMIT_VOTES))
nondet commit_qc_view = oneOf(VIEWS)
nondet commit_qc_block_number = oneOf(VIEWS)
nondet commit_qc_block_hash = oneOf(ALL_BLOCKS)
nondet commit_qc_agg_sig = REPLICAS.powerset().oneOf()
val commit_qc_vote: CommitVote = {
view: commit_qc_view,
block_number: commit_qc_block_number,
block_hash: commit_qc_block_hash
}
val commit_qc = {
vote: commit_qc_vote,
agg_sig: commit_qc_agg_sig
}
nondet has_high_vote = oneOf(Bool)
nondet has_commit_qc = oneOf(Bool)
nondet high_commit_qc_view = oneOf(VIEWS)
nondet has_high_commit_qc_view = oneOf(Bool)
val signed_timeout_vote = {
vote: {
view: msg_view,
high_vote: if (has_high_vote) Some(commit_vote.vote) else COMMIT_VOTE_NONE,
high_commit_qc_view: if (has_high_commit_qc_view) Some(high_commit_qc_view) else VIEW_NUMBER_NONE,
},
high_commit_qc: if (has_commit_qc) Some(commit_qc) else HIGH_COMMIT_QC_NONE,
sig: sig,
}
all {
// faulty replicas cannot forge the signatures of the correct replicas, enforce that
commit_qc_agg_sig.forall(id => or {
FAULTY.contains(id),
msgs_signed_commit.exists(sv => sv.sig == id and sv.vote == commit_qc_vote),
}),
// add the message by a faulty replica
msgs_signed_timeout' = msgs_signed_timeout.union(Set(signed_timeout_vote)),
}
},
all {
nondet msg_view = VIEWS.oneOf()
nondet sig = oneOf(FAULTY)
nondet justification_case = Set("commit", "timeout").oneOf()
// introduce a commit qc
nondet commit_qc_view = oneOf(VIEWS)
nondet commit_qc_block_number = oneOf(VIEWS)
nondet commit_qc_block_hash = oneOf(ALL_BLOCKS)
nondet commit_qc_agg_sig = REPLICAS.powerset().oneOf()
val commit_qc_vote: CommitVote = {
view: commit_qc_view,
block_number: commit_qc_block_number,
block_hash: commit_qc_block_hash
}
val commit_qc = {
vote: commit_qc_vote, agg_sig: commit_qc_agg_sig,
}
// Introduce a timeout qc.
// We recycle commit_qc when constructing timeout_qc, as commit_qc and timeout_qc
// are not used at the same time, see justification_case.
nondet timeout_qc_view = VIEWS.oneOf()
nondet timeout_qc_voters = REPLICAS.powerset().oneOf()
nondet timeout_qc_agg_sig = timeout_qc_voters.powerset().oneOf()
nondet timeout_qc_votes =
timeout_qc_voters.setOfMaps(msgs_signed_timeout.map(sv => sv.vote)).oneOf()
nondet has_high_commit_qc_view = oneOf(Bool)
val timeout_qc: TimeoutQC = {
votes: timeout_qc_votes,
agg_sig: timeout_qc_agg_sig,
high_commit_qc:
if (has_high_commit_qc_view) Some(commit_qc) else HIGH_COMMIT_QC_NONE,
ghost_view: timeout_qc_view,
}
// make new view
val new_view: NewView = {
justification:
if (justification_case == "commit") {
Commit(commit_qc)
} else {
Timeout(timeout_qc)
},
sig: sig,
}
all {
// faulty replicas cannot forge the signatures of the correct replicas, enforce that
commit_qc_agg_sig.forall(id => or {
FAULTY.contains(id),
msgs_signed_commit.exists(sv => sv.sig == id and sv.vote == commit_qc_vote),
}),
justification_case != "commit" implies timeout_qc_agg_sig.union(timeout_qc_voters).forall(id => or {
FAULTY.contains(id),
msgs_signed_timeout.exists(sv => sv.sig == id and sv.vote == timeout_qc_votes.get(id)),
}),
// send a new view
msgs_new_view' = msgs_new_view.union(Set(new_view)),
}
},
all {
nondet msg_view = VIEWS.oneOf()
nondet sig = oneOf(FAULTY)
nondet justification_case = Set("commit", "timeout").oneOf()
// introduce a commit qc
nondet commit_qc_view = oneOf(VIEWS)
nondet commit_qc_block_number = oneOf(VIEWS)
nondet commit_qc_block_hash = oneOf(ALL_BLOCKS)
nondet commit_qc_agg_sig = REPLICAS.powerset().oneOf()
val commit_qc_vote: CommitVote = {
view: commit_qc_view, block_number: commit_qc_block_number,
block_hash: commit_qc_block_hash
}
val commit_qc = {
vote: commit_qc_vote, agg_sig: commit_qc_agg_sig,
}
// Introduce a timeout qc.
// We recycle commit_qc when constructing timeout_qc, as commit_qc and timeout_qc
// are not used at the same time, see justification_case.
nondet timeout_qc_view = VIEWS.oneOf()
nondet timeout_qc_voters = REPLICAS.powerset().oneOf()
nondet timeout_qc_agg_sig = timeout_qc_voters.powerset().oneOf()
nondet timeout_qc_votes =
timeout_qc_voters.setOfMaps(msgs_signed_timeout.map(sv => sv.vote)).oneOf()
nondet has_high_commit_qc_view = oneOf(Bool)
val timeout_qc: TimeoutQC = {
votes: timeout_qc_votes,
agg_sig: timeout_qc_agg_sig,
high_commit_qc: if (has_high_commit_qc_view) Some(commit_qc) else HIGH_COMMIT_QC_NONE,
ghost_view: timeout_qc_view,
}
// non-deterministically select a block
nondet block = ALL_BLOCKS.oneOf()
nondet block_number = VIEWS.oneOf()
nondet has_block = oneOf(Bool)
// make new proposal
val proposal: Proposal = {
block: if (has_block) Some(block) else None,
justification:
if (justification_case == "commit") {
Commit(commit_qc)
} else {
Timeout(timeout_qc)
},
sig: sig,
ghost_block_number: block_number,
}
all {
// faulty replicas cannot forge the signatures of the correct replicas, enforce that
commit_qc_agg_sig.forall(id => or {
FAULTY.contains(id),
msgs_signed_commit.exists(sv => sv.sig == id and sv.vote == commit_qc_vote),
}),
justification_case != "commit" implies timeout_qc_agg_sig.union(timeout_qc_voters).forall(id => or {
FAULTY.contains(id),
msgs_signed_timeout.exists(sv => sv.sig == id and sv.vote == timeout_qc_votes.get(id)),
}),
msgs_proposal' = msgs_proposal.union(Set(proposal)),
},
},
ghost_step' = FaultyStep,
}
// a step by one of the replicas without timeouts
action replica_step_no_timeout(id: ReplicaId):bool = all{
any {
all {
msgs_signed_commit != Set(),
nondet signed_vote = oneOf(msgs_signed_commit)
on_commit(id, signed_vote),
},
all {
msgs_signed_timeout != Set(),
nondet vote = oneOf(msgs_signed_timeout)
on_timeout(id, vote),
},
all {
msgs_proposal != Set(),
nondet proposal = oneOf(msgs_proposal)
on_proposal(id, proposal),
},
all {
msgs_new_view != Set(),
nondet new_view = oneOf(msgs_new_view)
on_new_view(id, new_view),
}
},
}
// a step by one of the correct replicas
action replica_step(id: ReplicaId):bool = all{
any {
on_timer_is_finished(id),
replica_step_no_timeout(id),
},
}
action on_proposal(id: ReplicaId, proposal: Proposal): bool = all {
val self = replica_state.get(id)
// Get the proposal view.
val proposal_view = proposal.justification.view()
all {
// We only allow proposals for the current view if we have not voted in it yet.
or {
proposal_view == self.view and self.phase == PhasePrepare,
proposal_view > self.view
},
// since our views are bounded, we ignores out of bounds views
VIEWS.contains(proposal_view),
// We ignore proposals from the wrong leader.
proposal.sig == leader.get(proposal_view),
// Check that the proposal is valid.
proposal.proposal_verify(),
// Get the implied block number and hash (if any).
val block_number_and_opt_block_hash = proposal.justification.get_implied_block()
val block_number = block_number_and_opt_block_hash._1
val opt_block_hash = block_number_and_opt_block_hash._2
// Vote only if you have collected all committed blocks so far.
// (See a check in all {...} below.)
// Check if this is a reproposal or not, and do the necessary checks.
// As a side result, get the correct block hash.
val block_hash: BlockHash = match(opt_block_hash) {
| Some(hash) => {
// This is a reproposal. We let the leader repropose blocks without sending
// them in the proposal (it sends only the number + hash). That allows a
// leader to repropose a block without having it stored.
// It is an optimization that allows us to not wait for a leader that has
// the previous proposal stored (which can take 4f views), and to somewhat
// speed up reproposals by skipping block broadcast.
// This only saves time because we have a gossip network running in parallel,
// and any time a replica is able to create a finalized block (by possessing
// both the block and the commit QC) it broadcasts the finalized block (this
// was meant to propagate the block to full nodes, but of course validators
// will end up receiving it as well).
// However, this can be difficult to model and we might want to just
// ignore the gossip network in the formal model. We will still have liveness
// but in the model we'll end up waiting 4f views to get a leader that has the
// previous block before proposing a new one. This is not that bad, since
// then we can be sure that the consensus will continue even if the gossip
// network is failing for some reason.
// See validation below.
hash
}
| None => {
// See state verification below.
unwrap_or(proposal.block, "").hash()
}
}
// the replica vote to send
val vote: CommitVote = {
view: proposal_view,
block_number: block_number,
block_hash: block_hash,
}
all {
// Vote only if you have collected all committed blocks so far (see a check in all {...}).
block_number == length(self.committed_blocks),
// Validation.
if (is_some(opt_block_hash)) {
// For sanity reasons, we'll check that there's no block in the proposal.
// But this check should be completely unnecessary (in theory at least).
is_none(proposal.block)
} else {
all {
// This is a new proposal, so we need to verify it (i.e. execute it).
is_some(proposal.block),
// To verify the block, replica just tries to apply it to the current
// state. Current state is the result of applying all the committed blocks until now.
proposal.block.block_verify(block_number),
// not really part of the protocol but since we are tracking the block number, we have to check it
proposal.ghost_block_number == block_number,
// We cache the new proposals, waiting for them to be committed.
// See the update to cached_proposals below.
}
},
// Update the replica state
val self1 = {
...self,
view: proposal_view,
phase: PhaseCommit,
high_vote: Some(vote),
cached_proposals:
if (is_none(proposal.block)) {
self.cached_proposals
} else {
// the implementation caches the block, we simply mark its number + hash
self.cached_proposals.union(Set((block_number, block_hash)))
}
}
val self2 =
match (proposal.justification) {
| Commit(qc) => self1.process_commit_qc(Some(qc))
| Timeout(qc) => {
...self1.process_commit_qc(qc.high_commit_qc),
high_timeout_qc: max_timeout_qc(Some(qc), self1.high_timeout_qc)
}
}
replica_state' = replica_state.set(id, self2),
// Send the commit vote to all replicas (including ourselves).
msgs_signed_commit' = msgs_signed_commit.union(Set({ vote: vote, sig: sig_of_id(id) })),
// Record the justification.
ghost_justifications' =
ghost_justifications.put((id, proposal.justification.view()), proposal.justification),
// unchanged variables
msgs_signed_timeout' = msgs_signed_timeout, msgs_new_view' = msgs_new_view,
msgs_proposal' = msgs_proposal, store_signed_timeout' = store_signed_timeout,
store_signed_commit' = store_signed_commit,
proposer_view' = proposer_view,
ghost_step' = OnProposalStep(id),
}
}
}
// a replica receives SignedCommitVote
action on_commit(id: ReplicaId, signed_vote: SignedCommitVote): bool = all {
val self = replica_state.get(id)
all {
// if the vote isn't current, just ignore it.
signed_vote.vote.view >= self.view,
// Check that the signed vote is valid.
signed_commit_vote_verify(signed_vote),
// Store the vote. We will never store duplicate (same view and sender) votes.
is_new_signed_commit_vote_for_replica(id, signed_vote),
val new_store = store_signed_commit.get(id).union(Set(signed_vote))
all {
store_signed_commit' = store_signed_commit.set(id, new_store),
// Check if we now have a commit QC for this view.
val qc_opt = get_commit_qc(new_store, signed_vote)
match (qc_opt) {
| None => all {
replica_state' = replica_state,
msgs_new_view' = msgs_new_view,
ghost_justifications' = ghost_justifications,
}
| Some(qc) => {
val self1 = self.process_commit_qc(Some(qc))
start_new_view(id, self1, signed_vote.vote.view + 1)
}
},
msgs_signed_commit' = msgs_signed_commit,
msgs_proposal' = msgs_proposal, msgs_signed_timeout' = msgs_signed_timeout,
store_signed_timeout' = store_signed_timeout,
proposer_view' = proposer_view,
ghost_step' = OnCommitStep(id),
}
}
}
// If this is the first view, we immediately timeout. This will force the replicas
// to synchronize right at the beginning and will provide a justification for the
// proposer at view 1.
// Currently, this is completely non-deterministic, that is,
// this action can be triggered at any step
// TODO: currently, timers are non-deterministic, model them as integers?
// https://github.com/dnkolegov/chonkybft-spec/issues/25
action on_timer_is_finished(id: ReplicaId): bool = {
val self = replica_state.get(id)
val vote: SignedTimeoutVote = {
vote: {
view: self.view,
high_vote: self.high_vote,
high_commit_qc_view:
match (self.high_commit_qc) {
| None => VIEW_NUMBER_NONE
| Some(qc) => Some(qc.vote.view)
}
},
sig: sig_of_id(id),
high_commit_qc: self.high_commit_qc,
}
all {
// If we have already timed out, we don't need to send another timeout vote.
self.phase != PhaseTimeout,
replica_state' = replica_state.set(id, { ...self, phase: PhaseTimeout }),
msgs_signed_timeout' = msgs_signed_timeout.union(Set(vote)),
// unchanged variables
msgs_signed_commit' = msgs_signed_commit, msgs_new_view' = msgs_new_view,
msgs_proposal' = msgs_proposal, store_signed_timeout' = store_signed_timeout,
store_signed_commit' = store_signed_commit,
proposer_view' = proposer_view, ghost_justifications' = ghost_justifications,
ghost_step' = OnTimerIsFinishedStep(id),
}
}
// a replica receives SignedTimeoutVote
action on_timeout(id: ReplicaId, signed_vote: SignedTimeoutVote): bool = all {
val self = replica_state.get(id)
all {
// If the vote isn't current, just ignore it
signed_vote.vote.view >= self.view,
// Check that the signed vote is valid.
signed_vote.signed_timeout_vote_verify(),
// Store the vote. We will never store duplicate (same view and sender) votes.
is_new_signed_timeout_vote_for_replica(id, signed_vote),
val new_store = store_signed_timeout.get(id).union(Set(signed_vote))
all {
store_signed_timeout' = store_signed_timeout.set(id, new_store),
// Check if we now have a timeout QC for this view.
val qc_opt = get_timeout_qc(new_store, signed_vote.vote.view)
match (qc_opt) {
| None => all {
replica_state' = replica_state,
msgs_new_view' = msgs_new_view,
ghost_justifications' = ghost_justifications,
}
| Some(qc) => all {
val self1 = {
...self.process_commit_qc(qc.high_commit_qc),
high_timeout_qc: max_timeout_qc(qc_opt, self.high_timeout_qc)
}
start_new_view(id, self1, signed_vote.vote.view + 1),
}
},
// unchanged variables
msgs_signed_timeout' = msgs_signed_timeout, msgs_signed_commit' = msgs_signed_commit,
msgs_proposal' = msgs_proposal, proposer_view' = proposer_view,
store_signed_commit' = store_signed_commit,
ghost_step' = OnTimeoutStep(id),
}
}
}
// a replica receives NewView
action on_new_view(id: ReplicaId, new_view: NewView): bool = all {
val self = replica_state.get(id)
val new_view_view = new_view.justification.view()
all {
// If the message isn't current, just ignore it.
new_view_view >= self.view,
// Check that the new view is valid.
new_view.new_view_verify(),
// Update our state.
val self1 =
match (new_view.justification) {
| Commit(qc) => self.process_commit_qc(Some(qc))
| Timeout(qc) => {
...self.process_commit_qc(qc.high_commit_qc),
high_timeout_qc: max_timeout_qc(Some(qc), self.high_timeout_qc)
}
}
if (new_view_view > self.view) {
start_new_view(id, self1, new_view_view)
} else {
all {
replica_state' = replica_state,
msgs_new_view' = msgs_new_view,
ghost_justifications' = ghost_justifications,
}
},
// unchanged variables
msgs_signed_timeout' = msgs_signed_timeout, msgs_signed_commit' = msgs_signed_commit,
msgs_proposal' = msgs_proposal, store_signed_timeout' = store_signed_timeout,
store_signed_commit' = store_signed_commit,
proposer_view' = proposer_view,
ghost_step' = OnNewViewStep({ id: id, view: new_view_view }),
}
}
// A step by the proposer for replica_id.
// We pass new_block, which is used a substitute for: self.create_proposal(block_number).
action proposer_step(replica_id: ReplicaId, new_block: Block): bool = all {
val replica = replica_state.get(replica_id)
val replica_state_view = replica.view
val proposer_state_view = proposer_view.get(replica_id)
all {
// Check if we are in a new view and we are the leader.
// Note that by definition, it's impossible to be the leader for view 0.
// We always let view 0 timeout.
// This code only works for a non-faulty proposer.
CORRECT.contains(replica_id),
proposer_state_view < replica_state_view,
sig_of_id(replica_id) == leader.get(replica_state_view),
proposer_view' = proposer_view.set(replica_id, replica_state_view),
// Get the justification for this view. If we have both a commit QC
// and a timeout QC for this view (highly unlikely), we should prefer
// to use the commit QC.
val justification = replica.create_justification()
// Get the block number and check if this must be a reproposal.
val block_and_hash = justification.get_implied_block()
val block_number = block_and_hash._1
val opt_block_hash = block_and_hash._2
// Propose only if you have collected all committed blocks so far: see in all {...}
// Now we create the block.
val block =
match (opt_block_hash) {
| Some(h) =>
// There was some proposal last view that at least SUBQUORUM_WEIGHT replicas
// voted for and could have been finalized. We need to repropose it.
None
| None =>
// The previous proposal was finalized, so we can propose a new block.
// If we don't have the previous block, this call will fail. This is
// fine as we can just not propose anything and let our turn end.
// (see the check below)
Some(new_block)
}
// Create the proposal message and send it to all replicas
val proposal: Proposal = {
block: block,
justification: justification,
sig: sig_of_id(replica_id),
ghost_block_number: block_number,
}
all {
// assert!(justification.view() == cur_view)
justification.view() == replica_state_view,
// Propose only if you have collected all committed blocks so far.
val committed_blocks = replica_state.get(replica_id).committed_blocks
block_number == length(committed_blocks),
// check that we have received the previous block, if we are proposing a new one
or {
is_none(block),
block_number == 0,
replica_state.get(replica_id).cached_proposals.exists(((bn, bh)) => {
block_number == bn + 1
}),
},
// send the proposal
msgs_proposal' = msgs_proposal.union(Set(proposal)),
},
// unchanged variables
replica_state' = replica_state, msgs_signed_timeout' = msgs_signed_timeout,
msgs_signed_commit' = msgs_signed_commit, msgs_new_view' = msgs_new_view,
store_signed_timeout' = store_signed_timeout, ghost_justifications' = ghost_justifications,
store_signed_commit' = store_signed_commit,
ghost_step' = ProposerStep(replica_id),
}
}
// a correct replica changes to a new view
action start_new_view(id: ReplicaId, self: ReplicaState, view: ViewNumber): bool = {
// make sure that we do not go out of bounds with the new view
val new_self = { ...self, view: view, phase: PhasePrepare }
val justification = new_self.create_justification()
all {
VIEWS.contains(view),
// switch to the Prepare phase
replica_state' = replica_state.set(id, new_self),
// Send a new view message to the other replicas for synchronization
msgs_new_view' = msgs_new_view.union(Set({
justification: justification,
sig: sig_of_id(id)
})),
// store the justification
ghost_justifications' = ghost_justifications.put((id, view), justification)
}
}
// fetch a missing block from the peers (executed concurrently with the proposer and replica)
action fetcher_step(id: ReplicaId): bool = {
val self = replica_state.get(id)
match (self.high_commit_qc) {
| None =>
all {
// do nothing
false,
unchanged_replica,
msgs_signed_timeout' = msgs_signed_timeout, msgs_new_view' = msgs_new_view,
msgs_proposal' = msgs_proposal, msgs_signed_commit' = msgs_signed_commit,
ghost_step' = ghost_step,
}
| Some(qc) =>
val next_block_number = self.committed_blocks.length()
all {
qc.vote.block_number >= next_block_number,
// query other replicas whether they have a CommittedBlock
// with the given number, and fetch it from them. The fetched block and
// its commit_qc are verified locally.
any {
// fetch the block from a correct replica
nondet other_id = oneOf(CORRECT)
val other = replica_state.get(other_id)
all {
other.committed_blocks.length() > next_block_number,
val fetched_block = other.committed_blocks[next_block_number]
val new_self = {
...self,
committed_blocks: self.committed_blocks.append(fetched_block)
}
replica_state' = replica_state.set(id, new_self)
},
// fetch the block from a faulty replica
// introduce a commit qc
nondet commit_qc_view = oneOf(VIEWS)
nondet commit_qc_block_number = oneOf(VIEWS)
nondet commit_qc_block = oneOf(ALL_BLOCKS)
nondet commit_qc_agg_sig = REPLICAS.powerset().oneOf()
val commit_qc_vote: CommitVote = {
view: commit_qc_view, block_number: commit_qc_block_number,
block_hash: commit_qc_block
}
val commit_qc = { vote: commit_qc_vote, agg_sig: commit_qc_agg_sig }
val fetched_block: CommittedBlock = { commit_qc: qc, block: commit_qc_block }
// verify the block
all {
FAULTY != Set(),
// faulty replicas cannot forge the signatures of the correct replicas, enforce that
commit_qc_agg_sig.forall(id => or {
FAULTY.contains(id),
msgs_signed_commit.exists(sv => sv.sig == id and sv.vote == commit_qc_vote),
}),
// the replica verifies the received commit_qc
commit_qc_verify(commit_qc),
// store the received block
val new_self = {
...self,
committed_blocks: self.committed_blocks.append(fetched_block)
}
replica_state' = replica_state.set(id, new_self),
}
},
ghost_step' = FetchStep(id),
store_signed_timeout' = store_signed_timeout, store_signed_commit' = store_signed_commit,
proposer_view' = proposer_view, ghost_justifications' = ghost_justifications, replica_view' = replica_view,
msgs_signed_timeout' = msgs_signed_timeout, msgs_new_view' = msgs_new_view,
msgs_proposal' = msgs_proposal, msgs_signed_commit' = msgs_signed_commit,
}
}
}
// part of the state that is not touched by the Byzantine validators
action unchanged_replica = all {
store_signed_timeout' = store_signed_timeout, store_signed_commit' = store_signed_commit,
proposer_view' = proposer_view, ghost_justifications' = ghost_justifications,
replica_view' = replica_view, replica_state' = replica_state,
}
// -------------------------------------------------------------------------
// Auxiliary definitions
def block_verify(block: Option[Block], implied_block_number: BlockNumber): bool = {
// Validation is application-specific.
// For specification purposes, we check validity via ValidBlocks.
match (block) {
| Some(blk) => and {
VALID_BLOCKS.contains(blk),
// The implementation should check that the implied block number
// belongs to the data blob passed in block, as it can interpret the block.
// The specification does not make such a connection.
// The relation between the block numbers and committed blocks
// is checked in on_proposal.
implied_block_number >= 0,
}
| None => false
}
}
// Processed an (already verified) commit_qc received from the network
// as part of some message. It bumps the local high_commit_qc and if
// we have the proposal corresponding to this qc, we append it to the committed_blocks.
def process_commit_qc(self: ReplicaState, qc_opt: Option[CommitQC]): ReplicaState = {
match (qc_opt) {
| None => self
| Some(qc) =>
if (self.cached_proposals.contains((qc.vote.block_number, qc.vote.block_hash))
and self.committed_blocks.length() == qc.vote.block_number) {
// NOTE: the implementation retrieves the block by its hash from cached_proposals.
// In the Quint spec, the hash and the block are identical, so we simply put the hash.
val block = qc.vote.block_hash
{
...self,
committed_blocks: self.committed_blocks.append({ block: block, commit_qc: qc }),
high_commit_qc: max_commit_qc(Some(qc), self.high_commit_qc)
}
} else {
{
...self,
high_commit_qc: max_commit_qc(Some(qc), self.high_commit_qc)
}
}
}
}
// given two QCs, return the one with the largest view,
// or the left one, in case of the same view
def max_timeout_qc(qc1_opt: Option[TimeoutQC], qc2_opt: Option[TimeoutQC]): Option[TimeoutQC] = {
match (qc2_opt) {
| None => qc1_opt
| Some(qc2) =>
match (qc1_opt) {
| None => qc2_opt
| Some(qc1) =>
if (qc1.ghost_view >= qc2.ghost_view) qc1_opt else qc2_opt
}
}
}
// given two QCs, return the one with the largest view,
// or the left one, in case of the same view
def max_commit_qc(qc1_opt: Option[CommitQC], qc2_opt: Option[CommitQC]): Option[CommitQC] = {
match (qc2_opt) {
| None => qc1_opt
| Some(qc2) =>
match(qc1_opt) {
| None => qc2_opt
| Some(qc1) =>
if (qc1.vote.view >= qc2.vote.view) qc1_opt else qc2_opt
}
}
}
// If there is a quorum among timeout votes, return `Some(qc)`,
// where qc is the largest quorum among the votes. Otherwise, return None.
def get_timeout_qc(votes: Set[SignedTimeoutVote], view: ViewNumber): Option[TimeoutQC] = {
val view_signed_votes = votes.filter(v => v.vote.view == view)
if (view_signed_votes.size() < QUORUM_WEIGHT) {
None
} else {
val high_commit_qc: Option[CommitQC] = view_signed_votes.fold(HIGH_COMMIT_QC_NONE,
(prev_max_qc, sv) => max_commit_qc(prev_max_qc, sv.high_commit_qc))
// Importantly, view_signed_votes does not have two votes with the same signature.
// We check that in store_signed_timeout_all_inv.
val qc_votes: ReplicaKey -> TimeoutVote =
view_signed_votes.map(v => (v.sig, v.vote)).setToMap()
Some({
votes: qc_votes,
agg_sig: view_signed_votes.map(v => v.sig),
high_commit_qc: high_commit_qc,
ghost_view: view,
})
}
}