/
init.go
96 lines (78 loc) · 2.96 KB
/
init.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
package consensus
import (
"github.com/tendermint/tendermint/privval"
"github.com/tendermint/tendermint/p2p"
tmtCommon "github.com/tendermint/tendermint/libs/common"
tmtConfig "github.com/tendermint/tendermint/config"
tmtDb "github.com/tendermint/tendermint/libs/db"
tmtState "github.com/tendermint/tendermint/state"
"github.com/tendermint/tendermint/version"
"github.com/lightstreams-network/lightchain/network"
"github.com/lightstreams-network/lightchain/log"
stdtracer "github.com/lightstreams-network/lightchain/tracer"
)
func Init(cfg Config, ntw network.Network, trcCfg stdtracer.Config) error {
logger := log.NewLogger().With("engine", "consensus")
createConsensusDataDirIfNotExists(cfg.dataDir)
privateValidatorKeyFile := cfg.tendermintCfg.PrivValidatorKeyFile()
privateValidatorStateFile := cfg.tendermintCfg.PrivValidatorStateFile()
var pv *privval.FilePV
if tmtCommon.FileExists(privateValidatorKeyFile) {
pv = privval.LoadFilePV(privateValidatorKeyFile, privateValidatorStateFile)
logger.Info("Found private validator key file", "path", privateValidatorKeyFile)
} else {
pv = privval.GenFilePV(privateValidatorKeyFile, privateValidatorStateFile)
pv.Save()
logger.Info("Generated private validator key file", "path", privateValidatorKeyFile)
}
nodeKeyFile := cfg.tendermintCfg.NodeKeyFile()
if tmtCommon.FileExists(nodeKeyFile) {
logger.Info("Found node key", "path", nodeKeyFile)
} else {
if _, err := p2p.LoadOrGenNodeKey(nodeKeyFile); err != nil {
return err
}
logger.Info("Generated node key", "path", nodeKeyFile)
}
var genContent []byte
var cfgDoc []byte
var protocolBlockVersion version.Protocol
var err error
cfgFilePath := cfg.TendermintConfigFilePath()
genFile := cfg.tendermintCfg.GenesisFile()
if genContent, err = ntw.ConsensusGenesis(pv); err != nil {
return err
}
if cfgDoc, err = ntw.ConsensusConfig(); err != nil {
return err
}
if protocolBlockVersion, err = ntw.ConsensusProtocolBlockVersion(); err != nil {
return err
}
if tmtCommon.FileExists(genFile) {
logger.Info("Found genesis file", "path", genFile)
} else {
if err := tmtCommon.WriteFile(genFile, genContent, 0644); err != nil {
return err
}
}
if err := tmtCommon.WriteFile(cfgFilePath, cfgDoc, 0644); err != nil {
return err
}
logger.Info("Initializing consensus StateDB", "database", cfg.tendermintCfg.DBDir())
stateDB := tmtDb.NewDB("state", tmtDb.DBBackendType(cfg.tendermintCfg.DBBackend), cfg.tendermintCfg.DBDir())
state, err := tmtState.LoadStateFromDBOrGenesisFile(stateDB, cfg.tendermintCfg.GenesisFile())
state.Version.Consensus.Block = protocolBlockVersion
logger.Info("Saving and closing consensus StateDB", "database", cfg.tendermintCfg.DBDir())
tmtState.SaveState(stateDB, state)
stateDB.Close()
trc, err := newTracer(trcCfg)
if err != nil {
return err
}
trc.assertPersistedInitStateDb(cfg.tendermintCfg, ntw)
return nil
}
func createConsensusDataDirIfNotExists(dataDir string) {
tmtConfig.EnsureRoot(dataDir)
}