forked from DNAProject/DNA
/
import_cmd.go
175 lines (159 loc) · 5.54 KB
/
import_cmd.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
/*
* Copyright (C) 2018 The DNA Authors
* This file is part of The DNA library.
*
* The DNA is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The DNA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The DNA. If not, see <http://www.gnu.org/licenses/>.
*/
package cmd
import (
"bufio"
"fmt"
"io"
"os"
"github.com/dnaproject2/DNA/cmd/utils"
"github.com/dnaproject2/DNA/common/config"
"github.com/dnaproject2/DNA/common/log"
"github.com/dnaproject2/DNA/common/serialization"
"github.com/dnaproject2/DNA/core/genesis"
"github.com/dnaproject2/DNA/core/ledger"
"github.com/dnaproject2/DNA/core/types"
"github.com/gosuri/uiprogress"
"github.com/urfave/cli"
)
var ImportCommand = cli.Command{
Name: "import",
Usage: "Import blocks to DB from a file",
ArgsUsage: "",
Action: importBlocks,
Flags: []cli.Flag{
utils.ImportFileFlag,
utils.ImportEndHeightFlag,
utils.DataDirFlag,
utils.ConfigFlag,
utils.NetworkIdFlag,
utils.DisableEventLogFlag,
},
Description: "Note that import cmd doesn't support testmode",
}
func importBlocks(ctx *cli.Context) error {
log.InitLog(log.InfoLog)
cfg, err := SetDNAConfig(ctx)
if err != nil {
PrintErrorMsg("SetDNAConfig error:%s", err)
cli.ShowSubcommandHelp(ctx)
return nil
}
dbDir := utils.GetStoreDirPath(config.DefConfig.Common.DataDir, config.DefConfig.P2PNode.NetworkName)
stateHashHeight := config.GetStateHashCheckHeight(cfg.P2PNode.NetworkId)
ledger.DefLedger, err = ledger.NewLedger(dbDir, stateHashHeight)
if err != nil {
return fmt.Errorf("NewLedger error:%s", err)
}
bookKeepers, err := config.DefConfig.GetBookkeepers()
if err != nil {
return fmt.Errorf("GetBookkeepers error:%s", err)
}
genesisConfig := config.DefConfig.Genesis
genesisBlock, err := genesis.BuildGenesisBlock(bookKeepers, genesisConfig)
if err != nil {
return fmt.Errorf("BuildGenesisBlock error %s", err)
}
err = ledger.DefLedger.Init(bookKeepers, genesisBlock)
if err != nil {
return fmt.Errorf("init ledger error:%s", err)
}
dataDir := ctx.String(utils.GetFlagName(utils.DataDirFlag))
if dataDir == "" {
PrintErrorMsg("Missing %s argument.", utils.DataDirFlag.Name)
cli.ShowSubcommandHelp(ctx)
return nil
}
importFile := ctx.String(utils.GetFlagName(utils.ImportFileFlag))
if importFile == "" {
PrintErrorMsg("Missing %s argument.", utils.ImportFileFlag.Name)
cli.ShowSubcommandHelp(ctx)
return nil
}
endBlockHeight := uint32(ctx.Uint(utils.GetFlagName(utils.ImportEndHeightFlag)))
currBlockHeight := ledger.DefLedger.GetCurrentBlockHeight()
if endBlockHeight > 0 && currBlockHeight >= endBlockHeight {
PrintWarnMsg("CurrentBlockHeight:%d larger than or equal to EndBlockHeight:%d, No blocks to import.", currBlockHeight, endBlockHeight)
return nil
}
ifile, err := os.OpenFile(importFile, os.O_RDONLY, 0644)
if err != nil {
return fmt.Errorf("OpenFile error:%s", err)
}
defer ifile.Close()
fReader := bufio.NewReader(ifile)
metadata := utils.NewExportBlockMetadata()
err = metadata.Deserialize(fReader)
if err != nil {
return fmt.Errorf("block data file metadata deserialize error:%s", err)
}
if metadata.EndBlockHeight <= currBlockHeight {
PrintWarnMsg("CurrentBlockHeight:%d larger than or equal to EndBlockHeight:%d, No blocks to import.", currBlockHeight, endBlockHeight)
return nil
}
if endBlockHeight == 0 || endBlockHeight > metadata.EndBlockHeight {
endBlockHeight = metadata.EndBlockHeight
}
startBlockHeight := metadata.StartBlockHeight
if startBlockHeight > (currBlockHeight + 1) {
return fmt.Errorf("import block error: StartBlockHeight:%d larger than NextBlockHeight:%d", startBlockHeight, currBlockHeight+1)
}
//progress bar
uiprogress.Start()
bar := uiprogress.AddBar(int(endBlockHeight - startBlockHeight + 1)).
AppendCompleted().
AppendElapsed().
PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("Block(%d/%d)", b.Current()+int(currBlockHeight), int(endBlockHeight))
})
PrintInfoMsg("Start import blocks.")
for i := uint32(startBlockHeight); i <= endBlockHeight; i++ {
size, err := serialization.ReadUint32(fReader)
if err != nil {
return fmt.Errorf("read block height:%d error:%s", i, err)
}
compressData := make([]byte, size)
_, err = io.ReadFull(fReader, compressData)
if err != nil {
return fmt.Errorf("read block data height:%d error:%s", i, err)
}
if i <= currBlockHeight {
continue
}
blockData, err := utils.DecompressBlockData(compressData, metadata.CompressType)
if err != nil {
return fmt.Errorf("block height:%d decompress error:%s", i, err)
}
block, err := types.BlockFromRawBytes(blockData)
if err != nil {
return fmt.Errorf("block height:%d deserialize error:%s", i, err)
}
execResult, err := ledger.DefLedger.ExecuteBlock(block)
if err != nil {
return fmt.Errorf("block height:%d ExecuteBlock error:%s", i, err)
}
err = ledger.DefLedger.SubmitBlock(block, execResult)
if err != nil {
return fmt.Errorf("SubmitBlock block height:%d error:%s", i, err)
}
bar.Incr()
}
uiprogress.Stop()
PrintInfoMsg("Import block completed, current block height:%d.", ledger.DefLedger.GetCurrentBlockHeight())
return nil
}