Connect blocks in parallel without getting block header - optimization
This commit is contained in:
parent
9972f5a560
commit
f8fbbcfe29
@ -287,6 +287,25 @@ func (b *BitcoinRPC) GetBlock(hash string) (*Block, error) {
|
|||||||
return block, nil
|
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.
|
// GetBlockRaw returns block with given hash as bytes.
|
||||||
func (b *BitcoinRPC) GetBlockRaw(hash string) ([]byte, error) {
|
func (b *BitcoinRPC) GetBlockRaw(hash string) ([]byte, error) {
|
||||||
glog.V(1).Info("rpc: getblock (verbosity=0) ", hash)
|
glog.V(1).Info("rpc: getblock (verbosity=0) ", hash)
|
||||||
|
|||||||
16
blockbook.go
16
blockbook.go
@ -480,17 +480,21 @@ func connectBlocksParallel(
|
|||||||
higher uint32,
|
higher uint32,
|
||||||
numWorkers int,
|
numWorkers int,
|
||||||
) error {
|
) error {
|
||||||
|
type hashHeight struct {
|
||||||
|
hash string
|
||||||
|
height uint32
|
||||||
|
}
|
||||||
var err error
|
var err error
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
hch := make(chan string, numWorkers)
|
hch := make(chan hashHeight, numWorkers)
|
||||||
running := make([]bool, numWorkers)
|
running := make([]bool, numWorkers)
|
||||||
work := func(i int) {
|
work := func(i int) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
for hash := range hch {
|
for hh := range hch {
|
||||||
running[i] = true
|
running[i] = true
|
||||||
block, err := chain.GetBlock(hash)
|
block, err := chain.GetBlockWithoutHeader(hh.hash, hh.height)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Error("Connect block ", hash, " error ", err)
|
glog.Error("Connect block ", hh.height, " ", hh.hash, " error ", err)
|
||||||
running[i] = false
|
running[i] = false
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -500,7 +504,7 @@ func connectBlocksParallel(
|
|||||||
}
|
}
|
||||||
err = index.ConnectBlock(block)
|
err = index.ConnectBlock(block)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Error("Connect block ", hash, " error ", err)
|
glog.Error("Connect block ", hh.height, " ", hh.hash, " error ", err)
|
||||||
}
|
}
|
||||||
running[i] = false
|
running[i] = false
|
||||||
}
|
}
|
||||||
@ -516,7 +520,7 @@ func connectBlocksParallel(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
hch <- hash
|
hch <- hashHeight{hash, h}
|
||||||
if h > 0 && h%1000 == 0 {
|
if h > 0 && h%1000 == 0 {
|
||||||
glog.Info("connecting block ", h, " ", hash)
|
glog.Info("connecting block ", h, " ", hash)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user