-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
274 lines (228 loc) · 8.42 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
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2020 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"
"syscall"
"github.com/bitmark-inc/bitmarkd/mode"
"github.com/bitmark-inc/bitmarkd/util"
"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 = "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'},
}
program, options, arguments, err := getoptions.GetOS(flags)
if nil != err {
exitwithstatus.Message("%s: getoptions error: %s", program, err)
}
if len(options["version"]) > 0 {
processSetupCommand(program, []string{"version"})
return
}
if len(options["help"]) > 0 {
processSetupCommand(program, []string{"help"})
return
}
// command processing - need lock so do not affect an already running process
// these commands don't require the configuration and
// process data needed for initial setup
if len(arguments) > 0 {
if processSetupCommand(program, 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]
reader := newConfigReader()
rescheduleChannel := make(chan struct{})
calendar := newJobCalendar(rescheduleChannel)
reader.SetCalendar(calendar)
err = reader.FirstRefresh(configurationFile)
if nil != err {
exitwithstatus.Message("%s: failed to read configuration from: %q error: %s", program, configurationFile, err)
}
theConfiguration, err := reader.GetConfig()
if nil != err {
exitwithstatus.Message("%s: configuration is not found", program)
}
// start logging
if err = logger.Initialise(theConfiguration.Logging); nil != err {
exitwithstatus.Message("%s: logger setup failed with error: %s", program, err)
}
defer logger.Finalise()
watcherChannel := WatcherChannel{
change: make(chan struct{}, 1),
remove: make(chan struct{}, 1),
}
watcher, err := newFileWatcher(configurationFile, logger.New(FileWatcherLoggerPrefix), watcherChannel)
if nil != err {
exitwithstatus.Message("%s: file watcher setup failed with error: %s",
program,
err,
)
}
reader.SetWatcher(watcher)
configLogger := logger.New(ReaderLoggerPrefix)
err = reader.SetLog(configLogger)
if nil != err {
exitwithstatus.Message("%s: new logger '%s' failed with error: %s", program, ReaderLoggerPrefix, err)
}
calendarLogger := logger.New(jobCalendarPrefix)
calendar.SetLog(calendarLogger)
// create a logger channel for the main program
log := logger.New("main")
defer log.Info("shutting down…")
log.Info("starting…")
log.Infof("version: %s", version)
log.Debugf("theConfiguration: %v", theConfiguration)
// ------------------
// start of real main
// ------------------
// optional PID file
// use if not running under a supervisor program like daemon(8)
if "" != theConfiguration.PidFile {
lockFile, err := os.OpenFile(theConfiguration.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, theConfiguration.PidFile, err)
}
fmt.Fprintf(lockFile, "%d\n", os.Getpid())
lockFile.Close()
defer os.Remove(theConfiguration.PidFile)
}
// // if requested start profiling
// if "" != theConfiguration.ProfileFile {
// f, err := os.Create(theConfiguration.ProfileFile)
// if nil != err {
// log.Criticalf("cannot open profile output file: '%s' error: %s", theConfiguration.ProfileFile, err)
// exitwithstatus.Exit(1)
// }
// defer f.Close()
// pprof.StartCPUProfile(f)
// defer pprof.StopCPUProfile()
// }
// set the initial system mode - before any background tasks are started
mode.Initialise(theConfiguration.Chain)
defer mode.Finalise()
// ensure keys are set
if "" == theConfiguration.Peering.PublicKey || "" == theConfiguration.Peering.PrivateKey {
exitwithstatus.Message("%s: both peering Public and Private keys must be specified", program)
}
publicKey, err := zmqutil.ReadPublicKey(theConfiguration.Peering.PublicKey)
if nil != err {
log.Criticalf("read error on: %s error: %s", theConfiguration.Peering.PublicKey, err)
exitwithstatus.Message("%s: failed reading Public Key: %q error: %s", program, theConfiguration.Peering.PublicKey, err)
}
privateKey, err := zmqutil.ReadPrivateKey(theConfiguration.Peering.PrivateKey)
if nil != err {
log.Criticalf("read error on: %s error: %s", theConfiguration.Peering.PrivateKey, err)
exitwithstatus.Message("%s: failed reading Private Key: %q error: %s", program, theConfiguration.Peering.PrivateKey, err)
}
// general info
log.Infof("test mode: %v", mode.IsTesting())
// keys
log.Tracef("public key: %x", publicKey)
log.Tracef("private key: %x", privateKey)
// connection info
log.Debugf("%s = %#v", "Peering", theConfiguration.Peering)
// internal queues
ProofProxy()
SubmitQueue()
proofer := newProofer(logger.New(prooferLoggerPrefix), reader)
reader.SetProofer(proofer)
// start background processes
// these will has blocks, changing nonce to meet difficulty
// then submit a block to the right bitmarkd for verification
proofer.StartHashing()
// initialise encryption
err = zmqutil.StartAuthentication()
if nil != err {
log.Criticalf("zmq.AuthStart(): error: %s", err)
exitwithstatus.Message("%s: zmq.AuthStart() error: %s", program, err)
}
managerLogger := logger.New(JobManagerPrefix)
jobManager := newJobManager(
calendar,
proofer,
rescheduleChannel,
managerLogger,
)
jobManager.Start()
watcher.Start()
reader.FirstTimeRun()
reader.Start()
// start up bitmarkd clients these subscribe to bitmarkd
// blocks publisher to obtain blocks for mining
connection_setup:
for i, remote := range theConfiguration.Peering.Connect {
serverPublicKey, err := zmqutil.ReadPublicKey(remote.PublicKey)
if nil != err {
log.Warnf("client: %d invalid server publickey: %q error: %s", i, remote.PublicKey, err)
continue connection_setup
}
bc, err := util.NewConnection(remote.Blocks)
if nil != err {
log.Warnf("client: %d invalid blocks publisher: %q error: %s", i, remote.Blocks, err)
continue connection_setup
}
blocksAddress, blocksv6 := bc.CanonicalIPandPort("tcp://")
sc, err := util.NewConnection(remote.Submit)
if nil != err {
log.Warnf("client: %d invalid submit address: %q error: %s", i, remote.Submit, err)
continue connection_setup
}
submitAddress, submitv6 := sc.CanonicalIPandPort("tcp://")
log.Infof("client: %d subscribe: %q submit: %q", i, remote.Blocks, remote.Submit)
mlog := logger.New(fmt.Sprintf("submitter-%d", i))
err = Submitter(i, submitAddress, submitv6, serverPublicKey, publicKey, privateKey, mlog)
if nil != err {
log.Warnf("submitter: %d failed error: %s", i, err)
continue connection_setup
}
slog := logger.New(fmt.Sprintf("subscriber-%d", i))
err = Subscribe(i, blocksAddress, blocksv6, serverPublicKey, publicKey, privateKey, slog, proofer)
if nil != err {
log.Warnf("subscribe: %d failed error: %s", i, err)
continue connection_setup
}
}
// erase the private key from memory
//nolint:ignore SA4006 we want to make sure we clean privateKey
privateKey = []byte{}
// abort if no clients were connected
// 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, 1)
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")
}
}