speed up the block download a little
parent
d264ff2dc7
commit
48f3c7648f
38
blockbook.go
38
blockbook.go
|
@ -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}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue