Use block headers to generate node index.

This commit changes the node index creation to use block headers instead
of full blocks.  This speeds up the initial node index generation since it
doesn't require loading a bunch of full blocks at startup.
This commit is contained in:
Dave Collins 2014-01-19 13:00:12 -06:00
parent 1626994433
commit 583406ee51
2 changed files with 15 additions and 17 deletions

View file

@ -155,7 +155,7 @@ func (b *BlockChain) maybeAcceptBlock(block *btcutil.Block, fastAdd bool) error
// Create a new block node for the block and add it to the in-memory // Create a new block node for the block and add it to the in-memory
// block chain (could be either a side chain or the main chain). // block chain (could be either a side chain or the main chain).
newNode := newBlockNode(block) newNode := newBlockNode(blockHeader, blockHash, blockHeight)
if prevNode != nil { if prevNode != nil {
newNode.parent = prevNode newNode.parent = prevNode
newNode.height = blockHeight newNode.height = blockHeight

View file

@ -73,22 +73,16 @@ type blockNode struct {
timestamp time.Time timestamp time.Time
} }
// newBlockNode returns a new block node for the given block. It is completely // newBlockNode returns a new block node for the given block header. It is
// disconnected from the chain and the workSum value is just the work for the // completely disconnected from the chain and the workSum value is just the work
// passed block. The work sum is updated accordingly when the node is inserted // for the passed block. The work sum is updated accordingly when the node is
// into a chain. // inserted into a chain.
func newBlockNode(block *btcutil.Block) *blockNode { func newBlockNode(blockHeader *btcwire.BlockHeader, blockSha *btcwire.ShaHash, height int64) *blockNode {
// Get the block sha. It's ok to ignore the error here since
// sha has already been called and an error there would have caused
// an exit before this function is called.
blockSha, _ := block.Sha()
blockHeader := block.MsgBlock().Header
node := blockNode{ node := blockNode{
hash: blockSha, hash: blockSha,
parentHash: &blockHeader.PrevBlock, parentHash: &blockHeader.PrevBlock,
workSum: calcWork(blockHeader.Bits), workSum: calcWork(blockHeader.Bits),
height: block.Height(), height: height,
version: blockHeader.Version, version: blockHeader.Version,
bits: blockHeader.Bits, bits: blockHeader.Bits,
timestamp: blockHeader.Timestamp, timestamp: blockHeader.Timestamp,
@ -395,14 +389,18 @@ func (b *BlockChain) GenerateInitialIndex() error {
// It is used mainly to dynamically load previous blocks from database as they // It is used mainly to dynamically load previous blocks from database as they
// are needed to avoid needing to put the entire block chain in memory. // are needed to avoid needing to put the entire block chain in memory.
func (b *BlockChain) loadBlockNode(hash *btcwire.ShaHash) (*blockNode, error) { func (b *BlockChain) loadBlockNode(hash *btcwire.ShaHash) (*blockNode, error) {
// Load the block from the db. // Load the block header and height from the db.
block, err := b.db.FetchBlockBySha(hash) blockHeader, err := b.db.FetchBlockHeaderBySha(hash)
if err != nil {
return nil, err
}
blockHeight, err := b.db.FetchBlockHeightBySha(hash)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// Create the new block node for the block and set the work. // Create the new block node for the block and set the work.
node := newBlockNode(block) node := newBlockNode(blockHeader, hash, blockHeight)
node.inMainChain = true node.inMainChain = true
// Add the node to the chain. // Add the node to the chain.
@ -414,7 +412,7 @@ func (b *BlockChain) loadBlockNode(hash *btcwire.ShaHash) (*blockNode, error) {
// therefore is an error to insert into the chain // therefore is an error to insert into the chain
// 4) Neither 1 or 2 is true, but this is the first node being added // 4) Neither 1 or 2 is true, but this is the first node being added
// to the tree, so it's the root. // to the tree, so it's the root.
prevHash := &block.MsgBlock().Header.PrevBlock prevHash := &blockHeader.PrevBlock
if parentNode, ok := b.index[*prevHash]; ok { if parentNode, ok := b.index[*prevHash]; ok {
// Case 1 -- This node is a child of an existing block node. // Case 1 -- This node is a child of an existing block node.
// Update the node's work sum with the sum of the parent node's // Update the node's work sum with the sum of the parent node's