/
node.go
498 lines (462 loc) · 12.9 KB
/
node.go
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
package main
import(
"encoding/json"
"encoding/hex"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"path/filepath"
"sync"
"time"
)
//global variables
var localMessagePool = []Message{}
var viewID = 0
var nodeDelay = 200 * time.Millisecond
type Node struct{
nodeID string
addr string
pubKey []byte
privKey []byte
sequenceID int
view int
msgQueue chan []byte
mutex sync.Mutex
nodeTable map[string]string
requestPool map[string]*RequestMsg
prepareConfirmCount map[string]map[string]bool
commitConfirmCount map[string]map[string]bool
isCommitBroadcast map[string]bool
isReply map[string]bool
msgLog *MsgLog
primary string
totalPrimaryTable map[string]string
broadcastAddr string
block int
vicePrimary string
}
type MsgLog struct{
preprepareLog map[string]map[string]bool
prepareLog map[string]map[string]bool
commitLog map[string]map[string]bool
replyLog map[string]bool
}
func newNode(nodeID string, addr string, nodeTable map[string]string, totalPrimaryTable map[string]string)*Node{
n := new(Node)
n.nodeID = nodeID
n.addr = addr
n.pubKey = getPubKey(nodeID)
n.privKey = getPrivKey(nodeID)
n.sequenceID = 0
n.view = viewID
n.msgQueue = make(chan []byte)
n.mutex = sync.Mutex{}
n.nodeTable = nodeTable
n.requestPool = make(map[string]*RequestMsg)
n.prepareConfirmCount = make(map[string]map[string]bool)
n.commitConfirmCount = make(map[string]map[string]bool)
n.isCommitBroadcast = make(map[string]bool)
n.isReply = make(map[string]bool)
n.msgLog = &MsgLog{
make(map[string]map[string]bool),
make(map[string]map[string]bool),
make(map[string]map[string]bool),
make(map[string]bool),
}
n.primary = ""
n.totalPrimaryTable = totalPrimaryTable
n.broadcastAddr = ""
n.block = 0
n.vicePrimary = ""
return n
}
func (n *Node) Initiate(){
hierarchy, primary, broadcastAddr := formLayer(n.nodeTable, n.nodeID, n.totalPrimaryTable)
fmt.Println("results: ", hierarchy)
fmt.Println("primary: ", primary)
fmt.Println("broadcastAddr: ", broadcastAddr)
n.nodeTable = hierarchy
n.primary = primary
n.broadcastAddr = broadcastAddr
fmt.Println("NodeTable: " , n.nodeTable)
fmt.Println("Primary Node: " , n.primary)
fmt.Println("Broadcast Address: " , n.broadcastAddr)
if n.nodeID == "N1" || n.nodeID == "N2" || n.nodeID == "N3" {//(3/7) add N4-N7
n.vicePrimary = n.nodeID
}
go n.handleMsg()
ln, err := net.Listen("tcp", n.addr)
if err != nil{
panic(err)
}
defer ln.Close()
fmt.Printf("node server starts at %s\n", n.addr)
for{
conn, err := ln.Accept()
if err != nil{
panic(err)
}
go n.handleConnection(conn)
}
}
func (n* Node) handleConnection(conn net.Conn){
req, err := ioutil.ReadAll(conn)
if err != nil{
panic(err)
}
n.msgQueue <- req
}
func (n *Node) addSID() int{
n.mutex.Lock()
seq := n.sequenceID
n.sequenceID++
n.mutex.Unlock()
return seq
}
func (n *Node) handleMsg(){
for{
data := <- n.msgQueue
//!NEW
if n.block == 0{
if n.nodeID == primary || n.nodeID == n.vicePrimary{
//NEW! (20/6)
if n.broadcastAddr != ""{
send(data, n.broadcastAddr)
}
n.block = 1
}
}
header, payload, sig := splitMsg(data)
switch Header(header){
case Request:
n.handleRequest(payload, sig)
case PrePrepare:
n.handlePrePrepare(payload, sig)
case Prepare:
n.handlePrepare(payload, sig)
case Commit:
n.handleCommit(payload, sig)
n.block = 0
}
}
}
func (n *Node) handleRequest(payload []byte, sig []byte){
r := new(RequestMsg)
//convert json to struct format
err := json.Unmarshal(payload, r)
fmt.Println("request packet: " , r)
if err != nil{
log.Panic(err)
}
n.addSID()
digest := createDigest(*r)
digestmsg := generateDigest(r.CMessage.Request)
//verify digest
vdig := verifyDigest(digestmsg, r.CMessage.Digest)
fmt.Println("verifydigest: ", vdig)
if vdig == false{
fmt.Printf("verify digest failed\n")
return
}
strDigest := hex.EncodeToString(digest[:])
//store map; digest = key, requestmsg = value
n.requestPool[strDigest] = r
//decode string to byte format for signing
digestByte, _ := hex.DecodeString(strDigest)
signature, err := signMessage(digestByte, n.privKey)
if err != nil{
log.Panic(err)
}
prePreparePacket := PrePrepareMsg{
*r,
viewID,
n.sequenceID,
strDigest,
n.nodeID,
}
fmt.Println("sequenceID:(primary) " , n.sequenceID)
//seq--
//convert struct to json format
done, err := json.Marshal(prePreparePacket)
if err != nil{
log.Panic(err)
}
message := mergeMsg(PrePrepare, done, signature)
n.mutex.Lock()
//put preprepare msg into preprepare log
if n.msgLog.preprepareLog[prePreparePacket.Digest] == nil{
n.msgLog.preprepareLog[prePreparePacket.Digest] = make(map[string]bool)
}
n.msgLog.preprepareLog[prePreparePacket.Digest][n.nodeID] = true
n.mutex.Unlock()
//update(3/7)
if n.nodeID != "N1" && n.nodeID != "N2" && n.nodeID != "N3" {
fmt.Println("broadcasting (pre-prepare) message..")
n.broadcast(message)
}
n.sequenceID--
}
func (n *Node) handlePrePrepare(payload []byte, sig []byte){
//update(23/6)
if !isExist("./Penalties"){
err := os.Mkdir("./Penalties", 0700)
if err != nil{
log.Panic(err)
}
}
//create instance of preprepare
pp := new(PrePrepareMsg)
err := json.Unmarshal(payload, pp)
if err != nil{
log.Panic(err)
}
//get primary node's public key for verification
primaryNodePubKey := getPubKey(n.primary)
//decode string to byte format for signing
digestByte, _ := hex.DecodeString(pp.Digest)
//set approval conditions
if digest := createDigest(pp.Request); hex.EncodeToString(digest[:]) != pp.Digest{
//add penalty
note := fmt.Sprintf("preprepare phase: digest (%s) not match, further application rejected!\n", pp.NodeID)
n.recordPenalties(note)
} else if n.sequenceID+1 != pp.SequenceID{
//add penalty
note := fmt.Sprintf("preprepare phase: incorrect sequence (%d)(%d)(%s), further application rejected!\n", n.sequenceID+1, pp.SequenceID, pp.NodeID)
n.recordPenalties(note)
} else if !n.verifySignature(digestByte, sig, primaryNodePubKey){
//add penalty
note := fmt.Sprintf("preprepare phase: key verification (%s) failed, further application rejected!\n", pp.NodeID)
n.recordPenalties(note)
} else {
//success
fmt.Println("sequenceID: ", n.sequenceID)
n.sequenceID = pp.SequenceID
fmt.Println("preprepare phase: stored into message pool")
n.requestPool[pp.Digest] = &pp.Request
signature, err := signMessage(digestByte, n.privKey)
if err != nil{
log.Panic(err)
}
preparePacket := PrepareMsg{
viewID,
pp.SequenceID,
pp.Digest,
n.nodeID,
}
done, err := json.Marshal(preparePacket)
if err != nil{
log.Panic(err)
}
message := mergeMsg(Prepare, done, signature)
//put prepare msg into prepare log
n.mutex.Lock()
if n.msgLog.prepareLog[preparePacket.Digest] == nil{
n.msgLog.prepareLog[preparePacket.Digest] = make(map[string]bool)
}
n.msgLog.prepareLog[preparePacket.Digest][n.nodeID] = true
n.mutex.Unlock()
fmt.Println("broadcasting (prepare) message..")
//update (30/6)
time.Sleep(nodeDelay)
n.broadcast(message)
}
}
func (n *Node) handlePrepare(payload []byte, sig []byte){
pre := new(PrepareMsg)
err := json.Unmarshal(payload, pre)
if err != nil{
log.Panic(err)
}
msgNodePubKey := getPubKey(pre.NodeID)
//decode string to byte format
digestByte, _ := hex.DecodeString(pre.Digest)
// if n.nodeID == n.primary || n.nodeID == n.vicePrimary{
// n.sequenceID++
// }
fmt.Println("prepare request pool(pre-): ", n.requestPool[pre.Digest], "nodeID: ", pre.NodeID)
if _, ok := n.requestPool[pre.Digest]; !ok{
//add penalty
note := fmt.Sprintf("prepare phase: unable to retrieve (%s) digest, further application rejected!\n", pre.NodeID)
fmt.Println("prepare request pool: ", n.requestPool[pre.Digest])
n.recordPenalties(note)
} else if n.sequenceID != pre.SequenceID{
//add penalty
//update(24/6)
if n.nodeID != n.primary && n.nodeID != n.vicePrimary{
note := fmt.Sprintf("prepare phase: incorrect sequence (%s)(current sequenceID: %d)(received sequenceID: %d), further application rejected!\n", pre.NodeID, n.sequenceID, pre.SequenceID)
n.recordPenalties(note)
}
} else if !n.verifySignature(digestByte, sig, msgNodePubKey){
//add penalty
note := fmt.Sprintf("prepare phase: key verification (%s) failed, further application rejected!\n", pre.NodeID)
n.recordPenalties(note)
} else{
//success
n.setPrepareConfirmMap(pre.Digest, pre.NodeID, true)
count := 0
for range n.prepareConfirmCount[pre.Digest]{
count++
}
specifiedCount := 0
fmt.Println("nodecount per group: " , newNodeCount)
if n.nodeID == n.primary /* "N0" */{
specifiedCount = newNodeCount / 3 * 2 //<--problem with nodeCount
} else{
specifiedCount = (newNodeCount / 3 * 2) -1
}
fmt.Println("specifiedCount: ", specifiedCount)
if count >= specifiedCount && !n.isCommitBroadcast[pre.Digest]{
fmt.Println("minimum (prepare) consensus achieved!")
signature, err := signMessage(digestByte, n.privKey)
if err != nil{
log.Panic(err)
}
c := CommitMsg{
viewID,
pre.Digest,
pre.SequenceID,
n.nodeID,
}
done, err := json.Marshal(c)
if err != nil{
log.Panic(err)
}
fmt.Println("broadcasting (commit) message..")
message := mergeMsg(Commit, done, signature)
//put commit msg into commit log
n.mutex.Lock()
if n.msgLog.commitLog[c.Digest] == nil{
n.msgLog.commitLog[c.Digest] = make(map[string]bool)
}
n.msgLog.commitLog[c.Digest][n.nodeID] = true
n.mutex.Unlock()
time.Sleep(nodeDelay)
n.broadcast(message)
n.isCommitBroadcast[pre.Digest] = true
fmt.Println("committed successfully")
}
}
}
func (n *Node) handleCommit(payload []byte, sig []byte){
cmt := new(CommitMsg)
err := json.Unmarshal(payload, cmt)
if err != nil{
log.Panic(err)
}
msgNodePubKey := getPubKey(cmt.NodeID)
digestByte, _ := hex.DecodeString(cmt.Digest)
if _, ok := n.prepareConfirmCount[cmt.Digest]; !ok{
note := fmt.Sprintf("commit phase: unable to retrieve (%s) digest, further application rejected\n", cmt.NodeID)
n.recordPenalties(note)
//add penalty
} else if n.sequenceID != cmt.SequenceID{
note := fmt.Sprintf("commit phase: incorrect sequence (%s), further application rejected!\n", cmt.NodeID)
n.recordPenalties(note)
//add penalty
} else if !n.verifySignature(digestByte, sig, msgNodePubKey){
note := fmt.Sprintf("commit phase: key verification (%s) failed, further application rejected!\n", cmt.NodeID)
n.recordPenalties(note)
//add penalty
} else{
n.setCommitConfirmMap(cmt.Digest, cmt.NodeID, true)
count := 0
for range n.commitConfirmCount[cmt.Digest]{
count++
}
n.mutex.Lock()
if count >= newNodeCount / 3 * 2 && !n.isReply[cmt.Digest] && n.isCommitBroadcast[cmt.Digest]{
fmt.Println("minimum (commit) consensus achieved!")
signature, err := signMessage(digestByte, n.privKey)
if err != nil{
log.Panic(err)
}
d := ReplyMsg{
viewID,
int(time.Now().Unix()),
n.nodeID,
"success",
}
fmt.Println("reply message: ", d)
done, err := json.Marshal(d)
if err != nil{
log.Panic(err)
}
fmt.Println("broadcasting results..")
message := mergeMsg(Reply, done, signature)
send(message, n.requestPool[cmt.Digest].CAddr)
n.isReply[cmt.Digest] = true
fmt.Println("successfully replied!")
}
n.mutex.Unlock()
}
}
func (n *Node) verifyRequestDigest(digest string) error{
n.mutex.Lock()
_, ok := n.requestPool[digest]
if !ok{
n.mutex.Unlock()
return fmt.Errorf("verify request digest failed\n")
}
n.mutex.Unlock()
return nil
}
func (n *Node) broadcast(data []byte){
for _, i := range n.nodeTable{
if i != n.nodeID{
fmt.Println("node address: " , i)
send(data, i)
}
}
}
func (n* Node) reply(data []byte, cliaddr string){
conn, err := net.Dial("tcp", cliaddr)
if err != nil{
log.Println("connect error", err)
return
}
_, err = conn.Write(data)
if err != nil{
log.Fatal(err)
}
conn.Close()
}
func (n *Node) setPrepareConfirmMap(x, y string, b bool){
if _, ok := n.prepareConfirmCount[x]; !ok{
n.prepareConfirmCount[x] = make(map[string]bool)
fmt.Println("prepareConfirmCount(x): ", x)
fmt.Println("prepareConfirmCount(y): ", y)
}
n.prepareConfirmCount[x][y] = b
}
func (n *Node) setCommitConfirmMap(x, y string, b bool){
if _, ok := n.commitConfirmCount[x]; !ok{
n.commitConfirmCount[x] = make(map[string]bool)
}
n.commitConfirmCount[x][y] = b
}
//update(23/6)
func (n *Node) recordPenalties(s string){
oflag := 0
filename, _ := filepath.Abs(fmt.Sprintf("./Penalties/%s", n.nodeID))
write := fmt.Sprintf(s)
if !isExist(filename + "_penalty"){
err := ioutil.WriteFile(filename + "_penalty", []byte(write), 0644)
oflag = 1
if err != nil{
log.Panicf("penalty log: error creating file: %s", err)
}
}
f, err :=os.OpenFile(filename + "_penalty", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil{
log.Panicf("penalty log: error opening file: %s", err)
}
defer f.Close()
if oflag != 1{
if _, err = f.WriteString(write); err != nil{
log.Panicf("error recording results: %s", err)
}
}
oflag = 0
}