2018-07-12 02:41:17 +02:00
|
|
|
// Copyright (c) 2013-2018 The btcsuite developers
|
|
|
|
// Copyright (c) 2015-2018 The Decred developers
|
2013-07-18 16:49:28 +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-07-18 16:49:28 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"fmt"
|
2013-08-20 18:13:39 +02:00
|
|
|
"sync"
|
2013-07-18 16:49:28 +02:00
|
|
|
"time"
|
2014-07-02 18:04:59 +02:00
|
|
|
|
2021-10-15 07:45:32 +02:00
|
|
|
"github.com/lbryio/lbcd/chaincfg"
|
|
|
|
"github.com/lbryio/lbcd/chaincfg/chainhash"
|
|
|
|
"github.com/lbryio/lbcd/database"
|
|
|
|
"github.com/lbryio/lbcd/txscript"
|
|
|
|
"github.com/lbryio/lbcd/wire"
|
|
|
|
btcutil "github.com/lbryio/lbcutil"
|
2018-08-05 22:59:25 +02:00
|
|
|
|
2021-10-15 07:45:32 +02:00
|
|
|
"github.com/lbryio/lbcd/claimtrie"
|
2013-07-18 16:49:28 +02:00
|
|
|
)
|
|
|
|
|
2013-08-01 18:31:42 +02:00
|
|
|
const (
|
|
|
|
// maxOrphanBlocks is the maximum number of orphan blocks that can be
|
|
|
|
// queued.
|
|
|
|
maxOrphanBlocks = 100
|
|
|
|
)
|
2013-07-18 16:49:28 +02:00
|
|
|
|
2017-08-18 14:25:54 +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 12 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.
|
|
|
|
//
|
|
|
|
// For example, assume 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 7 6 4 genesis]
|
|
|
|
type BlockLocator []*chainhash.Hash
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// orphanBlock represents a block that we don't yet have the parent for. It
|
|
|
|
// is a normal block plus an expiration time to prevent caching the orphan
|
|
|
|
// forever.
|
|
|
|
type orphanBlock struct {
|
|
|
|
block *btcutil.Block
|
|
|
|
expiration time.Time
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// BestState houses information about the current best block and other info
|
|
|
|
// related to the state of the main chain as it exists from the point of view of
|
|
|
|
// the current best block.
|
|
|
|
//
|
|
|
|
// The BestSnapshot method can be used to obtain access to this information
|
|
|
|
// in a concurrent safe manner and the data will not be changed out from under
|
|
|
|
// the caller when chain state changes occur as the function name implies.
|
|
|
|
// However, the returned snapshot must be treated as immutable since it is
|
|
|
|
// shared by all callers.
|
|
|
|
type BestState struct {
|
2016-10-19 03:33:20 +02:00
|
|
|
Hash chainhash.Hash // The hash of the block.
|
|
|
|
Height int32 // The height of the block.
|
|
|
|
Bits uint32 // The difficulty bits of the block.
|
|
|
|
BlockSize uint64 // The size of the block.
|
|
|
|
BlockWeight uint64 // The weight of the block.
|
|
|
|
NumTxns uint64 // The number of txns in the block.
|
|
|
|
TotalTxns uint64 // The total number of txns in the chain.
|
|
|
|
MedianTime time.Time // Median time as per CalcPastMedianTime.
|
2015-08-26 06:03:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// newBestState returns a new best stats instance for the given parameters.
|
2016-10-19 03:33:20 +02:00
|
|
|
func newBestState(node *blockNode, blockSize, blockWeight, numTxns,
|
|
|
|
totalTxns uint64, medianTime time.Time) *BestState {
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
return &BestState{
|
2016-10-19 03:33:20 +02:00
|
|
|
Hash: node.hash,
|
|
|
|
Height: node.height,
|
|
|
|
Bits: node.bits,
|
|
|
|
BlockSize: blockSize,
|
|
|
|
BlockWeight: blockWeight,
|
|
|
|
NumTxns: numTxns,
|
|
|
|
TotalTxns: totalTxns,
|
|
|
|
MedianTime: medianTime,
|
2015-08-26 06:03:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// BlockChain provides functions for working with the bitcoin block chain.
|
|
|
|
// It includes functionality such as rejecting duplicate blocks, ensuring blocks
|
|
|
|
// follow all rules, orphan handling, checkpoint handling, and best chain
|
|
|
|
// selection with reorganization.
|
|
|
|
type BlockChain struct {
|
2015-08-26 06:03:18 +02:00
|
|
|
// The following fields are set when the instance is created and can't
|
|
|
|
// be changed afterwards, so there is no need to protect them with a
|
|
|
|
// separate mutex.
|
2016-12-02 00:54:41 +01:00
|
|
|
checkpoints []chaincfg.Checkpoint
|
2015-08-08 04:20:49 +02:00
|
|
|
checkpointsByHeight map[int32]*chaincfg.Checkpoint
|
2015-08-26 06:03:18 +02:00
|
|
|
db database.DB
|
|
|
|
chainParams *chaincfg.Params
|
2016-07-14 02:36:36 +02:00
|
|
|
timeSource MedianTimeSource
|
2015-09-25 01:22:00 +02:00
|
|
|
sigCache *txscript.SigCache
|
2016-02-19 05:51:18 +01:00
|
|
|
indexManager IndexManager
|
2016-10-19 03:33:20 +02:00
|
|
|
hashCache *txscript.HashCache
|
2015-08-26 06:03:18 +02:00
|
|
|
|
2016-08-10 23:02:23 +02:00
|
|
|
// The following fields are calculated based upon the provided chain
|
|
|
|
// parameters. They are also set when the instance is created and
|
|
|
|
// can't be changed afterwards, so there is no need to protect them with
|
|
|
|
// a separate mutex.
|
|
|
|
minRetargetTimespan int64 // target timespan / adjustment factor
|
|
|
|
maxRetargetTimespan int64 // target timespan * adjustment factor
|
|
|
|
blocksPerRetarget int32 // target timespan / target time per block
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// chainLock protects concurrent access to the vast majority of the
|
|
|
|
// fields in this struct below this point.
|
|
|
|
chainLock sync.RWMutex
|
|
|
|
|
2017-08-18 14:25:54 +02:00
|
|
|
// These fields are related to the memory block index. They both have
|
|
|
|
// their own locks, however they are often also protected by the chain
|
|
|
|
// lock to help prevent logic races when blocks are being processed.
|
|
|
|
//
|
|
|
|
// index houses the entire block index in memory. The block index is
|
|
|
|
// a tree-shaped structure.
|
|
|
|
//
|
|
|
|
// bestChain tracks the current active chain by making use of an
|
|
|
|
// efficient chain view into the block index.
|
|
|
|
index *blockIndex
|
|
|
|
bestChain *chainView
|
2015-08-26 06:03:18 +02:00
|
|
|
|
|
|
|
// These fields are related to handling of orphan blocks. They are
|
|
|
|
// protected by a combination of the chain lock and the orphan lock.
|
|
|
|
orphanLock sync.RWMutex
|
2016-08-08 21:04:33 +02:00
|
|
|
orphans map[chainhash.Hash]*orphanBlock
|
|
|
|
prevOrphans map[chainhash.Hash][]*orphanBlock
|
2015-08-26 06:03:18 +02:00
|
|
|
oldestOrphan *orphanBlock
|
|
|
|
|
|
|
|
// These fields are related to checkpoint handling. They are protected
|
|
|
|
// by the chain lock.
|
2017-02-04 04:18:39 +01:00
|
|
|
nextCheckpoint *chaincfg.Checkpoint
|
|
|
|
checkpointNode *blockNode
|
2015-08-26 06:03:18 +02:00
|
|
|
|
|
|
|
// The state is used as a fairly efficient way to cache information
|
|
|
|
// about the current best chain state that is returned to callers when
|
|
|
|
// requested. It operates on the principle of MVCC such that any time a
|
|
|
|
// new block becomes the best block, the state pointer is replaced with
|
|
|
|
// a new struct and the old state is left untouched. In this way,
|
|
|
|
// multiple callers can be pointing to different best chain states.
|
|
|
|
// This is acceptable for most callers because the state is only being
|
|
|
|
// queried at a specific point in time.
|
|
|
|
//
|
|
|
|
// In addition, some of the fields are stored in the database so the
|
|
|
|
// chain state can be quickly reconstructed on load.
|
|
|
|
stateLock sync.RWMutex
|
|
|
|
stateSnapshot *BestState
|
2016-08-29 00:51:55 +02:00
|
|
|
|
|
|
|
// The following caches are used to efficiently keep track of the
|
|
|
|
// current deployment threshold state of each rule change deployment.
|
|
|
|
//
|
|
|
|
// This information is stored in the database so it can be quickly
|
|
|
|
// reconstructed on load.
|
|
|
|
//
|
|
|
|
// warningCaches caches the current deployment threshold state for blocks
|
|
|
|
// in each of the **possible** deployments. This is used in order to
|
|
|
|
// detect when new unrecognized rule changes are being voted on and/or
|
|
|
|
// have been activated such as will be the case when older versions of
|
|
|
|
// the software are being used
|
|
|
|
//
|
|
|
|
// deploymentCaches caches the current deployment threshold state for
|
|
|
|
// blocks in each of the actively defined deployments.
|
|
|
|
warningCaches []thresholdStateCache
|
|
|
|
deploymentCaches []thresholdStateCache
|
|
|
|
|
|
|
|
// The following fields are used to determine if certain warnings have
|
|
|
|
// already been shown.
|
|
|
|
//
|
|
|
|
// unknownRulesWarned refers to warnings due to unknown rules being
|
|
|
|
// activated.
|
2019-08-27 04:09:13 +02:00
|
|
|
unknownRulesWarned bool
|
2017-08-11 22:17:54 +02:00
|
|
|
|
|
|
|
// The notifications field stores a slice of callbacks to be executed on
|
|
|
|
// certain blockchain events.
|
|
|
|
notificationsLock sync.RWMutex
|
|
|
|
notifications []NotificationCallback
|
2018-08-05 22:59:25 +02:00
|
|
|
|
|
|
|
claimTrie *claimtrie.ClaimTrie
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
2013-10-08 19:32:15 +02:00
|
|
|
// HaveBlock returns whether or not the chain instance has the block represented
|
|
|
|
// by the passed hash. This includes checking the various places a block can
|
|
|
|
// be like part of the main chain, on a side chain, or in the orphan pool.
|
2013-08-22 22:50:57 +02:00
|
|
|
//
|
2015-08-26 06:03:18 +02:00
|
|
|
// This function is safe for concurrent access.
|
2016-08-08 21:04:33 +02:00
|
|
|
func (b *BlockChain) HaveBlock(hash *chainhash.Hash) (bool, error) {
|
2014-07-07 18:42:28 +02:00
|
|
|
exists, err := b.blockExists(hash)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2016-07-25 17:16:57 +02:00
|
|
|
return exists || b.IsKnownOrphan(hash), nil
|
2013-08-22 22:50:57 +02:00
|
|
|
}
|
|
|
|
|
2013-08-20 18:19:34 +02:00
|
|
|
// IsKnownOrphan returns whether the passed hash is currently a known orphan.
|
|
|
|
// Keep in mind that only a limited number of orphans are held onto for a
|
|
|
|
// limited amount of time, so this function must not be used as an absolute
|
|
|
|
// way to test if a block is an orphan block. A full block (as opposed to just
|
|
|
|
// its hash) must be passed to ProcessBlock for that purpose. However, calling
|
|
|
|
// ProcessBlock with an orphan that already exists results in an error, so this
|
|
|
|
// function provides a mechanism for a caller to intelligently detect *recent*
|
|
|
|
// duplicate orphans and react accordingly.
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
2016-08-08 21:04:33 +02:00
|
|
|
func (b *BlockChain) IsKnownOrphan(hash *chainhash.Hash) bool {
|
2013-08-20 18:19:34 +02:00
|
|
|
// Protect concurrent access. Using a read lock only so multiple
|
|
|
|
// readers can query without blocking each other.
|
|
|
|
b.orphanLock.RLock()
|
2016-04-13 03:19:13 +02:00
|
|
|
_, exists := b.orphans[*hash]
|
|
|
|
b.orphanLock.RUnlock()
|
2013-08-20 18:19:34 +02:00
|
|
|
|
2016-04-13 03:19:13 +02:00
|
|
|
return exists
|
2013-08-20 18:19:34 +02:00
|
|
|
}
|
|
|
|
|
2013-08-20 18:13:39 +02:00
|
|
|
// GetOrphanRoot returns the head of the chain for the provided hash from the
|
2013-07-18 16:49:28 +02:00
|
|
|
// map of orphan blocks.
|
2013-08-20 18:13:39 +02:00
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
2016-08-08 21:04:33 +02:00
|
|
|
func (b *BlockChain) GetOrphanRoot(hash *chainhash.Hash) *chainhash.Hash {
|
2013-08-20 18:13:39 +02:00
|
|
|
// Protect concurrent access. Using a read lock only so multiple
|
|
|
|
// readers can query without blocking each other.
|
|
|
|
b.orphanLock.RLock()
|
|
|
|
defer b.orphanLock.RUnlock()
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// Keep looping while the parent of each orphaned block is
|
|
|
|
// known and is an orphan itself.
|
2013-08-22 21:37:06 +02:00
|
|
|
orphanRoot := hash
|
|
|
|
prevHash := hash
|
2013-07-18 16:49:28 +02:00
|
|
|
for {
|
|
|
|
orphan, exists := b.orphans[*prevHash]
|
|
|
|
if !exists {
|
|
|
|
break
|
|
|
|
}
|
2013-08-22 21:37:06 +02:00
|
|
|
orphanRoot = prevHash
|
2013-07-18 16:49:28 +02:00
|
|
|
prevHash = &orphan.block.MsgBlock().Header.PrevBlock
|
|
|
|
}
|
|
|
|
|
2013-08-22 21:37:06 +02:00
|
|
|
return orphanRoot
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// removeOrphanBlock removes the passed orphan block from the orphan pool and
|
|
|
|
// previous orphan index.
|
|
|
|
func (b *BlockChain) removeOrphanBlock(orphan *orphanBlock) {
|
2013-08-20 18:13:39 +02:00
|
|
|
// Protect concurrent access.
|
|
|
|
b.orphanLock.Lock()
|
|
|
|
defer b.orphanLock.Unlock()
|
|
|
|
|
2015-04-17 07:44:15 +02:00
|
|
|
// Remove the orphan block from the orphan pool.
|
2016-08-08 21:04:33 +02:00
|
|
|
orphanHash := orphan.block.Hash()
|
2013-07-18 16:49:28 +02:00
|
|
|
delete(b.orphans, *orphanHash)
|
|
|
|
|
2013-08-17 21:05:36 +02:00
|
|
|
// Remove the reference from the previous orphan index too. An indexing
|
|
|
|
// for loop is intentionally used over a range here as range does not
|
|
|
|
// reevaluate the slice on each iteration nor does it adjust the index
|
|
|
|
// for the modified slice.
|
2013-07-18 16:49:28 +02:00
|
|
|
prevHash := &orphan.block.MsgBlock().Header.PrevBlock
|
|
|
|
orphans := b.prevOrphans[*prevHash]
|
2013-08-17 21:05:36 +02:00
|
|
|
for i := 0; i < len(orphans); i++ {
|
2016-08-08 21:04:33 +02:00
|
|
|
hash := orphans[i].block.Hash()
|
2013-07-18 16:49:28 +02:00
|
|
|
if hash.IsEqual(orphanHash) {
|
|
|
|
copy(orphans[i:], orphans[i+1:])
|
|
|
|
orphans[len(orphans)-1] = nil
|
2013-08-04 20:41:05 +02:00
|
|
|
orphans = orphans[:len(orphans)-1]
|
2013-08-17 21:05:36 +02:00
|
|
|
i--
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-04 20:41:05 +02:00
|
|
|
b.prevOrphans[*prevHash] = orphans
|
2013-07-18 16:49:28 +02:00
|
|
|
|
|
|
|
// Remove the map entry altogether if there are no longer any orphans
|
|
|
|
// which depend on the parent hash.
|
|
|
|
if len(b.prevOrphans[*prevHash]) == 0 {
|
|
|
|
delete(b.prevOrphans, *prevHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addOrphanBlock adds the passed block (which is already determined to be
|
|
|
|
// an orphan prior calling this function) to the orphan pool. It lazily cleans
|
|
|
|
// up any expired blocks so a separate cleanup poller doesn't need to be run.
|
|
|
|
// It also imposes a maximum limit on the number of outstanding orphan
|
|
|
|
// blocks and will remove the oldest received orphan block if the limit is
|
|
|
|
// exceeded.
|
|
|
|
func (b *BlockChain) addOrphanBlock(block *btcutil.Block) {
|
|
|
|
// Remove expired orphan blocks.
|
|
|
|
for _, oBlock := range b.orphans {
|
|
|
|
if time.Now().After(oBlock.expiration) {
|
|
|
|
b.removeOrphanBlock(oBlock)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the oldest orphan block pointer so it can be discarded
|
|
|
|
// in case the orphan pool fills up.
|
|
|
|
if b.oldestOrphan == nil || oBlock.expiration.Before(b.oldestOrphan.expiration) {
|
|
|
|
b.oldestOrphan = oBlock
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit orphan blocks to prevent memory exhaustion.
|
|
|
|
if len(b.orphans)+1 > maxOrphanBlocks {
|
|
|
|
// Remove the oldest orphan to make room for the new one.
|
|
|
|
b.removeOrphanBlock(b.oldestOrphan)
|
|
|
|
b.oldestOrphan = nil
|
|
|
|
}
|
|
|
|
|
2013-08-20 18:13:39 +02:00
|
|
|
// Protect concurrent access. This is intentionally done here instead
|
|
|
|
// of near the top since removeOrphanBlock does its own locking and
|
|
|
|
// the range iterator is not invalidated by removing map entries.
|
|
|
|
b.orphanLock.Lock()
|
2014-03-09 17:04:47 +01:00
|
|
|
defer b.orphanLock.Unlock()
|
2013-08-20 18:13:39 +02:00
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// Insert the block into the orphan map with an expiration time
|
|
|
|
// 1 hour from now.
|
|
|
|
expiration := time.Now().Add(time.Hour)
|
|
|
|
oBlock := &orphanBlock{
|
|
|
|
block: block,
|
|
|
|
expiration: expiration,
|
|
|
|
}
|
2016-08-08 21:04:33 +02:00
|
|
|
b.orphans[*block.Hash()] = oBlock
|
2013-07-18 16:49:28 +02:00
|
|
|
|
|
|
|
// Add to previous hash lookup index for faster dependency lookups.
|
|
|
|
prevHash := &block.MsgBlock().Header.PrevBlock
|
|
|
|
b.prevOrphans[*prevHash] = append(b.prevOrphans[*prevHash], oBlock)
|
|
|
|
}
|
|
|
|
|
2016-06-22 06:51:43 +02:00
|
|
|
// SequenceLock represents the converted relative lock-time in seconds, and
|
|
|
|
// absolute block-height for a transaction input's relative lock-times.
|
|
|
|
// According to SequenceLock, after the referenced input has been confirmed
|
|
|
|
// within a block, a transaction spending that input can be included into a
|
|
|
|
// block either after 'seconds' (according to past median time), or once the
|
|
|
|
// 'BlockHeight' has been reached.
|
|
|
|
type SequenceLock struct {
|
|
|
|
Seconds int64
|
|
|
|
BlockHeight int32
|
|
|
|
}
|
|
|
|
|
|
|
|
// CalcSequenceLock computes a relative lock-time SequenceLock for the passed
|
|
|
|
// transaction using the passed UtxoViewpoint to obtain the past median time
|
|
|
|
// for blocks in which the referenced inputs of the transactions were included
|
|
|
|
// within. The generated SequenceLock lock can be used in conjunction with a
|
|
|
|
// block height, and adjusted median block time to determine if all the inputs
|
|
|
|
// referenced within a transaction have reached sufficient maturity allowing
|
|
|
|
// the candidate transaction to be included in a block.
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
2017-08-14 07:22:40 +02:00
|
|
|
func (b *BlockChain) CalcSequenceLock(tx *btcutil.Tx, utxoView *UtxoViewpoint, mempool bool) (*SequenceLock, error) {
|
2016-06-22 06:51:43 +02:00
|
|
|
b.chainLock.Lock()
|
|
|
|
defer b.chainLock.Unlock()
|
|
|
|
|
2017-08-18 14:25:54 +02:00
|
|
|
return b.calcSequenceLock(b.bestChain.Tip(), tx, utxoView, mempool)
|
2016-06-22 06:51:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// calcSequenceLock computes the relative lock-times for the passed
|
|
|
|
// transaction. See the exported version, CalcSequenceLock for further details.
|
|
|
|
//
|
|
|
|
// This function MUST be called with the chain state lock held (for writes).
|
2017-08-14 07:22:40 +02:00
|
|
|
func (b *BlockChain) calcSequenceLock(node *blockNode, tx *btcutil.Tx, utxoView *UtxoViewpoint, mempool bool) (*SequenceLock, error) {
|
2016-06-22 06:51:43 +02:00
|
|
|
// A value of -1 for each relative lock type represents a relative time
|
|
|
|
// lock value that will allow a transaction to be included in a block
|
|
|
|
// at any given height or time. This value is returned as the relative
|
|
|
|
// lock time in the case that BIP 68 is disabled, or has not yet been
|
|
|
|
// activated.
|
|
|
|
sequenceLock := &SequenceLock{Seconds: -1, BlockHeight: -1}
|
|
|
|
|
2016-09-27 04:00:28 +02:00
|
|
|
// The sequence locks semantics are always active for transactions
|
|
|
|
// within the mempool.
|
|
|
|
csvSoftforkActive := mempool
|
|
|
|
|
|
|
|
// If we're performing block validation, then we need to query the BIP9
|
|
|
|
// state.
|
|
|
|
if !csvSoftforkActive {
|
|
|
|
// Obtain the latest BIP9 version bits state for the
|
|
|
|
// CSV-package soft-fork deployment. The adherence of sequence
|
|
|
|
// locks depends on the current soft-fork state.
|
2017-02-03 19:13:53 +01:00
|
|
|
csvState, err := b.deploymentState(node.parent, chaincfg.DeploymentCSV)
|
2016-09-27 04:00:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
csvSoftforkActive = csvState == ThresholdActive
|
|
|
|
}
|
|
|
|
|
2016-06-22 06:51:43 +02:00
|
|
|
// If the transaction's version is less than 2, and BIP 68 has not yet
|
|
|
|
// been activated then sequence locks are disabled. Additionally,
|
|
|
|
// sequence locks don't apply to coinbase transactions Therefore, we
|
|
|
|
// return sequence lock values of -1 indicating that this transaction
|
|
|
|
// can be included within a block at any given height or time.
|
2016-09-27 04:00:28 +02:00
|
|
|
mTx := tx.MsgTx()
|
|
|
|
sequenceLockActive := mTx.Version >= 2 && csvSoftforkActive
|
2016-06-22 06:51:43 +02:00
|
|
|
if !sequenceLockActive || IsCoinBase(tx) {
|
|
|
|
return sequenceLock, nil
|
|
|
|
}
|
|
|
|
|
2016-09-27 04:00:28 +02:00
|
|
|
// Grab the next height from the PoV of the passed blockNode to use for
|
|
|
|
// inputs present in the mempool.
|
|
|
|
nextHeight := node.height + 1
|
2016-06-22 06:51:43 +02:00
|
|
|
|
|
|
|
for txInIndex, txIn := range mTx.TxIn {
|
multi: Rework utxoset/view to use outpoints.
This modifies the utxoset in the database and related UtxoViewpoint to
store and work with unspent transaction outputs on a per-output basis
instead of at a transaction level. This was inspired by similar recent
changes in Bitcoin Core.
The primary motivation is to simplify the code, pave the way for a
utxo cache, and generally focus on optimizing runtime performance.
The tradeoff is that this approach does somewhat increase the size of
the serialized utxoset since it means that the transaction hash is
duplicated for each output as a part of the key and some additional
details such as whether the containing transaction is a coinbase and the
block height it was a part of are duplicated in each output.
However, in practice, the size difference isn't all that large, disk
space is relatively cheap, certainly cheaper than memory, and it is much
more important to provide more efficient runtime operation since that is
the ultimate purpose of the daemon.
While performing this conversion, it also simplifies the code to remove
the transaction version information from the utxoset as well as the
spend journal. The logic for only serializing it under certain
circumstances is complicated and it isn't actually used anywhere aside
from the gettxout RPC where it also isn't used by anything important
either. Consequently, this also removes the version field of the
gettxout RPC result.
The utxos in the database are automatically migrated to the new format
with this commit and it is possible to interrupt and resume the
migration process.
Finally, it also updates the tests for the new format and adds a new
function to the tests to convert the old test data to the new format for
convenience. The data has already been converted and updated in the
commit.
An overview of the changes are as follows:
- Remove transaction version from both spent and unspent output entries
- Update utxo serialization format to exclude the version
- Modify the spend journal serialization format
- The old version field is now reserved and always stores zero and
ignores it when reading
- This allows old entries to be used by new code without having to
migrate the entire spend journal
- Remove version field from gettxout RPC result
- Convert UtxoEntry to represent a specific utxo instead of a
transaction with all remaining utxos
- Optimize for memory usage with an eye towards a utxo cache
- Combine details such as whether the txout was contained in a
coinbase, is spent, and is modified into a single packed field of
bit flags
- Align entry fields to eliminate extra padding since ultimately
there will be a lot of these in memory
- Introduce a free list for serializing an outpoint to the database
key format to significantly reduce pressure on the GC
- Update all related functions that previously dealt with transaction
hashes to accept outpoints instead
- Update all callers accordingly
- Only add individually requested outputs from the mempool when
constructing a mempool view
- Modify the spend journal to always store the block height and coinbase
information with every spent txout
- Introduce code to handle fetching the missing information from
another utxo from the same transaction in the event an old style
entry is encountered
- Make use of a database cursor with seek to do this much more
efficiently than testing every possible output
- Always decompress data loaded from the database now that a utxo entry
only consists of a specific output
- Introduce upgrade code to migrate the utxo set to the new format
- Store versions of the utxoset and spend journal buckets
- Allow migration process to be interrupted and resumed
- Update all tests to expect the correct encodings, remove tests that no
longer apply, and add new ones for the new expected behavior
- Convert old tests for the legacy utxo format deserialization code to
test the new function that is used during upgrade
- Update the utxostore test data and add function that was used to
convert it
- Introduce a few new functions on UtxoViewpoint
- AddTxOut for adding an individual txout versus all of them
- addTxOut to handle the common code between the new AddTxOut and
existing AddTxOuts
- RemoveEntry for removing an individual txout
- fetchEntryByHash for fetching any remaining utxo for a given
transaction hash
2017-09-03 09:59:15 +02:00
|
|
|
utxo := utxoView.LookupEntry(txIn.PreviousOutPoint)
|
2016-06-22 06:51:43 +02:00
|
|
|
if utxo == nil {
|
2017-08-14 07:22:40 +02:00
|
|
|
str := fmt.Sprintf("output %v referenced from "+
|
|
|
|
"transaction %s:%d either does not exist or "+
|
|
|
|
"has already been spent", txIn.PreviousOutPoint,
|
|
|
|
tx.Hash(), txInIndex)
|
|
|
|
return sequenceLock, ruleError(ErrMissingTxOut, str)
|
2016-06-22 06:51:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the input height is set to the mempool height, then we
|
|
|
|
// assume the transaction makes it into the next block when
|
|
|
|
// evaluating its sequence blocks.
|
|
|
|
inputHeight := utxo.BlockHeight()
|
|
|
|
if inputHeight == 0x7fffffff {
|
|
|
|
inputHeight = nextHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
// Given a sequence number, we apply the relative time lock
|
|
|
|
// mask in order to obtain the time lock delta required before
|
|
|
|
// this input can be spent.
|
|
|
|
sequenceNum := txIn.Sequence
|
|
|
|
relativeLock := int64(sequenceNum & wire.SequenceLockTimeMask)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
// Relative time locks are disabled for this input, so we can
|
|
|
|
// skip any further calculation.
|
|
|
|
case sequenceNum&wire.SequenceLockTimeDisabled == wire.SequenceLockTimeDisabled:
|
|
|
|
continue
|
|
|
|
case sequenceNum&wire.SequenceLockTimeIsSeconds == wire.SequenceLockTimeIsSeconds:
|
|
|
|
// This input requires a relative time lock expressed
|
2017-02-03 19:13:53 +01:00
|
|
|
// in seconds before it can be spent. Therefore, we
|
2016-06-22 06:51:43 +02:00
|
|
|
// need to query for the block prior to the one in
|
|
|
|
// which this input was included within so we can
|
|
|
|
// compute the past median time for the block prior to
|
|
|
|
// the one which included this referenced output.
|
2017-02-03 19:13:53 +01:00
|
|
|
prevInputHeight := inputHeight - 1
|
|
|
|
if prevInputHeight < 0 {
|
|
|
|
prevInputHeight = 0
|
2016-06-22 06:51:43 +02:00
|
|
|
}
|
2017-02-03 19:13:53 +01:00
|
|
|
blockNode := node.Ancestor(prevInputHeight)
|
|
|
|
medianTime := blockNode.CalcPastMedianTime()
|
2016-06-22 06:51:43 +02:00
|
|
|
|
|
|
|
// Time based relative time-locks as defined by BIP 68
|
|
|
|
// have a time granularity of RelativeLockSeconds, so
|
|
|
|
// we shift left by this amount to convert to the
|
|
|
|
// proper relative time-lock. We also subtract one from
|
|
|
|
// the relative lock to maintain the original lockTime
|
|
|
|
// semantics.
|
|
|
|
timeLockSeconds := (relativeLock << wire.SequenceLockTimeGranularity) - 1
|
|
|
|
timeLock := medianTime.Unix() + timeLockSeconds
|
|
|
|
if timeLock > sequenceLock.Seconds {
|
|
|
|
sequenceLock.Seconds = timeLock
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// The relative lock-time for this input is expressed
|
|
|
|
// in blocks so we calculate the relative offset from
|
|
|
|
// the input's height as its converted absolute
|
|
|
|
// lock-time. We subtract one from the relative lock in
|
|
|
|
// order to maintain the original lockTime semantics.
|
|
|
|
blockHeight := inputHeight + int32(relativeLock-1)
|
|
|
|
if blockHeight > sequenceLock.BlockHeight {
|
|
|
|
sequenceLock.BlockHeight = blockHeight
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sequenceLock, nil
|
|
|
|
}
|
|
|
|
|
2016-08-24 23:12:13 +02:00
|
|
|
// LockTimeToSequence converts the passed relative locktime to a sequence
|
|
|
|
// number in accordance to BIP-68.
|
|
|
|
// See: https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki
|
|
|
|
// * (Compatibility)
|
|
|
|
func LockTimeToSequence(isSeconds bool, locktime uint32) uint32 {
|
|
|
|
// If we're expressing the relative lock time in blocks, then the
|
|
|
|
// corresponding sequence number is simply the desired input age.
|
|
|
|
if !isSeconds {
|
|
|
|
return locktime
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the 22nd bit which indicates the lock time is in seconds, then
|
|
|
|
// shift the locktime over by 9 since the time granularity is in
|
|
|
|
// 512-second intervals (2^9). This results in a max lock-time of
|
|
|
|
// 33,553,920 seconds, or 1.1 years.
|
2016-11-03 00:18:48 +01:00
|
|
|
return wire.SequenceLockTimeIsSeconds |
|
|
|
|
locktime>>wire.SequenceLockTimeGranularity
|
2016-08-24 23:12:13 +02:00
|
|
|
}
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// getReorganizeNodes finds the fork point between the main chain and the passed
|
|
|
|
// node and returns a list of block nodes that would need to be detached from
|
|
|
|
// the main chain and a list of block nodes that would need to be attached to
|
|
|
|
// the fork point (which will be the end of the main chain after detaching the
|
|
|
|
// returned list of block nodes) in order to reorganize the chain such that the
|
|
|
|
// passed node is the new end of the main chain. The lists will be empty if the
|
|
|
|
// passed node is not on a side chain.
|
2015-08-26 06:03:18 +02:00
|
|
|
//
|
2017-09-14 23:41:21 +02:00
|
|
|
// This function may modify node statuses in the block index without flushing.
|
|
|
|
//
|
2015-08-26 06:03:18 +02:00
|
|
|
// This function MUST be called with the chain state lock held (for reads).
|
2013-07-18 16:49:28 +02:00
|
|
|
func (b *BlockChain) getReorganizeNodes(node *blockNode) (*list.List, *list.List) {
|
|
|
|
attachNodes := list.New()
|
|
|
|
detachNodes := list.New()
|
2017-09-06 05:42:54 +02:00
|
|
|
|
|
|
|
// Do not reorganize to a known invalid chain. Ancestors deeper than the
|
|
|
|
// direct parent are checked below but this is a quick check before doing
|
|
|
|
// more unnecessary work.
|
2017-10-13 23:36:40 +02:00
|
|
|
if b.index.NodeStatus(node.parent).KnownInvalid() {
|
|
|
|
b.index.SetStatusFlags(node, statusInvalidAncestor)
|
2013-07-18 16:49:28 +02:00
|
|
|
return detachNodes, attachNodes
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the fork point (if any) adding each block to the list of nodes
|
|
|
|
// to attach to the main tree. Push them onto the list in reverse order
|
|
|
|
// so they are attached in the appropriate order when iterating the list
|
|
|
|
// later.
|
2017-08-18 14:25:54 +02:00
|
|
|
forkNode := b.bestChain.FindFork(node)
|
2017-09-06 05:42:54 +02:00
|
|
|
invalidChain := false
|
2017-08-18 14:25:54 +02:00
|
|
|
for n := node; n != nil && n != forkNode; n = n.parent {
|
2017-10-13 23:36:40 +02:00
|
|
|
if b.index.NodeStatus(n).KnownInvalid() {
|
2017-09-06 05:42:54 +02:00
|
|
|
invalidChain = true
|
|
|
|
break
|
|
|
|
}
|
2017-08-18 14:25:54 +02:00
|
|
|
attachNodes.PushFront(n)
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
2017-09-06 05:42:54 +02:00
|
|
|
// If any of the node's ancestors are invalid, unwind attachNodes, marking
|
|
|
|
// each one as invalid for future reference.
|
|
|
|
if invalidChain {
|
|
|
|
var next *list.Element
|
|
|
|
for e := attachNodes.Front(); e != nil; e = next {
|
|
|
|
next = e.Next()
|
|
|
|
n := attachNodes.Remove(e).(*blockNode)
|
2017-10-13 23:36:40 +02:00
|
|
|
b.index.SetStatusFlags(n, statusInvalidAncestor)
|
2017-09-06 05:42:54 +02:00
|
|
|
}
|
|
|
|
return detachNodes, attachNodes
|
|
|
|
}
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// Start from the end of the main chain and work backwards until the
|
|
|
|
// common ancestor adding each block to the list of nodes to detach from
|
|
|
|
// the main chain.
|
2017-08-18 14:25:54 +02:00
|
|
|
for n := b.bestChain.Tip(); n != nil && n != forkNode; n = n.parent {
|
2013-07-18 16:49:28 +02:00
|
|
|
detachNodes.PushBack(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return detachNodes, attachNodes
|
|
|
|
}
|
|
|
|
|
|
|
|
// connectBlock handles connecting the passed node/block to the end of the main
|
|
|
|
// (best) chain.
|
2015-08-26 06:03:18 +02:00
|
|
|
//
|
|
|
|
// This passed utxo view must have all referenced txos the block spends marked
|
|
|
|
// as spent and all of the new txos the block creates added to it. In addition,
|
|
|
|
// the passed stxos slice must be populated with all of the information for the
|
|
|
|
// spent txos. This approach is used because the connection validation that
|
|
|
|
// must happen prior to calling this function requires the same details, so
|
|
|
|
// it would be inefficient to repeat it.
|
|
|
|
//
|
|
|
|
// This function MUST be called with the chain state lock held (for writes).
|
2018-05-29 06:13:59 +02:00
|
|
|
func (b *BlockChain) connectBlock(node *blockNode, block *btcutil.Block,
|
|
|
|
view *UtxoViewpoint, stxos []SpentTxOut) error {
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// Make sure it's extending the end of the best chain.
|
|
|
|
prevHash := &block.MsgBlock().Header.PrevBlock
|
2017-08-18 14:25:54 +02:00
|
|
|
if !prevHash.IsEqual(&b.bestChain.Tip().hash) {
|
2015-08-26 06:03:18 +02:00
|
|
|
return AssertError("connectBlock must be called with a block " +
|
2013-07-18 16:49:28 +02:00
|
|
|
"that extends the main chain")
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// Sanity check the correct number of stxos are provided.
|
|
|
|
if len(stxos) != countSpentOutputs(block) {
|
|
|
|
return AssertError("connectBlock called with inconsistent " +
|
|
|
|
"spent transaction out information")
|
|
|
|
}
|
|
|
|
|
2019-08-27 04:09:13 +02:00
|
|
|
// No warnings about unknown rules until the chain is current.
|
2018-08-05 22:59:25 +02:00
|
|
|
current := b.isCurrent()
|
|
|
|
if current {
|
2016-08-29 00:51:55 +02:00
|
|
|
// Warn if any unknown new rules are either about to activate or
|
|
|
|
// have already been activated.
|
|
|
|
if err := b.warnUnknownRuleActivations(node); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-05 22:59:25 +02:00
|
|
|
// Handle LBRY Claim Scripts
|
|
|
|
if b.claimTrie != nil {
|
|
|
|
shouldFlush := current && b.chainParams.Net != wire.TestNet
|
|
|
|
if err := b.ParseClaimScripts(block, node, view, shouldFlush); err != nil {
|
|
|
|
return ruleError(ErrBadClaimTrie, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 23:41:21 +02:00
|
|
|
// Write any block status changes to DB before updating best state.
|
|
|
|
err := b.index.flushToDB()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// Generate a new best state snapshot that will be used to update the
|
|
|
|
// database and later memory if all database updates are successful.
|
|
|
|
b.stateLock.RLock()
|
|
|
|
curTotalTxns := b.stateSnapshot.TotalTxns
|
|
|
|
b.stateLock.RUnlock()
|
|
|
|
numTxns := uint64(len(block.MsgBlock().Transactions))
|
|
|
|
blockSize := uint64(block.MsgBlock().SerializeSize())
|
2016-10-19 03:33:20 +02:00
|
|
|
blockWeight := uint64(GetBlockWeight(block))
|
|
|
|
state := newBestState(node, blockSize, blockWeight, numTxns,
|
2017-02-03 19:13:53 +01:00
|
|
|
curTotalTxns+numTxns, node.CalcPastMedianTime())
|
2015-08-26 06:03:18 +02:00
|
|
|
|
|
|
|
// Atomically insert info into the database.
|
2017-09-14 23:41:21 +02:00
|
|
|
err = b.db.Update(func(dbTx database.Tx) error {
|
2015-08-26 06:03:18 +02:00
|
|
|
// Update best block state.
|
|
|
|
err := dbPutBestState(dbTx, state, node.workSum)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the block hash and height to the block index which tracks
|
|
|
|
// the main chain.
|
2016-08-08 21:04:33 +02:00
|
|
|
err = dbPutBlockIndex(dbTx, block.Hash(), node.height)
|
2015-08-26 06:03:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the utxo set using the state of the utxo view. This
|
|
|
|
// entails removing all of the utxos spent and adding the new
|
|
|
|
// ones created by the block.
|
|
|
|
err = dbPutUtxoView(dbTx, view)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the transaction spend journal by adding a record for
|
|
|
|
// the block that contains all txos spent by it.
|
2016-08-08 21:04:33 +02:00
|
|
|
err = dbPutSpendJournalEntry(dbTx, block.Hash(), stxos)
|
2015-08-26 06:03:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-19 05:51:18 +01:00
|
|
|
// Allow the index manager to call each of the currently active
|
|
|
|
// optional indexes with the block being connected so they can
|
|
|
|
// update themselves accordingly.
|
|
|
|
if b.indexManager != nil {
|
2018-05-29 06:53:31 +02:00
|
|
|
err := b.indexManager.ConnectBlock(dbTx, block, stxos)
|
2016-02-19 05:51:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-26 06:03:18 +02:00
|
|
|
}
|
|
|
|
|
2017-02-02 09:44:22 +01:00
|
|
|
return nil
|
2015-08-26 06:03:18 +02:00
|
|
|
})
|
2013-07-18 16:49:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// Prune fully spent entries and mark all entries in the view unmodified
|
|
|
|
// now that the modifications have been committed to the database.
|
|
|
|
view.commit()
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// This node is now the end of the best chain.
|
2017-08-18 14:25:54 +02:00
|
|
|
b.bestChain.SetTip(node)
|
2015-08-26 06:03:18 +02:00
|
|
|
|
|
|
|
// Update the state for the best block. Notice how this replaces the
|
|
|
|
// entire struct instead of updating the existing one. This effectively
|
|
|
|
// allows the old version to act as a snapshot which callers can use
|
|
|
|
// freely without needing to hold a lock for the duration. See the
|
|
|
|
// comments on the state variable for more details.
|
|
|
|
b.stateLock.Lock()
|
|
|
|
b.stateSnapshot = state
|
|
|
|
b.stateLock.Unlock()
|
2013-07-18 16:49:28 +02:00
|
|
|
|
|
|
|
// Notify the caller that the block was connected to the main chain.
|
|
|
|
// The caller would typically want to react with actions such as
|
|
|
|
// updating wallets.
|
2015-08-26 06:03:18 +02:00
|
|
|
b.chainLock.Unlock()
|
2013-07-18 16:49:28 +02:00
|
|
|
b.sendNotification(NTBlockConnected, block)
|
2015-08-26 06:03:18 +02:00
|
|
|
b.chainLock.Lock()
|
2013-07-18 16:49:28 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// disconnectBlock handles disconnecting the passed node/block from the end of
|
|
|
|
// the main (best) chain.
|
2015-08-26 06:03:18 +02:00
|
|
|
//
|
|
|
|
// This function MUST be called with the chain state lock held (for writes).
|
|
|
|
func (b *BlockChain) disconnectBlock(node *blockNode, block *btcutil.Block, view *UtxoViewpoint) error {
|
2013-07-18 16:49:28 +02:00
|
|
|
// Make sure the node being disconnected is the end of the best chain.
|
2017-08-18 14:25:54 +02:00
|
|
|
if !node.hash.IsEqual(&b.bestChain.Tip().hash) {
|
2015-08-26 06:03:18 +02:00
|
|
|
return AssertError("disconnectBlock must be called with the " +
|
2013-07-18 16:49:28 +02:00
|
|
|
"block at the end of the main chain")
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// Load the previous block since some details for it are needed below.
|
2017-02-03 19:13:53 +01:00
|
|
|
prevNode := node.parent
|
2015-08-26 06:03:18 +02:00
|
|
|
var prevBlock *btcutil.Block
|
2017-02-03 19:13:53 +01:00
|
|
|
err := b.db.View(func(dbTx database.Tx) error {
|
2015-08-26 06:03:18 +02:00
|
|
|
var err error
|
2017-08-18 14:25:54 +02:00
|
|
|
prevBlock, err = dbFetchBlockByNode(dbTx, prevNode)
|
2015-08-26 06:03:18 +02:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-14 23:41:21 +02:00
|
|
|
// Write any block status changes to DB before updating best state.
|
|
|
|
err = b.index.flushToDB()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// Generate a new best state snapshot that will be used to update the
|
|
|
|
// database and later memory if all database updates are successful.
|
|
|
|
b.stateLock.RLock()
|
|
|
|
curTotalTxns := b.stateSnapshot.TotalTxns
|
|
|
|
b.stateLock.RUnlock()
|
|
|
|
numTxns := uint64(len(prevBlock.MsgBlock().Transactions))
|
|
|
|
blockSize := uint64(prevBlock.MsgBlock().SerializeSize())
|
2016-10-19 03:33:20 +02:00
|
|
|
blockWeight := uint64(GetBlockWeight(prevBlock))
|
2015-08-26 06:03:18 +02:00
|
|
|
newTotalTxns := curTotalTxns - uint64(len(block.MsgBlock().Transactions))
|
2016-10-19 03:33:20 +02:00
|
|
|
state := newBestState(prevNode, blockSize, blockWeight, numTxns,
|
2017-02-03 19:13:53 +01:00
|
|
|
newTotalTxns, prevNode.CalcPastMedianTime())
|
2015-08-26 06:03:18 +02:00
|
|
|
|
|
|
|
err = b.db.Update(func(dbTx database.Tx) error {
|
|
|
|
// Update best block state.
|
|
|
|
err := dbPutBestState(dbTx, state, node.workSum)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the block hash and height from the block index which
|
|
|
|
// tracks the main chain.
|
2016-08-08 21:04:33 +02:00
|
|
|
err = dbRemoveBlockIndex(dbTx, block.Hash(), node.height)
|
2015-08-26 06:03:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the utxo set using the state of the utxo view. This
|
|
|
|
// entails restoring all of the utxos spent and removing the new
|
|
|
|
// ones created by the block.
|
|
|
|
err = dbPutUtxoView(dbTx, view)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-29 06:53:31 +02:00
|
|
|
// Before we delete the spend journal entry for this back,
|
|
|
|
// we'll fetch it as is so the indexers can utilize if needed.
|
|
|
|
stxos, err := dbFetchSpendJournalEntry(dbTx, block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// Update the transaction spend journal by removing the record
|
2018-05-29 06:53:31 +02:00
|
|
|
// that contains all txos spent by the block.
|
2016-08-08 21:04:33 +02:00
|
|
|
err = dbRemoveSpendJournalEntry(dbTx, block.Hash())
|
2015-08-26 06:03:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-19 05:51:18 +01:00
|
|
|
// Allow the index manager to call each of the currently active
|
|
|
|
// optional indexes with the block being disconnected so they
|
|
|
|
// can update themselves accordingly.
|
|
|
|
if b.indexManager != nil {
|
2018-05-29 06:53:31 +02:00
|
|
|
err := b.indexManager.DisconnectBlock(dbTx, block, stxos)
|
2016-02-19 05:51:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
return nil
|
|
|
|
})
|
2013-07-18 16:49:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-05 22:59:25 +02:00
|
|
|
if b.claimTrie != nil {
|
|
|
|
if err = b.claimTrie.ResetHeight(node.parent.height); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// Prune fully spent entries and mark all entries in the view unmodified
|
|
|
|
// now that the modifications have been committed to the database.
|
|
|
|
view.commit()
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// This node's parent is now the end of the best chain.
|
2017-08-18 14:25:54 +02:00
|
|
|
b.bestChain.SetTip(node.parent)
|
2015-08-26 06:03:18 +02:00
|
|
|
|
|
|
|
// Update the state for the best block. Notice how this replaces the
|
|
|
|
// entire struct instead of updating the existing one. This effectively
|
|
|
|
// allows the old version to act as a snapshot which callers can use
|
|
|
|
// freely without needing to hold a lock for the duration. See the
|
|
|
|
// comments on the state variable for more details.
|
|
|
|
b.stateLock.Lock()
|
|
|
|
b.stateSnapshot = state
|
|
|
|
b.stateLock.Unlock()
|
2013-07-18 16:49:28 +02:00
|
|
|
|
2013-10-11 17:24:13 +02:00
|
|
|
// Notify the caller that the block was disconnected from the main
|
|
|
|
// chain. The caller would typically want to react with actions such as
|
2013-07-18 16:49:28 +02:00
|
|
|
// updating wallets.
|
2015-08-26 06:03:18 +02:00
|
|
|
b.chainLock.Unlock()
|
2013-07-18 16:49:28 +02:00
|
|
|
b.sendNotification(NTBlockDisconnected, block)
|
2015-08-26 06:03:18 +02:00
|
|
|
b.chainLock.Lock()
|
2013-07-18 16:49:28 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// countSpentOutputs returns the number of utxos the passed block spends.
|
|
|
|
func countSpentOutputs(block *btcutil.Block) int {
|
|
|
|
// Exclude the coinbase transaction since it can't spend anything.
|
|
|
|
var numSpent int
|
|
|
|
for _, tx := range block.Transactions()[1:] {
|
|
|
|
numSpent += len(tx.MsgTx().TxIn)
|
|
|
|
}
|
|
|
|
return numSpent
|
|
|
|
}
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// reorganizeChain reorganizes the block chain by disconnecting the nodes in the
|
|
|
|
// detachNodes list and connecting the nodes in the attach list. It expects
|
|
|
|
// that the lists are already in the correct order and are in sync with the
|
|
|
|
// end of the current best chain. Specifically, nodes that are being
|
2015-08-26 06:03:18 +02:00
|
|
|
// disconnected must be in reverse order (think of popping them off the end of
|
|
|
|
// the chain) and nodes the are being attached must be in forwards order
|
|
|
|
// (think pushing them onto the end of the chain).
|
2014-06-29 22:11:13 +02:00
|
|
|
//
|
2017-09-14 23:41:21 +02:00
|
|
|
// This function may modify node statuses in the block index without flushing.
|
|
|
|
//
|
2015-08-26 06:03:18 +02:00
|
|
|
// This function MUST be called with the chain state lock held (for writes).
|
2017-09-15 05:35:11 +02:00
|
|
|
func (b *BlockChain) reorganizeChain(detachNodes, attachNodes *list.List) error {
|
2018-07-12 02:41:17 +02:00
|
|
|
// Nothing to do if no reorganize nodes were provided.
|
|
|
|
if detachNodes.Len() == 0 && attachNodes.Len() == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the provided nodes match the current best chain.
|
|
|
|
tip := b.bestChain.Tip()
|
|
|
|
if detachNodes.Len() != 0 {
|
|
|
|
firstDetachNode := detachNodes.Front().Value.(*blockNode)
|
|
|
|
if firstDetachNode.hash != tip.hash {
|
|
|
|
return AssertError(fmt.Sprintf("reorganize nodes to detach are "+
|
|
|
|
"not for the current best chain -- first detach node %v, "+
|
|
|
|
"current chain %v", &firstDetachNode.hash, &tip.hash))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the provided nodes are for the same fork point.
|
|
|
|
if attachNodes.Len() != 0 && detachNodes.Len() != 0 {
|
|
|
|
firstAttachNode := attachNodes.Front().Value.(*blockNode)
|
|
|
|
lastDetachNode := detachNodes.Back().Value.(*blockNode)
|
|
|
|
if firstAttachNode.parent.hash != lastDetachNode.parent.hash {
|
|
|
|
return AssertError(fmt.Sprintf("reorganize nodes do not have the "+
|
|
|
|
"same fork point -- first attach parent %v, last detach "+
|
|
|
|
"parent %v", &firstAttachNode.parent.hash,
|
|
|
|
&lastDetachNode.parent.hash))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track the old and new best chains heads.
|
|
|
|
oldBest := tip
|
|
|
|
newBest := tip
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// All of the blocks to detach and related spend journal entries needed
|
|
|
|
// to unspend transaction outputs in the blocks being disconnected must
|
|
|
|
// be loaded from the database during the reorg check phase below and
|
|
|
|
// then they are needed again when doing the actual database updates.
|
|
|
|
// Rather than doing two loads, cache the loaded data into these slices.
|
|
|
|
detachBlocks := make([]*btcutil.Block, 0, detachNodes.Len())
|
2018-05-29 06:13:59 +02:00
|
|
|
detachSpentTxOuts := make([][]SpentTxOut, 0, detachNodes.Len())
|
2017-01-29 18:20:10 +01:00
|
|
|
attachBlocks := make([]*btcutil.Block, 0, attachNodes.Len())
|
2015-08-26 06:03:18 +02:00
|
|
|
|
|
|
|
// Disconnect all of the blocks back to the point of the fork. This
|
|
|
|
// entails loading the blocks and their associated spent txos from the
|
|
|
|
// database and using that information to unspend all of the spent txos
|
|
|
|
// and remove the utxos created by the blocks.
|
|
|
|
view := NewUtxoViewpoint()
|
2018-07-12 02:41:17 +02:00
|
|
|
view.SetBestHash(&oldBest.hash)
|
2015-08-26 06:03:18 +02:00
|
|
|
for e := detachNodes.Front(); e != nil; e = e.Next() {
|
|
|
|
n := e.Value.(*blockNode)
|
|
|
|
var block *btcutil.Block
|
|
|
|
err := b.db.View(func(dbTx database.Tx) error {
|
|
|
|
var err error
|
2017-08-18 14:25:54 +02:00
|
|
|
block, err = dbFetchBlockByNode(dbTx, n)
|
2015-08-26 06:03:18 +02:00
|
|
|
return err
|
|
|
|
})
|
2017-01-29 18:20:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-12 02:41:17 +02:00
|
|
|
if n.hash != *block.Hash() {
|
|
|
|
return AssertError(fmt.Sprintf("detach block node hash %v (height "+
|
|
|
|
"%v) does not match previous parent block hash %v", &n.hash,
|
|
|
|
n.height, block.Hash()))
|
|
|
|
}
|
2015-08-26 06:03:18 +02:00
|
|
|
|
|
|
|
// Load all of the utxos referenced by the block that aren't
|
|
|
|
// already in the view.
|
|
|
|
err = view.fetchInputUtxos(b.db, block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load all of the spent txos for the block from the spend
|
|
|
|
// journal.
|
2018-05-29 06:13:59 +02:00
|
|
|
var stxos []SpentTxOut
|
2015-08-26 06:03:18 +02:00
|
|
|
err = b.db.View(func(dbTx database.Tx) error {
|
multi: Rework utxoset/view to use outpoints.
This modifies the utxoset in the database and related UtxoViewpoint to
store and work with unspent transaction outputs on a per-output basis
instead of at a transaction level. This was inspired by similar recent
changes in Bitcoin Core.
The primary motivation is to simplify the code, pave the way for a
utxo cache, and generally focus on optimizing runtime performance.
The tradeoff is that this approach does somewhat increase the size of
the serialized utxoset since it means that the transaction hash is
duplicated for each output as a part of the key and some additional
details such as whether the containing transaction is a coinbase and the
block height it was a part of are duplicated in each output.
However, in practice, the size difference isn't all that large, disk
space is relatively cheap, certainly cheaper than memory, and it is much
more important to provide more efficient runtime operation since that is
the ultimate purpose of the daemon.
While performing this conversion, it also simplifies the code to remove
the transaction version information from the utxoset as well as the
spend journal. The logic for only serializing it under certain
circumstances is complicated and it isn't actually used anywhere aside
from the gettxout RPC where it also isn't used by anything important
either. Consequently, this also removes the version field of the
gettxout RPC result.
The utxos in the database are automatically migrated to the new format
with this commit and it is possible to interrupt and resume the
migration process.
Finally, it also updates the tests for the new format and adds a new
function to the tests to convert the old test data to the new format for
convenience. The data has already been converted and updated in the
commit.
An overview of the changes are as follows:
- Remove transaction version from both spent and unspent output entries
- Update utxo serialization format to exclude the version
- Modify the spend journal serialization format
- The old version field is now reserved and always stores zero and
ignores it when reading
- This allows old entries to be used by new code without having to
migrate the entire spend journal
- Remove version field from gettxout RPC result
- Convert UtxoEntry to represent a specific utxo instead of a
transaction with all remaining utxos
- Optimize for memory usage with an eye towards a utxo cache
- Combine details such as whether the txout was contained in a
coinbase, is spent, and is modified into a single packed field of
bit flags
- Align entry fields to eliminate extra padding since ultimately
there will be a lot of these in memory
- Introduce a free list for serializing an outpoint to the database
key format to significantly reduce pressure on the GC
- Update all related functions that previously dealt with transaction
hashes to accept outpoints instead
- Update all callers accordingly
- Only add individually requested outputs from the mempool when
constructing a mempool view
- Modify the spend journal to always store the block height and coinbase
information with every spent txout
- Introduce code to handle fetching the missing information from
another utxo from the same transaction in the event an old style
entry is encountered
- Make use of a database cursor with seek to do this much more
efficiently than testing every possible output
- Always decompress data loaded from the database now that a utxo entry
only consists of a specific output
- Introduce upgrade code to migrate the utxo set to the new format
- Store versions of the utxoset and spend journal buckets
- Allow migration process to be interrupted and resumed
- Update all tests to expect the correct encodings, remove tests that no
longer apply, and add new ones for the new expected behavior
- Convert old tests for the legacy utxo format deserialization code to
test the new function that is used during upgrade
- Update the utxostore test data and add function that was used to
convert it
- Introduce a few new functions on UtxoViewpoint
- AddTxOut for adding an individual txout versus all of them
- addTxOut to handle the common code between the new AddTxOut and
existing AddTxOuts
- RemoveEntry for removing an individual txout
- fetchEntryByHash for fetching any remaining utxo for a given
transaction hash
2017-09-03 09:59:15 +02:00
|
|
|
stxos, err = dbFetchSpendJournalEntry(dbTx, block)
|
2015-08-26 06:03:18 +02:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the loaded block and spend journal entry for later.
|
|
|
|
detachBlocks = append(detachBlocks, block)
|
|
|
|
detachSpentTxOuts = append(detachSpentTxOuts, stxos)
|
|
|
|
|
multi: Rework utxoset/view to use outpoints.
This modifies the utxoset in the database and related UtxoViewpoint to
store and work with unspent transaction outputs on a per-output basis
instead of at a transaction level. This was inspired by similar recent
changes in Bitcoin Core.
The primary motivation is to simplify the code, pave the way for a
utxo cache, and generally focus on optimizing runtime performance.
The tradeoff is that this approach does somewhat increase the size of
the serialized utxoset since it means that the transaction hash is
duplicated for each output as a part of the key and some additional
details such as whether the containing transaction is a coinbase and the
block height it was a part of are duplicated in each output.
However, in practice, the size difference isn't all that large, disk
space is relatively cheap, certainly cheaper than memory, and it is much
more important to provide more efficient runtime operation since that is
the ultimate purpose of the daemon.
While performing this conversion, it also simplifies the code to remove
the transaction version information from the utxoset as well as the
spend journal. The logic for only serializing it under certain
circumstances is complicated and it isn't actually used anywhere aside
from the gettxout RPC where it also isn't used by anything important
either. Consequently, this also removes the version field of the
gettxout RPC result.
The utxos in the database are automatically migrated to the new format
with this commit and it is possible to interrupt and resume the
migration process.
Finally, it also updates the tests for the new format and adds a new
function to the tests to convert the old test data to the new format for
convenience. The data has already been converted and updated in the
commit.
An overview of the changes are as follows:
- Remove transaction version from both spent and unspent output entries
- Update utxo serialization format to exclude the version
- Modify the spend journal serialization format
- The old version field is now reserved and always stores zero and
ignores it when reading
- This allows old entries to be used by new code without having to
migrate the entire spend journal
- Remove version field from gettxout RPC result
- Convert UtxoEntry to represent a specific utxo instead of a
transaction with all remaining utxos
- Optimize for memory usage with an eye towards a utxo cache
- Combine details such as whether the txout was contained in a
coinbase, is spent, and is modified into a single packed field of
bit flags
- Align entry fields to eliminate extra padding since ultimately
there will be a lot of these in memory
- Introduce a free list for serializing an outpoint to the database
key format to significantly reduce pressure on the GC
- Update all related functions that previously dealt with transaction
hashes to accept outpoints instead
- Update all callers accordingly
- Only add individually requested outputs from the mempool when
constructing a mempool view
- Modify the spend journal to always store the block height and coinbase
information with every spent txout
- Introduce code to handle fetching the missing information from
another utxo from the same transaction in the event an old style
entry is encountered
- Make use of a database cursor with seek to do this much more
efficiently than testing every possible output
- Always decompress data loaded from the database now that a utxo entry
only consists of a specific output
- Introduce upgrade code to migrate the utxo set to the new format
- Store versions of the utxoset and spend journal buckets
- Allow migration process to be interrupted and resumed
- Update all tests to expect the correct encodings, remove tests that no
longer apply, and add new ones for the new expected behavior
- Convert old tests for the legacy utxo format deserialization code to
test the new function that is used during upgrade
- Update the utxostore test data and add function that was used to
convert it
- Introduce a few new functions on UtxoViewpoint
- AddTxOut for adding an individual txout versus all of them
- addTxOut to handle the common code between the new AddTxOut and
existing AddTxOuts
- RemoveEntry for removing an individual txout
- fetchEntryByHash for fetching any remaining utxo for a given
transaction hash
2017-09-03 09:59:15 +02:00
|
|
|
err = view.disconnectTransactions(b.db, block, stxos)
|
2015-08-26 06:03:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
2018-07-12 02:41:17 +02:00
|
|
|
|
|
|
|
newBest = n.parent
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the fork point only if there are nodes to attach since otherwise
|
|
|
|
// blocks are only being disconnected and thus there is no fork point.
|
|
|
|
var forkNode *blockNode
|
|
|
|
if attachNodes.Len() > 0 {
|
|
|
|
forkNode = newBest
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Perform several checks to verify each block that needs to be attached
|
|
|
|
// to the main chain can be connected without violating any rules and
|
|
|
|
// without actually connecting the block.
|
|
|
|
//
|
2015-08-26 06:03:18 +02:00
|
|
|
// NOTE: These checks could be done directly when connecting a block,
|
|
|
|
// however the downside to that approach is that if any of these checks
|
|
|
|
// fail after disconnecting some blocks or attaching others, all of the
|
2013-07-18 16:49:28 +02:00
|
|
|
// operations have to be rolled back to get the chain back into the
|
|
|
|
// state it was before the rule violation (or other failure). There are
|
|
|
|
// at least a couple of ways accomplish that rollback, but both involve
|
2015-08-26 06:03:18 +02:00
|
|
|
// tweaking the chain and/or database. This approach catches these
|
|
|
|
// issues before ever modifying the chain.
|
2013-07-18 16:49:28 +02:00
|
|
|
for e := attachNodes.Front(); e != nil; e = e.Next() {
|
|
|
|
n := e.Value.(*blockNode)
|
2017-09-06 05:42:54 +02:00
|
|
|
|
2017-01-29 18:20:10 +01:00
|
|
|
var block *btcutil.Block
|
|
|
|
err := b.db.View(func(dbTx database.Tx) error {
|
2017-08-18 14:25:54 +02:00
|
|
|
var err error
|
|
|
|
block, err = dbFetchBlockByNode(dbTx, n)
|
|
|
|
return err
|
2017-01-29 18:20:10 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the loaded block for later.
|
|
|
|
attachBlocks = append(attachBlocks, block)
|
2015-08-26 06:03:18 +02:00
|
|
|
|
2017-09-06 05:42:54 +02:00
|
|
|
// Skip checks if node has already been fully validated. Although
|
|
|
|
// checkConnectBlock gets skipped, we still need to update the UTXO
|
|
|
|
// view.
|
2017-10-13 23:36:40 +02:00
|
|
|
if b.index.NodeStatus(n).KnownValid() {
|
2017-09-06 05:42:54 +02:00
|
|
|
err = view.fetchInputUtxos(b.db, block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = view.connectTransactions(block, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-12 02:41:17 +02:00
|
|
|
|
|
|
|
newBest = n
|
2017-09-06 05:42:54 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// Notice the spent txout details are not requested here and
|
|
|
|
// thus will not be generated. This is done because the state
|
|
|
|
// is not being immediately written to the database, so it is
|
|
|
|
// not needed.
|
2018-07-12 02:41:17 +02:00
|
|
|
//
|
|
|
|
// In the case the block is determined to be invalid due to a
|
|
|
|
// rule violation, mark it as invalid and mark all of its
|
|
|
|
// descendants as having an invalid ancestor.
|
2017-01-29 18:20:10 +01:00
|
|
|
err = b.checkConnectBlock(n, block, view, nil)
|
2013-07-18 16:49:28 +02:00
|
|
|
if err != nil {
|
2017-09-06 05:42:54 +02:00
|
|
|
if _, ok := err.(RuleError); ok {
|
2017-10-13 23:36:40 +02:00
|
|
|
b.index.SetStatusFlags(n, statusValidateFailed)
|
2018-07-12 02:41:17 +02:00
|
|
|
for de := e.Next(); de != nil; de = de.Next() {
|
|
|
|
dn := de.Value.(*blockNode)
|
|
|
|
b.index.SetStatusFlags(dn, statusInvalidAncestor)
|
|
|
|
}
|
2017-09-06 05:42:54 +02:00
|
|
|
}
|
2013-07-18 16:49:28 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-10-13 23:36:40 +02:00
|
|
|
b.index.SetStatusFlags(n, statusValid)
|
2017-09-06 05:42:54 +02:00
|
|
|
|
2018-07-12 02:41:17 +02:00
|
|
|
newBest = n
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// Reset the view for the actual connection code below. This is
|
|
|
|
// required because the view was previously modified when checking if
|
|
|
|
// the reorg would be successful and the connection code requires the
|
|
|
|
// view to be valid from the viewpoint of each block being connected or
|
|
|
|
// disconnected.
|
|
|
|
view = NewUtxoViewpoint()
|
2017-08-18 14:25:54 +02:00
|
|
|
view.SetBestHash(&b.bestChain.Tip().hash)
|
2015-08-26 06:03:18 +02:00
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// Disconnect blocks from the main chain.
|
2015-08-26 06:03:18 +02:00
|
|
|
for i, e := 0, detachNodes.Front(); e != nil; i, e = i+1, e.Next() {
|
2013-07-18 16:49:28 +02:00
|
|
|
n := e.Value.(*blockNode)
|
2015-08-26 06:03:18 +02:00
|
|
|
block := detachBlocks[i]
|
|
|
|
|
|
|
|
// Load all of the utxos referenced by the block that aren't
|
|
|
|
// already in the view.
|
|
|
|
err := view.fetchInputUtxos(b.db, block)
|
2013-07-18 16:49:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-26 06:03:18 +02:00
|
|
|
|
|
|
|
// Update the view to unspend all of the spent txos and remove
|
|
|
|
// the utxos created by the block.
|
multi: Rework utxoset/view to use outpoints.
This modifies the utxoset in the database and related UtxoViewpoint to
store and work with unspent transaction outputs on a per-output basis
instead of at a transaction level. This was inspired by similar recent
changes in Bitcoin Core.
The primary motivation is to simplify the code, pave the way for a
utxo cache, and generally focus on optimizing runtime performance.
The tradeoff is that this approach does somewhat increase the size of
the serialized utxoset since it means that the transaction hash is
duplicated for each output as a part of the key and some additional
details such as whether the containing transaction is a coinbase and the
block height it was a part of are duplicated in each output.
However, in practice, the size difference isn't all that large, disk
space is relatively cheap, certainly cheaper than memory, and it is much
more important to provide more efficient runtime operation since that is
the ultimate purpose of the daemon.
While performing this conversion, it also simplifies the code to remove
the transaction version information from the utxoset as well as the
spend journal. The logic for only serializing it under certain
circumstances is complicated and it isn't actually used anywhere aside
from the gettxout RPC where it also isn't used by anything important
either. Consequently, this also removes the version field of the
gettxout RPC result.
The utxos in the database are automatically migrated to the new format
with this commit and it is possible to interrupt and resume the
migration process.
Finally, it also updates the tests for the new format and adds a new
function to the tests to convert the old test data to the new format for
convenience. The data has already been converted and updated in the
commit.
An overview of the changes are as follows:
- Remove transaction version from both spent and unspent output entries
- Update utxo serialization format to exclude the version
- Modify the spend journal serialization format
- The old version field is now reserved and always stores zero and
ignores it when reading
- This allows old entries to be used by new code without having to
migrate the entire spend journal
- Remove version field from gettxout RPC result
- Convert UtxoEntry to represent a specific utxo instead of a
transaction with all remaining utxos
- Optimize for memory usage with an eye towards a utxo cache
- Combine details such as whether the txout was contained in a
coinbase, is spent, and is modified into a single packed field of
bit flags
- Align entry fields to eliminate extra padding since ultimately
there will be a lot of these in memory
- Introduce a free list for serializing an outpoint to the database
key format to significantly reduce pressure on the GC
- Update all related functions that previously dealt with transaction
hashes to accept outpoints instead
- Update all callers accordingly
- Only add individually requested outputs from the mempool when
constructing a mempool view
- Modify the spend journal to always store the block height and coinbase
information with every spent txout
- Introduce code to handle fetching the missing information from
another utxo from the same transaction in the event an old style
entry is encountered
- Make use of a database cursor with seek to do this much more
efficiently than testing every possible output
- Always decompress data loaded from the database now that a utxo entry
only consists of a specific output
- Introduce upgrade code to migrate the utxo set to the new format
- Store versions of the utxoset and spend journal buckets
- Allow migration process to be interrupted and resumed
- Update all tests to expect the correct encodings, remove tests that no
longer apply, and add new ones for the new expected behavior
- Convert old tests for the legacy utxo format deserialization code to
test the new function that is used during upgrade
- Update the utxostore test data and add function that was used to
convert it
- Introduce a few new functions on UtxoViewpoint
- AddTxOut for adding an individual txout versus all of them
- addTxOut to handle the common code between the new AddTxOut and
existing AddTxOuts
- RemoveEntry for removing an individual txout
- fetchEntryByHash for fetching any remaining utxo for a given
transaction hash
2017-09-03 09:59:15 +02:00
|
|
|
err = view.disconnectTransactions(b.db, block,
|
|
|
|
detachSpentTxOuts[i])
|
2015-08-26 06:03:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the database and chain state.
|
|
|
|
err = b.disconnectBlock(n, block, view)
|
2013-07-18 16:49:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connect the new best chain blocks.
|
2017-01-29 18:20:10 +01:00
|
|
|
for i, e := 0, attachNodes.Front(); e != nil; i, e = i+1, e.Next() {
|
2013-07-18 16:49:28 +02:00
|
|
|
n := e.Value.(*blockNode)
|
2017-01-29 18:20:10 +01:00
|
|
|
block := attachBlocks[i]
|
2015-08-26 06:03:18 +02:00
|
|
|
|
|
|
|
// Load all of the utxos referenced by the block that aren't
|
|
|
|
// already in the view.
|
|
|
|
err := view.fetchInputUtxos(b.db, block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the view to mark all utxos referenced by the block
|
|
|
|
// as spent and add all transactions being created by this block
|
|
|
|
// to it. Also, provide an stxo slice so the spent txout
|
|
|
|
// details are generated.
|
2018-05-29 06:13:59 +02:00
|
|
|
stxos := make([]SpentTxOut, 0, countSpentOutputs(block))
|
2015-08-26 06:03:18 +02:00
|
|
|
err = view.connectTransactions(block, &stxos)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the database and chain state.
|
|
|
|
err = b.connectBlock(n, block, view, stxos)
|
2013-07-18 16:49:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-03 19:13:53 +01:00
|
|
|
// Log the point where the chain forked and old and new best chain
|
|
|
|
// heads.
|
2018-07-12 02:41:17 +02:00
|
|
|
if forkNode != nil {
|
|
|
|
log.Infof("REORGANIZE: Chain forks at %v (height %v)", forkNode.hash,
|
|
|
|
forkNode.height)
|
|
|
|
}
|
|
|
|
log.Infof("REORGANIZE: Old best chain head was %v (height %v)",
|
|
|
|
&oldBest.hash, oldBest.height)
|
|
|
|
log.Infof("REORGANIZE: New best chain head is %v (height %v)",
|
|
|
|
newBest.hash, newBest.height)
|
2013-07-30 05:32:12 +02:00
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// connectBestChain handles connecting the passed block to the chain while
|
|
|
|
// respecting proper chain selection according to the chain with the most
|
|
|
|
// proof of work. In the typical case, the new block simply extends the main
|
|
|
|
// 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
|
2016-10-13 02:43:01 +02:00
|
|
|
// cumulatively has the most proof of work. It returns whether or not the block
|
|
|
|
// ended up on the main chain (either due to extending the main chain or causing
|
|
|
|
// a reorganization to become the main chain).
|
2014-06-26 22:50:13 +02:00
|
|
|
//
|
|
|
|
// The flags modify the behavior of this function as follows:
|
2015-08-26 06:03:18 +02:00
|
|
|
// - BFFastAdd: Avoids several expensive transaction validation operations.
|
|
|
|
// This is useful when using checkpoints.
|
|
|
|
//
|
|
|
|
// This function MUST be called with the chain state lock held (for writes).
|
2016-10-13 02:43:01 +02:00
|
|
|
func (b *BlockChain) connectBestChain(node *blockNode, block *btcutil.Block, flags BehaviorFlags) (bool, error) {
|
2014-06-26 22:50:13 +02:00
|
|
|
fastAdd := flags&BFFastAdd == BFFastAdd
|
|
|
|
|
2018-08-09 03:26:22 +02:00
|
|
|
flushIndexState := func() {
|
|
|
|
// Intentionally ignore errors writing updated node status to DB. If
|
|
|
|
// it fails to write, it's not the end of the world. If the block is
|
|
|
|
// valid, we flush in connectBlock and if the block is invalid, the
|
|
|
|
// worst that can happen is we revalidate the block after a restart.
|
|
|
|
if writeErr := b.index.flushToDB(); writeErr != nil {
|
|
|
|
log.Warnf("Error flushing block index changes to disk: %v",
|
|
|
|
writeErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// We are extending the main (best) chain with a new block. This is the
|
|
|
|
// most common case.
|
2017-02-03 19:13:53 +01:00
|
|
|
parentHash := &block.MsgBlock().Header.PrevBlock
|
2017-08-18 14:25:54 +02:00
|
|
|
if parentHash.IsEqual(&b.bestChain.Tip().hash) {
|
2017-09-06 05:42:54 +02:00
|
|
|
// Skip checks if node has already been fully validated.
|
2017-10-13 23:36:40 +02:00
|
|
|
fastAdd = fastAdd || b.index.NodeStatus(node).KnownValid()
|
2017-09-06 05:42:54 +02:00
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// Perform several checks to verify the block can be connected
|
2015-08-26 06:03:18 +02:00
|
|
|
// to the main chain without violating any rules and without
|
|
|
|
// actually connecting the block.
|
|
|
|
view := NewUtxoViewpoint()
|
2017-02-03 19:13:53 +01:00
|
|
|
view.SetBestHash(parentHash)
|
2018-05-29 06:13:59 +02:00
|
|
|
stxos := make([]SpentTxOut, 0, countSpentOutputs(block))
|
2013-11-18 22:23:51 +01:00
|
|
|
if !fastAdd {
|
2015-08-26 06:03:18 +02:00
|
|
|
err := b.checkConnectBlock(node, block, view, &stxos)
|
2017-09-14 23:41:21 +02:00
|
|
|
if err == nil {
|
|
|
|
b.index.SetStatusFlags(node, statusValid)
|
|
|
|
} else if _, ok := err.(RuleError); ok {
|
|
|
|
b.index.SetStatusFlags(node, statusValidateFailed)
|
|
|
|
} else {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2018-08-09 03:26:22 +02:00
|
|
|
flushIndexState()
|
2017-09-14 23:41:21 +02:00
|
|
|
|
2013-11-18 22:23:51 +01:00
|
|
|
if err != nil {
|
2016-10-13 02:43:01 +02:00
|
|
|
return false, err
|
2013-11-18 22:23:51 +01:00
|
|
|
}
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// In the fast add case the code to check the block connection
|
|
|
|
// was skipped, so the utxo view needs to load the referenced
|
|
|
|
// utxos, spend them, and add the new utxos being created by
|
|
|
|
// this block.
|
|
|
|
if fastAdd {
|
|
|
|
err := view.fetchInputUtxos(b.db, block)
|
|
|
|
if err != nil {
|
2016-10-13 02:43:01 +02:00
|
|
|
return false, err
|
2015-08-26 06:03:18 +02:00
|
|
|
}
|
|
|
|
err = view.connectTransactions(block, &stxos)
|
|
|
|
if err != nil {
|
2016-10-13 02:43:01 +02:00
|
|
|
return false, err
|
2015-08-26 06:03:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// Connect the block to the main chain.
|
2015-08-26 06:03:18 +02:00
|
|
|
err := b.connectBlock(node, block, view, stxos)
|
2013-07-18 16:49:28 +02:00
|
|
|
if err != nil {
|
2018-08-09 03:30:46 +02:00
|
|
|
// If we got hit with a rule error, then we'll mark
|
|
|
|
// that status of the block as invalid and flush the
|
|
|
|
// index state to disk before returning with the error.
|
|
|
|
if _, ok := err.(RuleError); ok {
|
|
|
|
b.index.SetStatusFlags(
|
|
|
|
node, statusValidateFailed,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
flushIndexState()
|
|
|
|
|
2016-10-13 02:43:01 +02:00
|
|
|
return false, err
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 03:31:15 +02:00
|
|
|
// If this is fast add, or this block node isn't yet marked as
|
|
|
|
// valid, then we'll update its status and flush the state to
|
|
|
|
// disk again.
|
|
|
|
if fastAdd || !b.index.NodeStatus(node).KnownValid() {
|
|
|
|
b.index.SetStatusFlags(node, statusValid)
|
|
|
|
flushIndexState()
|
|
|
|
}
|
|
|
|
|
2016-10-13 02:43:01 +02:00
|
|
|
return true, nil
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
2013-11-18 22:23:51 +01:00
|
|
|
if fastAdd {
|
2015-04-17 07:44:15 +02:00
|
|
|
log.Warnf("fastAdd set in the side chain case? %v\n",
|
2016-08-08 21:04:33 +02:00
|
|
|
block.Hash())
|
2013-11-18 22:23:51 +01:00
|
|
|
}
|
2013-07-18 16:49:28 +02:00
|
|
|
|
|
|
|
// We're extending (or creating) a side chain, but the cumulative
|
|
|
|
// work for this new side chain is not enough to make it the new chain.
|
2017-08-18 14:25:54 +02:00
|
|
|
if node.workSum.Cmp(b.bestChain.Tip().workSum) <= 0 {
|
2013-07-30 05:32:12 +02:00
|
|
|
// Log information about how the block is forking the chain.
|
2017-08-18 14:25:54 +02:00
|
|
|
fork := b.bestChain.FindFork(node)
|
2017-02-03 19:13:53 +01:00
|
|
|
if fork.hash.IsEqual(parentHash) {
|
2013-07-30 05:32:12 +02:00
|
|
|
log.Infof("FORK: Block %v forks the chain at height %d"+
|
|
|
|
"/block %v, but does not cause a reorganize",
|
|
|
|
node.hash, fork.height, fork.hash)
|
|
|
|
} else {
|
|
|
|
log.Infof("EXTEND FORK: Block %v extends a side chain "+
|
|
|
|
"which forks the chain at height %d/block %v",
|
|
|
|
node.hash, fork.height, fork.hash)
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
2014-06-26 22:50:13 +02:00
|
|
|
|
2016-10-13 02:43:01 +02:00
|
|
|
return false, nil
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We're extending (or creating) a side chain and the cumulative work
|
|
|
|
// for this new side chain is more than the old best chain, so this side
|
|
|
|
// chain needs to become the main chain. In order to accomplish that,
|
|
|
|
// find the common ancestor of both sides of the fork, disconnect the
|
|
|
|
// blocks that form the (now) old fork from the main chain, and attach
|
|
|
|
// the blocks that form the new chain to the main chain starting at the
|
|
|
|
// common ancenstor (the point where the chain forked).
|
|
|
|
detachNodes, attachNodes := b.getReorganizeNodes(node)
|
|
|
|
|
|
|
|
// Reorganize the chain.
|
2017-09-15 05:35:11 +02:00
|
|
|
log.Infof("REORGANIZE: Block %v is causing a reorganize.", node.hash)
|
|
|
|
err := b.reorganizeChain(detachNodes, attachNodes)
|
2017-09-14 23:41:21 +02:00
|
|
|
|
|
|
|
// Either getReorganizeNodes or reorganizeChain could have made unsaved
|
|
|
|
// changes to the block index, so flush regardless of whether there was an
|
|
|
|
// error. The index would only be dirty if the block failed to connect, so
|
|
|
|
// we can ignore any errors writing.
|
|
|
|
if writeErr := b.index.flushToDB(); writeErr != nil {
|
|
|
|
log.Warnf("Error flushing block index changes to disk: %v", writeErr)
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
2017-09-14 23:41:21 +02:00
|
|
|
return err == nil, err
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
2016-08-29 00:51:55 +02:00
|
|
|
// isCurrent returns whether or not the chain believes it is current. Several
|
2013-09-23 15:20:54 +02:00
|
|
|
// factors are used to guess, but the key factors that allow the chain to
|
|
|
|
// believe it is current are:
|
|
|
|
// - Latest block height is after the latest checkpoint (if enabled)
|
2021-08-04 00:24:20 +02:00
|
|
|
// - Latest block has a timestamp newer than ~6 hours ago (as LBRY block time is one fourth of bitcoin)
|
2013-09-23 15:20:54 +02:00
|
|
|
//
|
2016-08-29 00:51:55 +02:00
|
|
|
// This function MUST be called with the chain state lock held (for reads).
|
|
|
|
func (b *BlockChain) isCurrent() bool {
|
2013-09-23 15:20:54 +02:00
|
|
|
// Not current if the latest main (best) chain height is before the
|
|
|
|
// latest known good checkpoint (when checkpoints are enabled).
|
2016-12-02 00:54:41 +01:00
|
|
|
checkpoint := b.LatestCheckpoint()
|
2017-08-18 14:25:54 +02:00
|
|
|
if checkpoint != nil && b.bestChain.Tip().height < checkpoint.Height {
|
2013-09-23 15:20:54 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-04 00:24:20 +02:00
|
|
|
// Not current if the latest best block has a timestamp before 7 hours
|
2013-09-23 15:20:54 +02:00
|
|
|
// ago.
|
2016-11-03 05:02:04 +01:00
|
|
|
//
|
|
|
|
// The chain appears to be current if none of the checks reported
|
2013-09-23 15:20:54 +02:00
|
|
|
// otherwise.
|
2021-08-04 00:24:20 +02:00
|
|
|
hours := b.timeSource.AdjustedTime().Add(-7 * time.Hour).Unix()
|
|
|
|
return b.bestChain.Tip().timestamp >= hours
|
2013-09-23 15:20:54 +02:00
|
|
|
}
|
|
|
|
|
2016-08-29 00:51:55 +02:00
|
|
|
// IsCurrent returns whether or not the chain believes it is current. Several
|
|
|
|
// factors are used to guess, but the key factors that allow the chain to
|
|
|
|
// believe it is current are:
|
|
|
|
// - Latest block height is after the latest checkpoint (if enabled)
|
|
|
|
// - Latest block has a timestamp newer than 24 hours ago
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (b *BlockChain) IsCurrent() bool {
|
|
|
|
b.chainLock.RLock()
|
|
|
|
defer b.chainLock.RUnlock()
|
|
|
|
|
|
|
|
return b.isCurrent()
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// BestSnapshot returns information about the current best chain block and
|
|
|
|
// related state as of the current point in time. The returned instance must be
|
|
|
|
// treated as immutable since it is shared by all callers.
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (b *BlockChain) BestSnapshot() *BestState {
|
|
|
|
b.stateLock.RLock()
|
|
|
|
snapshot := b.stateSnapshot
|
|
|
|
b.stateLock.RUnlock()
|
|
|
|
return snapshot
|
|
|
|
}
|
|
|
|
|
2018-08-02 01:50:56 +02:00
|
|
|
// HeaderByHash returns the block header identified by the given hash or an
|
|
|
|
// error if it doesn't exist. Note that this will return headers from both the
|
|
|
|
// main and side chains.
|
|
|
|
func (b *BlockChain) HeaderByHash(hash *chainhash.Hash) (wire.BlockHeader, error) {
|
|
|
|
node := b.index.LookupNode(hash)
|
|
|
|
if node == nil {
|
|
|
|
err := fmt.Errorf("block %s is not known", hash)
|
2017-01-31 07:36:15 +01:00
|
|
|
return wire.BlockHeader{}, err
|
|
|
|
}
|
2018-08-02 01:50:56 +02:00
|
|
|
|
|
|
|
return node.Header(), nil
|
2017-01-31 07:36:15 +01:00
|
|
|
}
|
|
|
|
|
2017-08-18 14:25:54 +02:00
|
|
|
// MainChainHasBlock returns whether or not the block with the given hash is in
|
|
|
|
// the main chain.
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (b *BlockChain) MainChainHasBlock(hash *chainhash.Hash) bool {
|
|
|
|
node := b.index.LookupNode(hash)
|
|
|
|
return node != nil && b.bestChain.Contains(node)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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, this function will
|
|
|
|
// return the block locator for the latest known tip of the main (best) chain if
|
|
|
|
// the passed hash is not currently known.
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (b *BlockChain) BlockLocatorFromHash(hash *chainhash.Hash) BlockLocator {
|
|
|
|
b.chainLock.RLock()
|
|
|
|
node := b.index.LookupNode(hash)
|
|
|
|
locator := b.bestChain.blockLocator(node)
|
|
|
|
b.chainLock.RUnlock()
|
|
|
|
return locator
|
|
|
|
}
|
|
|
|
|
|
|
|
// LatestBlockLocator returns a block locator for the latest known tip of the
|
|
|
|
// main (best) chain.
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (b *BlockChain) LatestBlockLocator() (BlockLocator, error) {
|
|
|
|
b.chainLock.RLock()
|
|
|
|
locator := b.bestChain.BlockLocator(nil)
|
|
|
|
b.chainLock.RUnlock()
|
|
|
|
return locator, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockHeightByHash returns the height of the block with the given hash in the
|
|
|
|
// main chain.
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (b *BlockChain) BlockHeightByHash(hash *chainhash.Hash) (int32, error) {
|
|
|
|
node := b.index.LookupNode(hash)
|
|
|
|
if node == nil || !b.bestChain.Contains(node) {
|
|
|
|
str := fmt.Sprintf("block %s is not in the main chain", hash)
|
|
|
|
return 0, errNotInMainChain(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
return node.height, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockHashByHeight returns the hash of the block at the given height in the
|
|
|
|
// main chain.
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (b *BlockChain) BlockHashByHeight(blockHeight int32) (*chainhash.Hash, error) {
|
|
|
|
node := b.bestChain.NodeByHeight(blockHeight)
|
|
|
|
if node == nil {
|
|
|
|
str := fmt.Sprintf("no block at height %d exists", blockHeight)
|
|
|
|
return nil, errNotInMainChain(str)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return &node.hash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HeightRange returns a range of block hashes for the given start and end
|
|
|
|
// heights. It is inclusive of the start height and exclusive of the end
|
|
|
|
// height. The end height will be limited to the current main chain height.
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (b *BlockChain) HeightRange(startHeight, endHeight int32) ([]chainhash.Hash, error) {
|
|
|
|
// Ensure requested heights are sane.
|
|
|
|
if startHeight < 0 {
|
|
|
|
return nil, fmt.Errorf("start height of fetch range must not "+
|
|
|
|
"be less than zero - got %d", startHeight)
|
|
|
|
}
|
|
|
|
if endHeight < startHeight {
|
|
|
|
return nil, fmt.Errorf("end height of fetch range must not "+
|
|
|
|
"be less than the start height - got start %d, end %d",
|
|
|
|
startHeight, endHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is nothing to do when the start and end heights are the same,
|
|
|
|
// so return now to avoid the chain view lock.
|
|
|
|
if startHeight == endHeight {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab a lock on the chain view to prevent it from changing due to a
|
|
|
|
// reorg while building the hashes.
|
|
|
|
b.bestChain.mtx.Lock()
|
|
|
|
defer b.bestChain.mtx.Unlock()
|
|
|
|
|
|
|
|
// When the requested start height is after the most recent best chain
|
|
|
|
// height, there is nothing to do.
|
|
|
|
latestHeight := b.bestChain.tip().height
|
|
|
|
if startHeight > latestHeight {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit the ending height to the latest height of the chain.
|
|
|
|
if endHeight > latestHeight+1 {
|
|
|
|
endHeight = latestHeight + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch as many as are available within the specified range.
|
|
|
|
hashes := make([]chainhash.Hash, 0, endHeight-startHeight)
|
|
|
|
for i := startHeight; i < endHeight; i++ {
|
|
|
|
hashes = append(hashes, b.bestChain.nodeByHeight(i).hash)
|
|
|
|
}
|
|
|
|
return hashes, nil
|
|
|
|
}
|
|
|
|
|
2018-01-19 20:59:34 +01:00
|
|
|
// HeightToHashRange returns a range of block hashes for the given start height
|
|
|
|
// and end hash, inclusive on both ends. The hashes are for all blocks that are
|
|
|
|
// ancestors of endHash with height greater than or equal to startHeight. The
|
|
|
|
// end hash must belong to a block that is known to be valid.
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (b *BlockChain) HeightToHashRange(startHeight int32,
|
|
|
|
endHash *chainhash.Hash, maxResults int) ([]chainhash.Hash, error) {
|
|
|
|
|
|
|
|
endNode := b.index.LookupNode(endHash)
|
|
|
|
if endNode == nil {
|
|
|
|
return nil, fmt.Errorf("no known block header with hash %v", endHash)
|
|
|
|
}
|
|
|
|
if !b.index.NodeStatus(endNode).KnownValid() {
|
|
|
|
return nil, fmt.Errorf("block %v is not yet validated", endHash)
|
|
|
|
}
|
|
|
|
endHeight := endNode.height
|
|
|
|
|
|
|
|
if startHeight < 0 {
|
|
|
|
return nil, fmt.Errorf("start height (%d) is below 0", startHeight)
|
|
|
|
}
|
|
|
|
if startHeight > endHeight {
|
|
|
|
return nil, fmt.Errorf("start height (%d) is past end height (%d)",
|
|
|
|
startHeight, endHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
resultsLength := int(endHeight - startHeight + 1)
|
|
|
|
if resultsLength > maxResults {
|
|
|
|
return nil, fmt.Errorf("number of results (%d) would exceed max (%d)",
|
|
|
|
resultsLength, maxResults)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk backwards from endHeight to startHeight, collecting block hashes.
|
|
|
|
node := endNode
|
|
|
|
hashes := make([]chainhash.Hash, resultsLength)
|
|
|
|
for i := resultsLength - 1; i >= 0; i-- {
|
|
|
|
hashes[i] = node.hash
|
|
|
|
node = node.parent
|
|
|
|
}
|
|
|
|
return hashes, nil
|
|
|
|
}
|
|
|
|
|
2018-01-23 05:38:25 +01:00
|
|
|
// IntervalBlockHashes returns hashes for all blocks that are ancestors of
|
|
|
|
// endHash where the block height is a positive multiple of interval.
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (b *BlockChain) IntervalBlockHashes(endHash *chainhash.Hash, interval int,
|
|
|
|
) ([]chainhash.Hash, error) {
|
|
|
|
|
|
|
|
endNode := b.index.LookupNode(endHash)
|
|
|
|
if endNode == nil {
|
|
|
|
return nil, fmt.Errorf("no known block header with hash %v", endHash)
|
|
|
|
}
|
|
|
|
if !b.index.NodeStatus(endNode).KnownValid() {
|
|
|
|
return nil, fmt.Errorf("block %v is not yet validated", endHash)
|
|
|
|
}
|
|
|
|
endHeight := endNode.height
|
|
|
|
|
|
|
|
resultsLength := int(endHeight) / interval
|
|
|
|
hashes := make([]chainhash.Hash, resultsLength)
|
|
|
|
|
|
|
|
b.bestChain.mtx.Lock()
|
|
|
|
defer b.bestChain.mtx.Unlock()
|
|
|
|
|
|
|
|
blockNode := endNode
|
|
|
|
for index := int(endHeight) / interval; index > 0; index-- {
|
|
|
|
// Use the bestChain chainView for faster lookups once lookup intersects
|
|
|
|
// the best chain.
|
|
|
|
blockHeight := int32(index * interval)
|
|
|
|
if b.bestChain.contains(blockNode) {
|
|
|
|
blockNode = b.bestChain.nodeByHeight(blockHeight)
|
|
|
|
} else {
|
|
|
|
blockNode = blockNode.Ancestor(blockHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
hashes[index-1] = blockNode.hash
|
|
|
|
}
|
|
|
|
|
|
|
|
return hashes, nil
|
|
|
|
}
|
|
|
|
|
2017-08-20 03:35:37 +02:00
|
|
|
// locateInventory returns the node of the block after the first known block in
|
|
|
|
// the locator along with the number of subsequent nodes needed to either reach
|
|
|
|
// the provided stop hash or the provided max number of entries.
|
|
|
|
//
|
|
|
|
// In addition, there are two special cases:
|
|
|
|
//
|
|
|
|
// - When no locators are provided, the stop hash is treated as a request for
|
|
|
|
// that block, so it will either return the node associated with the stop hash
|
|
|
|
// if it is known, or nil if it is unknown
|
|
|
|
// - When locators are provided, but none of them are known, nodes starting
|
|
|
|
// after the genesis block will be returned
|
|
|
|
//
|
|
|
|
// This is primarily a helper function for the locateBlocks and locateHeaders
|
|
|
|
// functions.
|
|
|
|
//
|
|
|
|
// This function MUST be called with the chain state lock held (for reads).
|
|
|
|
func (b *BlockChain) locateInventory(locator BlockLocator, hashStop *chainhash.Hash, maxEntries uint32) (*blockNode, uint32) {
|
|
|
|
// There are no block locators so a specific block is being requested
|
|
|
|
// as identified by the stop hash.
|
|
|
|
stopNode := b.index.LookupNode(hashStop)
|
|
|
|
if len(locator) == 0 {
|
|
|
|
if stopNode == nil {
|
|
|
|
// No blocks with the stop hash were found so there is
|
|
|
|
// nothing to do.
|
|
|
|
return nil, 0
|
|
|
|
}
|
|
|
|
return stopNode, 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the most recent locator block hash in the main chain. In the
|
|
|
|
// case none of the hashes in the locator are in the main chain, fall
|
|
|
|
// back to the genesis block.
|
|
|
|
startNode := b.bestChain.Genesis()
|
|
|
|
for _, hash := range locator {
|
|
|
|
node := b.index.LookupNode(hash)
|
|
|
|
if node != nil && b.bestChain.Contains(node) {
|
|
|
|
startNode = node
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start at the block after the most recently known block. When there
|
|
|
|
// is no next block it means the most recently known block is the tip of
|
|
|
|
// the best chain, so there is nothing more to do.
|
|
|
|
startNode = b.bestChain.Next(startNode)
|
|
|
|
if startNode == nil {
|
|
|
|
return nil, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate how many entries are needed.
|
|
|
|
total := uint32((b.bestChain.Tip().height - startNode.height) + 1)
|
|
|
|
if stopNode != nil && b.bestChain.Contains(stopNode) &&
|
|
|
|
stopNode.height >= startNode.height {
|
|
|
|
|
|
|
|
total = uint32((stopNode.height - startNode.height) + 1)
|
|
|
|
}
|
|
|
|
if total > maxEntries {
|
|
|
|
total = maxEntries
|
|
|
|
}
|
|
|
|
|
|
|
|
return startNode, total
|
|
|
|
}
|
|
|
|
|
|
|
|
// locateBlocks returns the hashes of the blocks after the first known block in
|
|
|
|
// the locator until the provided stop hash is reached, or up to the provided
|
|
|
|
// max number of block hashes.
|
|
|
|
//
|
|
|
|
// See the comment on the exported function for more details on special cases.
|
|
|
|
//
|
|
|
|
// This function MUST be called with the chain state lock held (for reads).
|
|
|
|
func (b *BlockChain) locateBlocks(locator BlockLocator, hashStop *chainhash.Hash, maxHashes uint32) []chainhash.Hash {
|
|
|
|
// Find the node after the first known block in the locator and the
|
|
|
|
// total number of nodes after it needed while respecting the stop hash
|
|
|
|
// and max entries.
|
|
|
|
node, total := b.locateInventory(locator, hashStop, maxHashes)
|
|
|
|
if total == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate and return the found hashes.
|
|
|
|
hashes := make([]chainhash.Hash, 0, total)
|
|
|
|
for i := uint32(0); i < total; i++ {
|
|
|
|
hashes = append(hashes, node.hash)
|
|
|
|
node = b.bestChain.Next(node)
|
|
|
|
}
|
|
|
|
return hashes
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocateBlocks returns the hashes of the blocks after the first known block in
|
|
|
|
// the locator until the provided stop hash is reached, or up to the provided
|
|
|
|
// max number of block hashes.
|
|
|
|
//
|
|
|
|
// In addition, there are two special cases:
|
|
|
|
//
|
|
|
|
// - When no locators are provided, the stop hash is treated as a request for
|
|
|
|
// that block, so it will either return the stop hash itself if it is known,
|
|
|
|
// or nil if it is unknown
|
|
|
|
// - When locators are provided, but none of them are known, hashes starting
|
|
|
|
// after the genesis block will be returned
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (b *BlockChain) LocateBlocks(locator BlockLocator, hashStop *chainhash.Hash, maxHashes uint32) []chainhash.Hash {
|
|
|
|
b.chainLock.RLock()
|
|
|
|
hashes := b.locateBlocks(locator, hashStop, maxHashes)
|
|
|
|
b.chainLock.RUnlock()
|
|
|
|
return hashes
|
|
|
|
}
|
|
|
|
|
|
|
|
// locateHeaders returns the headers of the blocks after the first known block
|
|
|
|
// in the locator until the provided stop hash is reached, or up to the provided
|
|
|
|
// max number of block headers.
|
|
|
|
//
|
|
|
|
// See the comment on the exported function for more details on special cases.
|
|
|
|
//
|
|
|
|
// This function MUST be called with the chain state lock held (for reads).
|
|
|
|
func (b *BlockChain) locateHeaders(locator BlockLocator, hashStop *chainhash.Hash, maxHeaders uint32) []wire.BlockHeader {
|
|
|
|
// Find the node after the first known block in the locator and the
|
|
|
|
// total number of nodes after it needed while respecting the stop hash
|
|
|
|
// and max entries.
|
|
|
|
node, total := b.locateInventory(locator, hashStop, maxHeaders)
|
|
|
|
if total == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate and return the found headers.
|
|
|
|
headers := make([]wire.BlockHeader, 0, total)
|
|
|
|
for i := uint32(0); i < total; i++ {
|
|
|
|
headers = append(headers, node.Header())
|
|
|
|
node = b.bestChain.Next(node)
|
|
|
|
}
|
|
|
|
return headers
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocateHeaders returns the headers of the blocks after the first known block
|
|
|
|
// in the locator until the provided stop hash is reached, or up to a max of
|
|
|
|
// wire.MaxBlockHeadersPerMsg headers.
|
|
|
|
//
|
|
|
|
// In addition, there are two special cases:
|
|
|
|
//
|
|
|
|
// - When no locators are provided, the stop hash is treated as a request for
|
|
|
|
// that header, so it will either return the header for the stop hash itself
|
|
|
|
// if it is known, or nil if it is unknown
|
|
|
|
// - When locators are provided, but none of them are known, headers starting
|
|
|
|
// after the genesis block will be returned
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (b *BlockChain) LocateHeaders(locator BlockLocator, hashStop *chainhash.Hash) []wire.BlockHeader {
|
|
|
|
b.chainLock.RLock()
|
|
|
|
headers := b.locateHeaders(locator, hashStop, wire.MaxBlockHeadersPerMsg)
|
|
|
|
b.chainLock.RUnlock()
|
|
|
|
return headers
|
|
|
|
}
|
|
|
|
|
2018-08-05 22:59:25 +02:00
|
|
|
// ClaimTrie returns the claimTrie associated wit hthe chain.
|
|
|
|
func (b *BlockChain) ClaimTrie() *claimtrie.ClaimTrie {
|
|
|
|
return b.claimTrie
|
|
|
|
}
|
|
|
|
|
2016-02-19 05:51:18 +01:00
|
|
|
// IndexManager provides a generic interface that the is called when blocks are
|
|
|
|
// connected and disconnected to and from the tip of the main chain for the
|
|
|
|
// purpose of supporting optional indexes.
|
|
|
|
type IndexManager interface {
|
|
|
|
// Init is invoked during chain initialize in order to allow the index
|
2017-09-04 02:50:38 +02:00
|
|
|
// manager to initialize itself and any indexes it is managing. The
|
|
|
|
// channel parameter specifies a channel the caller can close to signal
|
|
|
|
// that the process should be interrupted. It can be nil if that
|
|
|
|
// behavior is not desired.
|
|
|
|
Init(*BlockChain, <-chan struct{}) error
|
2016-02-19 05:51:18 +01:00
|
|
|
|
|
|
|
// ConnectBlock is invoked when a new block has been connected to the
|
2018-05-29 06:53:31 +02:00
|
|
|
// main chain. The set of output spent within a block is also passed in
|
|
|
|
// so indexers can access the previous output scripts input spent if
|
|
|
|
// required.
|
|
|
|
ConnectBlock(database.Tx, *btcutil.Block, []SpentTxOut) error
|
2016-02-19 05:51:18 +01:00
|
|
|
|
|
|
|
// DisconnectBlock is invoked when a block has been disconnected from
|
2018-05-29 06:53:31 +02:00
|
|
|
// the main chain. The set of outputs scripts that were spent within
|
|
|
|
// this block is also returned so indexers can clean up the prior index
|
|
|
|
// state for this block.
|
|
|
|
DisconnectBlock(database.Tx, *btcutil.Block, []SpentTxOut) error
|
2016-02-19 05:51:18 +01:00
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// Config is a descriptor which specifies the blockchain instance configuration.
|
|
|
|
type Config struct {
|
|
|
|
// DB defines the database which houses the blocks and will be used to
|
|
|
|
// store all metadata created by this package such as the utxo set.
|
|
|
|
//
|
|
|
|
// This field is required.
|
|
|
|
DB database.DB
|
|
|
|
|
2017-09-04 02:50:38 +02:00
|
|
|
// Interrupt specifies a channel the caller can close to signal that
|
|
|
|
// long running operations, such as catching up indexes or performing
|
|
|
|
// database migrations, should be interrupted.
|
|
|
|
//
|
|
|
|
// This field can be nil if the caller does not desire the behavior.
|
|
|
|
Interrupt <-chan struct{}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// ChainParams identifies which chain parameters the chain is associated
|
|
|
|
// with.
|
|
|
|
//
|
|
|
|
// This field is required.
|
|
|
|
ChainParams *chaincfg.Params
|
|
|
|
|
2017-01-18 23:58:38 +01:00
|
|
|
// Checkpoints hold caller-defined checkpoints that should be added to
|
|
|
|
// the default checkpoints in ChainParams. Checkpoints must be sorted
|
|
|
|
// by height.
|
2016-12-02 00:54:41 +01:00
|
|
|
//
|
2017-01-18 23:58:38 +01:00
|
|
|
// This field can be nil if the caller does not wish to specify any
|
|
|
|
// checkpoints.
|
2016-12-02 00:54:41 +01:00
|
|
|
Checkpoints []chaincfg.Checkpoint
|
|
|
|
|
2016-07-14 02:36:36 +02:00
|
|
|
// TimeSource defines the median time source to use for things such as
|
|
|
|
// block processing and determining whether or not the chain is current.
|
|
|
|
//
|
|
|
|
// The caller is expected to keep a reference to the time source as well
|
|
|
|
// and add time samples from other peers on the network so the local
|
|
|
|
// time is adjusted to be in agreement with other peers.
|
|
|
|
TimeSource MedianTimeSource
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// SigCache defines a signature cache to use when when validating
|
|
|
|
// signatures. This is typically most useful when individual
|
|
|
|
// transactions are already being validated prior to their inclusion in
|
|
|
|
// a block such as what is usually done via a transaction memory pool.
|
|
|
|
//
|
|
|
|
// This field can be nil if the caller is not interested in using a
|
|
|
|
// signature cache.
|
|
|
|
SigCache *txscript.SigCache
|
2016-02-19 05:51:18 +01:00
|
|
|
|
|
|
|
// IndexManager defines an index manager to use when initializing the
|
|
|
|
// chain and connecting and disconnecting blocks.
|
|
|
|
//
|
|
|
|
// This field can be nil if the caller does not wish to make use of an
|
|
|
|
// index manager.
|
|
|
|
IndexManager IndexManager
|
2016-10-19 03:33:20 +02:00
|
|
|
|
|
|
|
// HashCache defines a transaction hash mid-state cache to use when
|
|
|
|
// validating transactions. This cache has the potential to greatly
|
|
|
|
// speed up transaction validation as re-using the pre-calculated
|
|
|
|
// mid-state eliminates the O(N^2) validation complexity due to the
|
|
|
|
// SigHashAll flag.
|
|
|
|
//
|
|
|
|
// This field can be nil if the caller is not interested in using a
|
|
|
|
// signature cache.
|
|
|
|
HashCache *txscript.HashCache
|
2018-08-05 22:59:25 +02:00
|
|
|
|
|
|
|
ClaimTrie *claimtrie.ClaimTrie
|
2015-08-26 06:03:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a BlockChain instance using the provided configuration details.
|
|
|
|
func New(config *Config) (*BlockChain, error) {
|
|
|
|
// Enforce required config fields.
|
|
|
|
if config.DB == nil {
|
|
|
|
return nil, AssertError("blockchain.New database is nil")
|
|
|
|
}
|
|
|
|
if config.ChainParams == nil {
|
|
|
|
return nil, AssertError("blockchain.New chain parameters nil")
|
|
|
|
}
|
2017-01-12 17:19:51 +01:00
|
|
|
if config.TimeSource == nil {
|
|
|
|
return nil, AssertError("blockchain.New timesource is nil")
|
|
|
|
}
|
2015-08-26 06:03:18 +02:00
|
|
|
|
2017-01-18 23:58:38 +01:00
|
|
|
// Generate a checkpoint by height map from the provided checkpoints
|
|
|
|
// and assert the provided checkpoints are sorted by height as required.
|
2015-08-08 04:20:49 +02:00
|
|
|
var checkpointsByHeight map[int32]*chaincfg.Checkpoint
|
2017-01-18 23:58:38 +01:00
|
|
|
var prevCheckpointHeight int32
|
2016-12-02 00:54:41 +01:00
|
|
|
if len(config.Checkpoints) > 0 {
|
2015-08-08 04:20:49 +02:00
|
|
|
checkpointsByHeight = make(map[int32]*chaincfg.Checkpoint)
|
2016-12-02 00:54:41 +01:00
|
|
|
for i := range config.Checkpoints {
|
|
|
|
checkpoint := &config.Checkpoints[i]
|
2017-01-18 23:58:38 +01:00
|
|
|
if checkpoint.Height <= prevCheckpointHeight {
|
|
|
|
return nil, AssertError("blockchain.New " +
|
|
|
|
"checkpoints are not sorted by height")
|
|
|
|
}
|
|
|
|
|
2014-05-26 17:27:50 +02:00
|
|
|
checkpointsByHeight[checkpoint.Height] = checkpoint
|
2017-01-18 23:58:38 +01:00
|
|
|
prevCheckpointHeight = checkpoint.Height
|
2014-05-26 17:27:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 00:54:41 +01:00
|
|
|
params := config.ChainParams
|
2017-01-31 01:32:50 +01:00
|
|
|
targetTimespan := int64(params.TargetTimespan / time.Second)
|
|
|
|
targetTimePerBlock := int64(params.TargetTimePerBlock / time.Second)
|
2013-07-18 16:49:28 +02:00
|
|
|
b := BlockChain{
|
2016-12-02 00:54:41 +01:00
|
|
|
checkpoints: config.Checkpoints,
|
2014-05-26 17:27:50 +02:00
|
|
|
checkpointsByHeight: checkpointsByHeight,
|
2015-08-26 06:03:18 +02:00
|
|
|
db: config.DB,
|
|
|
|
chainParams: params,
|
2016-07-14 02:36:36 +02:00
|
|
|
timeSource: config.TimeSource,
|
2015-08-26 06:03:18 +02:00
|
|
|
sigCache: config.SigCache,
|
2016-02-19 05:51:18 +01:00
|
|
|
indexManager: config.IndexManager,
|
2018-06-13 02:27:22 +02:00
|
|
|
minRetargetTimespan: targetTimespan - (targetTimespan / 8),
|
|
|
|
maxRetargetTimespan: targetTimespan + (targetTimespan / 2),
|
2016-08-10 23:02:23 +02:00
|
|
|
blocksPerRetarget: int32(targetTimespan / targetTimePerBlock),
|
2016-04-25 11:12:09 +02:00
|
|
|
index: newBlockIndex(config.DB, params),
|
2016-10-19 03:33:20 +02:00
|
|
|
hashCache: config.HashCache,
|
2017-08-18 14:25:54 +02:00
|
|
|
bestChain: newChainView(nil),
|
2016-08-08 21:04:33 +02:00
|
|
|
orphans: make(map[chainhash.Hash]*orphanBlock),
|
|
|
|
prevOrphans: make(map[chainhash.Hash][]*orphanBlock),
|
2016-08-29 00:51:55 +02:00
|
|
|
warningCaches: newThresholdCaches(vbNumBits),
|
|
|
|
deploymentCaches: newThresholdCaches(chaincfg.DefinedDeployments),
|
2018-08-05 22:59:25 +02:00
|
|
|
claimTrie: config.ClaimTrie,
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
2015-08-26 06:03:18 +02:00
|
|
|
|
|
|
|
// Initialize the chain state from the passed database. When the db
|
|
|
|
// does not yet contain any chain state, both it and the chain state
|
|
|
|
// will be initialized to contain only the genesis block.
|
|
|
|
if err := b.initChainState(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-06-06 22:22:50 +02:00
|
|
|
// Helper function to insert the output in genesis block in to the
|
|
|
|
// transaction database.
|
|
|
|
fn := func(dbTx database.Tx) error {
|
|
|
|
genesisBlock := btcutil.NewBlock(b.chainParams.GenesisBlock)
|
|
|
|
view := NewUtxoViewpoint()
|
|
|
|
if err := view.connectTransactions(genesisBlock, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return dbPutUtxoView(dbTx, view)
|
|
|
|
}
|
|
|
|
if err := b.db.Update(fn); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
multi: Rework utxoset/view to use outpoints.
This modifies the utxoset in the database and related UtxoViewpoint to
store and work with unspent transaction outputs on a per-output basis
instead of at a transaction level. This was inspired by similar recent
changes in Bitcoin Core.
The primary motivation is to simplify the code, pave the way for a
utxo cache, and generally focus on optimizing runtime performance.
The tradeoff is that this approach does somewhat increase the size of
the serialized utxoset since it means that the transaction hash is
duplicated for each output as a part of the key and some additional
details such as whether the containing transaction is a coinbase and the
block height it was a part of are duplicated in each output.
However, in practice, the size difference isn't all that large, disk
space is relatively cheap, certainly cheaper than memory, and it is much
more important to provide more efficient runtime operation since that is
the ultimate purpose of the daemon.
While performing this conversion, it also simplifies the code to remove
the transaction version information from the utxoset as well as the
spend journal. The logic for only serializing it under certain
circumstances is complicated and it isn't actually used anywhere aside
from the gettxout RPC where it also isn't used by anything important
either. Consequently, this also removes the version field of the
gettxout RPC result.
The utxos in the database are automatically migrated to the new format
with this commit and it is possible to interrupt and resume the
migration process.
Finally, it also updates the tests for the new format and adds a new
function to the tests to convert the old test data to the new format for
convenience. The data has already been converted and updated in the
commit.
An overview of the changes are as follows:
- Remove transaction version from both spent and unspent output entries
- Update utxo serialization format to exclude the version
- Modify the spend journal serialization format
- The old version field is now reserved and always stores zero and
ignores it when reading
- This allows old entries to be used by new code without having to
migrate the entire spend journal
- Remove version field from gettxout RPC result
- Convert UtxoEntry to represent a specific utxo instead of a
transaction with all remaining utxos
- Optimize for memory usage with an eye towards a utxo cache
- Combine details such as whether the txout was contained in a
coinbase, is spent, and is modified into a single packed field of
bit flags
- Align entry fields to eliminate extra padding since ultimately
there will be a lot of these in memory
- Introduce a free list for serializing an outpoint to the database
key format to significantly reduce pressure on the GC
- Update all related functions that previously dealt with transaction
hashes to accept outpoints instead
- Update all callers accordingly
- Only add individually requested outputs from the mempool when
constructing a mempool view
- Modify the spend journal to always store the block height and coinbase
information with every spent txout
- Introduce code to handle fetching the missing information from
another utxo from the same transaction in the event an old style
entry is encountered
- Make use of a database cursor with seek to do this much more
efficiently than testing every possible output
- Always decompress data loaded from the database now that a utxo entry
only consists of a specific output
- Introduce upgrade code to migrate the utxo set to the new format
- Store versions of the utxoset and spend journal buckets
- Allow migration process to be interrupted and resumed
- Update all tests to expect the correct encodings, remove tests that no
longer apply, and add new ones for the new expected behavior
- Convert old tests for the legacy utxo format deserialization code to
test the new function that is used during upgrade
- Update the utxostore test data and add function that was used to
convert it
- Introduce a few new functions on UtxoViewpoint
- AddTxOut for adding an individual txout versus all of them
- addTxOut to handle the common code between the new AddTxOut and
existing AddTxOuts
- RemoveEntry for removing an individual txout
- fetchEntryByHash for fetching any remaining utxo for a given
transaction hash
2017-09-03 09:59:15 +02:00
|
|
|
// Perform any upgrades to the various chain-specific buckets as needed.
|
|
|
|
if err := b.maybeUpgradeDbBuckets(config.Interrupt); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-02-19 05:51:18 +01:00
|
|
|
// Initialize and catch up all of the currently active optional indexes
|
|
|
|
// as needed.
|
|
|
|
if config.IndexManager != nil {
|
2017-09-04 02:50:38 +02:00
|
|
|
err := config.IndexManager.Init(&b, config.Interrupt)
|
|
|
|
if err != nil {
|
2016-02-19 05:51:18 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 09:44:22 +01:00
|
|
|
// Initialize rule change threshold state caches.
|
2016-08-29 00:51:55 +02:00
|
|
|
if err := b.initThresholdCaches(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-08-05 22:59:25 +02:00
|
|
|
if b.claimTrie != nil {
|
|
|
|
err := rebuildMissingClaimTrieData(&b, config.Interrupt)
|
|
|
|
if err != nil {
|
|
|
|
b.claimTrie.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-18 14:25:54 +02:00
|
|
|
bestNode := b.bestChain.Tip()
|
2015-08-26 06:03:18 +02:00
|
|
|
log.Infof("Chain state (height %d, hash %v, totaltx %d, work %v)",
|
2017-08-18 14:25:54 +02:00
|
|
|
bestNode.height, bestNode.hash, b.stateSnapshot.TotalTxns,
|
|
|
|
bestNode.workSum)
|
2015-08-26 06:03:18 +02:00
|
|
|
|
|
|
|
return &b, nil
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
2018-08-05 22:59:25 +02:00
|
|
|
|
|
|
|
func rebuildMissingClaimTrieData(b *BlockChain, done <-chan struct{}) error {
|
|
|
|
target := b.bestChain.Height()
|
|
|
|
if b.claimTrie.Height() == target {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if b.claimTrie.Height() > target {
|
|
|
|
return b.claimTrie.ResetHeight(target)
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
lastReport := time.Now()
|
|
|
|
// TODO: move this view inside the loop (or recreate it every 5 sec.)
|
|
|
|
// as accumulating all inputs has potential to use a huge amount of RAM
|
|
|
|
// but we need to get the spent inputs working for that to be possible
|
|
|
|
view := NewUtxoViewpoint()
|
|
|
|
for h := int32(0); h < target; h++ {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return fmt.Errorf("rebuild unfinished at height %d", b.claimTrie.Height())
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
n := b.bestChain.NodeByHeight(h + 1)
|
|
|
|
|
|
|
|
var block *btcutil.Block
|
|
|
|
err := b.db.View(func(dbTx database.Tx) error {
|
|
|
|
var err error
|
|
|
|
block, err = dbFetchBlockByNode(dbTx, n)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = view.fetchInputUtxos(b.db, block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = view.connectTransactions(block, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if h >= b.claimTrie.Height() {
|
|
|
|
err = b.ParseClaimScripts(block, n, view, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if time.Since(lastReport) > time.Second*5 {
|
|
|
|
lastReport = time.Now()
|
|
|
|
log.Infof("Rebuilding claim trie data to %d. At: %d", target, h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Infof("Completed rebuilding claim trie data to %d. Took %s ",
|
|
|
|
b.claimTrie.Height(), time.Since(start))
|
|
|
|
return nil
|
|
|
|
}
|