2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2013-2014 The btcsuite 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 (
|
|
|
|
"fmt"
|
2014-07-02 18:04:59 +02:00
|
|
|
|
2015-01-27 22:00:49 +01:00
|
|
|
"github.com/btcsuite/btcd/database"
|
2015-02-05 22:16:39 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2015-01-15 17:23:47 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2013-07-18 16:49:28 +02:00
|
|
|
)
|
|
|
|
|
2013-09-30 23:21:35 +02:00
|
|
|
// TxData contains contextual information about transactions such as which block
|
2013-07-18 16:49:28 +02:00
|
|
|
// they were found in and whether or not the outputs are spent.
|
2013-09-30 23:21:35 +02:00
|
|
|
type TxData struct {
|
2013-10-28 21:17:53 +01:00
|
|
|
Tx *btcutil.Tx
|
2015-02-05 22:16:39 +01:00
|
|
|
Hash *wire.ShaHash
|
2015-08-08 04:20:49 +02:00
|
|
|
BlockHeight int32
|
2013-09-30 23:21:35 +02:00
|
|
|
Spent []bool
|
|
|
|
Err error
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
2013-09-30 23:21:35 +02:00
|
|
|
// TxStore is used to store transactions needed by other transactions for things
|
|
|
|
// such as script validation and double spend prevention. This also allows the
|
|
|
|
// transaction data to be treated as a view since it can contain the information
|
|
|
|
// from the point-of-view of different points in the chain.
|
2015-02-05 22:16:39 +01:00
|
|
|
type TxStore map[wire.ShaHash]*TxData
|
2013-09-30 23:21:35 +02:00
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// connectTransactions updates the passed map by applying transaction and
|
2013-08-05 22:20:35 +02:00
|
|
|
// spend information for all the transactions in the passed block. Only
|
2013-07-18 16:49:28 +02:00
|
|
|
// transactions in the passed map are updated.
|
2013-09-30 23:21:35 +02:00
|
|
|
func connectTransactions(txStore TxStore, block *btcutil.Block) error {
|
2013-07-18 16:49:28 +02:00
|
|
|
// Loop through all of the transactions in the block to see if any of
|
|
|
|
// them are ones we need to update and spend based on the results map.
|
2013-10-28 21:17:53 +01:00
|
|
|
for _, tx := range block.Transactions() {
|
2013-07-18 16:49:28 +02:00
|
|
|
// Update the transaction store with the transaction information
|
|
|
|
// if it's one of the requested transactions.
|
2013-10-28 21:17:53 +01:00
|
|
|
msgTx := tx.MsgTx()
|
|
|
|
if txD, exists := txStore[*tx.Sha()]; exists {
|
2013-09-30 23:21:35 +02:00
|
|
|
txD.Tx = tx
|
|
|
|
txD.BlockHeight = block.Height()
|
2013-10-28 21:17:53 +01:00
|
|
|
txD.Spent = make([]bool, len(msgTx.TxOut))
|
2013-09-30 23:21:35 +02:00
|
|
|
txD.Err = nil
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Spend the origin transaction output.
|
2013-10-28 21:17:53 +01:00
|
|
|
for _, txIn := range msgTx.TxIn {
|
2014-10-01 14:53:47 +02:00
|
|
|
originHash := &txIn.PreviousOutPoint.Hash
|
|
|
|
originIndex := txIn.PreviousOutPoint.Index
|
2013-07-18 16:49:28 +02:00
|
|
|
if originTx, exists := txStore[*originHash]; exists {
|
2014-06-29 22:11:13 +02:00
|
|
|
if originIndex > uint32(len(originTx.Spent)) {
|
|
|
|
continue
|
|
|
|
}
|
2013-09-30 23:21:35 +02:00
|
|
|
originTx.Spent[originIndex] = true
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// disconnectTransactions updates the passed map by undoing transaction and
|
|
|
|
// spend information for all transactions in the passed block. Only
|
|
|
|
// transactions in the passed map are updated.
|
2013-09-30 23:21:35 +02:00
|
|
|
func disconnectTransactions(txStore TxStore, block *btcutil.Block) error {
|
2013-07-18 16:49:28 +02:00
|
|
|
// Loop through all of the transactions in the block to see if any of
|
2013-08-29 15:56:27 +02:00
|
|
|
// them are ones that need to be undone based on the transaction store.
|
2013-10-28 21:17:53 +01:00
|
|
|
for _, tx := range block.Transactions() {
|
2013-08-29 15:56:27 +02:00
|
|
|
// Clear this transaction from the transaction store if needed.
|
|
|
|
// Only clear it rather than deleting it because the transaction
|
|
|
|
// connect code relies on its presence to decide whether or not
|
|
|
|
// to update the store and any transactions which exist on both
|
|
|
|
// sides of a fork would otherwise not be updated.
|
2013-10-28 21:17:53 +01:00
|
|
|
if txD, exists := txStore[*tx.Sha()]; exists {
|
2013-09-30 23:21:35 +02:00
|
|
|
txD.Tx = nil
|
|
|
|
txD.BlockHeight = 0
|
|
|
|
txD.Spent = nil
|
2015-01-27 22:00:49 +01:00
|
|
|
txD.Err = database.ErrTxShaMissing
|
2013-08-29 15:56:27 +02:00
|
|
|
}
|
2013-07-18 16:49:28 +02:00
|
|
|
|
|
|
|
// Unspend the origin transaction output.
|
2013-10-28 21:17:53 +01:00
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
2014-10-01 14:53:47 +02:00
|
|
|
originHash := &txIn.PreviousOutPoint.Hash
|
|
|
|
originIndex := txIn.PreviousOutPoint.Index
|
2013-08-29 15:56:27 +02:00
|
|
|
originTx, exists := txStore[*originHash]
|
2013-09-30 23:21:35 +02:00
|
|
|
if exists && originTx.Tx != nil && originTx.Err == nil {
|
2014-06-29 22:11:13 +02:00
|
|
|
if originIndex > uint32(len(originTx.Spent)) {
|
|
|
|
continue
|
|
|
|
}
|
2013-09-30 23:21:35 +02:00
|
|
|
originTx.Spent[originIndex] = false
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-10-10 19:23:46 +02:00
|
|
|
// fetchTxStoreMain fetches transaction data about the provided set of
|
2013-10-13 09:27:50 +02:00
|
|
|
// transactions from the point of view of the end of the main chain. It takes
|
|
|
|
// a flag which specifies whether or not fully spent transaction should be
|
|
|
|
// included in the results.
|
2015-02-05 22:16:39 +01:00
|
|
|
func fetchTxStoreMain(db database.Db, txSet map[wire.ShaHash]struct{}, includeSpent bool) TxStore {
|
2013-10-11 17:22:23 +02:00
|
|
|
// Just return an empty store now if there are no requested hashes.
|
|
|
|
txStore := make(TxStore)
|
|
|
|
if len(txSet) == 0 {
|
|
|
|
return txStore
|
|
|
|
}
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// The transaction store map needs to have an entry for every requested
|
|
|
|
// transaction. By default, all the transactions are marked as missing.
|
|
|
|
// Each entry will be filled in with the appropriate data below.
|
2015-02-05 22:16:39 +01:00
|
|
|
txList := make([]*wire.ShaHash, 0, len(txSet))
|
2013-10-10 19:23:46 +02:00
|
|
|
for hash := range txSet {
|
|
|
|
hashCopy := hash
|
2015-01-27 22:00:49 +01:00
|
|
|
txStore[hash] = &TxData{Hash: &hashCopy, Err: database.ErrTxShaMissing}
|
2013-10-10 19:23:46 +02:00
|
|
|
txList = append(txList, &hashCopy)
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ask the database (main chain) for the list of transactions. This
|
|
|
|
// will return the information from the point of view of the end of the
|
2013-10-13 09:27:50 +02:00
|
|
|
// main chain. Choose whether or not to include fully spent
|
|
|
|
// transactions depending on the passed flag.
|
2015-02-07 02:04:57 +01:00
|
|
|
var txReplyList []*database.TxListReply
|
2013-10-13 09:27:50 +02:00
|
|
|
if includeSpent {
|
2015-02-07 02:04:57 +01:00
|
|
|
txReplyList = db.FetchTxByShaList(txList)
|
|
|
|
} else {
|
|
|
|
txReplyList = db.FetchUnSpentTxByShaList(txList)
|
2013-10-13 09:27:50 +02:00
|
|
|
}
|
2013-07-18 16:49:28 +02:00
|
|
|
for _, txReply := range txReplyList {
|
|
|
|
// Lookup the existing results entry to modify. Skip
|
|
|
|
// this reply if there is no corresponding entry in
|
|
|
|
// the transaction store map which really should not happen, but
|
|
|
|
// be safe.
|
|
|
|
txD, ok := txStore[*txReply.Sha]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in the transaction details. A copy is used here since
|
|
|
|
// there is no guarantee the returned data isn't cached and
|
|
|
|
// this code modifies the data. A bug caused by modifying the
|
|
|
|
// cached data would likely be difficult to track down and could
|
|
|
|
// cause subtle errors, so avoid the potential altogether.
|
2013-09-30 23:21:35 +02:00
|
|
|
txD.Err = txReply.Err
|
2013-07-18 16:49:28 +02:00
|
|
|
if txReply.Err == nil {
|
2013-10-28 21:17:53 +01:00
|
|
|
txD.Tx = btcutil.NewTx(txReply.Tx)
|
2013-09-30 23:21:35 +02:00
|
|
|
txD.BlockHeight = txReply.Height
|
|
|
|
txD.Spent = make([]bool, len(txReply.TxSpent))
|
|
|
|
copy(txD.Spent, txReply.TxSpent)
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-30 23:21:35 +02:00
|
|
|
return txStore
|
|
|
|
}
|
|
|
|
|
2013-10-10 19:23:46 +02:00
|
|
|
// fetchTxStore fetches transaction data about the provided set of transactions
|
2013-09-30 23:21:35 +02:00
|
|
|
// from the point of view of the given node. For example, a given node might
|
|
|
|
// be down a side chain where a transaction hasn't been spent from its point of
|
|
|
|
// view even though it might have been spent in the main chain (or another side
|
|
|
|
// chain). Another scenario is where a transaction exists from the point of
|
|
|
|
// view of the main chain, but doesn't exist in a side chain that branches
|
|
|
|
// before the block that contains the transaction on the main chain.
|
2015-02-05 22:16:39 +01:00
|
|
|
func (b *BlockChain) fetchTxStore(node *blockNode, txSet map[wire.ShaHash]struct{}) (TxStore, error) {
|
2013-09-30 23:21:35 +02:00
|
|
|
// Get the previous block node. This function is used over simply
|
|
|
|
// accessing node.parent directly as it will dynamically create previous
|
|
|
|
// block nodes as needed. This helps allow only the pieces of the chain
|
|
|
|
// that are needed to remain in memory.
|
|
|
|
prevNode, err := b.getPrevNodeFromNode(node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we haven't selected a best chain yet or we are extending the main
|
2013-10-13 09:27:50 +02:00
|
|
|
// (best) chain with a new block, fetch the requested set from the point
|
|
|
|
// of view of the end of the main (best) chain without including fully
|
|
|
|
// spent transactions in the results. This is a little more efficient
|
|
|
|
// since it means less transaction lookups are needed.
|
2013-07-18 16:49:28 +02:00
|
|
|
if b.bestChain == nil || (prevNode != nil && prevNode.hash.IsEqual(b.bestChain.hash)) {
|
2013-10-13 09:27:50 +02:00
|
|
|
txStore := fetchTxStoreMain(b.db, txSet, false)
|
2013-07-18 16:49:28 +02:00
|
|
|
return txStore, nil
|
|
|
|
}
|
|
|
|
|
2013-10-13 09:27:50 +02:00
|
|
|
// Fetch the requested set from the point of view of the end of the
|
|
|
|
// main (best) chain including fully spent transactions. The fully
|
|
|
|
// spent transactions are needed because the following code unspends
|
|
|
|
// them to get the correct point of view.
|
|
|
|
txStore := fetchTxStoreMain(b.db, txSet, true)
|
|
|
|
|
2013-07-18 16:49:28 +02:00
|
|
|
// The requested node is either on a side chain or is a node on the main
|
|
|
|
// chain before the end of it. In either case, we need to undo the
|
|
|
|
// transactions and spend information for the blocks which would be
|
|
|
|
// disconnected during a reorganize to the point of view of the
|
|
|
|
// node just before the requested node.
|
|
|
|
detachNodes, attachNodes := b.getReorganizeNodes(prevNode)
|
|
|
|
for e := detachNodes.Front(); e != nil; e = e.Next() {
|
|
|
|
n := e.Value.(*blockNode)
|
|
|
|
block, err := b.db.FetchBlockBySha(n.hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
disconnectTransactions(txStore, block)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The transaction store is now accurate to either the node where the
|
|
|
|
// requested node forks off the main chain (in the case where the
|
|
|
|
// requested node is on a side chain), or the requested node itself if
|
|
|
|
// the requested node is an old node on the main chain. Entries in the
|
|
|
|
// attachNodes list indicate the requested node is on a side chain, so
|
|
|
|
// if there are no nodes to attach, we're done.
|
|
|
|
if attachNodes.Len() == 0 {
|
|
|
|
return txStore, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The requested node is on a side chain, so we need to apply the
|
|
|
|
// transactions and spend information from each of the nodes to attach.
|
|
|
|
for e := attachNodes.Front(); e != nil; e = e.Next() {
|
|
|
|
n := e.Value.(*blockNode)
|
|
|
|
block, exists := b.blockCache[*n.hash]
|
|
|
|
if !exists {
|
|
|
|
return nil, fmt.Errorf("unable to find block %v in "+
|
|
|
|
"side chain cache for transaction search",
|
|
|
|
n.hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
connectTransactions(txStore, block)
|
|
|
|
}
|
|
|
|
|
|
|
|
return txStore, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchInputTransactions fetches the input transactions referenced by the
|
|
|
|
// transactions in the given block from its point of view. See fetchTxList
|
|
|
|
// for more details on what the point of view entails.
|
2013-09-30 23:21:35 +02:00
|
|
|
func (b *BlockChain) fetchInputTransactions(node *blockNode, block *btcutil.Block) (TxStore, error) {
|
2013-07-18 16:49:28 +02:00
|
|
|
// Build a map of in-flight transactions because some of the inputs in
|
2013-07-29 22:48:52 +02:00
|
|
|
// this block could be referencing other transactions earlier in this
|
|
|
|
// block which are not yet in the chain.
|
2015-02-05 22:16:39 +01:00
|
|
|
txInFlight := map[wire.ShaHash]int{}
|
2013-10-28 21:17:53 +01:00
|
|
|
transactions := block.Transactions()
|
|
|
|
for i, tx := range transactions {
|
|
|
|
txInFlight[*tx.Sha()] = i
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through all of the transaction inputs (except for the coinbase
|
2013-10-10 19:23:46 +02:00
|
|
|
// which has no inputs) collecting them into sets of what is needed and
|
2013-07-18 16:49:28 +02:00
|
|
|
// what is already known (in-flight).
|
2015-02-05 22:16:39 +01:00
|
|
|
txNeededSet := make(map[wire.ShaHash]struct{})
|
2013-09-30 23:21:35 +02:00
|
|
|
txStore := make(TxStore)
|
2013-07-29 22:48:52 +02:00
|
|
|
for i, tx := range transactions[1:] {
|
2013-10-28 21:17:53 +01:00
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
2013-07-18 16:49:28 +02:00
|
|
|
// Add an entry to the transaction store for the needed
|
|
|
|
// transaction with it set to missing by default.
|
2014-10-01 14:53:47 +02:00
|
|
|
originHash := &txIn.PreviousOutPoint.Hash
|
2015-01-27 22:00:49 +01:00
|
|
|
txD := &TxData{Hash: originHash, Err: database.ErrTxShaMissing}
|
2013-07-18 16:49:28 +02:00
|
|
|
txStore[*originHash] = txD
|
|
|
|
|
2013-07-29 22:48:52 +02:00
|
|
|
// It is acceptable for a transaction input to reference
|
|
|
|
// the output of another transaction in this block only
|
|
|
|
// if the referenced transaction comes before the
|
|
|
|
// current one in this block. Update the transaction
|
|
|
|
// store acccordingly when this is the case. Otherwise,
|
|
|
|
// we still need the transaction.
|
|
|
|
//
|
|
|
|
// NOTE: The >= is correct here because i is one less
|
|
|
|
// than the actual position of the transaction within
|
|
|
|
// the block due to skipping the coinbase.
|
|
|
|
if inFlightIndex, ok := txInFlight[*originHash]; ok &&
|
|
|
|
i >= inFlightIndex {
|
|
|
|
|
|
|
|
originTx := transactions[inFlightIndex]
|
2013-09-30 23:21:35 +02:00
|
|
|
txD.Tx = originTx
|
|
|
|
txD.BlockHeight = node.height
|
2013-10-28 21:17:53 +01:00
|
|
|
txD.Spent = make([]bool, len(originTx.MsgTx().TxOut))
|
2013-09-30 23:21:35 +02:00
|
|
|
txD.Err = nil
|
2013-07-18 16:49:28 +02:00
|
|
|
} else {
|
2014-07-02 17:56:22 +02:00
|
|
|
txNeededSet[*originHash] = struct{}{}
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-05 22:20:35 +02:00
|
|
|
// Request the input transactions from the point of view of the node.
|
2013-10-10 19:23:46 +02:00
|
|
|
txNeededStore, err := b.fetchTxStore(node, txNeededSet)
|
2013-07-18 16:49:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the results of the requested transactions and the in-flight
|
|
|
|
// transactions.
|
|
|
|
for _, txD := range txNeededStore {
|
2013-09-30 23:21:35 +02:00
|
|
|
txStore[*txD.Hash] = txD
|
|
|
|
}
|
|
|
|
|
|
|
|
return txStore, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FetchTransactionStore fetches the input transactions referenced by the
|
|
|
|
// passed transaction from the point of view of the end of the main chain. It
|
|
|
|
// also attempts to fetch the transaction itself so the returned TxStore can be
|
|
|
|
// examined for duplicate transactions.
|
2013-10-28 21:17:53 +01:00
|
|
|
func (b *BlockChain) FetchTransactionStore(tx *btcutil.Tx) (TxStore, error) {
|
2013-10-10 19:23:46 +02:00
|
|
|
// Create a set of needed transactions from the transactions referenced
|
|
|
|
// by the inputs of the passed transaction. Also, add the passed
|
|
|
|
// transaction itself as a way for the caller to detect duplicates.
|
2015-02-05 22:16:39 +01:00
|
|
|
txNeededSet := make(map[wire.ShaHash]struct{})
|
2014-07-02 17:56:22 +02:00
|
|
|
txNeededSet[*tx.Sha()] = struct{}{}
|
2013-10-28 21:17:53 +01:00
|
|
|
for _, txIn := range tx.MsgTx().TxIn {
|
2014-10-01 14:53:47 +02:00
|
|
|
txNeededSet[txIn.PreviousOutPoint.Hash] = struct{}{}
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
2013-10-10 19:23:46 +02:00
|
|
|
// Request the input transactions from the point of view of the end of
|
2013-10-13 09:27:50 +02:00
|
|
|
// the main chain without including fully spent trasactions in the
|
|
|
|
// results. Fully spent transactions are only needed for chain
|
|
|
|
// reorganization which does not apply here.
|
|
|
|
txStore := fetchTxStoreMain(b.db, txNeededSet, false)
|
2013-07-18 16:49:28 +02:00
|
|
|
return txStore, nil
|
|
|
|
}
|