forked from bitfashioned/xxbft-calcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (104 loc) · 3.49 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
116
117
118
119
120
121
package main
import (
"fmt"
"math"
)
type result struct {
T int
F float64
L float64
}
func main() {
// Ask user to input the parameters
var p params
fmt.Println("Enter the network size")
fmt.Scanf("%d", &p.network)
fmt.Println("Enter the percentage of honest nodes")
var h int
fmt.Scanf("%d", &h)
// Compute the number of honest nodes
p.honest = int(math.Ceil(float64(p.network) * float64(h) / 100))
// Compute the number of faulty nodes
p.faulty = p.network - p.honest
fmt.Printf("Nodes: %d\n", p.network)
fmt.Printf("GOOD: %d\n", p.honest)
fmt.Printf("BAD: %d\n\n", p.faulty)
// Choose the caclulation to perform
fmt.Println("Select the calculation to perform")
fmt.Println(" 1 -> Failure probabilities for multiple quorums for a given endorser set size")
fmt.Println(" 2 -> Needed quorum to achieve a given failure probability for a given endorser set size")
fmt.Println(" 3 -> Needed endorser set size to achieve a given failure probability for a given quorum")
var choice int
fmt.Scanf("%d", &choice)
switch choice {
case 1:
// Ask user to input the endorser set size
fmt.Println("Enter the endorser set size")
fmt.Scanf("%d", &p.endorsers)
// Ask user to input the lowest quorum percentage
fmt.Println("Enter the lowest quorum percentage")
var low int
fmt.Scanf("%d", &low)
// Ask user to input the highest quorum percentage
fmt.Println("Enter the highest quorum percentage")
var high int
fmt.Scanf("%d", &high)
// Compute the failure probability for each quorum percentage
results := make([]result, high-low+1)
for q := low; q <= high; q++ {
p.quorum = float64(q) / 100
ps := GetFailureProbability(p)
pl := GetLivenessProbability(p)
results[q-low] = result{T: q, F: ps, L: pl}
}
fmt.Println()
fmt.Println()
for _, val := range results {
fmt.Printf("Quorum percentage %d%%: p(safety) = %e, p(liveness)= %e\n", val.T, val.F, val.L)
}
case 2:
// Ask user to input the endorser set size
fmt.Println("Enter the endorser set size")
fmt.Scanf("%d", &p.endorsers)
// Ask user to input the failure probability
fmt.Println("Enter the failure probability")
var epsilon float64
fmt.Scanf("%e", &epsilon)
// Ask user to input the lowest quorum percentage
fmt.Println("Enter the lowest quorum percentage")
var start int
fmt.Scanf("%d", &start)
// Compute the needed quorum percentage
q, prob := FindQ(p, float64(start)/100, epsilon)
// Compute the liveness probability
p.quorum = float64(q) / 100
pl := GetLivenessProbability(p)
fmt.Printf("\n\nQuorum percentage %d%%: p(safety) = %e, p(liveness)= %e\n", q, prob, pl)
case 3:
// Ask user to input the failure probability
fmt.Println("Enter the failure probability")
var epsilon float64
fmt.Scanf("%e", &epsilon)
// Ask user to input the quorum percentage
fmt.Println("Enter the quorum percentage")
var q int
fmt.Scanf("%d", &q)
p.quorum = float64(q) / 100
// Ask user to input the lowest endorser set size
fmt.Println("Enter the lowest endorser set size")
var start int
fmt.Scanf("%d", &start)
// Ask user to input the endorser set size step increment
fmt.Println("Enter the endorser set size step increment")
var step int
fmt.Scanf("%d", &step)
// Compute the needed endorser set size
e, prob := FindE(p, start, step, epsilon)
// Compute the liveness probability
p.endorsers = e
pl := GetLivenessProbability(p)
fmt.Printf("\n\nEndorser set size %d: p(safety) = %e, p(liveness)= %e\n", e, prob, pl)
default:
fmt.Println("Invalid choice")
}
}