-
Notifications
You must be signed in to change notification settings - Fork 15
/
protocol.go
99 lines (87 loc) · 2.89 KB
/
protocol.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
package main
/*
The simulation-file can be used with the `cothority/simul` and be run either
locally or on deterlab. Contrary to the `test` of the protocol, the simulation
is much more realistic, as it tests the protocol on different nodes, and not
only in a test-environment.
The Setup-method is run once on the client and will create all structures
and slices necessary to the simulation. It also receives a 'dir' argument
of a directory where it can write files. These files will be copied over to
the simulation so that they are available.
The Run-method is called only once by the root-node of the tree defined in
Setup. It should run the simulation in different rounds. It can also
measure the time each run takes.
In the Node-method you can read the files that have been created by the
'Setup'-method.
*/
import (
"errors"
"strconv"
"github.com/BurntSushi/toml"
"github.com/dedis/cothority_template/protocol"
"go.dedis.ch/onet/v3"
"go.dedis.ch/onet/v3/log"
"go.dedis.ch/onet/v3/simul/monitor"
)
func init() {
onet.SimulationRegister("TemplateProtocol", NewSimulationProtocol)
}
// SimulationProtocol implements onet.Simulation.
type SimulationProtocol struct {
onet.SimulationBFTree
}
// NewSimulationProtocol is used internally to register the simulation (see the init()
// function above).
func NewSimulationProtocol(config string) (onet.Simulation, error) {
es := &SimulationProtocol{}
_, err := toml.Decode(config, es)
if err != nil {
return nil, err
}
return es, nil
}
// Setup implements onet.Simulation.
func (s *SimulationProtocol) 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 *SimulationProtocol) 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 implements onet.Simulation.
func (s *SimulationProtocol) Run(config *onet.SimulationConfig) error {
size := config.Tree.Size()
log.Lvl2("Size is:", size, "rounds:", s.Rounds)
for round := 0; round < s.Rounds; round++ {
log.Lvl1("Starting round", round)
round := monitor.NewTimeMeasure("round")
p, err := config.Overlay.CreateProtocol("Template", config.Tree,
onet.NilServiceID)
if err != nil {
return err
}
go p.Start()
children := <-p.(*protocol.TemplateProtocol).ChildCount
round.Record()
if children != size {
return errors.New("Didn't get " + strconv.Itoa(size) +
" children")
}
}
return nil
}