-
Notifications
You must be signed in to change notification settings - Fork 28
/
crux.go
156 lines (130 loc) · 3.6 KB
/
crux.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
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
package main
import (
"github.com/blk-io/crux/api"
"github.com/blk-io/crux/config"
"github.com/blk-io/crux/enclave"
"github.com/blk-io/crux/server"
"github.com/blk-io/crux/storage"
log "github.com/sirupsen/logrus"
"net/http"
"os"
"path"
"strings"
"time"
)
func main() {
config.InitFlags()
args := os.Args
if len(args) == 1 {
exit()
}
for _, arg := range args[1:] {
if strings.Contains(arg, ".conf") {
err := config.LoadConfig(arg)
if err != nil {
log.Fatalln(err)
}
break
}
}
config.ParseCommandLine()
verbosity := 1
if config.GetInt(config.Verbosity) > config.GetInt(config.VerbosityShorthand) {
verbosity = config.GetInt(config.Verbosity)
} else {
verbosity = config.GetInt(config.VerbosityShorthand)
}
var level log.Level
switch verbosity {
case 0:
level = log.FatalLevel
case 1:
level = log.WarnLevel
case 2:
level = log.InfoLevel
default:
level = log.DebugLevel
}
log.SetLevel(level)
keyFile := config.GetString(config.GenerateKeys)
if keyFile != "" {
err := enclave.DoKeyGeneration(keyFile)
if err != nil {
log.Fatalln(err)
}
log.Printf("Key pair successfully written to %s", keyFile)
os.Exit(0)
}
workDir := config.GetString(config.WorkDir)
dbStorage := config.GetString(config.Storage)
ipcFile := config.GetString(config.Socket)
storagePath := path.Join(workDir, dbStorage)
ipcPath := path.Join(workDir, ipcFile)
var db storage.DataStore
var err error
if config.GetBool(config.BerkeleyDb) {
db, err = storage.InitBerkeleyDb(storagePath)
} else {
db, err = storage.InitLevelDb(storagePath)
}
if err != nil {
log.Fatalf("Unable to initialise storage, error: %v", err)
}
defer db.Close()
allOtherNodes := config.GetString(config.OtherNodes)
otherNodes := strings.Split(allOtherNodes, ",")
url := config.GetString(config.Url)
if url == "" {
log.Fatalln("URL must be specified")
}
port := config.GetInt(config.Port)
if port < 0 {
log.Fatalln("Port must be specified")
}
httpClient := &http.Client{
Timeout: time.Second * 10,
}
grpc := config.GetBool(config.UseGRPC)
pi := api.InitPartyInfo(url, otherNodes, httpClient, grpc)
privKeys := config.GetString(config.PrivateKeys)
pubKeys := config.GetString(config.PublicKeys)
pubKeyFiles := strings.Split(pubKeys, ",")
privKeyFiles := strings.Split(privKeys, ",")
if len(privKeyFiles) != len(pubKeyFiles) {
log.Fatalln("Private keys provided must have corresponding public keys")
}
if len(privKeyFiles) == 0 {
log.Fatalln("Node key files must be provided")
}
for i, keyFile := range privKeyFiles {
privKeyFiles[i] = path.Join(workDir, keyFile)
}
for i, keyFile := range pubKeyFiles {
pubKeyFiles[i] = path.Join(workDir, keyFile)
}
enc := enclave.Init(db, pubKeyFiles, privKeyFiles, pi, http.DefaultClient, grpc)
pi.RegisterPublicKeys(enc.PubKeys)
tls := config.GetBool(config.Tls)
var tlsCertFile, tlsKeyFile string
if tls {
servCert := config.GetString(config.TlsServerCert)
servKey := config.GetString(config.TlsServerKey)
if (len(servCert) != len(servKey)) || (len(servCert) <= 0) {
log.Fatalf("Please provide server certificate and key for TLS %s %s %d ", servKey, servCert, len(servCert))
}
tlsCertFile = path.Join(workDir, servCert)
tlsKeyFile = path.Join(workDir, servKey)
}
grpcJsonport := config.GetInt(config.GrpcJsonPort)
networkInterface := config.GetString(config.NetworkInterface)
_, err = server.Init(enc, networkInterface, port, ipcPath, grpc, grpcJsonport, tls, tlsCertFile, tlsKeyFile)
if err != nil {
log.Fatalf("Error starting server: %v\n", err)
}
pi.PollPartyInfo()
select {}
}
func exit() {
config.Usage()
os.Exit(1)
}