Implement a fast path for the Initial Block Download.

It is not necessary to do all of the transaction validation on
blocks if they have been confirmed to be in the block chain leading
up to the final checkpoint in a given blockschain.

This algorithm fetches block headers from the peer, then once it has
established the full blockchain connection, it requests blocks.
Any blocks before the final checkpoint pass true for fastAdd on
btcchain operation, which causes it to do less valiation on the block.
This commit is contained in:
Dale Rahn 2013-11-18 16:23:51 -05:00
parent 55331de532
commit 992d11830c
6 changed files with 138 additions and 111 deletions

View file

@ -14,7 +14,11 @@ import (
// It performs several validation checks which depend on its position within // It performs several validation checks which depend on its position within
// the block chain before adding it. The block is expected to have already gone // the block chain before adding it. The block is expected to have already gone
// through ProcessBlock before calling this function with it. // through ProcessBlock before calling this function with it.
func (b *BlockChain) maybeAcceptBlock(block *btcutil.Block) error { // The fastAdd argument modifies the behavior of the function by avoiding the
// somewhat expensive operation: BIP34 validation, it also passes the argument
// down to connectBestChain()
func (b *BlockChain) maybeAcceptBlock(block *btcutil.Block, fastAdd bool) error {
// Get a block node for the block previous to this one. Will be nil // Get a block node for the block previous to this one. Will be nil
// if this is the genesis block. // if this is the genesis block.
prevNode, err := b.getPrevNodeFromBlock(block) prevNode, err := b.getPrevNodeFromBlock(block)
@ -31,10 +35,13 @@ func (b *BlockChain) maybeAcceptBlock(block *btcutil.Block) error {
} }
block.SetHeight(blockHeight) block.SetHeight(blockHeight)
// Ensure the difficulty specified in the block header matches the
// calculated difficulty based on the previous block and difficulty
// retarget rules.
blockHeader := block.MsgBlock().Header blockHeader := block.MsgBlock().Header
if !fastAdd {
// Ensure the difficulty specified in the block header matches
// the calculated difficulty based on the previous block and
// difficulty
// retarget rules.
expectedDifficulty, err := b.calcNextRequiredDifficulty(prevNode, block) expectedDifficulty, err := b.calcNextRequiredDifficulty(prevNode, block)
if err != nil { if err != nil {
return err return err
@ -46,8 +53,8 @@ func (b *BlockChain) maybeAcceptBlock(block *btcutil.Block) error {
return RuleError(str) return RuleError(str)
} }
// Ensure the timestamp for the block header is after the median time of // Ensure the timestamp for the block header is after the
// the last several blocks (medianTimeBlocks). // median time of the last several blocks (medianTimeBlocks).
medianTime, err := b.calcPastMedianTime(prevNode) medianTime, err := b.calcPastMedianTime(prevNode)
if err != nil { if err != nil {
log.Errorf("calcPastMedianTime: %v", err) log.Errorf("calcPastMedianTime: %v", err)
@ -55,19 +62,22 @@ func (b *BlockChain) maybeAcceptBlock(block *btcutil.Block) error {
} }
if !blockHeader.Timestamp.After(medianTime) { if !blockHeader.Timestamp.After(medianTime) {
str := "block timestamp of %v is not after expected %v" str := "block timestamp of %v is not after expected %v"
str = fmt.Sprintf(str, blockHeader.Timestamp, medianTime) str = fmt.Sprintf(str, blockHeader.Timestamp,
medianTime)
return RuleError(str) return RuleError(str)
} }
// Ensure all transactions in the block are finalized. // Ensure all transactions in the block are finalized.
for _, tx := range block.Transactions() { for _, tx := range block.Transactions() {
if !IsFinalizedTransaction(tx, blockHeight, blockHeader.Timestamp) { if !IsFinalizedTransaction(tx, blockHeight,
str := fmt.Sprintf("block contains unfinalized "+ blockHeader.Timestamp) {
"transaction %v", tx.Sha()) str := fmt.Sprintf("block contains "+
"unfinalized transaction %v", tx.Sha())
return RuleError(str) return RuleError(str)
} }
} }
}
// Ensure chain matches up to predetermined checkpoints. // Ensure chain matches up to predetermined checkpoints.
// It's safe to ignore the error on Sha since it's already cached. // It's safe to ignore the error on Sha since it's already cached.
blockHash, _ := block.Sha() blockHash, _ := block.Sha()
@ -83,7 +93,9 @@ func (b *BlockChain) maybeAcceptBlock(block *btcutil.Block) error {
return RuleError(str) return RuleError(str)
} }
// Reject version 1 blocks once a majority of the network has upgraded. if !fastAdd {
// Reject version 1 blocks once a majority of the network has
// upgraded.
// Rules: // Rules:
// 95% (950 / 1000) for main network // 95% (950 / 1000) for main network
// 75% (75 / 100) for the test network // 75% (75 / 100) for the test network
@ -91,20 +103,22 @@ func (b *BlockChain) maybeAcceptBlock(block *btcutil.Block) error {
if blockHeader.Version == 1 { if blockHeader.Version == 1 {
minRequired := uint64(950) minRequired := uint64(950)
numToCheck := uint64(1000) numToCheck := uint64(1000)
if b.btcnet == btcwire.TestNet3 || b.btcnet == btcwire.TestNet { if b.btcnet == btcwire.TestNet3 || b.btcnet ==
btcwire.TestNet {
minRequired = 75 minRequired = 75
numToCheck = 100 numToCheck = 100
} }
if b.isMajorityVersion(2, prevNode, minRequired, numToCheck) { if b.isMajorityVersion(2, prevNode, minRequired,
numToCheck) {
str := "new blocks with version %d are no longer valid" str := "new blocks with version %d are no longer valid"
str = fmt.Sprintf(str, blockHeader.Version) str = fmt.Sprintf(str, blockHeader.Version)
return RuleError(str) return RuleError(str)
} }
} }
// Ensure coinbase starts with serialized block heights for blocks // Ensure coinbase starts with serialized block heights for
// whose version is the serializedHeightVersion or newer once a majority // blocks whose version is the serializedHeightVersion or
// of the network has upgraded. // newer once a majority of the network has upgraded.
// Rules: // Rules:
// 75% (750 / 1000) for main network // 75% (750 / 1000) for main network
// 51% (51 / 100) for the test network // 51% (51 / 100) for the test network
@ -112,31 +126,34 @@ func (b *BlockChain) maybeAcceptBlock(block *btcutil.Block) error {
if blockHeader.Version >= serializedHeightVersion { if blockHeader.Version >= serializedHeightVersion {
minRequired := uint64(750) minRequired := uint64(750)
numToCheck := uint64(1000) numToCheck := uint64(1000)
if b.btcnet == btcwire.TestNet3 || b.btcnet == btcwire.TestNet { if b.btcnet == btcwire.TestNet3 || b.btcnet ==
btcwire.TestNet {
minRequired = 51 minRequired = 51
numToCheck = 100 numToCheck = 100
} }
if b.isMajorityVersion(serializedHeightVersion, prevNode, if b.isMajorityVersion(serializedHeightVersion,
minRequired, numToCheck) { prevNode, minRequired, numToCheck) {
expectedHeight := int64(0) expectedHeight := int64(0)
if prevNode != nil { if prevNode != nil {
expectedHeight = prevNode.height + 1 expectedHeight = prevNode.height + 1
} }
coinbaseTx := block.Transactions()[0] coinbaseTx := block.Transactions()[0]
err := checkSerializedHeight(coinbaseTx, expectedHeight) err := checkSerializedHeight(coinbaseTx,
expectedHeight)
if err != nil { if err != nil {
return err return err
} }
} }
} }
// Prune block nodes which are no longer needed before creating a new // Prune block nodes which are no longer needed before creating
// node. // a new node.
err = b.pruneBlockNodes() err = b.pruneBlockNodes()
if err != nil { if err != nil {
return err return err
} }
}
// 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).
@ -150,7 +167,7 @@ func (b *BlockChain) maybeAcceptBlock(block *btcutil.Block) error {
// Connect the passed block to the chain while respecting proper chain // Connect the passed block to the chain while respecting proper chain
// selection according to the chain with the most proof of work. This // selection according to the chain with the most proof of work. This
// also handles validation of the transaction scripts. // also handles validation of the transaction scripts.
err = b.connectBestChain(newNode, block) err = b.connectBestChain(newNode, block, fastAdd)
if err != nil { if err != nil {
return err return err
} }

View file

@ -874,7 +874,10 @@ func (b *BlockChain) reorganizeChain(detachNodes, attachNodes *list.List) error
// chain. However, it may also be extending (or creating) a side chain (fork) // chain. However, it may also be extending (or creating) a side chain (fork)
// which may or may not end up becoming the main chain depending on which fork // which may or may not end up becoming the main chain depending on which fork
// cumulatively has the most proof of work. // cumulatively has the most proof of work.
func (b *BlockChain) connectBestChain(node *blockNode, block *btcutil.Block) error { // The fastAdd argument avoids the call to checkConnectBlock which does
// several expensive transaction validation operations.
func (b *BlockChain) connectBestChain(node *blockNode, block *btcutil.Block, fastAdd bool) error {
// We haven't selected a best chain yet or we are extending the main // We haven't selected a best chain yet or we are extending the main
// (best) chain with a new block. This is the most common case. // (best) chain with a new block. This is the most common case.
if b.bestChain == nil || node.parent.hash.IsEqual(b.bestChain.hash) { if b.bestChain == nil || node.parent.hash.IsEqual(b.bestChain.hash) {
@ -883,13 +886,15 @@ func (b *BlockChain) connectBestChain(node *blockNode, block *btcutil.Block) err
// be necessary to get this node to the main chain) without // be necessary to get this node to the main chain) without
// violating any rules and without actually connecting the // violating any rules and without actually connecting the
// block. // block.
if !fastAdd {
err := b.checkConnectBlock(node, block) err := b.checkConnectBlock(node, block)
if err != nil { if err != nil {
return err return err
} }
}
// Connect the block to the main chain. // Connect the block to the main chain.
err = b.connectBlock(node, block) err := b.connectBlock(node, block)
if err != nil { if err != nil {
return err return err
} }
@ -901,6 +906,10 @@ func (b *BlockChain) connectBestChain(node *blockNode, block *btcutil.Block) err
return nil return nil
} }
if fastAdd {
bsha, _ := block.Sha()
log.Warnf("fastAdd set in the side chain case? %v\n", bsha)
}
// We're extending (or creating) a side chain which may or may not // We're extending (or creating) a side chain which may or may not
// become the main chain, but in either case we need the block stored // become the main chain, but in either case we need the block stored

View file

@ -47,7 +47,7 @@ func TestHaveBlock(t *testing.T) {
btcchain.TstSetCoinbaseMaturity(1) btcchain.TstSetCoinbaseMaturity(1)
for i := 1; i < len(blocks); i++ { for i := 1; i < len(blocks); i++ {
err = chain.ProcessBlock(blocks[i]) err = chain.ProcessBlock(blocks[i], false)
if err != nil { if err != nil {
t.Errorf("ProcessBlock fail on block %v: %v\n", i, err) t.Errorf("ProcessBlock fail on block %v: %v\n", i, err)
return return
@ -55,7 +55,7 @@ func TestHaveBlock(t *testing.T) {
} }
// Insert an orphan block. // Insert an orphan block.
if err := chain.ProcessBlock(btcutil.NewBlock(&Block100000)); err != nil { if err := chain.ProcessBlock(btcutil.NewBlock(&Block100000), false); err != nil {
t.Errorf("Unable to process block: %v", err) t.Errorf("Unable to process block: %v", err)
return return
} }

2
doc.go
View file

@ -112,7 +112,7 @@ intentionally causes an error by attempting to process a duplicate block.
// Process a block. For this example, we are going to intentionally // Process a block. For this example, we are going to intentionally
// cause an error by trying to process the genesis block which already // cause an error by trying to process the genesis block which already
// exists. // exists.
err = chain.ProcessBlock(genesisBlock) err = chain.ProcessBlock(genesisBlock, false)
if err != nil { if err != nil {
fmt.Printf("Failed to process block: %v\n", err) fmt.Printf("Failed to process block: %v\n", err)
return return

View file

@ -74,7 +74,7 @@ func (b *BlockChain) processOrphans(hash *btcwire.ShaHash) error {
i-- i--
// Potentially accept the block into the block chain. // Potentially accept the block into the block chain.
err := b.maybeAcceptBlock(orphan.block) err := b.maybeAcceptBlock(orphan.block, false)
if err != nil { if err != nil {
return err return err
} }
@ -92,7 +92,7 @@ func (b *BlockChain) processOrphans(hash *btcwire.ShaHash) error {
// the block chain. It includes functionality such as rejecting duplicate // the block chain. It includes functionality such as rejecting duplicate
// blocks, ensuring blocks follow all rules, orphan handling, and insertion into // blocks, ensuring blocks follow all rules, orphan handling, and insertion into
// the block chain along with best chain selection and reorganization. // the block chain along with best chain selection and reorganization.
func (b *BlockChain) ProcessBlock(block *btcutil.Block) error { func (b *BlockChain) ProcessBlock(block *btcutil.Block, fastAdd bool) error {
blockHash, err := block.Sha() blockHash, err := block.Sha()
if err != nil { if err != nil {
return err return err
@ -138,7 +138,7 @@ func (b *BlockChain) ProcessBlock(block *btcutil.Block) error {
blockHeader.Timestamp, checkpointTime) blockHeader.Timestamp, checkpointTime)
return RuleError(str) return RuleError(str)
} }
if !fastAdd {
// Even though the checks prior to now have already ensured the // Even though the checks prior to now have already ensured the
// proof of work exceeds the claimed amount, the claimed amount // proof of work exceeds the claimed amount, the claimed amount
// is a field in the block header which could be forged. This // is a field in the block header which could be forged. This
@ -156,6 +156,7 @@ func (b *BlockChain) ProcessBlock(block *btcutil.Block) error {
return RuleError(str) return RuleError(str)
} }
} }
}
// Handle orphan blocks. // Handle orphan blocks.
prevHash := &blockHeader.PrevBlock prevHash := &blockHeader.PrevBlock
@ -172,7 +173,7 @@ func (b *BlockChain) ProcessBlock(block *btcutil.Block) error {
// The block has passed all context independent checks and appears sane // The block has passed all context independent checks and appears sane
// enough to potentially accept it into the block chain. // enough to potentially accept it into the block chain.
err = b.maybeAcceptBlock(block) err = b.maybeAcceptBlock(block, fastAdd)
if err != nil { if err != nil {
return err return err
} }

View file

@ -58,7 +58,7 @@ func TestReorganization(t *testing.T) {
btcchain.TstSetCoinbaseMaturity(1) btcchain.TstSetCoinbaseMaturity(1)
for i := 1; i < len(blocks); i++ { for i := 1; i < len(blocks); i++ {
err = chain.ProcessBlock(blocks[i]) err = chain.ProcessBlock(blocks[i], false)
if err != nil { if err != nil {
t.Errorf("ProcessBlock fail on block %v: %v\n", i, err) t.Errorf("ProcessBlock fail on block %v: %v\n", i, err)
return return