diff --git a/bchain/coins/btc/bitcoinrpc.go b/bchain/coins/btc/bitcoinrpc.go index c52aaa57..63f2bb8d 100644 --- a/bchain/coins/btc/bitcoinrpc.go +++ b/bchain/coins/btc/bitcoinrpc.go @@ -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) } diff --git a/bchain/types.go b/bchain/types.go index 97a9fb06..6dd9da46 100644 --- a/bchain/types.go +++ b/bchain/types.go @@ -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) diff --git a/db/sync.go b/db/sync.go index 733c7042..30ba0c9a 100644 --- a/db/sync.go +++ b/db/sync.go @@ -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