diff options
author | Felix Lange <fjl@twurst.com> | 2015-05-27 23:35:08 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2015-05-28 07:20:58 +0800 |
commit | a8bc2181c94f5d3a9455c4fa526f8722a21ecb04 (patch) | |
tree | 4d33730a7d5a987576692cfd0b0cfa7e3b9840fc | |
parent | 67effb94b6fadac7b207cb5333c91f578326762e (diff) | |
download | go-tangerine-a8bc2181c94f5d3a9455c4fa526f8722a21ecb04.tar.gz go-tangerine-a8bc2181c94f5d3a9455c4fa526f8722a21ecb04.tar.zst go-tangerine-a8bc2181c94f5d3a9455c4fa526f8722a21ecb04.zip |
cmd/utils: skip batches with known blocks during import
This makes block importing restartable.
-rw-r--r-- | cmd/utils/cmd.go | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 2e2b627df..2a5e2ec6a 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -204,12 +204,11 @@ func ImportChain(chain *core.ChainManager, fn string) error { defer fh.Close() stream := rlp.NewStream(fh, 0) - // Remove all existing blocks and start the import. - chain.Reset() + // Run actual the import. batchSize := 2500 blocks := make(types.Blocks, batchSize) n := 0 - for { + for batch := 0; ; batch++ { // Load a batch of RLP blocks. if checkInterrupt() { return fmt.Errorf("interrupted") @@ -232,6 +231,11 @@ func ImportChain(chain *core.ChainManager, fn string) error { if checkInterrupt() { return fmt.Errorf("interrupted") } + if hasAllBlocks(chain, blocks[:i]) { + glog.Infof("skipping batch %d, all blocks present [%x / %x]", + batch, blocks[0].Hash().Bytes()[:4], blocks[i-1].Hash().Bytes()[:4]) + continue + } if _, err := chain.InsertChain(blocks[:i]); err != nil { return fmt.Errorf("invalid block %d: %v", n, err) } @@ -239,6 +243,15 @@ func ImportChain(chain *core.ChainManager, fn string) error { return nil } +func hasAllBlocks(chain *core.ChainManager, bs []*types.Block) bool { + for _, b := range bs { + if !chain.HasBlock(b.Hash()) { + return false + } + } + return true +} + func ExportChain(chainmgr *core.ChainManager, fn string) error { glog.Infoln("Exporting blockchain to", fn) fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm) |