-
Notifications
You must be signed in to change notification settings - Fork 106
/
simulation.go
78 lines (70 loc) · 1.82 KB
/
simulation.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
package ntree
import (
"github.com/BurntSushi/toml"
"gopkg.in/dedis/cothority.v0/lib/dbg"
"gopkg.in/dedis/cothority.v0/lib/monitor"
"gopkg.in/dedis/cothority.v0/lib/sda"
)
func init() {
sda.SimulationRegister("NaiveTree", NewSimulation)
}
// Simulation holds everything necessary for one NTree-round
type Simulation struct {
sda.SimulationBFTree
Message string
// 0 - no check
// 1 - check only direct children's signature
// 2 - check the whole subtree
Checking int
}
// NewSimulation creates a new NTree-simulation
func NewSimulation(config string) (sda.Simulation, error) {
es := &Simulation{Checking: 2}
_, err := toml.Decode(config, es)
if err != nil {
return nil, err
}
return es, nil
}
// Setup prepares the simulation on the local end
func (e *Simulation) Setup(dir string, hosts []string) (
*sda.SimulationConfig, error) {
sc := &sda.SimulationConfig{}
e.CreateEntityList(sc, hosts, 2000)
err := e.CreateTree(sc)
if err != nil {
return nil, err
}
return sc, nil
}
// Run starts the simulation on the simulation-side
func (e *Simulation) Run(config *sda.SimulationConfig) error {
msg := []byte(e.Message)
size := config.Tree.Size()
dbg.Lvl2("Size is:", size, "rounds:", e.Rounds)
for round := 0; round < e.Rounds; round++ {
dbg.Lvl1("Starting round", round, "with message", string(msg))
round := monitor.NewTimeMeasure("round")
p, err := config.Overlay.CreateProtocol(config.Tree, "NaiveTree")
if err != nil {
dbg.Error("Quitting the simulation....", err)
return err
}
pi := p.(*Protocol)
pi.Message = msg
pi.verifySignature = e.Checking
done := make(chan bool)
pi.TreeNodeInstance.OnDoneCallback(func() bool {
done <- true
return true
})
err = pi.Start()
if err != nil {
dbg.Error("Quitting the simulation....", err)
return err
}
<-done
round.Record()
}
return nil
}