-
Notifications
You must be signed in to change notification settings - Fork 106
/
ntree_simulation.go
93 lines (82 loc) · 2.46 KB
/
ntree_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package byzcoinNtree
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"
"gopkg.in/dedis/cothority.v0/protocols/byzcoin"
"gopkg.in/dedis/cothority.v0/protocols/byzcoin/blockchain"
)
func init() {
sda.SimulationRegister("ByzCoinNtree", NewSimulation)
sda.ProtocolRegisterName("ByzCoinNtree", func(n *sda.TreeNodeInstance) (sda.ProtocolInstance, error) { return NewNtreeProtocol(n) })
}
// Simulation implements da.Simulation interface
type Simulation struct {
// sda fields:
sda.SimulationBFTree
// your simulation specific fields:
byzcoin.SimulationConfig
}
// NewSimulation returns a new Ntree simulation
func NewSimulation(config string) (sda.Simulation, error) {
es := &Simulation{}
_, err := toml.Decode(config, es)
if err != nil {
return nil, err
}
return es, nil
}
// Setup implements sda.Simulation interface
func (e *Simulation) Setup(dir string, hosts []string) (*sda.SimulationConfig, error) {
err := blockchain.EnsureBlockIsAvailable(dir)
if err != nil {
dbg.Fatal("Couldn't get block:", err)
}
sc := &sda.SimulationConfig{}
e.CreateEntityList(sc, hosts, 2000)
err = e.CreateTree(sc)
if err != nil {
return nil, err
}
return sc, nil
}
// Run implements sda.Simulation interface
func (e *Simulation) Run(sdaConf *sda.SimulationConfig) error {
dbg.Lvl2("Naive Tree Simulation starting with: Rounds=", e.Rounds)
server := NewNtreeServer(e.Blocksize)
for round := 0; round < e.Rounds; round++ {
client := byzcoin.NewClient(server)
err := client.StartClientSimulation(blockchain.GetBlockDir(), e.Blocksize)
if err != nil {
dbg.Error("ClientSimulation:", err)
}
dbg.Lvl1("Starting round", round)
// create an empty node
node := sdaConf.Overlay.NewTreeNodeInstanceFromProtoName(sdaConf.Tree, "ByzCoinNtree")
// instantiate a byzcoin protocol
rComplete := monitor.NewTimeMeasure("round")
pi, err := server.Instantiate(node)
if err != nil {
return err
}
sdaConf.Overlay.RegisterProtocolInstance(pi)
nt := pi.(*Ntree)
// Register when the protocol is finished (all the nodes have finished)
done := make(chan bool)
nt.RegisterOnDone(func(sig *NtreeSignature) {
rComplete.Record()
dbg.Lvl3("Done")
done <- true
})
go func() {
if err := nt.Start(); err != nil {
dbg.Error("Couldn't start ntree protocol:", err)
}
}()
// wait for the end
<-done
dbg.Lvl3("Round", round, "finished")
}
return nil
}