-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
220 lines (204 loc) · 6.88 KB
/
service.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
package main
import (
"bytes"
"crypto/sha256"
"errors"
"fmt"
"math"
"github.com/BurntSushi/toml"
tournament "github.com/ceyhunalp/calypso_experiments/tournament_lottery"
"github.com/dedis/cothority"
"github.com/dedis/cothority/byzcoin"
"github.com/dedis/onet"
"github.com/dedis/onet/log"
"github.com/dedis/onet/simul/monitor"
)
/*
* Defines the simulation for the service-template
*/
func init() {
onet.SimulationRegister("TournamentLottery", NewTournamentService)
}
// SimulationService only holds the BFTree simulation
type SimulationService struct {
onet.SimulationBFTree
NumTransactions int
BlockInterval int
BlockWait int
}
// NewSimulationService returns the new simulation, where all fields are
// initialised using the config-file
func NewTournamentService(config string) (onet.Simulation, error) {
es := &SimulationService{}
_, err := toml.Decode(config, es)
if err != nil {
return nil, err
}
return es, nil
}
// Setup creates the tree used for that simulation
func (s *SimulationService) Setup(dir string, hosts []string) (
*onet.SimulationConfig, error) {
sc := &onet.SimulationConfig{}
s.CreateRoster(sc, hosts, 2000)
err := s.CreateTree(sc)
if err != nil {
return nil, err
}
return sc, nil
}
// Node can be used to initialize each node before it will be run
// by the server. Here we call the 'Node'-method of the
// SimulationBFTree structure which will load the roster- and the
// tree-structure to speed up the first round.
func (s *SimulationService) Node(config *onet.SimulationConfig) error {
index, _ := config.Roster.Search(config.Server.ServerIdentity.ID)
if index < 0 {
log.Fatal("Didn't find this node in roster")
}
log.Lvl3("Initializing node-index", index)
return s.SimulationBFTree.Node(config)
}
// Run is used on the destination machines and runs a number of
// rounds
func (s *SimulationService) Run(config *onet.SimulationConfig) error {
log.Info("Total # of rounds is:", s.Rounds)
//size := config.Tree.Size()
//log.Info("Size of the tree:", size)
for round := 0; round < s.Rounds; round++ {
log.Info("Starting run", round)
byzd, err := tournament.SetupByzcoin(config.Roster, s.BlockInterval)
numTransactions := s.NumTransactions
numRounds := int(math.Ceil(math.Log2(float64(numTransactions))))
numTransactionsLeft := numTransactions
participantList := make([]int, numTransactions)
for i := 0; i < numTransactions; i++ {
participantList[i] = 1
}
isOdd := false
for i := 0; i < numRounds; i++ {
log.Info("Starting lottery round", i)
if numTransactionsLeft%2 != 0 {
numTransactionsLeft -= 1
isOdd = true
}
lotteryData := make([]*tournament.LotteryData, numTransactionsLeft)
commitTxnList := make([]*tournament.TransactionReply, numTransactionsLeft)
wait := 0
comtime := monitor.NewTimeMeasure("commit_time")
for i := 0; i < numTransactionsLeft; i++ {
lotteryData[i] = tournament.CreateLotteryData()
if i == numTransactionsLeft-1 {
wait = s.BlockWait
}
//log.Lvl1("[TournamentLottery] AddCommit called")
commitTxnList[i], err = byzd.AddCommitTransaction(lotteryData[i], wait)
if err != nil {
log.Errorf("AddCommitTransaction failed: %v", err)
return err
}
}
comtime.Record()
commitProofList := make([]byzcoin.Proof, numTransactionsLeft)
wrproof := monitor.NewTimeMeasure("write_proof")
for i := 0; i < numTransactionsLeft; i++ {
commitProofResp, err := byzd.Cl.GetProof(commitTxnList[i].InstanceID.Slice())
if err != nil {
log.Errorf("GetProof(Commit) failed: %v", err)
return err
}
if !commitProofResp.Proof.InclusionProof.Match() {
return errors.New("Commit inclusion proof does not match")
}
commitProofList[i] = commitProofResp.Proof
}
wrproof.Record()
wait = 0
secretTxnList := make([]*tournament.TransactionReply, numTransactionsLeft)
trt := monitor.NewTimeMeasure("tournament_reveal")
for i := 0; i < numTransactionsLeft; i++ {
if i == numTransactionsLeft-1 {
wait = s.BlockWait
}
//log.Lvl1("[TournametLottery] AddSecret called")
secretTxnList[i], err = byzd.AddSecretTransaction(lotteryData[i], wait)
if err != nil {
log.Errorf("AddSecretTransaction failed: %v", err)
return err
}
}
trt.Record()
secretProofList := make([]byzcoin.Proof, numTransactionsLeft)
tspt := monitor.NewTimeMeasure("tournament_proof")
for i := 0; i < numTransactionsLeft; i++ {
secretProofResp, err := byzd.Cl.GetProof(secretTxnList[i].InstanceID.Slice())
if err != nil {
log.Errorf("GetProof(Secret) failed: %v", err)
return err
}
if !secretProofResp.Proof.InclusionProof.Match() {
return errors.New("Secret inclusion proof does not match")
}
secretProofList[i] = secretProofResp.Proof
}
tspt.Record()
revealedCommitList := make([]tournament.DataStore, numTransactionsLeft)
revealedSecretList := make([]tournament.DataStore, numTransactionsLeft)
trvt := monitor.NewTimeMeasure("tournament_get_winner")
for i := 0; i < numTransactionsLeft; i++ {
err = commitProofList[i].ContractValue(cothority.Suite, tournament.ContractLotteryStoreID, &revealedCommitList[i])
if err != nil {
log.Errorf("did not get a commit instance" + err.Error())
return errors.New("did not get a commit instance" + err.Error())
}
err = secretProofList[i].ContractValue(cothority.Suite, tournament.ContractLotteryStoreID, &revealedSecretList[i])
if err != nil {
log.Errorf("did not get a secret instance" + err.Error())
return errors.New("did not get a secret instance" + err.Error())
}
}
var winnerList []int
for i := 0; i < numTransactionsLeft; {
//These are the hashes
leftSecret := revealedSecretList[i].Data
rightSecret := revealedSecretList[i+1].Data
leftDigest := sha256.Sum256(leftSecret[:])
rightDigest := sha256.Sum256(rightSecret[:])
if bytes.Compare(leftDigest[:], revealedCommitList[i].Data[:]) != 0 {
fmt.Println("Digests do not match - winner is", i+1)
winnerList = append(winnerList, i+1)
} else {
if bytes.Compare(rightDigest[:], revealedCommitList[i+1].Data[:]) != 0 {
fmt.Println("Digests do not match - winner is", i)
winnerList = append(winnerList, i)
} else {
result := make([]byte, 32)
tournament.SafeXORBytes(result, leftSecret[:], rightSecret[:])
lastDigit := int(result[31]) % 2
if lastDigit == 0 {
winnerList = append(winnerList, i)
} else {
winnerList = append(winnerList, i+1)
}
}
}
i += 2
}
if isOdd {
winnerList = append(winnerList, numTransactionsLeft)
numTransactionsLeft += 1
}
numTransactionsLeft = int(math.Ceil(float64(numTransactionsLeft) / 2))
isOdd = false
trvt.Record()
tournament.OrganizeList(participantList, winnerList)
}
for i := 0; i < numTransactions; i++ {
if participantList[i] == 1 {
log.Info("Winner is", i)
break
}
}
}
return nil
}