-
Notifications
You must be signed in to change notification settings - Fork 34
/
initmain.go
125 lines (107 loc) · 3.13 KB
/
initmain.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
package main
import (
"encoding/json"
"fmt"
"github.com/copernet/copernicus/conf"
"github.com/copernet/copernicus/crypto"
"github.com/copernet/copernicus/log"
"github.com/copernet/copernicus/logic/lchain"
"github.com/copernet/copernicus/logic/lreindex"
"github.com/copernet/copernicus/logic/ltx"
"github.com/copernet/copernicus/model"
"github.com/copernet/copernicus/model/chain"
"github.com/copernet/copernicus/model/mempool"
"github.com/copernet/copernicus/model/pow"
"github.com/copernet/copernicus/model/utxo"
"github.com/copernet/copernicus/model/wallet"
"github.com/copernet/copernicus/persist"
"github.com/copernet/copernicus/persist/blkdb"
"github.com/copernet/copernicus/persist/db"
"github.com/copernet/copernicus/persist/disk"
"os"
"path/filepath"
)
func appInitMain(args []string) {
conf.Cfg = conf.InitConfig(args)
if conf.Cfg == nil {
fmt.Println("please run `./copernicus -h` for usage.")
os.Exit(0)
}
if conf.Cfg.P2PNet.TestNet {
model.SetTestNetParams()
} else if conf.Cfg.P2PNet.RegTest {
model.SetRegTestParams()
}
pow.UpdateMinimumChainWork()
fmt.Println("Current data dir:\033[0;32m", conf.DataDir, "\033[0m")
//init log
logDir := filepath.Join(conf.DataDir, log.DefaultLogDirname)
if !conf.FileExists(logDir) {
err := os.MkdirAll(logDir, os.ModePerm)
if err != nil {
panic("logdir create failed: " + err.Error())
}
}
logConf := struct {
FileName string `json:"filename"`
Level int `json:"level"`
}{
FileName: logDir + "/" + conf.Cfg.Log.FileName + ".log",
Level: log.GetLevel(conf.Cfg.Log.Level),
}
configuration, err := json.Marshal(logConf)
if err != nil {
panic(err)
}
log.Init(string(configuration))
// Init UTXO DB
utxoDbCfg := &db.DBOption{
FilePath: conf.DataDir + "/chainstate",
CacheSize: (1 << 20) * 8,
Wipe: conf.Cfg.Reindex,
}
utxoConfig := utxo.UtxoConfig{Do: utxoDbCfg}
utxo.InitUtxoLruTip(&utxoConfig)
// Init blocktree DB
blkDbCfg := &db.DBOption{
FilePath: conf.DataDir + "/blocks/index",
CacheSize: (1 << 20) * 8,
Wipe: conf.Cfg.Reindex,
}
blkdbCfg := blkdb.BlockTreeDBConfig{Do: blkDbCfg}
blkdb.InitBlockTreeDB(&blkdbCfg)
btd := blkdb.GetInstance()
persist.InitPersistGlobal(btd)
if !chain.InitGlobalChain(btd) {
return
}
// Load blockindex DB
//lblockindex.LoadBlockIndexDB()
// when reindexing, we reuse the genesis block already on the disk
if !conf.Cfg.Reindex {
lchain.InitGenesisChain()
}
mempool.InitMempool()
crypto.InitSecp256()
wallet.InitWallet()
ltx.ScriptVerifyInit()
if conf.Cfg.Reindex {
disk.CleanupBlockRevFiles()
err := lreindex.Reindex()
if err != nil {
log.Error("fatal error occurred when reindex: %s, will shutdown!", err)
shutdownRequestChannel <- struct{}{}
}
if chain.GetInstance().Genesis() == nil {
log.Warn("after reindex, genesis block is not init, reindex may not worked, init genesis block now")
lchain.InitGenesisChain()
}
gChain := chain.GetInstance()
log.Info(`
----After reindex----
chain height: <%d>
chain's index map count: %d
tip block index: %s
---------------------`, gChain.Height(), gChain.IndexMapSize(), gChain.Tip().String())
}
}