speed up the block download a little

pull/1/head
Jan Pochyla 2017-09-11 15:06:16 +02:00
parent d264ff2dc7
commit 48f3c7648f
1 changed files with 34 additions and 4 deletions

View File

@ -261,8 +261,16 @@ func connectBlock(
index Indexer,
hash string,
) error {
for hash != "" {
block, err := blocks.GetBlock(hash)
bch := make(chan blockResult, 8)
done := make(chan struct{})
defer close(done)
go getBlockChain(hash, blocks, bch, done)
for res := range bch {
err := res.err
block := res.block
if err != nil {
return err
}
@ -273,8 +281,6 @@ func connectBlock(
if err := index.ConnectBlock(block, addrs); err != nil {
return err
}
hash = block.Next
}
return nil
@ -346,3 +352,27 @@ func getBlockRange(
results <- blockResult{block: block}
}
}
func getBlockChain(
hash string,
blocks Blocks,
out chan blockResult,
done chan struct{},
) {
defer close(out)
for hash != "" {
select {
case <-done:
return
default:
}
block, err := blocks.GetBlock(hash)
if err != nil {
out <- blockResult{err: err}
return
}
hash = block.Next
out <- blockResult{block: block}
}
}