-
Notifications
You must be signed in to change notification settings - Fork 212
/
p2p.go
160 lines (136 loc) · 4.05 KB
/
p2p.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
// package p2p cmd is the main executable for running p2p tests and simulations
package main
import (
"fmt"
"github.com/spacemeshos/go-spacemesh/api"
"github.com/spacemeshos/go-spacemesh/api/grpcserver"
cmdp "github.com/spacemeshos/go-spacemesh/cmd"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/metrics"
"github.com/spacemeshos/go-spacemesh/p2p"
"github.com/spf13/cobra"
"io"
"net/http"
_ "net/http/pprof"
"os"
)
// Cmd is the p2p cmd
var Cmd = &cobra.Command{
Use: "p2p",
Short: "start p2p",
Run: func(cmd *cobra.Command, args []string) {
p2pApp := NewP2PApp()
defer p2pApp.Cleanup()
p2pApp.Initialize(cmd)
p2pApp.Start(cmd, args)
},
}
func init() {
cmdp.AddCommands(Cmd)
}
// P2PApp is an app struct used to run only p2p functionality
type P2PApp struct {
*cmdp.BaseApp
p2p p2p.Service
closers []io.Closer
}
// NewP2PApp creates the base app
func NewP2PApp() *P2PApp {
return &P2PApp{BaseApp: cmdp.NewBaseApp()}
}
// Cleanup closes all services
func (app *P2PApp) Cleanup() {
for _, c := range app.closers {
err := c.Close()
if err != nil {
log.With().Warning("error closing service", log.Err(err))
}
}
// TODO: move to array of cleanup functions and execute all here
}
// Start creates a p2p instance and starts it with testing features enabled by the config.
func (app *P2PApp) Start(cmd *cobra.Command, args []string) {
// init p2p services
log.JSONLog(true)
log.DebugMode(true)
log.Info("Initializing P2P services")
logger := log.NewDefault("P2P_Test")
swarm, err := p2p.New(cmdp.Ctx, app.Config.P2P, logger, app.Config.DataDir())
if err != nil {
log.Panic("Error init p2p services, err: %v", err)
}
app.p2p = swarm
// Testing stuff
api.ApproveAPIGossipMessages(cmdp.Ctx, app.p2p)
metrics.StartCollectingMetrics(app.Config.MetricsPort)
// start the node
err = app.p2p.Start()
defer app.p2p.Shutdown()
if err != nil {
log.Panic("Error starting p2p services, err: %v", err)
}
if app.Config.PprofHTTPServer {
pprof := &http.Server{}
pprof.Addr = ":6060"
pprof.Handler = nil
go func() { err := pprof.ListenAndServe(); log.Error("error running pprof server err=%v", err) }()
app.closers = append(app.closers, pprof)
}
app.startAPI()
<-cmdp.Ctx.Done()
}
func (app *P2PApp) startAPI() {
apiConf := &app.Config.API
// Make sure we only create the server once.
var grpcSvc *grpcserver.Server
registerService := func(svc grpcserver.ServiceAPI) {
if grpcSvc == nil {
grpcSvc = grpcserver.NewServerWithInterface(apiConf.GrpcServerPort, apiConf.GrpcServerInterface)
}
svc.RegisterService(grpcSvc)
}
// Register the requested services one by one
// We only support a subset of API services: gateway, globalstate, transaction
if apiConf.StartMeshService || apiConf.StartNodeService || apiConf.StartSmesherService {
log.Panic("unsupported GRPC API service requested")
}
if apiConf.StartGatewayService {
registerService(grpcserver.NewGatewayService(app.p2p))
}
if apiConf.StartGlobalStateService {
registerService(grpcserver.NewGlobalStateService(nil, nil))
}
if apiConf.StartTransactionService {
registerService(grpcserver.NewTransactionService(app.p2p, nil, nil, nil))
}
// Now that the services are registered, start the server.
if grpcSvc != nil {
grpcSvc.Start()
app.closers = append(app.closers, grpcSvc)
}
var jsonSvc *grpcserver.JSONHTTPServer
if apiConf.StartJSONServer {
if grpcSvc == nil {
// This panics because it should not happen.
// It should be caught inside apiConf.
log.Panic("one or more new GRPC services must be enabled with new JSON gateway server.")
}
jsonSvc = grpcserver.NewJSONHTTPServer(apiConf.JSONServerPort, apiConf.GrpcServerPort)
jsonSvc.StartService(
apiConf.StartDebugService,
apiConf.StartGatewayService,
apiConf.StartGlobalStateService,
apiConf.StartMeshService,
apiConf.StartNodeService,
apiConf.StartSmesherService,
apiConf.StartTransactionService,
)
app.closers = append(app.closers, jsonSvc)
}
}
func main() {
if err := Cmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}