/
Coordinator.go
206 lines (192 loc) · 5.29 KB
/
Coordinator.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
package main
import (
"net"
"fmt"
"./util"
"./coordinator"
"bufio"
"os"
"github.com/dedis/crypto/nist"
"github.com/dedis/crypto/random"
"time"
"log"
"github.com/dedis/crypto/abstract"
"proto"
)
// pointer to coordinator itself
var anonCoordinator *coordinator.Coordinator
/**
* start server listener to handle event
*/
func startServerListener() {
fmt.Println("[debug] Coordinator server listener started...");
buf := make([]byte, 4096)
for {
n,addr,err := anonCoordinator.Socket.ReadFromUDP(buf)
util.CheckErr(err)
coordinator.Handle(buf,addr,anonCoordinator,n)
}
}
/**
* initialize coordinator
*/
func initCoordinator() {
config := util.ReadConfig()
fmt.Println(config)
ServerAddr,err := net.ResolveUDPAddr("udp","127.0.0.1:"+config["local_port"])
util.CheckErr(err)
suite := nist.NewAES128SHA256QR512()
a := suite.Secret().Pick(random.Stream)
A := suite.Point().Mul(nil, a)
anonCoordinator = &coordinator.Coordinator{ServerAddr,nil,nil,
coordinator.CONFIGURATION,suite,a,A,nil, make(map[string]*net.UDPAddr),
make(map[string]abstract.Point), make(map[string][]byte), nil, nil,
make(map[string]int),make(map[string]abstract.Point)}
}
/**
* clear all buffer data
*/
func clearBuffer() {
// clear buffer
anonCoordinator.NewClientsBuffer = nil
// msg sender's record nym
anonCoordinator.MsgLog = nil
}
/**
* send announcement signal to first server
* send reputation list
*/
func announce() {
firstServer := anonCoordinator.GetFirstServer()
if firstServer == nil {
anonCoordinator.Status = coordinator.MESSAGE
return
}
// construct reputation list (public & encrypted reputation)
size := len(anonCoordinator.ReputationMap)
keys := make([]abstract.Point,size)
vals := make([][]byte,size)
i := 0
for k, v := range anonCoordinator.ReputationMap {
keys[i] = anonCoordinator.ReputationKeyMap[k]
vals[i] = v
i++
}
byteKeys := util.ProtobufEncodePointList(keys)
byteVals := util.SerializeTwoDimensionArray(vals)
params := map[string]interface{}{
"keys" : byteKeys,
"vals" : byteVals,
}
event := &proto.Event{proto.ANNOUNCEMENT,params}
util.Send(anonCoordinator.Socket,firstServer,util.Encode(event))
}
/**
* send round-end signal to last server in topology
* add new clients into the reputation map
*/
func roundEnd() {
lastServer := anonCoordinator.GetLastServer()
if lastServer == nil {
anonCoordinator.Status = coordinator.READY_FOR_NEW_ROUND
return
}
// add new clients into reputation map
for _,nym := range anonCoordinator.NewClientsBuffer {
anonCoordinator.AddIntoDecryptedMap(nym,0)
}
// add previous clients into reputation map
// construct the parameters
size := len(anonCoordinator.DecryptedReputationMap)
keys := make([]abstract.Point,size)
vals := make([]int,size)
i := 0
for k, v := range anonCoordinator.DecryptedReputationMap {
keys[i] = anonCoordinator.DecryptedKeysMap[k]
vals[i] = v
i++
}
byteKeys := util.ProtobufEncodePointList(keys)
// send signal to server
pm := map[string]interface{} {
"keys" : byteKeys,
"vals" : vals,
"is_start" : true,
}
event := &proto.Event{proto.ROUND_END,pm}
util.Send(anonCoordinator.Socket,lastServer,util.Encode(event))
}
/**
* start vote phase, actually, if we partition the clients to servers,
* we can let server send this signal to clients. Here, for simplicity, we
* just send it from controller
*/
func vote() {
pm := map[string]interface{} {}
event := &proto.Event{proto.VOTE,pm}
for _, val := range anonCoordinator.Clients {
util.Send(anonCoordinator.Socket, val, util.Encode(event))
}
}
func main() {
// init coordinator
initCoordinator()
// bind to socket
conn, err := net.ListenUDP("udp",anonCoordinator.LocalAddr )
util.CheckErr(err)
anonCoordinator.Socket = conn
// start listener
go startServerListener()
fmt.Println("** Note: Type ok to finish the server configuration. **")
// read ok to start life cycle
reader := bufio.NewReader(os.Stdin)
for {
data, _, _ := reader.ReadLine()
command := string(data)
if command == "ok" {
break
}
}
fmt.Println("[debug] Servers in the current network:")
fmt.Println(anonCoordinator.ServerList)
anonCoordinator.Status = coordinator.READY_FOR_NEW_ROUND
for {
// wait for the status changed to READY_FOR_NEW_ROUND
for i := 0; i < 100; i++ {
if anonCoordinator.Status == coordinator.READY_FOR_NEW_ROUND {
break
}
time.Sleep(1000 * time.Millisecond)
}
// clear buffer at the beginning of each round
clearBuffer()
fmt.Println("******************** New round begin ********************")
if anonCoordinator.Status != coordinator.READY_FOR_NEW_ROUND {
log.Fatal("Fails to be ready for the new round")
os.Exit(1)
}
anonCoordinator.Status = coordinator.ANNOUNCE
fmt.Println("[coordinator] Announcement phase started...")
// start announce phase
announce()
for i := 0; i < 100; i++ {
if anonCoordinator.Status == coordinator.MESSAGE {
break
}
time.Sleep(1000 * time.Millisecond)
}
if anonCoordinator.Status != coordinator.MESSAGE {
log.Fatal("Fails to be ready for message phase")
os.Exit(1)
}
// start message and vote phase
fmt.Println("[coordinator] Messaging phase started...")
// 10 secs for msg
time.Sleep(10000 * time.Millisecond)
vote()
fmt.Println("[coordinator] Voting phase started...")
// 10 secs for vote
time.Sleep(10000 * time.Millisecond)
roundEnd()
}
}