2016-04-25 11:12:09 +02:00
|
|
|
// Copyright (c) 2013-2017 The btcsuite developers
|
2013-08-28 18:45:21 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-30 21:54:30 +01:00
|
|
|
package blockchain
|
2013-08-28 18:45:21 +02:00
|
|
|
|
|
|
|
import (
|
2016-08-08 21:04:33 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2015-08-26 11:54:55 +02:00
|
|
|
"github.com/btcsuite/btcd/database"
|
2015-02-05 22:16:39 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2013-08-28 18:45:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// BlockLocator is used to help locate a specific block. The algorithm for
|
|
|
|
// building the block locator is to add the hashes in reverse order until
|
|
|
|
// the genesis block is reached. In order to keep the list of locator hashes
|
|
|
|
// to a reasonable number of entries, first the most recent previous 10 block
|
|
|
|
// hashes are added, then the step is doubled each loop iteration to
|
|
|
|
// exponentially decrease the number of hashes as a function of the distance
|
|
|
|
// from the block being located.
|
2013-10-13 09:06:18 +02:00
|
|
|
//
|
|
|
|
// For example, assume you have a block chain with a side chain as depicted
|
|
|
|
// below:
|
|
|
|
// genesis -> 1 -> 2 -> ... -> 15 -> 16 -> 17 -> 18
|
|
|
|
// \-> 16a -> 17a
|
|
|
|
//
|
|
|
|
// The block locator for block 17a would be the hashes of blocks:
|
|
|
|
// [17a 16a 15 14 13 12 11 10 9 8 6 2 genesis]
|
2016-08-08 21:04:33 +02:00
|
|
|
type BlockLocator []*chainhash.Hash
|
2013-08-28 18:45:21 +02:00
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// blockLocatorFromHash returns a block locator for the passed block hash.
|
2017-01-11 22:21:14 +01:00
|
|
|
// See BlockLocator for details on the algorithm used to create a block locator.
|
2013-08-28 18:45:21 +02:00
|
|
|
//
|
|
|
|
// In addition to the general algorithm referenced above, there are a couple of
|
|
|
|
// special cases which are handled:
|
|
|
|
//
|
|
|
|
// - If the genesis hash is passed, there are no previous hashes to add and
|
|
|
|
// therefore the block locator will only consist of the genesis hash
|
|
|
|
// - If the passed hash is not currently known, the block locator will only
|
|
|
|
// consist of the passed hash
|
2015-08-26 06:03:18 +02:00
|
|
|
//
|
2016-04-25 11:12:09 +02:00
|
|
|
// This function MUST be called with the block index lock held (for reads).
|
|
|
|
func (bi *blockIndex) blockLocatorFromHash(hash *chainhash.Hash) BlockLocator {
|
2013-08-28 18:45:21 +02:00
|
|
|
// The locator contains the requested hash at the very least.
|
2015-02-05 22:16:39 +01:00
|
|
|
locator := make(BlockLocator, 0, wire.MaxBlockLocatorsPerMsg)
|
2013-09-26 03:20:53 +02:00
|
|
|
locator = append(locator, hash)
|
2013-08-28 18:45:21 +02:00
|
|
|
|
|
|
|
// Nothing more to do if a locator for the genesis hash was requested.
|
2016-04-25 11:12:09 +02:00
|
|
|
if hash.IsEqual(bi.chainParams.GenesisHash) {
|
2013-08-28 18:45:21 +02:00
|
|
|
return locator
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to find the height of the block that corresponds to the
|
2013-10-11 17:24:13 +02:00
|
|
|
// passed hash, and if it's on a side chain, also find the height at
|
|
|
|
// which it forks from the main chain.
|
2015-08-08 04:20:49 +02:00
|
|
|
blockHeight := int32(-1)
|
|
|
|
forkHeight := int32(-1)
|
2016-04-25 11:12:09 +02:00
|
|
|
node, exists := bi.index[*hash]
|
2013-08-28 18:45:21 +02:00
|
|
|
if !exists {
|
|
|
|
// Try to look up the height for passed block hash. Assume an
|
|
|
|
// error means it doesn't exist and just return the locator for
|
|
|
|
// the block itself.
|
2015-08-26 06:03:18 +02:00
|
|
|
var height int32
|
2016-04-25 11:12:09 +02:00
|
|
|
err := bi.db.View(func(dbTx database.Tx) error {
|
2015-08-26 06:03:18 +02:00
|
|
|
var err error
|
|
|
|
height, err = dbFetchHeightByHash(dbTx, hash)
|
|
|
|
return err
|
|
|
|
})
|
2013-08-28 18:45:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return locator
|
|
|
|
}
|
|
|
|
|
2015-02-12 22:10:08 +01:00
|
|
|
blockHeight = height
|
2013-08-28 18:45:21 +02:00
|
|
|
} else {
|
|
|
|
blockHeight = node.height
|
|
|
|
|
|
|
|
// Find the height at which this node forks from the main chain
|
|
|
|
// if the node is on a side chain.
|
|
|
|
if !node.inMainChain {
|
|
|
|
for n := node; n.parent != nil; n = n.parent {
|
|
|
|
if n.inMainChain {
|
|
|
|
forkHeight = n.height
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the block locators according to the algorithm described in
|
2015-08-26 06:03:18 +02:00
|
|
|
// in the BlockLocator comment and make sure to leave room for the final
|
|
|
|
// genesis hash.
|
|
|
|
//
|
|
|
|
// The error is intentionally ignored here since the only way the code
|
|
|
|
// could fail is if there is something wrong with the database which
|
|
|
|
// will be caught in short order anyways and it's also safe to ignore
|
|
|
|
// block locators.
|
2016-04-25 11:12:09 +02:00
|
|
|
_ = bi.db.View(func(dbTx database.Tx) error {
|
2015-08-26 06:03:18 +02:00
|
|
|
iterNode := node
|
|
|
|
increment := int32(1)
|
|
|
|
for len(locator) < wire.MaxBlockLocatorsPerMsg-1 {
|
|
|
|
// Once there are 10 locators, exponentially increase
|
|
|
|
// the distance between each block locator.
|
|
|
|
if len(locator) > 10 {
|
|
|
|
increment *= 2
|
|
|
|
}
|
|
|
|
blockHeight -= increment
|
|
|
|
if blockHeight < 1 {
|
|
|
|
break
|
|
|
|
}
|
2013-08-28 18:45:21 +02:00
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// As long as this is still on the side chain, walk
|
|
|
|
// backwards along the side chain nodes to each block
|
|
|
|
// height.
|
|
|
|
if forkHeight != -1 && blockHeight > forkHeight {
|
|
|
|
// Intentionally use parent field instead of the
|
2016-04-25 11:12:09 +02:00
|
|
|
// PrevNodeFromNode function since we don't
|
2015-08-26 06:03:18 +02:00
|
|
|
// want to dynamically load nodes when building
|
|
|
|
// block locators. Side chain blocks should
|
|
|
|
// always be in memory already, and if they
|
|
|
|
// aren't for some reason it's ok to skip them.
|
|
|
|
for iterNode != nil && blockHeight > iterNode.height {
|
|
|
|
iterNode = iterNode.parent
|
|
|
|
}
|
|
|
|
if iterNode != nil && iterNode.height == blockHeight {
|
|
|
|
locator = append(locator, iterNode.hash)
|
|
|
|
}
|
|
|
|
continue
|
2013-08-28 18:45:21 +02:00
|
|
|
}
|
2015-08-26 06:03:18 +02:00
|
|
|
|
|
|
|
// The desired block height is in the main chain, so
|
|
|
|
// look it up from the main chain database.
|
|
|
|
h, err := dbFetchHashByHeight(dbTx, blockHeight)
|
|
|
|
if err != nil {
|
|
|
|
// This shouldn't happen and it's ok to ignore
|
|
|
|
// block locators, so just continue to the next
|
|
|
|
// one.
|
|
|
|
log.Warnf("Lookup of known valid height failed %v",
|
|
|
|
blockHeight)
|
|
|
|
continue
|
2013-08-28 18:45:21 +02:00
|
|
|
}
|
2015-08-26 06:03:18 +02:00
|
|
|
locator = append(locator, h)
|
2013-08-28 18:45:21 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
return nil
|
|
|
|
})
|
2013-08-28 18:45:21 +02:00
|
|
|
|
|
|
|
// Append the appropriate genesis block.
|
2016-04-25 11:12:09 +02:00
|
|
|
locator = append(locator, bi.chainParams.GenesisHash)
|
2013-08-28 18:45:21 +02:00
|
|
|
return locator
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// BlockLocatorFromHash returns a block locator for the passed block hash.
|
|
|
|
// See BlockLocator for details on the algorithm used to create a block locator.
|
|
|
|
//
|
|
|
|
// In addition to the general algorithm referenced above, there are a couple of
|
|
|
|
// special cases which are handled:
|
|
|
|
//
|
|
|
|
// - If the genesis hash is passed, there are no previous hashes to add and
|
|
|
|
// therefore the block locator will only consist of the genesis hash
|
|
|
|
// - If the passed hash is not currently known, the block locator will only
|
|
|
|
// consist of the passed hash
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
2016-08-08 21:04:33 +02:00
|
|
|
func (b *BlockChain) BlockLocatorFromHash(hash *chainhash.Hash) BlockLocator {
|
2015-08-26 06:03:18 +02:00
|
|
|
b.chainLock.RLock()
|
2016-04-25 11:12:09 +02:00
|
|
|
b.index.RLock()
|
|
|
|
locator := b.index.blockLocatorFromHash(hash)
|
|
|
|
b.index.RUnlock()
|
2015-08-26 06:03:18 +02:00
|
|
|
b.chainLock.RUnlock()
|
|
|
|
return locator
|
|
|
|
}
|
|
|
|
|
2013-08-28 18:45:21 +02:00
|
|
|
// LatestBlockLocator returns a block locator for the latest known tip of the
|
|
|
|
// main (best) chain.
|
2015-08-26 06:03:18 +02:00
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
2013-08-28 18:45:21 +02:00
|
|
|
func (b *BlockChain) LatestBlockLocator() (BlockLocator, error) {
|
2015-08-26 06:03:18 +02:00
|
|
|
b.chainLock.RLock()
|
2016-04-25 11:12:09 +02:00
|
|
|
b.index.RLock()
|
|
|
|
locator := b.index.blockLocatorFromHash(b.bestNode.hash)
|
|
|
|
b.index.RUnlock()
|
2015-08-26 06:03:18 +02:00
|
|
|
b.chainLock.RUnlock()
|
|
|
|
return locator, nil
|
2013-08-28 18:45:21 +02:00
|
|
|
}
|