-
Notifications
You must be signed in to change notification settings - Fork 2
/
LibraBFT2.txt
208 lines (168 loc) · 5.88 KB
/
LibraBFT2.txt
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
type BlockId
type Block = struct {
parent: BlockId,
round: uint,
}
function Hash(Block): BlockId
const blocks: [BlockId]Block
axiom forall id: BlockId. Hash(blocks[id]) == id
const h: uint // number of honest replicas
const f: uint // number of faulty replicas
axiom h > 2f
const root: BlockId // root of block tree
axiom blocks[root].round == 0 && blocks[root].parent == root
type HonestReplicaId = 1..h // ids of honest replicas
type HonestQuorum = {q: Set<HonestReplicaId> | size(q) >= h - f} // all possible quorums of honest nodes
// primitive function that returns true iff id' can reach id by following parent link 0 or more times
function Reaches(id': BlockId, id: BlockId): bool;
function Consistent(id': BlockId, id: BlockId) : bool {
Reaches(id', id) || Reaches(id, id')
}
//// global model variables
// all collected votes sent by honest replicas
var voteStore: [BlockId]Set<HonestReplicaId>
// id of the latest globally-committed block
var globallyCommitted: BlockId
//// per-replica variables
// all blocks on which quorum has been established
var qcStore: [HonestReplicaId]Set<BlockId>
// monotonically increasing round of the last voted block
var lastVoteRound: [HonestReplicaId]uint
// monotonically increasing round of the preferred block
var preferredBlockRound: [HonestReplicaId]uint
//// procedures
procedure Initialize()
{
voteStore := (lambda id: BlockId. if id = root then HonestReplicaId else {})
globallyCommitted := root
qcStore := (lambda r: HonestReplicaId. { root })
lastVoteRound := (lambda r: HonestReplicaId. 0)
preferredBlockRound := (lambda r: HonestReplicaId. 0)
}
//// start things off
procedure Main() {
var r: HonestReplicaId
var blockId: BlockId
// propose or commit nondeterministically
if * {
async OnProposal(r, blockId)
}
else if * {
async OnQuorumCertificate(r, blockId)
}
else {
async OnCommit(r, blockId)
}
// keep going nondeterministically
if * {
async Main()
}
}
//// helper procedure to add quorum certificate
procedure TryAddQuorumCertificate(r: HonestReplicaId, blockId: BlockId) : bool
reads qcStore, voteStore, preferredBlockRound
writes qcStore, preferredBlockRound
{
var numHonestVoters: uint
var numEquivocators: uint
var b'', b': Block
// numHonestVoters is the number of honest votes seen by this replica
assume numHonestVoters <= size(voteStore[blockId])
// numEquivocators is the number of faulty replicas who voted for blockId
assume numEquivocators <= f
if numHonestVoters + numEquivocators >= h {
qcStore[r] := qcStore[r] + blockId
}
// possibly update preferred block
if blockId in qcStore[r] {
b'' := blocks[blockId]
b' := blocks[b''.parent]
if b'.round > preferredBlockRound[r] {
preferredBlockRound[r] := b'.round
}
}
blockId in qcStore[r]
}
//// possibly vote
procedure TryVote(r: HonestReplicaId, newBlockId: BlockId)
reads voteStore, lastVoteRound, preferredBlockRound
writes voteStore, lastVoteRound
{
var newBlock, b'': Block
newBlock := blocks[newBlockId]
b'' := blocks[newBlock.parent]
if newBlock.round > lastVoteRound[r] && b''.round >= preferredBlockRound[r] {
lastVoteRound[r] := newBlock.round
voteStore[newBlockId] := voteStore[newBlockId] + r
}
}
//// event handler at a replica to process a proposal
procedure OnProposal(r: HonestReplicaId, newBlockId: BlockId)
reads qcStore, preferredBlockRound
writes qcStore, preferredBlockRound
{
var newBlock: Block
newBlock := blocks[newBlockId]
if !TryAddQuorumCertificate(r, newBlock.parent) {
return
}
async TryVote(r, newBlockId)
}
//// event handler at a replica to add a quorum certificate
procedure OnQuorumCertificate(r: HonestReplicaId, blockId: BlockId)
reads qcStore, voteStore, preferredBlockRound
writes qcStore, preferredBlockRound
{
TryAddQuorumCertificate(r, blockId)
}
//// event handler at a replica to commit a block
procedure OnCommit(r: HonestReplicaId, id'': BlockId)
reads qcStore, globallyCommitted
writes globallyCommitted
{
var id', id: BlockId
var b'', b', b: Block
if !(id'' in qcStore[r]) {
return
}
b'' := blocks[id'']
id' := b''.parent
b' := blocks[id']
id := b'.parent
b := blocks[id]
if b''.round == b'.round + 1 && b'.round == b.round + 1 {
assert Consistent(id, globallyCommitted)
if Reaches(id, globallyCommitted) {
globallyCommitted := id
}
}
}
//// Invariants
function OneChain(id: BlockId, id': BlockId): bool
{
id = blocks[id'].parent
}
function ContiguousOneChain(id: BlockId, id': BlockId): bool
{
id = blocks[id'].parent && blocks[id].round + 1 == blocks[id'].round
}
function TwoChain(id: BlockId, id': BlockId, id'': BlockId) : bool
{
OneChain(id, id') && OneChain(id', id'')
}
function ContiguousTwoChain(id: BlockId, id': BlockId, id'': BlockId) : bool
{
ContiguousOneChain(id, id') && ContiguousOneChain(id', id'')
}
forall r in HonestReplicaId. forall id, id' in qcStore[r].
OneChain(id, id') ==> (id' == root || blocks[id].round < blocks[id'].round)
forall r in HonestReplicaId. forall id, id' in qcStore[r].
blocks[id].round == blocks[id'].round ==> id == id'
forall r in HonestReplicaId. forall id, id', id'' in qcStore[r].
TwoChain(id, id', id'') ==>
(exists q: HonestQuorum. (forall x: HonestReplicaId. x in q ==> preferredBlockRound[x] >= blocks[id].round))
forall r in HonestReplicaId. forall id, id', id'' in qcStore[r].
ContiguousTwoChain(id, id', id'') ==>
(forall x in qcStore[r]. blocks[x].round > blocks[id''].round ==> Reaches(x, id))
forall rx, ry in HonestReplicaId. forall xid, xid', xid'' in qcStore[rx]. forall yid, yid', yid'' in qcStore[ry].
ContiguousTwoChain(xid, xid', xid'') && ContiguousTwoChain(yid, yid', yid'') ==> Consistent(xid, yid)