MCPcopy Create free account
hub / github.com/CPChain/chain / ImportChain

Method ImportChain

protocols/cpc/api.go:226–272  ·  view source on GitHub ↗

ImportChain imports a blockchain from a local file.

(file string)

Source from the content-addressed store, hash-verified

224
225// ImportChain imports a blockchain from a local file.
226func (api *PrivateAdminAPI) ImportChain(file string) (bool, error) {
227 // Make sure the can access the file to import
228 in, err := os.Open(file)
229 if err != nil {
230 return false, err
231 }
232 defer in.Close()
233
234 var reader io.Reader = in
235 if strings.HasSuffix(file, ".gz") {
236 if reader, err = gzip.NewReader(reader); err != nil {
237 return false, err
238 }
239 }
240
241 // Run actual the import in pre-configured batches
242 stream := rlp.NewStream(reader, 0)
243
244 blocks, index := make([]*types.Block, 0, 2500), 0
245 for batch := 0; ; batch++ {
246 // Load a batch of blocks from the input file
247 for len(blocks) < cap(blocks) {
248 block := new(types.Block)
249 if err := stream.Decode(block); err == io.EOF {
250 break
251 } else if err != nil {
252 return false, fmt.Errorf("block %d: failed to parse: %v", index, err)
253 }
254 blocks = append(blocks, block)
255 index++
256 }
257 if len(blocks) == 0 {
258 break
259 }
260
261 if hasAllBlocks(api.cpc.BlockChain(), blocks) {
262 blocks = blocks[:0]
263 continue
264 }
265 // Import the batch and reset the buffer
266 if _, err := api.cpc.BlockChain().InsertChain(blocks); err != nil {
267 return false, fmt.Errorf("batch %d: failed to insert: %v", batch, err)
268 }
269 blocks = blocks[:0]
270 }
271 return true, nil
272}
273
274// PublicDebugAPI is the collection of cpchain full node APIs exposed
275// over the public debugging endpoint.

Callers 1

importChainFunction · 0.80

Calls 5

hasAllBlocksFunction · 0.85
OpenMethod · 0.65
CloseMethod · 0.65
BlockChainMethod · 0.65
InsertChainMethod · 0.65

Tested by

no test coverage detected