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, index Indexer,
hash string, hash string,
) error { ) error {
for hash != "" { bch := make(chan blockResult, 8)
block, err := blocks.GetBlock(hash) 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 { if err != nil {
return err return err
} }
@ -273,8 +281,6 @@ func connectBlock(
if err := index.ConnectBlock(block, addrs); err != nil { if err := index.ConnectBlock(block, addrs); err != nil {
return err return err
} }
hash = block.Next
} }
return nil return nil
@ -346,3 +352,27 @@ func getBlockRange(
results <- blockResult{block: block} 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}
}
}