-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle-server-test.go
More file actions
194 lines (160 loc) · 5.09 KB
/
single-server-test.go
File metadata and controls
194 lines (160 loc) · 5.09 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
"sync"
"log"
"strings"
"time"
"math/rand"
"os"
"os/exec"
"github.com/FairBlock/vsskyber"
bls "github.com/drand/kyber-bls12381"
"github.com/joho/godotenv"
)
func startChainProgram(id string, threshold string, addressList string, path string, timeout string, manager string) {
cmd := exec.Command(path, "tx", "dkg", "start-keygen", id, threshold, timeout, addressList, "--from", manager, "--keyring-backend", "test", "-y")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
fmt.Printf("Failed to start Go program : %s\n", err)
} else {
fmt.Printf("Started Go program")
}
}
func startGoProgram(path string, addr string, key string, port string, capacity string) {
cmd := exec.Command("go", "run", "./cmd/dkgd/main.go", "vald-start", "--validator-addr", addr, "--validator-key", key, "--tofnd-port", port, "--channel-capacity", capacity)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = path
err := cmd.Start()
if err != nil {
fmt.Printf("Failed to start Go program %s: %s\n", path, err)
} else {
fmt.Printf("Started Go program: %s\n", path)
}
err = cmd.Wait()
if err != nil {
exitErr, ok := err.(*exec.ExitError)
if ok && exitErr.ExitCode() != 0 {
fmt.Println("Command exited with non-zero status code:", exitErr.ExitCode())
} else {
log.Fatal("Command execution error:", err)
}
} else {
fmt.Println("Command executed successfully")
}
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
// Access the environment variables
addressesString := os.Getenv("ADDRESSES")
addresses := strings.Split(addressesString, ",")
keysString := os.Getenv("KEYS")
keys := strings.Split(keysString, ",")
portsString := os.Getenv("PORTS")
ports := strings.Split(portsString, ",")
shareTestIds := os.Getenv("shareTestIds")
_=shareTestIds
thresholdString := os.Getenv("THRESHOLD")
capacity := os.Getenv("ChannelCap")
timeout := os.Getenv("Timeout")
manager := os.Getenv("Manager")
path := os.Getenv("PathTodkgd")
corePath := os.Getenv("PathToCore")
// if len(addresses) != len(keys) {
// log.Fatal("Mismatch in number of keys and addresses!", len(addresses), len(keys))
// }
if len(keys) != len(ports) {
log.Fatal("Mismatch in number of ports and keys!", len(ports), len(keys))
}
numCalls := len(ports)
// Create a WaitGroup to wait for goroutines to finish
var wg sync.WaitGroup
// Increment the WaitGroup counter for each goroutine
wg.Add(numCalls)
for i := 0; i < numCalls; i++ {
go func(id int) {
// Decrement the WaitGroup counter when the goroutine finishes
defer wg.Done()
// Call the function
startGoProgram(corePath, addresses[id], keys[id], ports[id], capacity)
}(i)
}
jsonBytes, err := json.Marshal(addresses)
if err != nil {
fmt.Println("Error:", err)
return
}
jsonAddressString := string(jsonBytes)
//~/go/bin/dkgd tx dkg start-keygen 104 1 1 '["cosmos1uvvze65ey932l5l32kfgzlnut8e5f4zp2w26dk","cosmos136wuzlrrceanv5jn0p25um3d426wrc47epsxaj"]' --from alice
time.Sleep(90 * time.Second)
rand.Seed(time.Now().UnixNano())
// Generate a random integer between 0 and 100
randomNumber := rand.Intn(300)
startChainProgram(strconv.Itoa(randomNumber), thresholdString, jsonAddressString, path, timeout, manager)
wg.Wait()
//Keep the programs running until interrupted
for i := 0; i < len(addresses); i++ {
copys := corePath + "/share-" + strconv.Itoa(i) + ".txt"
copyp := corePath + "/pk-" + strconv.Itoa(i) + ".txt"
//fmt.Println(copy)
cmd := exec.Command("cp", copys, "./")
err := cmd.Run()
if err != nil {
fmt.Printf("Failed to start Go program: %s\n", err)
}
cmd = exec.Command("cp", copyp, "./")
err = cmd.Run()
if err != nil {
fmt.Printf("Failed to start Go program: %s\n", err)
}
}
verify_shares([]string{thresholdString, shareTestIds})
}
func verify_shares(args []string) {
threshold, _ := strconv.ParseUint(args[0], 10, 32)
chosen := strings.Split(args[1], ",")
var shareThreshold []vsskyber.Share
if len(chosen) < int(threshold) {
fmt.Println("Number of shares less than threshold.")
os.Exit(-1)
}
for i := 0; i < int(threshold)+1; i++ {
s, err := ioutil.ReadFile("share-" + string(chosen[i]) + ".txt")
if err != nil {
fmt.Printf("Failed to read file: %s\n", err)
return
}
index, _ := strconv.ParseUint(chosen[i], 10, 32)
share := vsskyber.Share{Index: bls.NewKyberScalar().SetInt64(int64(index + 1)), Value: bls.NewKyberScalar().SetBytes(s)}
shareThreshold = append(shareThreshold, share)
}
recMasterSecretKey, err := vsskyber.RegenerateSecret(uint32(threshold)+1, shareThreshold)
if err != nil{
fmt.Println(err)
}
s := bls.NewBLS12381Suite()
pkRec := s.G1().Point()
pkRec.Mul(recMasterSecretKey, s.G1().Point().Base())
pkb, err := ioutil.ReadFile("pk-0.txt")
if err != nil {
fmt.Printf("Failed to read file: %s\n", err)
return
}
pk := s.G1().Point()
pk.UnmarshalBinary(pkb)
if pk.Equal(pkRec) {
fmt.Println("The MSK and reconstructed MSK are equal")
} else {
fmt.Println("wrong shares or pk", pk, pkRec, recMasterSecretKey)
return
}
}