-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
executable file
·182 lines (142 loc) · 4.48 KB
/
node.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
package neatcon
import (
"io/ioutil"
"os"
"strings"
"github.com/neatlab/neatio/chain/consensus/neatcon/consensus"
"github.com/neatlab/neatio/chain/consensus/neatcon/epoch"
"github.com/neatlab/neatio/chain/consensus/neatcon/types"
"github.com/neatlab/neatio/chain/core"
"github.com/neatlab/neatio/chain/log"
"github.com/neatlab/neatio/params"
cmn "github.com/neatlib/common-go"
cfg "github.com/neatlib/config-go"
dbm "github.com/neatlib/db-go"
)
type Node struct {
cmn.BaseService
privValidator *types.PrivValidator
epochDB dbm.DB
evsw types.EventSwitch
consensusState *consensus.ConsensusState
consensusReactor *consensus.ConsensusReactor
cch core.CrossChainHelper
logger log.Logger
}
func NewNodeNotStart(backend *backend, config cfg.Config, chainConfig *params.ChainConfig, cch core.CrossChainHelper, genDoc *types.GenesisDoc) *Node {
var privValidator *types.PrivValidator
privValidatorFile := config.GetString("priv_validator_file")
if _, err := os.Stat(privValidatorFile); err == nil {
privValidator = types.LoadPrivValidator(privValidatorFile)
}
epochDB := dbm.NewDB("epoch", config.GetString("db_backend"), config.GetString("db_dir"))
ep := epoch.InitEpoch(epochDB, genDoc, backend.logger)
if privValidator != nil && ep.Validators.HasAddress(privValidator.Address[:]) {
backend.shouldStart = true
} else {
backend.shouldStart = false
}
consensusState := consensus.NewConsensusState(backend, config, chainConfig, cch, ep)
if privValidator != nil {
consensusState.SetPrivValidator(privValidator)
}
consensusReactor := consensus.NewConsensusReactor(consensusState)
eventSwitch := types.NewEventSwitch()
SetEventSwitch(eventSwitch, consensusReactor)
node := &Node{
privValidator: privValidator,
epochDB: epochDB,
evsw: eventSwitch,
cch: cch,
consensusState: consensusState,
consensusReactor: consensusReactor,
logger: backend.logger,
}
node.BaseService = *cmn.NewBaseService(backend.logger, "Node", node)
return node
}
func (n *Node) OnStart() error {
n.logger.Info("(n *Node) OnStart()")
if n.privValidator == nil {
return ErrNoPrivValidator
}
_, err := n.evsw.Start()
if err != nil {
n.logger.Errorf("Failed to start switch: %v", err)
return err
}
_, err = n.consensusReactor.Start()
if err != nil {
n.logger.Errorf("Failed to start Consensus Reactor. Error: %v", err)
return err
}
n.consensusReactor.AfterStart()
return nil
}
func (n *Node) OnStop() {
n.logger.Info("(n *Node) OnStop() called")
n.BaseService.OnStop()
n.evsw.Stop()
n.consensusReactor.Stop()
}
func (n *Node) RunForever() {
cmn.TrapSignal(func() {
n.Stop()
})
}
func SetEventSwitch(evsw types.EventSwitch, eventables ...types.Eventable) {
for _, e := range eventables {
e.SetEventSwitch(evsw)
}
}
func (n *Node) ConsensusState() *consensus.ConsensusState {
return n.consensusState
}
func (n *Node) ConsensusReactor() *consensus.ConsensusReactor {
return n.consensusReactor
}
func (n *Node) EventSwitch() types.EventSwitch {
return n.evsw
}
func (n *Node) PrivValidator() *types.PrivValidator {
return n.privValidator
}
func ProtocolAndAddress(listenAddr string) (string, string) {
protocol, address := "tcp", listenAddr
parts := strings.SplitN(address, "://", 2)
if len(parts) == 2 {
protocol, address = parts[0], parts[1]
}
return protocol, address
}
func MakeNeatConNode(backend *backend, config cfg.Config, chainConfig *params.ChainConfig, cch core.CrossChainHelper) *Node {
var genDoc *types.GenesisDoc
genDocFile := config.GetString("genesis_file")
if !cmn.FileExists(genDocFile) {
if chainConfig.NeatChainId == params.MainnetChainConfig.NeatChainId {
genDoc, _ = types.GenesisDocFromJSON([]byte(types.MainnetGenesisJSON))
} else if chainConfig.NeatChainId == params.TestnetChainConfig.NeatChainId {
genDoc, _ = types.GenesisDocFromJSON([]byte(types.TestnetGenesisJSON))
} else {
return nil
}
} else {
genDoc = readGenesisFromFile(genDocFile)
}
config.Set("chain_id", genDoc.ChainID)
return NewNodeNotStart(backend, config, chainConfig, cch, genDoc)
}
func readGenesisFromFile(genDocFile string) *types.GenesisDoc {
jsonBlob, err := ioutil.ReadFile(genDocFile)
if err != nil {
cmn.Exit(cmn.Fmt("Couldn't read GenesisDoc file: %v", err))
}
genDoc, err := types.GenesisDocFromJSON(jsonBlob)
if err != nil {
cmn.PanicSanity(cmn.Fmt("Genesis doc parse json error: %v", err))
}
if genDoc.ChainID == "" {
cmn.PanicSanity(cmn.Fmt("Genesis doc %v must include non-empty chain_id", genDocFile))
}
return genDoc
}