-
Notifications
You must be signed in to change notification settings - Fork 13
/
setup.go
119 lines (88 loc) · 2.74 KB
/
setup.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
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2019 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package publish
import (
"sync"
"github.com/bitmark-inc/bitmarkd/background"
"github.com/bitmark-inc/bitmarkd/fault"
"github.com/bitmark-inc/bitmarkd/zmqutil"
"github.com/bitmark-inc/logger"
)
// Configuration - a block of configuration data
// this is read from the configuration file
type Configuration struct {
Broadcast []string `gluamapper:"broadcast" json:"broadcast"`
PrivateKey string `gluamapper:"private_key" json:"private_key"`
PublicKey string `gluamapper:"public_key" json:"public_key"`
}
// globals for background process
type publishData struct {
sync.RWMutex // to allow locking
log *logger.L // logger
brdc broadcaster // for broadcasting blocks, transactions etc.
publicKey []byte
// for background
background *background.T
// set once during initialise
initialised bool
}
// global data
var globalData publishData
// Initialise - setup peer background processes
func Initialise(configuration *Configuration, version string) error {
globalData.Lock()
defer globalData.Unlock()
// no need to start if already started
if globalData.initialised {
return fault.AlreadyInitialised
}
globalData.log = logger.New("publish")
globalData.log.Info("starting…")
if 0 == len(configuration.Broadcast) {
globalData.log.Info("no broadcasts - disabling")
return nil
}
// read the keys
privateKey, err := zmqutil.ReadPrivateKey(configuration.PrivateKey)
if nil != err {
globalData.log.Errorf("read private key file: %q error: %s", configuration.PrivateKey, err)
return err
}
publicKey, err := zmqutil.ReadPublicKey(configuration.PublicKey)
if nil != err {
globalData.log.Errorf("read public key file: %q error: %s", configuration.PublicKey, err)
return err
}
globalData.log.Tracef("private key: %q", privateKey)
globalData.log.Tracef("public key: %q", publicKey)
globalData.publicKey = publicKey
if err := globalData.brdc.initialise(privateKey, publicKey, configuration.Broadcast); nil != err {
return err
}
// all data initialised
globalData.initialised = true
// start background processes
globalData.log.Info("start background…")
processes := background.Processes{
&globalData.brdc,
}
globalData.background = background.Start(processes, globalData.log)
return nil
}
// Finalise - stop all background tasks
func Finalise() error {
if !globalData.initialised {
return fault.NotInitialised
}
globalData.log.Info("shutting down…")
globalData.log.Flush()
// stop background
globalData.background.Stop()
// finally...
globalData.initialised = false
globalData.log.Info("finished")
globalData.log.Flush()
return nil
}