Hide GetBlockWithoutHeader BTC optimization from BlockChain interface

indexv1
Martin Boehm 2018-03-12 17:37:32 +01:00
parent 4c51d7cb0a
commit b55306b6eb
3 changed files with 11 additions and 8 deletions

View File

@ -337,10 +337,14 @@ func (b *BitcoinRPC) GetBlockHeader(hash string) (*bchain.BlockHeader, error) {
}
// GetBlock returns block with given hash.
func (b *BitcoinRPC) GetBlock(hash string) (*bchain.Block, error) {
func (b *BitcoinRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) {
if !b.ParseBlocks {
return b.GetBlockFull(hash)
}
// optimization
if height > 0 {
return b.getBlockWithoutHeader(hash, height)
}
header, err := b.GetBlockHeader(hash)
if err != nil {
return nil, err
@ -357,9 +361,9 @@ func (b *BitcoinRPC) GetBlock(hash string) (*bchain.Block, error) {
return block, nil
}
// GetBlockWithoutHeader is an optimization - it does not call GetBlockHeader to get prev, next hashes
// 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) (*bchain.Block, error) {
func (b *BitcoinRPC) getBlockWithoutHeader(hash string, height uint32) (*bchain.Block, error) {
if !b.ParseBlocks {
return b.GetBlockFull(hash)
}

View File

@ -94,8 +94,7 @@ type BlockChain interface {
GetBestBlockHeight() (uint32, error)
GetBlockHash(height uint32) (string, error)
GetBlockHeader(hash string) (*BlockHeader, error)
GetBlock(hash string) (*Block, error)
GetBlockWithoutHeader(hash string, height uint32) (*Block, error)
GetBlock(hash string, height uint32) (*Block, error)
GetMempool() ([]string, error)
GetTransaction(txid string) (*Tx, error)
EstimateSmartFee(blocks int, conservative bool) (float64, error)

View File

@ -180,7 +180,7 @@ func (w *SyncWorker) connectBlocksParallel(lower, higher uint32) error {
var block *bchain.Block
for hh := range hch {
for {
block, err = w.chain.GetBlockWithoutHeader(hh.hash, hh.height)
block, err = w.chain.GetBlock(hh.hash, hh.height)
if err != nil {
// signal came while looping in the error loop
if hchClosed.Load() == true {
@ -252,7 +252,7 @@ func (w *SyncWorker) connectBlockChunk(lower, higher uint32) error {
}
for height <= higher {
block, err := w.chain.GetBlock(hash)
block, err := w.chain.GetBlock(hash, height)
if err != nil {
return err
}
@ -335,7 +335,7 @@ func (w *SyncWorker) getBlockChain(hash string, out chan blockResult, done chan
return
default:
}
block, err := w.chain.GetBlock(hash)
block, err := w.chain.GetBlock(hash, 0)
if err != nil {
out <- blockResult{err: err}
return