-
Notifications
You must be signed in to change notification settings - Fork 4
/
initcmd.go
95 lines (83 loc) · 2.34 KB
/
initcmd.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
package main
import (
"encoding/json"
"errors"
"github.com/LemoFoundationLtd/lemochain-core/chain"
"github.com/LemoFoundationLtd/lemochain-core/chain/types"
"github.com/LemoFoundationLtd/lemochain-core/common/log"
"github.com/LemoFoundationLtd/lemochain-core/main/node"
"github.com/LemoFoundationLtd/lemochain-core/store"
"gopkg.in/urfave/cli.v1"
"os"
)
var (
initCommand = cli.Command{
Action: initGenesis,
Name: "init",
Usage: "Bootstrap and initialize a new genesis block",
ArgsUsage: "<genesisPath>",
Flags: []cli.Flag{
node.DataDirFlag,
},
Category: "BLOCKCHAIN COMMANDS",
Description: `
The init command initializes a new genesis block.
It expects the genesis file as argument.`,
}
)
var (
ErrFileReadFailed = errors.New("open genesis config file failed")
ErrInvalidGenesisFile = errors.New("invalid genesis file")
)
// initGenesis 初始化创始块action
func initGenesis(ctx *cli.Context) error {
// init log
log.Setup(log.LevelInfo, false, false)
// open special genesis config file
genesisFile := ctx.Args().First()
dir := ctx.GlobalString(node.DataDirFlag.Name)
if len(genesisFile) == 0 {
log.Crit("Must supply genesis json file path")
}
block := setupGenesisBlock(genesisFile, dir)
log.Infof("init genesis succeed. hash: %s", block.Hash().Hex())
return nil
}
func setupGenesisBlock(genesisFile, datadir string) *types.Block {
genesis, err := loadGenesisFile(genesisFile)
if err != nil {
panic(err)
}
return saveBlock(datadir, genesis)
}
// saveBlock save block to db
func saveBlock(datadir string, genesis *chain.Genesis) *types.Block {
chaindata := node.GetChainDataPath(datadir)
db := store.NewChainDataBase(chaindata, "", "")
defer func() {
if err := db.Close(); err != nil {
log.Errorf("close db failed. %v", err)
}
}()
return chain.SetupGenesisBlock(db, genesis)
}
// loadGenesisFile
func loadGenesisFile(filePath string) (*chain.Genesis, error) {
file, err := os.Open(filePath)
if err != nil {
log.Errorf("%v", err)
return nil, ErrFileReadFailed
}
defer func() {
if err := file.Close(); err != nil {
log.Errorf("close genesis file failed. %v", err)
}
}()
// decode genesis config file string
genesis := new(chain.Genesis)
if err := json.NewDecoder(file).Decode(genesis); err != nil {
log.Errorf("%v", err)
return nil, ErrInvalidGenesisFile
}
return genesis, nil
}