forked from 0xakk0r0kamui/constant-chain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
incognito.go
167 lines (161 loc) · 5.12 KB
/
incognito.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
package main
import (
"fmt"
"github.com/incognitochain/incognito-chain/common"
"log"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"strconv"
"github.com/incognitochain/incognito-chain/database"
_ "github.com/incognitochain/incognito-chain/database/lvdb"
"github.com/incognitochain/incognito-chain/databasemp"
_ "github.com/incognitochain/incognito-chain/databasemp/lvdb"
"github.com/incognitochain/incognito-chain/limits"
"github.com/incognitochain/incognito-chain/wallet"
)
var (
cfg *config
)
// winServiceMain is only invoked on Windows. It detects when incognito network is running
// as a service and reacts accordingly.
var winServiceMain func() (bool, error)
// mainMaster is the real main function for Incognito network. It is necessary to work around
// the fact that deferred functions do not run when os.Exit() is called. The
// optional serverChan parameter is mainly used by the service code to be
// notified with the server once it is setup so it can gracefully stop it when
// requested from the service control manager.
func mainMaster(serverChan chan<- *Server) error {
tempConfig, _, err := loadConfig()
if err != nil {
log.Println("Load config error")
log.Println(err)
return err
}
cfg = tempConfig
// Get a channel that will be closed when a shutdown signal has been
// triggered either from an OS signal such as SIGINT (Ctrl+C) or from
// another subsystem such as the RPC server.
interrupt := interruptListener()
defer Logger.log.Warn("Shutdown complete")
// Show version at startup.
version := version()
Logger.log.Infof("Version %s", version)
// Return now if an interrupt signal was triggered.
if interruptRequested(interrupt) {
return nil
}
db, err := database.Open("leveldb", filepath.Join(cfg.DataDir, cfg.DatabaseDir))
// Create db and use it.
if err != nil {
Logger.log.Error("could not open connection to leveldb")
Logger.log.Error(err)
panic(err)
}
// Create db for mempool and use it
dbmp, err := databasemp.Open("leveldbmempool", filepath.Join(cfg.DataDir, cfg.DatabaseMempoolDir))
if err != nil {
Logger.log.Error("could not open connection to leveldb")
Logger.log.Error(err)
panic(err)
}
// Check wallet and start it
var walletObj *wallet.Wallet
if cfg.Wallet {
walletObj = &wallet.Wallet{}
walletConf := wallet.WalletConfig{
DataDir: cfg.DataDir,
DataFile: cfg.WalletName,
DataPath: filepath.Join(cfg.DataDir, cfg.WalletName),
IncrementalFee: 0, // 0 mili PRV
}
if cfg.WalletShardID >= 0 {
// check shardID of wallet
temp := byte(cfg.WalletShardID)
walletConf.ShardID = &temp
}
walletObj.SetConfig(&walletConf)
err = walletObj.LoadWallet(cfg.WalletPassphrase)
if err != nil {
if cfg.WalletAutoInit {
Logger.log.Critical("\n **** Auto init wallet flag is TRUE ****\n")
walletObj.Init(cfg.WalletPassphrase, 0, cfg.WalletName)
walletObj.Save(cfg.WalletPassphrase)
} else {
// write log and exit when can not load wallet
Logger.log.Criticalf("Can not load wallet with %s. Please use incognitoctl to create a new wallet", walletObj.GetConfig().DataPath)
return err
}
}
}
// Create server and start it.
server := Server{}
server.wallet = walletObj
err = server.NewServer(cfg.Listener, db, dbmp, activeNetParams.Params, version, interrupt)
if err != nil {
Logger.log.Errorf("Unable to start server on %+v", cfg.Listener)
Logger.log.Error(err)
return err
}
defer func() {
Logger.log.Warn("Gracefully shutting down the server...")
server.Stop()
server.WaitForShutdown()
Logger.log.Warn("Server shutdown complete")
}()
server.Start()
if serverChan != nil {
serverChan <- &server
}
// Check Metric analyzation system
env := os.Getenv("GrafanaURL")
if env != "" {
Logger.log.Criticalf("Metric Server: %+v", os.Getenv("GrafanaURL"))
}
// Wait until the interrupt signal is received from an OS signal or
// shutdown is requested through one of the subsystems such as the RPC
// server.
<-interrupt
return nil
}
func main() {
limitThreads := os.Getenv("CPU")
if limitThreads == "" {
runtime.GOMAXPROCS(runtime.NumCPU())
} else {
numThreads, err := strconv.Atoi(limitThreads)
if err != nil {
panic(err)
}
runtime.GOMAXPROCS(numThreads)
}
fmt.Println("NumCPU", 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(30)
// Up some limits.
if err := limits.SetLimits(); err != nil {
fmt.Fprintf(os.Stderr, "failed to set limits: %+v\n", err)
os.Exit(common.ExitByOs)
}
// Call serviceMain on Windows to handle running as a service. When
// the return isService flag is true, exit now since we ran as a
// service. Otherwise, just fall through to normal operation.
if runtime.GOOS == "windows" {
isService, err := winServiceMain()
if err != nil {
fmt.Println(err)
os.Exit(common.ExitByOs)
}
if isService {
os.Exit(common.ExitCodeUnknow)
}
}
// Work around defer not working after os.Exit()
if err := mainMaster(nil); err != nil {
os.Exit(common.ExitByOs)
}
}