-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
115 lines (102 loc) · 2.43 KB
/
main.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
package main
import (
"github.com/tinychain/algorand/common"
"github.com/urfave/cli"
"os"
"time"
)
func main() {
app := initApp()
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func initApp() *cli.App {
app := cli.NewApp()
app.Name = "Algorand demo"
app.Version = "0.1"
app.Author = "LowesYang"
app.Usage = "Algorand simulation demo for different scenario."
app.Commands = []cli.Command{
{
Name: "regular",
Aliases: []string{"r"},
Usage: "run regular Algorand algorithm",
Action: regularRun,
Flags: []cli.Flag{
cli.Uint64Flag{
Name: "num,n",
Value: 100,
Usage: "amount of users",
},
cli.Uint64Flag{
Name: "token,t",
Value: 1000,
Usage: "token balance per users",
},
cli.Uint64Flag{
Name: "malicious,m",
Value: 0,
Usage: "amount of malicious users. Malicious user will use default strategy.",
},
cli.IntFlag{
Name: "mtype,i",
Value: 0,
Usage: "malicious type: 0 Honest, 1 block proposal misbehaving; 2 vote empty block in BA*; 3 vote nothing",
},
cli.IntFlag{
Name: "latency,l",
Value: 0,
Usage: "max network latency(milliseconds). Each user will simulate a random latency between 0 and ${value}",
},
},
},
}
return app
}
func regularRun(c *cli.Context) {
UserAmount = c.Uint64("num")
TokenPerUser = c.Uint64("token")
Malicious = c.Uint64("malicious")
NetworkLatency = c.Int("latency")
maliciousType := c.Int("type")
var (
nodes []*Algorand
i = 0
)
for ; uint64(i) <= UserAmount-Malicious; i++ {
node := NewAlgorand(PID(i), Honest)
go node.Start()
nodes = append(nodes, node)
}
for ; uint64(i) <= UserAmount; i++ {
node := NewAlgorand(PID(i), maliciousType)
go node.Start()
nodes = append(nodes, node)
}
ticker := time.NewTicker(20 * time.Second)
for {
select {
case <-ticker.C:
printChainInfo(nodes)
}
}
}
type meta struct {
round uint64
hash common.Hash
}
func printChainInfo(nodes []*Algorand) {
chains := make(map[meta][]PID)
for _, node := range nodes {
last := node.chain.last
key := meta{last.Round, last.Hash()}
chains[key] = append(chains[key], node.id)
}
for meta, pids := range chains {
log.Infof("%d nodes reach consensus on chain round %d, hash %s", len(pids), meta.round, meta.hash)
}
snapshot := proposerSelectedHistogram.Snapshot()
log.Infof("average selected user number in block proposal is %v", snapshot.Mean())
}