forked from copernet/copernicus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (101 loc) · 3.16 KB
/
main.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
// Copyright (c) 2013-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"context"
"errors"
"fmt"
"github.com/copernet/copernicus/log"
"net/http"
_ "net/http/pprof"
"os"
"runtime"
"runtime/debug"
"github.com/copernet/copernicus/conf"
"github.com/copernet/copernicus/model"
"github.com/copernet/copernicus/net/limits"
"github.com/copernet/copernicus/net/server"
"github.com/copernet/copernicus/rpc"
"github.com/copernet/copernicus/util"
"net"
)
const (
// blockDbNamePrefix is the prefix for the block database name. The
// database type is appended to this value to form the full block
// database name.
blockDbNamePrefix = "blocks"
)
// bchMain is the real main function for copernicus. It is necessary to work around
// the fact that deferred functions do not run when os.Exit() is called.
func bchMain(ctx context.Context, args []string) error {
// Load configuration and parse command line. This function also
// initializes logging and configures it accordingly.
appInitMain(args)
go func() {
listenAddr := net.JoinHostPort(conf.Cfg.PProf.IP, conf.Cfg.PProf.Port)
fmt.Printf("Profile server listening on %s\n", listenAddr)
profileRedirect := http.RedirectHandler("/debug/pprof", http.StatusSeeOther)
http.Handle("/", profileRedirect)
err := fmt.Errorf("%v", http.ListenAndServe(listenAddr, nil))
fmt.Println(err.Error())
}()
interrupt := interruptListener()
timeSource := util.GetMedianTimeSource()
s, err := server.NewServer(model.ActiveNetParams, timeSource, interrupt)
if err != nil {
fmt.Printf("Init server error: %s \n", err.Error())
return err
}
var rpcServer *rpc.Server
if !conf.Cfg.P2PNet.DisableRPC {
rpcServer, err = rpc.InitRPCServer(timeSource)
if err != nil {
return errors.New("failed to init rpc")
}
// Start the rebroadcastHandler, which ensures user tx received by
// the RPC server are rebroadcast until being included in a block.
//go s.rebroadcastHandler()
rpcServer.Start()
}
server.SetMsgHandle(context.TODO(), s.MsgChan, s)
if interruptRequested(interrupt) {
return nil
}
s.Start()
defer func() {
s.Stop()
// Shutdown the RPC server if it's not disabled.
if !conf.Cfg.P2PNet.DisableRPC {
rpcServer.Stop()
}
}()
go func() {
<-rpcServer.RequestedProcessShutdown()
shutdownRequestChannel <- struct{}{}
}()
<-interrupt
return nil
}
func main() {
// Use all processor cores.
runtime.GOMAXPROCS(runtime.NumCPU())
// Block and transaction processing can cause bursty allocations. This
// limits the garbage collector from excessively overallocating during
// bursts. This value was arrived at with the help of profiling live
// usage.
debug.SetGCPercent(10)
// Up some limits.
if runtime.GOOS != "plan9" && runtime.GOOS != "windows" {
if err := limits.SetLimits(); err != nil {
fmt.Fprintf(os.Stderr, "failed to set limits: %v\n", err)
os.Exit(1)
}
}
args := os.Args
// Work around defer not working after os.Exit()
if err := bchMain(context.Background(), args); err != nil {
log.Error("bchMain failed with err %+v", err)
panic("bchMain failed with err: " + err.Error())
}
}