forked from kimborgen/rapidchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leader.go
198 lines (161 loc) · 6.01 KB
/
leader.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
package main
import (
"encoding/binary"
"fmt"
"log"
"math"
"time"
)
// Start a completly new iteration. With leader election and if you are leader, perform leader duties.
func startNewIteration(nodeCtx *NodeCtx) {
// launch leader election protocol
leaderElection(nodeCtx)
// If this node is leader then initate leader protocol
if nodeCtx.committee.CurrentLeader.Bytes == nodeCtx.self.Priv.Pub.Bytes {
// go debug(nodeCtx)
// wait untill tx pool is large enough
for {
l := nodeCtx.txPool.len()
if l >= 10 {
break
}
time.Sleep(100 * time.Millisecond)
// fmt.Print(l)
}
leader(nodeCtx)
}
}
type byte32sortHelper struct {
original [32]byte
toSort [32]byte
}
// finds current leader of comittee, using epoch randomness and iteration number
func leaderElection(nodeCtx *NodeCtx) {
// The paper doesnt specifically mention any leader election protocols, so we assume that the leader election protocol
// used in bootstrap is also used in the normal protocol, with the adition of iteration (unless the same leader would
// be selected).
// TODO actually add a setup phase where one must publish their hash. This way there will always
// be a leader even if some nodes are offline. But with the assumption that every node is online
// this works fine.
// get current randomness
recBlock := nodeCtx.blockchain.getLastReconfigurationBlock()
rnd := recBlock.Randomness
// get current iteration
_currIteration := nodeCtx.i.getI()
currI := make([]byte, 8)
binary.LittleEndian.PutUint64(currI, uint64(_currIteration))
listOfHashes := make([]byte32sortHelper, len(nodeCtx.committee.Members))
// calculate hash(id | rnd | currI) for every member
ii := 0
for _, m := range nodeCtx.committee.Members {
connoctated := byteSliceAppend(m.Pub.Bytes[:], rnd[:], currI)
hsh := hash(connoctated)
listOfHashes[ii] = byte32sortHelper{m.Pub.Bytes, hsh}
ii++
}
// sort list
listOfHashes = sortListOfByte32SortHelper(listOfHashes)
// calculate hash of self
selfHash := hash(byteSliceAppend(nodeCtx.self.Priv.Pub.Bytes[:], rnd[:], currI))
// fmt.Println("self: ", bytes32ToString(selfHash), bytes32ToString(nodeCtx.self.Priv.Pub.Bytes))
// for i, lof := range listOfHashes {
// fmt.Println(i, bytes32ToString(lof.toSort), bytes32ToString(lof.original))
// }
// the leader is the lowest in list except if selfHash is lower than that.
// fmt.Println(byte32Operations(selfHash, "<", listOfHashes[0].toSort))
if byte32Operations(selfHash, "<", listOfHashes[0].toSort) {
nodeCtx.committee.CurrentLeader = nodeCtx.self.Priv.Pub
log.Println("I am leader!", nodeCtx.amILeader())
} else {
leader := listOfHashes[0].original
nodeCtx.committee.CurrentLeader = nodeCtx.committee.Members[leader].Pub
}
}
func shouldISendCrossTX(nodeCtx *NodeCtx) bool {
// log(m) nodes as defined in thesis
// get current randomness
recBlock := nodeCtx.blockchain.getLastReconfigurationBlock()
rnd := recBlock.Randomness
// get current iteration
_currIteration := nodeCtx.i.getI()
currI := make([]byte, 8)
binary.LittleEndian.PutUint64(currI, uint64(_currIteration))
listOfHashes := make([]byte32sortHelper, len(nodeCtx.committee.Members))
// calculate hash(id | rnd | currI) for every member
ii := 0
for _, m := range nodeCtx.committee.Members {
connoctated := byteSliceAppend(m.Pub.Bytes[:], rnd[:], currI)
hsh := hash(connoctated)
listOfHashes[ii] = byte32sortHelper{m.Pub.Bytes, hsh}
ii++
}
// sort list
listOfHashes = sortListOfByte32SortHelper(listOfHashes)
// calculate hash of self
selfHash := hash(byteSliceAppend(nodeCtx.self.Priv.Pub.Bytes[:], rnd[:], currI))
// fmt.Println("self: ", bytes32ToString(selfHash), bytes32ToString(nodeCtx.self.Priv.Pub.Bytes))
// for i, lof := range listOfHashes {
// fmt.Println(i, bytes32ToString(lof.toSort), bytes32ToString(lof.original))
// }
// log(m)
_cutoff := math.Log2(float64(len(nodeCtx.committee.Members)))
cutoff := int(math.Abs(_cutoff))
// the leader is the lowest in list except if selfHash is lower than that.
// fmt.Println(byte32Operations(selfHash, "<", listOfHashes[0].toSort))
if byte32Operations(selfHash, "<", listOfHashes[cutoff].toSort) {
return true
}
return false
}
func basicLeaderElectionasdf(nodeCtx *NodeCtx) *PubKey {
// Find out who is the leader, returns ID of leader
// For now just pick the one with the lowest ID.
// TODO: create an actual leader election protocol based on epoch randomness and nonce, assume every node is online
// TODO: figure out some complete leader election protocol
// set yourself to the lowest seen
var lowestID *PubKey = nodeCtx.self.Priv.Pub
lowestIDBigInt := toBigInt(lowestID.Bytes)
for k := range nodeCtx.committee.Members {
kBI := toBigInt(k)
if kBI.Cmp(lowestIDBigInt) < 0 {
lowestID = nodeCtx.committee.Members[k].Pub
lowestIDBigInt = kBI
}
}
if false {
// go trough all nodes in system and give the lowest id instead of lowest id in committee
for k := range nodeCtx.allInfo {
kBI := toBigInt(k)
if kBI.Cmp(lowestIDBigInt) < 0 {
lowestID = nodeCtx.allInfo[k].Pub
lowestIDBigInt = kBI
}
}
}
fmt.Println("Leader", lowestID == nodeCtx.self.Priv.Pub)
nodeCtx.committee.CurrentLeader = lowestID
return lowestID
}
func leader(nodeCtx *NodeCtx) {
// initates leader process
// create a block
block := createProposeBlock(nodeCtx)
// ida-gossip the block
IDAGossip(nodeCtx, block.encode(), "block")
// wait until we have recivied and recreated IDA message
for !nodeCtx.blockchain.isProposedBlock(block.GossipHash) {
time.Sleep(100 * time.Millisecond)
}
// sleep a delta before iniation consensus
time.Sleep(time.Duration(2*nodeCtx.flagArgs.delta) * time.Millisecond)
// create a propose msg to initate consensus
cMsg := new(ConsensusMsg)
cMsg.GossipHash = block.GossipHash
cMsg.Tag = "propose"
cMsg.Pub = nodeCtx.self.Priv.Pub
cMsg.sign(nodeCtx.self.Priv)
msg := Msg{"consensus", cMsg, nodeCtx.self.Priv.Pub}
// start consensus rounds.
log.Printf("Leader starting conseuss in committee %s\n", bytes32ToString(nodeCtx.committee.ID))
sendMsgToCommitteeAndSelf(msg, nodeCtx)
}