Connect blocks in parallel without getting block header - optimization

This commit is contained in:
Martin Boehm 2018-03-01 00:32:40 +01:00
parent 9972f5a560
commit f8fbbcfe29
2 changed files with 29 additions and 6 deletions

View File

@ -287,6 +287,25 @@ func (b *BitcoinRPC) GetBlock(hash string) (*Block, error) {
return block, nil
}
// GetBlockWithoutHeader is an optimization - it does not call GetBlockHeader to get prev, next hashes
// instead it sets to header only block hash and height passed in parameters
func (b *BitcoinRPC) GetBlockWithoutHeader(hash string, height uint32) (*Block, error) {
if b.Parser == nil {
return b.GetBlockFull(hash)
}
data, err := b.GetBlockRaw(hash)
if err != nil {
return nil, err
}
block, err := b.Parser.ParseBlock(data)
if err != nil {
return nil, err
}
block.BlockHeader.Hash = hash
block.BlockHeader.Height = height
return block, nil
}
// GetBlockRaw returns block with given hash as bytes.
func (b *BitcoinRPC) GetBlockRaw(hash string) ([]byte, error) {
glog.V(1).Info("rpc: getblock (verbosity=0) ", hash)

View File

@ -480,17 +480,21 @@ func connectBlocksParallel(
higher uint32,
numWorkers int,
) error {
type hashHeight struct {
hash string
height uint32
}
var err error
var wg sync.WaitGroup
hch := make(chan string, numWorkers)
hch := make(chan hashHeight, numWorkers)
running := make([]bool, numWorkers)
work := func(i int) {
defer wg.Done()
for hash := range hch {
for hh := range hch {
running[i] = true
block, err := chain.GetBlock(hash)
block, err := chain.GetBlockWithoutHeader(hh.hash, hh.height)
if err != nil {
glog.Error("Connect block ", hash, " error ", err)
glog.Error("Connect block ", hh.height, " ", hh.hash, " error ", err)
running[i] = false
continue
}
@ -500,7 +504,7 @@ func connectBlocksParallel(
}
err = index.ConnectBlock(block)
if err != nil {
glog.Error("Connect block ", hash, " error ", err)
glog.Error("Connect block ", hh.height, " ", hh.hash, " error ", err)
}
running[i] = false
}
@ -516,7 +520,7 @@ func connectBlocksParallel(
if err != nil {
break
}
hch <- hash
hch <- hashHeight{hash, h}
if h > 0 && h%1000 == 0 {
glog.Info("connecting block ", h, " ", hash)
}