-
Notifications
You must be signed in to change notification settings - Fork 212
/
p2p.go
119 lines (99 loc) · 2.86 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
// package p2p cmd is the main executable for running p2p tests and simulations
package main
import (
"fmt"
"github.com/spacemeshos/go-spacemesh/api"
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.Warning("Error when closing service err=%v")
}
}
// 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)
}
// start api servers
if app.Config.API.StartGrpcServer || app.Config.API.StartJSONServer {
// start grpc if specified or if json rpc specified
log.Info("Started the GRPC Service")
grpc := api.NewGrpcService(app.Config.API.GrpcServerPort, app.p2p, nil, nil, nil, nil, nil, nil, nil, 0, nil, nil, nil)
grpc.StartService()
app.closers = append(app.closers, grpc)
}
if app.Config.API.StartJSONServer {
log.Info("Started the JSON Service")
json := api.NewJSONHTTPServer(app.Config.API.JSONServerPort, app.Config.API.GrpcServerPort)
json.StartService()
app.closers = append(app.closers, json)
}
<-cmdp.Ctx.Done()
}
func main() {
if err := Cmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}