From a17ac3fcaf7f55167e93d7870f6c207a39f427d2 Mon Sep 17 00:00:00 2001 From: laizy Date: Wed, 9 Oct 2019 16:27:23 +0800 Subject: [PATCH 1/2] 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 From 1f7d97d6d32fdd32a75aa5c89ac43a91a60d51a6 Mon Sep 17 00:00:00 2001 From: laizy Date: Mon, 28 Oct 2019 16:17:29 +0800 Subject: [PATCH 2/2] fix local variable name --- cmd/import_cmd.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmd/import_cmd.go b/cmd/import_cmd.go index 5a86b33a42..5cc0180024 100644 --- a/cmd/import_cmd.go +++ b/cmd/import_cmd.go @@ -137,9 +137,9 @@ func importBlocks(ctx *cli.Context) error { PrintInfoMsg("Start import blocks.") var readErr error - blockes := make(chan *types.Block, 10) + blocks := make(chan *types.Block, 10) go func() { - defer close(blockes) + defer close(blocks) for i := uint32(startBlockHeight); i <= endBlockHeight; i++ { size, err := serialization.ReadUint32(fReader) if err != nil { @@ -167,11 +167,11 @@ func importBlocks(ctx *cli.Context) error { readErr = fmt.Errorf("block height:%d deserialize error:%s", i, err) break } - blockes <- block + blocks <- block } }() - for block := range blockes { + for block := range blocks { execResult, err := ledger.DefLedger.ExecuteBlock(block) if err != nil { return fmt.Errorf("block height:%d ExecuteBlock error:%s", block.Header.Height, err)