-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
313 lines (273 loc) · 10 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright (c) 2014-2018 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"os"
"os/signal"
//"runtime/pprof"
"syscall"
"github.com/bitmark-inc/bitmarkd/announce"
"github.com/bitmark-inc/bitmarkd/asset"
"github.com/bitmark-inc/bitmarkd/block"
"github.com/bitmark-inc/bitmarkd/blockheader"
"github.com/bitmark-inc/bitmarkd/chain"
"github.com/bitmark-inc/bitmarkd/mode"
"github.com/bitmark-inc/bitmarkd/payment"
"github.com/bitmark-inc/bitmarkd/peer"
"github.com/bitmark-inc/bitmarkd/proof"
"github.com/bitmark-inc/bitmarkd/publish"
"github.com/bitmark-inc/bitmarkd/reservoir"
"github.com/bitmark-inc/bitmarkd/rpc"
"github.com/bitmark-inc/bitmarkd/storage"
"github.com/bitmark-inc/bitmarkd/zmqutil"
"github.com/bitmark-inc/exitwithstatus"
"github.com/bitmark-inc/getoptions"
"github.com/bitmark-inc/logger"
)
// set by the linker: go build -ldflags "-X main.version=M.N" ./...
var version string = "zero" // do not change this value
// main program
func main() {
// ensure exit handler is first
defer exitwithstatus.Handler()
flags := []getoptions.Option{
{Long: "help", HasArg: getoptions.NO_ARGUMENT, Short: 'h'},
{Long: "verbose", HasArg: getoptions.NO_ARGUMENT, Short: 'v'},
{Long: "quiet", HasArg: getoptions.NO_ARGUMENT, Short: 'q'},
{Long: "version", HasArg: getoptions.NO_ARGUMENT, Short: 'V'},
{Long: "config-file", HasArg: getoptions.REQUIRED_ARGUMENT, Short: 'c'},
{Long: "memory-stats", HasArg: getoptions.NO_ARGUMENT, Short: 'm'},
}
program, options, arguments, err := getoptions.GetOS(flags)
if nil != err {
exitwithstatus.Message("%s: getoptions error: %s", program, err)
}
if len(options["version"]) > 0 {
exitwithstatus.Message("%s: version: %s", program, version)
}
if len(options["help"]) > 0 {
exitwithstatus.Message("usage: %s [--help] [--verbose] [--quiet] --config-file=FILE [[command|help] arguments...]", program)
}
// these commands do not require the configuration and
// process data needed for initial setup
if len(arguments) > 0 && processSetupCommand(arguments) {
return
}
if 1 != len(options["config-file"]) {
exitwithstatus.Message("%s: only one config-file option is required, %d were detected", program, len(options["config-file"]))
}
// read options and parse the configuration file
configurationFile := options["config-file"][0]
masterConfiguration, err := getConfiguration(configurationFile)
if nil != err {
exitwithstatus.Message("%s: failed to read configuration from: %q error: %s", program, configurationFile, err)
}
// these commands require the configuration and
// perform enquires on the configuration
if len(arguments) > 0 && processConfigCommand(arguments, masterConfiguration) {
return
}
// start logging
if err = logger.Initialise(masterConfiguration.Logging); nil != err {
exitwithstatus.Message("%s: logger setup failed with error: %s", program, err)
}
defer logger.Finalise()
// create a logger channel for the main program
log := logger.New("main")
defer log.Info("finished")
log.Info("starting…")
log.Infof("version: %s", version)
log.Debugf("masterConfiguration: %v", masterConfiguration)
// ------------------
// start of real main
// ------------------
// optional PID file
// use if not running under a supervisor program like daemon(8)
if "" != masterConfiguration.PidFile {
lockFile, err := os.OpenFile(masterConfiguration.PidFile, os.O_WRONLY|os.O_EXCL|os.O_CREATE, os.ModeExclusive|0600)
if err != nil {
if os.IsExist(err) {
exitwithstatus.Message("%s: another instance is already running", program)
}
exitwithstatus.Message("%s: PID file: %q creation failed, error: %s", program, masterConfiguration.PidFile, err)
}
fmt.Fprintf(lockFile, "%d\n", os.Getpid())
lockFile.Close()
defer os.Remove(masterConfiguration.PidFile)
}
// set the initial system mode - before any background tasks are started
err = mode.Initialise(masterConfiguration.Chain)
if nil != err {
log.Criticalf("mode initialise error: %s", err)
exitwithstatus.Message("mode initialise error: %s", err)
}
defer mode.Finalise()
// // if requested start profiling
// if "" != masterConfiguration.ProfileFile {
// f, err := os.Create(masterConfiguration.ProfileFile)
// if nil != err {
// log.Criticalf("cannot open profile output file: '%s' error: %s", masterConfiguration.ProfileFile, err)
// exitwithstatus.Exit(1)
// }
// defer f.Close()
// pprof.StartCPUProfile(f)
// defer pprof.StopCPUProfile()
// }
// general info
log.Infof("test mode: %v", mode.IsTesting())
log.Infof("database: %q", masterConfiguration.Database)
// connection info
log.Debugf("%s = %#v", "ClientRPC", masterConfiguration.ClientRPC)
log.Debugf("%s = %#v", "Peering", masterConfiguration.Peering)
log.Debugf("%s = %#v", "Publishing", masterConfiguration.Publishing)
log.Debugf("%s = %#v", "Proofing", masterConfiguration.Proofing)
// start the data storage
log.Info("initialise storage")
reindexRequired, err := storage.Initialise(masterConfiguration.Database.Name, storage.ReadWrite)
if nil != err {
log.Criticalf("storage initialise error: %s", err)
exitwithstatus.Message("storage initialise error: %s", err)
}
defer storage.Finalise()
if reindexRequired {
log.Warn("database reindex required")
}
// start asset cache
err = asset.Initialise()
if nil != err {
log.Criticalf("asset initialise error: %s", err)
exitwithstatus.Message("asset initialise error: %s", err)
}
defer asset.Finalise()
// start the reservoir (verified transaction data cache)
log.Info("initialise reservoir")
err = reservoir.Initialise(masterConfiguration.ReservoirFile)
if nil != err {
log.Criticalf("reservoir initialise error: %s", err)
exitwithstatus.Message("reservoir initialise error: %s", err)
}
defer reservoir.Finalise()
// block header data
log.Info("initialise blockheader")
err = blockheader.Initialise()
if nil != err {
log.Criticalf("blockheader initialise error: %s", err)
exitwithstatus.Message("blockheader initialise error: %s", err)
}
defer blockheader.Finalise()
// block data storage - depends on storage and mode
log.Info("initialise block")
err = block.Initialise(reindexRequired)
if nil != err {
log.Criticalf("block initialise error: %s", err)
exitwithstatus.Message("block initialise error: %s", err)
}
defer block.Finalise()
// if reindexing was done
if reindexRequired {
err = storage.ReindexDone()
if nil != err {
log.Criticalf("index regeneration error: %s", err)
exitwithstatus.Message("index regeneration error: %s", err)
}
log.Warn("index was regenerated")
}
// these commands are allowed to access the internal database
if len(arguments) > 0 && processDataCommand(log, arguments, masterConfiguration) {
return
}
// network announcements need to be before peer and rpc initialisation
log.Info("initialise announce")
nodesDomain := "" // initially none
switch masterConfiguration.Nodes {
case "":
log.Critical("nodes cannot be blank choose from: none, chain or sub.domain.tld")
exitwithstatus.Message("nodes cannot be blank choose from: none, chain or sub.domain.tld")
case "none":
nodesDomain = "" // nodes disabled
case "chain":
switch cn := mode.ChainName(); cn { // ***** FIX THIS: is there a better way?
case chain.Local:
nodesDomain = "nodes.localdomain"
case chain.Testing:
nodesDomain = "nodes.test.bitmark.com"
case chain.Bitmark:
nodesDomain = "nodes.live.bitmark.com"
default:
log.Criticalf("unexpected chain name: %q", cn)
exitwithstatus.Message("unexpected chain name: %q", cn)
}
default:
// domain names are complex to validate so just rely on
// trying to fetch the TXT records for validation
nodesDomain = masterConfiguration.Nodes // just assume it is a domain name
}
err = announce.Initialise(nodesDomain, masterConfiguration.PeerFile)
if nil != err {
log.Criticalf("announce initialise error: %s", err)
exitwithstatus.Message("announce initialise error: %s", err)
}
defer announce.Finalise()
// start payment services
err = payment.Initialise(&masterConfiguration.Payment)
if nil != err {
log.Criticalf("payment initialise error: %s", err)
exitwithstatus.Message("payment initialise error: %s", err)
}
defer payment.Finalise()
// initialise encryption
err = zmqutil.StartAuthentication()
if nil != err {
log.Criticalf("zmq.AuthStart: error: %s", err)
exitwithstatus.Message("zmq.AuthStart: error: %s", err)
}
// start up the peering background processes
err = peer.Initialise(&masterConfiguration.Peering, version)
if nil != err {
log.Criticalf("peer initialise error: %s", err)
exitwithstatus.Message("peer initialise error: %s", err)
}
defer peer.Finalise()
// start up the publishing background processes
err = publish.Initialise(&masterConfiguration.Publishing, version)
if nil != err {
log.Criticalf("publish initialise error: %s", err)
exitwithstatus.Message("publish initialise error: %s", err)
}
defer publish.Finalise()
// start up the rpc background processes
err = rpc.Initialise(&masterConfiguration.ClientRPC, &masterConfiguration.HttpsRPC, version)
if nil != err {
log.Criticalf("rpc initialise error: %s", err)
exitwithstatus.Message("peer initialise error: %s", err)
}
defer rpc.Finalise()
// start proof background processes
err = proof.Initialise(&masterConfiguration.Proofing)
if nil != err {
log.Criticalf("proof initialise error: %s", err)
exitwithstatus.Message("proof initialise error: %s", err)
}
defer proof.Finalise()
// if memory logging enabled
if len(options["memory-stats"]) > 0 {
go memstats()
}
// wait for CTRL-C before shutting down to allow manual testing
if 0 == len(options["quiet"]) {
fmt.Printf("\n\nWaiting for CTRL-C (SIGINT) or 'kill <pid>' (SIGTERM)…")
}
// turn Signals into channel messages
ch := make(chan os.Signal)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
sig := <-ch
log.Infof("received signal: %v", sig)
if 0 == len(options["quiet"]) {
fmt.Printf("\nreceived signal: %v\n", sig)
fmt.Printf("\nshutting down…\n")
}
log.Info("shutting down…")
mode.Set(mode.Stopped)
}