-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_chain.go
83 lines (67 loc) · 2.18 KB
/
import_chain.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
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"strings"
"github.com/spf13/viper"
"github.com/dnerochain/dnero/blockchain"
"github.com/dnerochain/dnero/core"
"github.com/dnerochain/dnero/snapshot"
"github.com/dnerochain/dnero/store/database/backend"
"github.com/dnerochain/dnero/store/kvstore"
)
func handleError(err error) {
if err != nil {
fmt.Printf("Error: %v\n", err)
printUsage()
os.Exit(1)
}
}
func printUsage() {
fmt.Println("Usage: import_chain -chain=<chain_id> -config=<path_to_config_home> -snapshot=<path_to_snapshot_file> -chain_import=<path_to chain_files_directory>")
}
func main() {
chainPtr := flag.String("chain", "", "chain id")
configPathPtr := flag.String("config", "", "path to dnero config home")
snapshotPathPtr := flag.String("snapshot", "", "path to snapshot file")
chainImportDirPathPtr := flag.String("chain_import", "", "path to chain files directory")
flag.Parse()
chainID := *chainPtr
configPath := *configPathPtr
snapshotPath := *snapshotPathPtr
chainImportDirPath := *chainImportDirPathPtr
mainDBPath := path.Join(configPath, "db", "main")
refDBPath := path.Join(configPath, "db", "ref")
db, _ := backend.NewLDBDatabase(mainDBPath, refDBPath, 256, 0)
initConfig(configPath)
root := core.NewBlock()
if chainID == "" {
root.ChainID = core.MainnetChainID
} else {
root.ChainID = chainID
}
store := kvstore.NewKVStore(db)
chain := blockchain.NewChain(root.ChainID, store, root)
_, err := snapshot.ValidateSnapshot(snapshotPath, chainImportDirPath, "")
if err != nil {
log.Fatalf("Snapshot validation failed, err: %v", err)
}
if _, _, err := snapshot.ImportSnapshot(snapshotPath, chainImportDirPath, "", chain, db, nil); err != nil {
log.Fatalf("Failed to load snapshot: %v, err: %v", snapshotPath, err)
}
os.Exit(0)
}
func initConfig(cfgPath string) {
viper.AddConfigPath(cfgPath)
// Search config (without extension).
viper.SetConfigName("config")
viper.AutomaticEnv() // read in environment variables that match
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}