2013-07-18 16:49:28 +02:00
|
|
|
// Copyright (c) 2013 Conformal Systems LLC.
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package btcchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/conformal/btcutil"
|
|
|
|
"github.com/conformal/btcwire"
|
|
|
|
)
|
|
|
|
|
|
|
|
// maybeAcceptBlock potentially accepts a block into the memory block chain.
|
|
|
|
// 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
|
|
|
|
// through ProcessBlock before calling this function with it.
|
|
|
|
func (b *BlockChain) maybeAcceptBlock(block *btcutil.Block) error {
|
|
|
|
// Get a block node for the block previous to this one. Will be nil
|
|
|
|
// if this is the genesis block.
|
|
|
|
prevNode, err := b.getPrevNodeFromBlock(block)
|
|
|
|
if err != nil {
|
2013-07-24 19:31:14 +02:00
|
|
|
log.Errorf("getPrevNodeFromBlock: %v", err)
|
2013-07-18 16:49:28 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-10-11 17:24:13 +02:00
|
|
|
// The height of this block is one more than the referenced previous
|
|
|
|
// block.
|
2013-07-18 16:49:28 +02:00
|
|
|
blockHeight := int64(0)
|
|
|
|
if prevNode != nil {
|
|
|
|
blockHeight = prevNode.height + 1
|
|
|
|
}
|
2013-07-29 23:58:48 +02:00
|
|
|
block.SetHeight(blockHeight)
|
2013-07-18 16:49:28 +02:00
|
|
|
|
|
|
|
// 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
|
2013-07-24 18:37:45 +02:00
|
|
|
expectedDifficulty, err := b.calcNextRequiredDifficulty(prevNode, block)
|
2013-07-18 16:49:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
blockDifficulty := blockHeader.Bits
|
|
|
|
if blockDifficulty != expectedDifficulty {
|
|
|
|
str := "block difficulty of %d is not the expected value of %d"
|
|
|
|
str = fmt.Sprintf(str, blockDifficulty, expectedDifficulty)
|
|
|
|
return RuleError(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the timestamp for the block header is after the median time of
|
|
|
|
// the last several blocks (medianTimeBlocks).
|
|
|
|
medianTime, err := b.calcPastMedianTime(prevNode)
|
|
|
|
if err != nil {
|
2013-07-24 19:31:14 +02:00
|
|
|
log.Errorf("calcPastMedianTime: %v", err)
|
2013-07-18 16:49:28 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !blockHeader.Timestamp.After(medianTime) {
|
|
|
|
str := "block timestamp of %v is not after expected %v"
|
|
|
|
str = fmt.Sprintf(str, blockHeader.Timestamp, medianTime)
|
|
|
|
return RuleError(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure all transactions in the block are finalized.
|
2013-10-28 21:17:53 +01:00
|
|
|
for _, tx := range block.Transactions() {
|
2013-09-30 23:40:07 +02:00
|
|
|
if !IsFinalizedTransaction(tx, blockHeight, blockHeader.Timestamp) {
|
2013-07-18 16:49:28 +02:00
|
|
|
str := fmt.Sprintf("block contains unfinalized "+
|
2013-10-28 21:17:53 +01:00
|
|
|
"transaction %v", tx.Sha())
|
2013-07-18 16:49:28 +02:00
|
|
|
return RuleError(str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure chain matches up to predetermined checkpoints.
|
|
|
|
// It's safe to ignore the error on Sha since it's already cached.
|
|
|
|
blockHash, _ := block.Sha()
|
|
|
|
if !b.verifyCheckpoint(blockHeight, blockHash) {
|
|
|
|
// TODO(davec): This should probably be a distinct error type
|
|
|
|
// (maybe CheckpointError). Since this error shouldn't happen
|
|
|
|
// unless the peer is connected to a rogue network serving up an
|
|
|
|
// alternate chain, the caller would likely need to react by
|
|
|
|
// disconnecting peers and rolling back the chain to the last
|
|
|
|
// known good point.
|
|
|
|
str := fmt.Sprintf("block at height %d does not match "+
|
|
|
|
"checkpoint hash", blockHeight)
|
|
|
|
return RuleError(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reject version 1 blocks once a majority of the network has upgraded.
|
|
|
|
// Rules:
|
|
|
|
// 95% (950 / 1000) for main network
|
|
|
|
// 75% (75 / 100) for the test network
|
|
|
|
// This is part of BIP_0034.
|
|
|
|
if blockHeader.Version == 1 {
|
|
|
|
minRequired := uint64(950)
|
|
|
|
numToCheck := uint64(1000)
|
|
|
|
if b.btcnet == btcwire.TestNet3 || b.btcnet == btcwire.TestNet {
|
|
|
|
minRequired = 75
|
|
|
|
numToCheck = 100
|
|
|
|
}
|
|
|
|
if b.isMajorityVersion(2, prevNode, minRequired, numToCheck) {
|
|
|
|
str := "new blocks with version %d are no longer valid"
|
|
|
|
str = fmt.Sprintf(str, blockHeader.Version)
|
|
|
|
return RuleError(str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure coinbase starts with serialized block heights for blocks
|
|
|
|
// whose version is the serializedHeightVersion or newer once a majority
|
|
|
|
// of the network has upgraded.
|
|
|
|
// Rules:
|
|
|
|
// 75% (750 / 1000) for main network
|
|
|
|
// 51% (51 / 100) for the test network
|
|
|
|
// This is part of BIP_0034.
|
|
|
|
if blockHeader.Version >= serializedHeightVersion {
|
|
|
|
minRequired := uint64(750)
|
|
|
|
numToCheck := uint64(1000)
|
|
|
|
if b.btcnet == btcwire.TestNet3 || b.btcnet == btcwire.TestNet {
|
|
|
|
minRequired = 51
|
|
|
|
numToCheck = 100
|
|
|
|
}
|
|
|
|
if b.isMajorityVersion(serializedHeightVersion, prevNode,
|
|
|
|
minRequired, numToCheck) {
|
|
|
|
|
|
|
|
expectedHeight := int64(0)
|
|
|
|
if prevNode != nil {
|
|
|
|
expectedHeight = prevNode.height + 1
|
|
|
|
}
|
2013-10-28 21:17:53 +01:00
|
|
|
coinbaseTx := block.Transactions()[0]
|
2013-07-18 16:49:28 +02:00
|
|
|
err := checkSerializedHeight(coinbaseTx, expectedHeight)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-01 18:31:42 +02:00
|
|
|
// Prune block nodes which are no longer needed before creating a new
|
|
|
|
// node.
|
|
|
|
err = b.pruneBlockNodes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// 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).
|
|
|
|
newNode := newBlockNode(block)
|
|
|
|
if prevNode != nil {
|
|
|
|
newNode.parent = prevNode
|
|
|
|
newNode.height = blockHeight
|
|
|
|
newNode.workSum.Add(prevNode.workSum, newNode.workSum)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect the passed block to the chain while respecting proper chain
|
|
|
|
// selection according to the chain with the most proof of work. This
|
|
|
|
// also handles validation of the transaction scripts.
|
|
|
|
err = b.connectBestChain(newNode, block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify the caller that the new block was accepted into the block
|
|
|
|
// chain. The caller would typically want to react by relaying the
|
|
|
|
// inventory to other peers.
|
|
|
|
b.sendNotification(NTBlockAccepted, block)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|