From f1c37b01cfbf4106cc26d05a3f3ae142fa31b579 Mon Sep 17 00:00:00 2001 From: laizy Date: Wed, 9 Oct 2019 16:27:23 +0800 Subject: [PATCH] speedup import block processing --- cmd/import_cmd.go | 63 ++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/cmd/import_cmd.go b/cmd/import_cmd.go index aebea4c100..5a86b33a42 100644 --- a/cmd/import_cmd.go +++ b/cmd/import_cmd.go @@ -136,39 +136,56 @@ func importBlocks(ctx *cli.Context) error { 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) + var readErr error + blockes := make(chan *types.Block, 10) + go func() { + defer close(blockes) + for i := uint32(startBlockHeight); i <= endBlockHeight; i++ { + size, err := serialization.ReadUint32(fReader) + if err != nil { + readErr = fmt.Errorf("read block height:%d error:%s", i, err) + break + } + compressData := make([]byte, size) + _, err = io.ReadFull(fReader, compressData) + if err != nil { + readErr = fmt.Errorf("read block data height:%d error:%s", i, err) + break + } + + if i <= currBlockHeight { + continue + } + + blockData, err := utils.DecompressBlockData(compressData, metadata.CompressType) + if err != nil { + readErr = fmt.Errorf("block height:%d decompress error:%s", i, err) + break + } + block, err := types.BlockFromRawBytes(blockData) + if err != nil { + readErr = fmt.Errorf("block height:%d deserialize error:%s", i, err) + break + } + blockes <- block } + }() - 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) - } + for block := range blockes { execResult, err := ledger.DefLedger.ExecuteBlock(block) if err != nil { - return fmt.Errorf("block height:%d ExecuteBlock error:%s", i, err) + return fmt.Errorf("block height:%d ExecuteBlock error:%s", block.Header.Height, err) } err = ledger.DefLedger.SubmitBlock(block, execResult) if err != nil { - return fmt.Errorf("SubmitBlock block height:%d error:%s", i, err) + return fmt.Errorf("SubmitBlock block height:%d error:%s", block.Header.Height, err) } bar.Incr() } + if readErr != nil { + return readErr + } + uiprogress.Stop() PrintInfoMsg("Import block completed, current block height:%d.", ledger.DefLedger.GetCurrentBlockHeight()) return nil