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

Function exportBlocks

cmd/export_cmd.go:50–147  ·  view source on GitHub ↗
(ctx *cli.Context)

Source from the content-addressed store, hash-verified

48}
49
50func exportBlocks(ctx *cli.Context) error {
51 SetRpcPort(ctx)
52 exportFile := ctx.String(utils.GetFlagName(utils.ExportFileFlag))
53 if exportFile == "" {
54 PrintErrorMsg("Missing %s argument.", utils.ExportFileFlag.Name)
55 cli.ShowSubcommandHelp(ctx)
56 return nil
57 }
58
59 startHeight := ctx.Uint(utils.GetFlagName(utils.ExportStartHeightFlag))
60 endHeight := ctx.Uint(utils.GetFlagName(utils.ExportEndHeightFlag))
61 if endHeight > 0 && startHeight > endHeight {
62 return fmt.Errorf("export error: start height should smaller than end height")
63 }
64 blockCount, err := utils.GetBlockCount()
65 if err != nil {
66 return fmt.Errorf("GetBlockCount error:%s", err)
67 }
68 currentBlockHeight := uint(blockCount - 1)
69 if startHeight > currentBlockHeight {
70 PrintWarnMsg("StartBlockHeight:%d larger than CurrentBlockHeight:%d, No blocks to export.", startHeight, currentBlockHeight)
71 return nil
72 }
73 if endHeight == 0 || endHeight > currentBlockHeight {
74 endHeight = currentBlockHeight
75 }
76
77 speed := ctx.String(utils.GetFlagName(utils.ExportSpeedFlag))
78 var sleepTime time.Duration
79 switch speed {
80 case "h":
81 sleepTime = 0
82 case "m":
83 sleepTime = time.Millisecond * 2
84 default:
85 sleepTime = time.Millisecond * 5
86 }
87
88 exportFile = utils.GenExportBlocksFileName(exportFile, uint32(startHeight), uint32(endHeight))
89 ef, err := os.OpenFile(exportFile, os.O_RDWR|os.O_CREATE, 0664)
90 if err != nil {
91 return fmt.Errorf("open file:%s error:%s", exportFile, err)
92 }
93 defer ef.Close()
94 fWriter := bufio.NewWriter(ef)
95
96 metadata := utils.NewExportBlockMetadata()
97 metadata.StartBlockHeight = uint32(startHeight)
98 metadata.EndBlockHeight = uint32(endHeight)
99 err = metadata.Serialize(fWriter)
100 if err != nil {
101 return fmt.Errorf("write export metadata error:%s", err)
102 }
103
104 //progress bar
105 uiprogress.Start()
106 bar := uiprogress.AddBar(int(endHeight - startHeight + 1)).
107 AppendCompleted().

Callers

nothing calls this directly

Calls 15

SerializeMethod · 0.95
GetFlagNameFunction · 0.92
GetBlockCountFunction · 0.92
GenExportBlocksFileNameFunction · 0.92
NewExportBlockMetadataFunction · 0.92
GetBlockDataFunction · 0.92
CompressBlockDataFunction · 0.92
WriteUint32Function · 0.92
SetRpcPortFunction · 0.85
PrintErrorMsgFunction · 0.85
PrintWarnMsgFunction · 0.85
PrintInfoMsgFunction · 0.85

Tested by

no test coverage detected