-
Notifications
You must be signed in to change notification settings - Fork 2
/
iserver.go
102 lines (88 loc) · 2.13 KB
/
iserver.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
package iserver
import (
"github.com/empow-blockchain/go-empow/chainbase"
"github.com/empow-blockchain/go-empow/common"
"github.com/empow-blockchain/go-empow/consensus"
"github.com/empow-blockchain/go-empow/core/tx"
"github.com/empow-blockchain/go-empow/ilog"
"github.com/empow-blockchain/go-empow/metrics/exporter"
"github.com/empow-blockchain/go-empow/p2p"
"github.com/empow-blockchain/go-empow/rpc"
)
// Service defines APIs of resident goroutines.
type Service interface {
Start() error
Stop()
}
// IServer is application for EMPOW.
type IServer struct {
config *common.Config
cBase *chainbase.ChainBase
p2p *p2p.NetService
rpcServer *rpc.Server
consensus consensus.Consensus
debug *DebugServer
exporter *exporter.Exporter
}
// New returns a iserver application
func New(conf *common.Config) *IServer {
tx.ChainID = conf.P2P.ChainID
cBase, err := chainbase.New(conf)
if err != nil {
ilog.Fatalf("New chainbase failed: %v.", err)
}
exporter := exporter.New()
p2pService, err := p2p.NewNetService(conf.P2P)
if err != nil {
ilog.Fatalf("network initialization failed, stop the program! err:%v", err)
}
consensus := consensus.New(consensus.Pob, conf, cBase, p2pService)
rpcServer := rpc.New(cBase.TxPool(), cBase, conf, p2pService)
debug := NewDebugServer(conf.Debug, p2pService, cBase.BlockCache(), cBase.BlockChain())
return &IServer{
config: conf,
cBase: cBase,
p2p: p2pService,
rpcServer: rpcServer,
consensus: consensus,
debug: debug,
exporter: exporter,
}
}
// Start starts iserver application.
func (s *IServer) Start() error {
Services := []Service{
s.p2p,
s.consensus,
s.rpcServer,
}
for _, s := range Services {
if err := s.Start(); err != nil {
return err
}
}
conf := s.config
if conf.Debug != nil {
if err := s.debug.Start(); err != nil {
return err
}
}
return nil
}
// Stop stops iserver application.
func (s *IServer) Stop() {
conf := s.config
if conf.Debug != nil {
s.debug.Stop()
}
Services := []Service{
s.rpcServer,
s.consensus,
s.p2p,
}
for _, s := range Services {
s.Stop()
}
s.exporter.Close()
s.cBase.Close()
}