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
package consensus
import (
"Walnut-HS/pkg/network"
"Walnut-HS/pkg/protobuf"
"bytes"
"context"
"encoding/json"
"fmt"
"runtime"
"sort"
"sync"
"time"
)
var MAXMESSAGE = 2048
var BATCHSIZE = 4194 // each block contains BATCHSIZE regular commands
// 4194 (1M);
// 8388 (2M);
type TimeStatistic struct {
View uint32
StartTime time.Time
EndTime time.Time
IsCommittee bool
}
var ViewChangedList []uint32
var PAYLOADSIZE = 250 // 250 bytes per request
var VIEWS = uint32(100) // The number of views the experiment run in total
// Init some parameters and handle catchup messages
func (p *HonestParty) Init(ctx context.Context) {
// Initialize message sets
p.nxtcom = make([]*protobuf.Vrf, 0, p.lambda+100)
p.newVrfView = make([]*protobuf.NewVrfView, 0, p.QCsize)
p.newView = make([]*protobuf.NewView, 0, p.QCsize)
p.prepareVoteMsg = make([]*protobuf.PrepareVote, 0, p.QCsize)
p.precommitVoteMsg = make([]*protobuf.PrecommitVote, 0, p.QCsize)
p.commitVoteMsg = make([]*protobuf.CommitVote, 0, p.QCsize)
p.sttranMsg = make([]*protobuf.StateTransfer, 0, p.QCsize)
// handle catchup messages
go p.MonitorCatchup(ctx)
// Update vrf view and send vrf messages
go p.MonitorNewVrfView(ctx)
}
func (p *HonestParty) MonitorNewVrfView(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case newvrfviewMsg := <-p.GetMessage("NewVrfView", p.ViewVrf):
newvrfview := network.Decapsulation("NewVrfView", newvrfviewMsg).(*protobuf.NewVrfView)
p.OnReceiveNewVrfView(newvrfview)
}
}
}
func (p *HonestParty) MonitorCatchup(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case catchupMsg := <-p.GetMessage("Catchup", catchuint32):
catchup := network.Decapsulation("Catchup", catchupMsg).(*protobuf.Catchup)
p.OnReceiveCatchup(catchup)
}
}
}
func (p *HonestParty) WaitBlock() {
proposalMsg := <-p.GetMessage("Block", p.ViewSt)
proposal := network.Decapsulation("Block", proposalMsg).(*protobuf.Block)
SaveBlock(p.Blockstore, proposal)
}
// Run the consensus
func (p *HonestParty) Run(timeout int, startTime time.Time) {
latencyTimeStart := make([]time.Time, VIEWS)
latency := make([]int64, VIEWS)
latencyStTimeStart := make([]time.Time, VIEWS)
latencySt := make([]int64, VIEWS)
statisticMap := make(map[uint32]*TimeStatistic, 0)
var mapMutex sync.RWMutex
for p.View < VIEWS && p.ViewSt < VIEWS {
p.sttranMsg = p.sttranMsg[:0]
if p.CurComBool[p.PID] {
SaveView(p.Blockstore, p.View)
// Initialize message sets
p.nxtcom = p.nxtcom[:0]
p.newView = p.newView[:0]
p.prepareVoteMsg = p.prepareVoteMsg[:0]
p.precommitVoteMsg = p.precommitVoteMsg[:0]
p.commitVoteMsg = p.commitVoteMsg[:0]
// Set time counter
to := p.NewTimeout()
timer := time.Duration(timeout) * time.Second * time.Duration(to)
ctx := context.Background()
ctxWithTimeout, cancel := context.WithTimeout(ctx, timer)
defer cancel()
// Latency
latencyTimeStart[p.View] = time.Now()
mapMutex.Lock()
statisticMap[p.View] = &TimeStatistic{
View: p.View,
StartTime: time.Now(),
EndTime: time.Now(),
IsCommittee: true,
}
mapMutex.Unlock()
// repeated timeout
if to >= 1<<4 {
v := p.bcommitrefer.View
c := p.Com
p.SendCatchup()
p.WaitHistory(v, c, ctxWithTimeout, cancel)
}
// Can be put in the beginning of each view
p.SendNewVrfView()
p.SendNewView(p.GetLeader(p.View))
// Monitor vrf
go p.WaitVrf(ctxWithTimeout, p.View)
// Wait to trigger committee rotation
if p.View == 20 {
time.Sleep(200 * time.Millisecond)
}
// run eventloop to handle different type of messages in consensus
if p.PID != p.GetLeader(p.View) {
proposalreceived := p.WaitProposal(ctxWithTimeout)
if proposalreceived {
p.EventLoop(ctxWithTimeout, cancel)
}
} else {
p.EventLoop(ctxWithTimeout, cancel)
}
latency[p.bcommitrefer.View] = time.Since(latencyTimeStart[p.bcommitrefer.View]).Milliseconds()
mapMutex.Lock()
statisticMap[p.View].EndTime = time.Now()
mapMutex.Unlock()
p.View += 1
} else {
// Latency
latencyStTimeStart[p.ViewSt] = time.Now()
mapMutex.Lock()
statisticMap[p.View] = &TimeStatistic{
View: p.View,
StartTime: time.Now(),
EndTime: time.Now(),
IsCommittee: false,
}
mapMutex.Unlock()
p.WaitBlock()
p.WaitStateTransfer()
if statisticMap[p.bcommitrefer.View] != nil {
mapMutex.Lock()
statisticMap[p.bcommitrefer.View].EndTime = time.Now()
mapMutex.Unlock()
}
latencySt[p.bcommitrefer.View] = time.Since(latencyStTimeStart[p.bcommitrefer.View]).Milliseconds()
}
runtime.GC()
}
fmt.Println("PID: ", p.PID, " Latency: ")
latencyBytes, _ := json.Marshal(latency)
fmt.Println(string(latencyBytes))
latencyStBytes, _ := json.Marshal(latencySt)
fmt.Println(string(latencyStBytes))
fmt.Println("Committee Changed At ", ViewChangedList)
fmt.Println("Consensus used time : ", time.Since(startTime))
time.Sleep(3 * time.Second)
}
func (p *HonestParty) WaitStateTransfer() {
for {
sttranMsg := <-p.GetMessage("StateTransfer", p.ViewSt)
if len(p.sttranMsg) < int(p.QCsize) {
sttran := network.Decapsulation("StateTransfer", sttranMsg).(*protobuf.StateTransfer)
p.OnReceiveSt(sttran)
if len(p.sttranMsg) >= int(p.QCsize) {
return
}
}
}
}
// Wait for History messages
func (p *HonestParty) WaitHistory(view uint32, com uint32, ctx context.Context, cancel context.CancelFunc) {
for {
select {
case <-ctx.Done():
return
case historyMsg := <-p.GetMessage("History", view):
if historyMsg.Com == com {
history := network.Decapsulation("History", historyMsg).(*protobuf.History)
p.OnReceiveHist(history)
cancel()
return
}
}
}
}
// if receive a valid proposal return true
func (p *HonestParty) WaitProposal(ctx context.Context) bool {
select {
case <-ctx.Done():
return false
case proposalMsg := <-p.GetMessage("Proposal", p.View):
if proposalMsg.Sender == p.GetLeader(p.View) && proposalMsg.Com == p.Com {
proposal := network.Decapsulation("Proposal", proposalMsg).(*protobuf.Proposal)
p.OnReceiveProposal(proposal)
return true
}
return false
}
}
func (p *HonestParty) WaitVrf(ctx context.Context, view uint32) {
for {
select {
case <-ctx.Done():
return
case vrfMsg := <-p.GetMessage("Vrf", view):
if vrfMsg.Com == p.Com {
vrf := network.Decapsulation("Vrf", vrfMsg).(*protobuf.Vrf)
p.OnReceiveVrf(vrf)
}
}
}
}
// Handle the messages received
func (p *HonestParty) EventLoop(ctx context.Context, cancel context.CancelFunc) {
for {
select {
case <-ctx.Done():
return
case newviewMsg := <-p.GetMessage("NewView", p.View-1):
if p.PID == p.GetLeader(p.View) && newviewMsg.Com == p.Com && len(p.newView) < int(p.QCsize) {
newview := network.Decapsulation("NewView", newviewMsg).(*protobuf.NewView)
p.OnReceiveNewView(newview)
}
case preparevoteMsg := <-p.GetMessage("PrepareVote", p.View):
if len(p.prepareVoteMsg) < int(p.QCsize) && p.CurComBool[preparevoteMsg.Sender] && preparevoteMsg.Com == p.Com && p.PID == p.GetLeader(p.View) {
preparevote := network.Decapsulation("PrepareVote", preparevoteMsg).(*protobuf.PrepareVote)
p.OnReceivePrepareVote(preparevote)
}
case precommitvoteMsg := <-p.GetMessage("PrecommitVote", p.View):
if len(p.precommitVoteMsg) < int(p.QCsize) && p.CurComBool[precommitvoteMsg.Sender] && precommitvoteMsg.Com == p.Com && p.PID == p.GetLeader(p.View) {
precommitvote := network.Decapsulation("PrecommitVote", precommitvoteMsg).(*protobuf.PrecommitVote)
p.OnReceivePrecommitVote(precommitvote)
}
case commitvoteMsg := <-p.GetMessage("CommitVote", p.View):
if len(p.commitVoteMsg) < int(p.QCsize) && p.CurComBool[commitvoteMsg.Sender] && commitvoteMsg.Com == p.Com && p.PID == p.GetLeader(p.View) {
commitvote := network.Decapsulation("CommitVote", commitvoteMsg).(*protobuf.CommitVote)
p.OnReceiveCommitVote(commitvote)
}
if len(p.commitVoteMsg) >= int(p.QCsize) {
cancel()
return
}
case prepareQCMsg := <-p.GetMessage("PrepareQCSend", p.View):
if prepareQCMsg.Com == p.Com && prepareQCMsg.Sender == p.GetLeader(p.View) {
prepareqcsend := network.Decapsulation("PrepareQCSend", prepareQCMsg).(*protobuf.PrepareQCSend)
p.OnReceivePrepareQCSend(prepareqcsend)
}
case precommitQCMsg := <-p.GetMessage("PrecommitQCSend", p.View):
if precommitQCMsg.Com == p.Com && precommitQCMsg.Sender == p.GetLeader(p.View) {
precommitqcsend := network.Decapsulation("PrecommitQCSend", precommitQCMsg).(*protobuf.PrecommitQCSend)
p.OnReceivePrecommitQCSend(precommitqcsend)
}
case commitQCMsg := <-p.GetMessage("CommitQCSend", p.View):
if p.PID != p.GetLeader(p.View) && commitQCMsg.Com == p.Com && commitQCMsg.Sender == p.GetLeader(p.View) {
commitqcsend := network.Decapsulation("CommitQCSend", commitQCMsg).(*protobuf.CommitQCSend)
p.OnReceiveCommitQCSend(commitqcsend)
cancel()
return
}
}
}
}
func (p *HonestParty) PrintNxtcom() {
sort.Slice(p.nxtcom, func(i, j int) bool {
if bytes.Compare(p.nxtcom[i].Rho, p.nxtcom[j].Rho) < 0 {
return true
} else {
return false
}
})
Ids := make([]uint32, 0, len(p.nxtcom))
Rhos := make([]byte, 0, len(p.nxtcom))
Views := make([]uint32, 0, len(p.nxtcom))
for _, vrf := range p.nxtcom {
Ids = append(Ids, vrf.Id)
Rhos = append(Rhos, vrf.Rho[0])
Views = append(Views, vrf.View)
}
fmt.Println("PID: ", p.PID, ", Ids: ", Ids, ", Rhos: ", Rhos, ", Views: ", Views)
}