-
Notifications
You must be signed in to change notification settings - Fork 0
/
MC_ffg_examples.tla
235 lines (212 loc) · 11.5 KB
/
MC_ffg_examples.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
----------------------------- MODULE MC_ffg_examples -----------------------------
(*
* Fixed chains and falsy invariants to check reachability of certain states.
*
* Thomas Pani, 2024.
*
* Subject to Apache 2.0. See `LICENSE.md`.
*)
EXTENDS MC_ffg
Init_SingleChain ==
LET
config == Gen(1)
id == Gen(1)
current_slot == MAX_SLOT
view_blocks == SetAsFun({
\* | 0 | 1 | 2 | 3 | 4 |
\* genesis <- BLOCK1 <- BLOCK2 <- BLOCK3 <- BLOCK4
<<"genesis", GenesisBlock>>,
<<"BLOCK1", [ parent_hash |-> "genesis", slot |-> 1, votes |-> {}, body |-> "BLOCK1" ] >>,
<<"BLOCK2", [ parent_hash |-> "BLOCK1", slot |-> 2, votes |-> {}, body |-> "BLOCK2" ] >>,
<<"BLOCK3", [ parent_hash |-> "BLOCK2", slot |-> 3, votes |-> {}, body |-> "BLOCK3" ] >>,
<<"BLOCK4", [ parent_hash |-> "BLOCK3", slot |-> 4, votes |-> {}, body |-> "BLOCK4" ] >>
})
view_votes == Gen(MAX_VOTES)
chava == Gen(1)
IN
/\ single_node_state = [ configuration |-> config, identity |-> id, current_slot |-> current_slot, view_blocks |-> view_blocks, view_votes |-> view_votes, chava |-> chava ]
/\ Precompute
/\ IsValidNodeState(single_node_state)
\* Precompute for the single chain setup is correct
Inv_SingleChain_PrecomputeOK ==
LET
b(hash) == single_node_state.view_blocks[hash]
bs(hashes) == { single_node_state.view_blocks[hash] : hash \in hashes }
IN
/\ PRECOMPUTED__IS_ANCESTOR_DESCENDANT_RELATIONSHIP = SetAsFun({
<<b("genesis"), bs({ "genesis" })>>,
<<b("BLOCK1"), bs({ "genesis", "BLOCK1" })>>,
<<b("BLOCK2"), bs({ "genesis", "BLOCK1", "BLOCK2" })>>,
<<b("BLOCK3"), bs({ "genesis", "BLOCK1", "BLOCK2", "BLOCK3" })>>,
<<b("BLOCK4"), bs({ "genesis", "BLOCK1", "BLOCK2", "BLOCK3", "BLOCK4" })>> })
Init_Forest ==
LET
config == Gen(1)
id == Gen(1)
current_slot == MAX_SLOT
view_blocks == SetAsFun({
\* | 0 | 1 | 2 | 3 | 4 |
\* genesis <- BLOCK1 <- BLOCK2 <- BLOCK3 <- BLOCK4
\* ^--- BLOCK5 <- BLOCK6 <- BLOCK7
\*
\* (... more blocks below)
<<"genesis", GenesisBlock>>,
<<"BLOCK1", [ parent_hash |-> "genesis", slot |-> 1, votes |-> {}, body |-> "BLOCK1" ] >>,
<<"BLOCK2", [ parent_hash |-> "BLOCK1", slot |-> 2, votes |-> {}, body |-> "BLOCK2" ] >>,
<<"BLOCK3", [ parent_hash |-> "BLOCK2", slot |-> 3, votes |-> {}, body |-> "BLOCK3" ] >>,
<<"BLOCK4", [ parent_hash |-> "BLOCK3", slot |-> 4, votes |-> {}, body |-> "BLOCK4" ] >>,
<<"BLOCK5", [ parent_hash |-> "genesis", slot |-> 1, votes |-> {}, body |-> "BLOCK5" ] >>,
<<"BLOCK6", [ parent_hash |-> "BLOCK5", slot |-> 2, votes |-> {}, body |-> "BLOCK6" ] >>,
<<"BLOCK7", [ parent_hash |-> "BLOCK6", slot |-> 3, votes |-> {}, body |-> "BLOCK7" ] >>,
\*
\* | 0 | 1 | 2 | 3 | 4 |
\* BLOCK8
<<"BLOCK8", [ parent_hash |-> "not_here", slot |-> 1, votes |-> {}, body |-> "BLOCK8" ] >>,
\*
\* | 0 | 1 | 2 | 3 | 4 |
\* BLOCK9 <- BLOCK10
<<"BLOCK9", [ parent_hash |-> "not_here", slot |-> 1, votes |-> {}, body |-> "BLOCK9" ] >>,
<<"BLOCK10", [ parent_hash |-> "BLOCK9", slot |-> 2, votes |-> {}, body |-> "BLOCK10" ] >>
})
view_votes == Gen(MAX_VOTES)
chava == Gen(1)
IN
/\ single_node_state = [ configuration |-> config, identity |-> id, current_slot |-> current_slot, view_blocks |-> view_blocks, view_votes |-> view_votes, chava |-> chava ]
/\ Precompute
/\ IsValidNodeState(single_node_state)
\* Precompute for the forest chain setup is correct
Inv_Forest_PrecomputeOK ==
LET
b(hash) == single_node_state.view_blocks[hash]
bs(hashes) == { single_node_state.view_blocks[hash] : hash \in hashes }
IN
/\ PRECOMPUTED__IS_ANCESTOR_DESCENDANT_RELATIONSHIP = SetAsFun({
<<b("genesis"), bs({ "genesis" })>>,
<<b("BLOCK1"), bs({ "genesis", "BLOCK1" })>>,
<<b("BLOCK2"), bs({ "genesis", "BLOCK1", "BLOCK2" })>>,
<<b("BLOCK3"), bs({ "genesis", "BLOCK1", "BLOCK2", "BLOCK3" })>>,
<<b("BLOCK4"), bs({ "genesis", "BLOCK1", "BLOCK2", "BLOCK3", "BLOCK4" })>>,
<<b("BLOCK5"), bs({ "genesis", "BLOCK5" })>>,
<<b("BLOCK6"), bs({ "genesis", "BLOCK5", "BLOCK6" })>>,
<<b("BLOCK7"), bs({ "genesis", "BLOCK5", "BLOCK6", "BLOCK7" })>>,
<<b("BLOCK8"), bs({ "BLOCK8" })>>,
<<b("BLOCK9"), bs({ "BLOCK9" })>>,
<<b("BLOCK10"), bs({ "BLOCK9", "BLOCK10" })>> })
Init_ShortFork ==
LET
config == Gen(1)
id == Gen(1)
current_slot == MAX_SLOT
view_blocks == SetAsFun({
\* | 0 | 1 |
\* genesis <- BLOCK1
\* ^---.
\* BLOCK2
<<"genesis", GenesisBlock>>,
<<"BLOCK1", [ parent_hash |-> "genesis", slot |-> 1, votes |-> {}, body |-> "BLOCK1" ] >>,
<<"BLOCK2", [ parent_hash |-> "genesis", slot |-> 1, votes |-> {}, body |-> "BLOCK2" ] >>
})
view_votes == Gen(MAX_VOTES)
chava == Gen(1)
IN
/\ single_node_state = [ configuration |-> config, identity |-> id, current_slot |-> current_slot, view_blocks |-> view_blocks, view_votes |-> view_votes, chava |-> chava ]
/\ Precompute
/\ IsValidNodeState(single_node_state)
\* ============================================================================
\* Falsy invariants to check reachability of certain states.
\* ============================================================================
\* Find a complete chain BLOCK1 ->* genesis
CompleteChain_Example ==
LET
block == single_node_state.view_blocks["BLOCK1"]
setup == "BLOCK1" \in DOMAIN single_node_state.view_blocks
IN setup => ~is_complete_chain(block, single_node_state)
\* Find a complete chain BLOCK1 ->* genesis, joining all blocks
CompleteChainLong_Example ==
LET
block == single_node_state.view_blocks["BLOCK1"]
setup == "BLOCK1" \in DOMAIN single_node_state.view_blocks /\ is_complete_chain(block, single_node_state)
IN setup => Size(get_blockchain(block, single_node_state)) < MAX_SLOT+1
\* Find a chain such that `ancestor` and `descendant` are in an ancestor-descendant relationship, with at least 1 other block inbetween
AncestorDescendant_Example ==
LET
ancestor == single_node_state.view_blocks["BLOCK1"]
descendant == single_node_state.view_blocks["BLOCK2"]
setup == /\ "BLOCK1" \in DOMAIN single_node_state.view_blocks
/\ "BLOCK2" \in DOMAIN single_node_state.view_blocks
/\ descendant.parent_hash /= "BLOCK1"
IN setup => ~is_ancestor_descendant_relationship(ancestor, descendant, single_node_state)
\* Find a common ancestor of 2 blocks, which are not in an ancestor-descendant relationship (i.e., they are conflicting), that is not their immediate parent
CommonAncestor_Example ==
LET
block1 == single_node_state.view_blocks["BLOCK1"]
block2 == single_node_state.view_blocks["BLOCK2"]
setup ==
/\ "BLOCK1" \in DOMAIN single_node_state.view_blocks
/\ "BLOCK2" \in DOMAIN single_node_state.view_blocks
/\ ~is_ancestor_descendant_relationship(block1, block2, single_node_state)
/\ ~is_ancestor_descendant_relationship(block2, block1, single_node_state)
/\ block1.parent_hash /= block2.parent_hash
IN setup => ~have_common_ancestor(block1, block2, single_node_state)
\* Find a chain where two blocks are conflicting, even though they have a common ancestor (i.e., there is a fork in the chain)
Conflicting_Example ==
LET
block1 == single_node_state.view_blocks["BLOCK1"]
block2 == single_node_state.view_blocks["BLOCK2"]
setup ==
/\ "BLOCK1" \in DOMAIN single_node_state.view_blocks
/\ "BLOCK2" \in DOMAIN single_node_state.view_blocks
/\ have_common_ancestor(block1, block2, single_node_state)
IN setup => ~are_conflicting(block1, block2, single_node_state)
\* Find a slashable node (i.e., an equivocating or surround-voting node)
SlashableNode_Example == get_slashable_nodes(single_node_state.view_votes) = {}
\* Find a (slashable) node that was not surround-voting (i.e., it was equivocating)
Equivocation_Example ==
LET no_surrounding_votes ==
\forall vote1, vote2 \in single_node_state.view_votes :
/\ ~does_first_vote_surround_second_vote(vote1, vote2)
/\ ~does_first_vote_surround_second_vote(vote2, vote1)
IN no_surrounding_votes => get_slashable_nodes(single_node_state.view_votes) = {}
\* Find a (slashable) node that was not equivocating (i.e., it was surround-voting)
SurroundVoting_Example ==
LET no_equivocating_votes ==
\forall vote1, vote2 \in single_node_state.view_votes : ~are_equivocating_votes(vote1, vote2)
IN no_equivocating_votes => get_slashable_nodes(single_node_state.view_votes) = {}
\* Find a validator linking to a checkpoint in the next slot (i.e., one that cast a valid FFG vote for checkpoint slots 3->4)
ValidatorsLinkingNextSlot_Example ==
LET source_checkpoint == [ block_hash |-> "BLOCK1", block_slot |-> 1, chkp_slot |-> 2 ]
IN get_validators_in_FFG_votes_linking_to_a_checkpoint_in_next_slot(source_checkpoint, single_node_state) = {}
\* Find at least 3 justifying votes for checkpoint (assuming their sources are justified)
VotesInSupport_Example ==
LET
checkpoint == [ block_hash |-> "BLOCK1", block_slot |-> 1, chkp_slot |-> 2 ]
setup == "BLOCK1" \in DOMAIN single_node_state.view_blocks
IN setup => Cardinality(VotesInSupportAssumingJustifiedSource(checkpoint, single_node_state)) < 3
\* Find justifying votes for a checkpoint
JustifyingVotes_Example ==
LET
checkpoint == [ block_hash |-> "BLOCK1", block_slot |-> 1, chkp_slot |-> 2 ]
setup == "BLOCK1" \in DOMAIN single_node_state.view_blocks
IN setup => ~is_justified_checkpoint(checkpoint, single_node_state)
\* Find finalizing (and justifying) votes for a checkpoint
FinalizingVotes_Example ==
LET
checkpoint == [ block_hash |-> "BLOCK1", block_slot |-> 1, chkp_slot |-> 2 ]
setup == "BLOCK1" \in DOMAIN single_node_state.view_blocks
IN setup => ~is_finalized_checkpoint(checkpoint, single_node_state)
\* Find a finalized checkpoint (in addition to the genesis checkpoint)
FinalizedCheckpoint_Example ==
get_finalized_checkpoints(single_node_state) = { genesis_checkpoint(single_node_state) }
\* Find at least two finalized blocks (in addition to the genesis block)
FinalizedBlocks_Example ==
LET
finalized_checkpoints == get_finalized_checkpoints(single_node_state)
finalized_blocks == { get_block_from_hash(checkpoint.block_hash, single_node_state) : checkpoint \in finalized_checkpoints }
IN Cardinality(finalized_blocks) < 3
\* Find at least two conflicting finalized blocks
Finalized_And_Conflicting_Blocks_Example ==
LET
finalized_checkpoints == get_finalized_checkpoints(single_node_state)
finalized_blocks == { get_block_from_hash(checkpoint.block_hash, single_node_state) : checkpoint \in finalized_checkpoints }
IN \A block1, block2 \in finalized_blocks : ~are_conflicting(block1, block2, single_node_state)
=============================================================================