Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

speedup import block processing #1112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 40 additions & 23 deletions cmd/import_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
blocks := make(chan *types.Block, 10)
go func() {
defer close(blocks)
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
}
blocks <- 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 blocks {
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
Expand Down