MCPcopy Create free account
hub / github.com/DNAProject/DNA / importBlocks

Function importBlocks

cmd/import_cmd.go:57–178  ·  view source on GitHub ↗
(ctx *cli.Context)

Source from the content-addressed store, hash-verified

55}
56
57func importBlocks(ctx *cli.Context) error {
58 log.InitLog(log.InfoLog)
59
60 cfg, err := SetBlockchainConfig(ctx)
61 if err != nil {
62 PrintErrorMsg("SetBlockchainConfig error:%s", err)
63 cli.ShowSubcommandHelp(ctx)
64 return nil
65 }
66 dbDir := utils.GetStoreDirPath(config.DefConfig.Common.DataDir, config.DefConfig.P2PNode.NetworkName)
67
68 stateHashHeight := config.GetStateHashCheckHeight(cfg.P2PNode.NetworkId)
69 ledger.DefLedger, err = ledger.NewLedger(dbDir, stateHashHeight)
70 if err != nil {
71 return fmt.Errorf("NewLedger error:%s", err)
72 }
73 bookKeepers, err := config.DefConfig.GetBookkeepers()
74 if err != nil {
75 return fmt.Errorf("GetBookkeepers error:%s", err)
76 }
77 genesisConfig := config.DefConfig.Genesis
78 genesisBlock, err := genesis.BuildGenesisBlock(bookKeepers, genesisConfig)
79 if err != nil {
80 return fmt.Errorf("BuildGenesisBlock error %s", err)
81 }
82 err = ledger.DefLedger.Init(bookKeepers, genesisBlock)
83 if err != nil {
84 return fmt.Errorf("init ledger error:%s", err)
85 }
86
87 dataDir := ctx.String(utils.GetFlagName(utils.DataDirFlag))
88 if dataDir == "" {
89 PrintErrorMsg("Missing %s argument.", utils.DataDirFlag.Name)
90 cli.ShowSubcommandHelp(ctx)
91 return nil
92 }
93 importFile := ctx.String(utils.GetFlagName(utils.ImportFileFlag))
94 if importFile == "" {
95 PrintErrorMsg("Missing %s argument.", utils.ImportFileFlag.Name)
96 cli.ShowSubcommandHelp(ctx)
97 return nil
98 }
99 endBlockHeight := uint32(ctx.Uint(utils.GetFlagName(utils.ImportEndHeightFlag)))
100 currBlockHeight := ledger.DefLedger.GetCurrentBlockHeight()
101
102 if endBlockHeight > 0 && currBlockHeight >= endBlockHeight {
103 PrintWarnMsg("CurrentBlockHeight:%d larger than or equal to EndBlockHeight:%d, No blocks to import.", currBlockHeight, endBlockHeight)
104 return nil
105 }
106
107 ifile, err := os.OpenFile(importFile, os.O_RDONLY, 0644)
108 if err != nil {
109 return fmt.Errorf("OpenFile error:%s", err)
110 }
111 defer ifile.Close()
112 fReader := bufio.NewReader(ifile)
113
114 metadata := utils.NewExportBlockMetadata()

Callers

nothing calls this directly

Calls 15

DeserializeMethod · 0.95
InitLogFunction · 0.92
GetStoreDirPathFunction · 0.92
GetStateHashCheckHeightFunction · 0.92
NewLedgerFunction · 0.92
BuildGenesisBlockFunction · 0.92
GetFlagNameFunction · 0.92
NewExportBlockMetadataFunction · 0.92
ReadUint32Function · 0.92
DecompressBlockDataFunction · 0.92
BlockFromRawBytesFunction · 0.92
SetBlockchainConfigFunction · 0.85

Tested by

no test coverage detected