/
incognito.go
291 lines (267 loc) · 9.1 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"strconv"
"github.com/incognitochain/incognito-chain/dataaccessobject/rawdb_consensus"
"github.com/incognitochain/incognito-chain/metadata/evmcaller"
"github.com/incognitochain/incognito-chain/pruner"
"github.com/incognitochain/incognito-chain/config"
"github.com/incognitochain/incognito-chain/metrics/monitor"
"github.com/incognitochain/incognito-chain/portal"
bnbrelaying "github.com/incognitochain/incognito-chain/relaying/bnb"
"github.com/incognitochain/incognito-chain/utils"
"github.com/btcsuite/btcd/chaincfg"
"github.com/incognitochain/incognito-chain/blockchain"
"github.com/incognitochain/incognito-chain/databasemp"
_ "github.com/incognitochain/incognito-chain/databasemp/lvdb"
"github.com/incognitochain/incognito-chain/incdb"
_ "github.com/incognitochain/incognito-chain/incdb/lvdb"
"github.com/incognitochain/incognito-chain/limits"
btcrelaying "github.com/incognitochain/incognito-chain/relaying/btc"
"github.com/incognitochain/incognito-chain/wallet"
)
//go:generate mockery -dir=incdb/ -name=Database
// winServiceMain is only invoked on Windows. It detects when incognito network is running
// as a service and reacts accordingly.
var winServiceMain func() (bool, error)
func getBTCRelayingChain(btcRelayingChainID, btcDataFolderName string) (*btcrelaying.BlockChain, error) {
relayingChainParams := map[string]*chaincfg.Params{
portal.TestnetBTCChainID: btcrelaying.GetTestNet3Params(),
portal.Testnet2BTCChainID: btcrelaying.GetTestNet3ParamsForInc2(),
portal.MainnetBTCChainID: btcrelaying.GetMainNetParams(),
}
relayingChainGenesisBlkHeight := map[string]int32{
portal.TestnetBTCChainID: int32(2063133),
portal.Testnet2BTCChainID: int32(2064989),
portal.MainnetBTCChainID: int32(697298),
}
return btcrelaying.GetChainV2(
filepath.Join(config.Config().DataDir, btcDataFolderName),
relayingChainParams[btcRelayingChainID],
relayingChainGenesisBlkHeight[btcRelayingChainID],
)
}
func getBNBRelayingChainState(bnbRelayingChainID string) (*bnbrelaying.BNBChainState, error) {
bnbChainState := new(bnbrelaying.BNBChainState)
err := bnbChainState.LoadBNBChainState(
filepath.Join(config.Config().DataDir, "bnbrelayingv3"),
bnbRelayingChainID,
)
if err != nil {
log.Printf("Error getBNBRelayingChainState: %v\n", err)
return nil, err
}
return bnbChainState, nil
}
// 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 {
//read basic config from file or flag
cfg := config.LoadConfig()
// Initialize log rotation. After log rotation has been initialized, the
// logger variables may be used.
initLogRotator(cfg.LogFileName)
// Parse, validate, and set debug log level(s).
if err := parseAndSetDebugLevels(cfg.LogLevel); err != nil {
fmt.Fprintln(os.Stderr, err)
panic(err)
}
config.LoadParam()
portal.SetupParam()
err := wallet.InitPublicKeyBurningAddressByte()
if err != nil {
Logger.log.Error(err)
panic(err)
}
//create genesis block
blockchain.CreateGenesisBlocks()
// 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 := incdb.OpenMultipleDB("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)
}
p := pruner.NewPrunerManager(db)
if p == nil { //cannot init pruner
return nil
}
//check if offline prune flag is available
if config.Config().OfflinePrune {
p.OfflinePrune()
return nil
}
// Create db for mempool and use it
consensusDB, err := incdb.Open("leveldb", filepath.Join(cfg.DataDir, "consensus"))
if err != nil {
Logger.log.Error("could not open connection to leveldb")
Logger.log.Error(err)
panic(err)
}
rawdb_consensus.SetConsensusDatabase(consensusDB)
// Create db for mempool and use it
dbmp, err := databasemp.Open("leveldbmempool", filepath.Join(cfg.DataDir, cfg.MempoolDir))
if err != nil {
Logger.log.Error("could not open connection to leveldb! Clear mempool DB")
Logger.log.Error(err)
panic(err)
}
// Check wallet and start it
var walletObj *wallet.Wallet
if cfg.EnableWallet {
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 btcrelaying chain
btcChain, err := getBTCRelayingChain(
portal.GetPortalParams().RelayingParam.BTCRelayingHeaderChainID,
portal.GetPortalParams().RelayingParam.BTCDataFolderName,
)
if err != nil {
Logger.log.Error("could not get or create btc relaying chain")
Logger.log.Error(err)
panic(err)
}
defer func() {
Logger.log.Warn("Gracefully shutting down the btc database...")
db := btcChain.GetDB()
db.Close()
}()
// Create bnbrelaying chain state
bnbChainState, err := getBNBRelayingChainState(portal.GetPortalParams().RelayingParam.BNBRelayingHeaderChainID)
if err != nil {
Logger.log.Error("could not get or create bnb relaying chain state")
Logger.log.Error(err)
panic(err)
}
useOutcoinDb := len(cfg.UseOutcoinDatabase) >= 1
var outcoinDb *incdb.Database = nil
if useOutcoinDb {
temp, err := incdb.Open("leveldb", filepath.Join(cfg.DataDir, cfg.OutcoinDatabaseDir))
if err != nil {
Logger.log.Error("could not open leveldb instance for coin storing")
}
outcoinDb = &temp
}
// Create server and start it.
server := Server{}
server.wallet = walletObj
err = server.NewServer(cfg.Listener, db, dbmp, outcoinDb, cfg.NumIndexerWorkers, cfg.IndexerAccessTokens, version, btcChain, bnbChainState, p, interrupt)
if err != nil {
Logger.log.Errorf("Unable to start server on %+v", cfg.Listener)
Logger.log.Error(err)
return err
}
// Init EVM caller cacher
evmcaller.InitCacher()
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())
monitor.SetGlobalParam("CPU", runtime.NumCPU())
} else {
numThreads, err := strconv.Atoi(limitThreads)
if err != nil {
panic(err)
}
runtime.GOMAXPROCS(numThreads)
monitor.SetGlobalParam("CPU", 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(100)
if os.Getenv("Profiling") != "" {
go http.ListenAndServe(":"+os.Getenv("Profiling"), nil)
}
// Up some limits.
if err := limits.SetLimits(); err != nil {
fmt.Fprintf(os.Stderr, "failed to set limits: %+v\n", err)
os.Exit(utils.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(utils.ExitByOs)
}
if isService {
os.Exit(utils.ExitCodeUnknow)
}
}
// Work around defer not working after os.Exit()
if err := mainMaster(nil); err != nil {
os.Exit(utils.ExitByOs)
}
}