Make use of new spent vs unspent btcdb APIs.

This commit modifies the code to make use of the new btcd APIs that allow
fetching of transaction lists which either do or do not include fully
spent transactions.  It is more efficient to avoid fetching fully spent
transactions from the database when they aren't needed.
This commit is contained in:
Dave Collins 2013-10-13 02:27:50 -05:00
parent 9a29855c16
commit d7e057a020

View file

@ -100,8 +100,10 @@ func disconnectTransactions(txStore TxStore, block *btcutil.Block) error {
} }
// fetchTxStoreMain fetches transaction data about the provided set of // fetchTxStoreMain fetches transaction data about the provided set of
// transactions from the point of view of the end of the main chain. // transactions from the point of view of the end of the main chain. It takes
func fetchTxStoreMain(db btcdb.Db, txSet map[btcwire.ShaHash]bool) TxStore { // a flag which specifies whether or not fully spent transaction should be
// included in the results.
func fetchTxStoreMain(db btcdb.Db, txSet map[btcwire.ShaHash]bool, includeSpent bool) TxStore {
// Just return an empty store now if there are no requested hashes. // Just return an empty store now if there are no requested hashes.
txStore := make(TxStore) txStore := make(TxStore)
if len(txSet) == 0 { if len(txSet) == 0 {
@ -120,8 +122,13 @@ func fetchTxStoreMain(db btcdb.Db, txSet map[btcwire.ShaHash]bool) TxStore {
// Ask the database (main chain) for the list of transactions. This // 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 // will return the information from the point of view of the end of the
// main chain. // main chain. Choose whether or not to include fully spent
txReplyList := db.FetchUnSpentTxByShaList(txList) // transactions depending on the passed flag.
fetchFunc := db.FetchUnSpentTxByShaList
if includeSpent {
fetchFunc = db.FetchTxByShaList
}
txReplyList := fetchFunc(txList)
for _, txReply := range txReplyList { for _, txReply := range txReplyList {
// Lookup the existing results entry to modify. Skip // Lookup the existing results entry to modify. Skip
// this reply if there is no corresponding entry in // this reply if there is no corresponding entry in
@ -166,17 +173,22 @@ func (b *BlockChain) fetchTxStore(node *blockNode, txSet map[btcwire.ShaHash]boo
return nil, err return nil, err
} }
// Fetch the requested set from the point of view of the end of the
// main (best) chain.
txStore := fetchTxStoreMain(b.db, txSet)
// If we haven't selected a best chain yet or we are extending the main // If we haven't selected a best chain yet or we are extending the main
// (best) chain with a new block, everything is accurate, so return the // (best) chain with a new block, fetch the requested set from the point
// results now. // 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.
if b.bestChain == nil || (prevNode != nil && prevNode.hash.IsEqual(b.bestChain.hash)) { if b.bestChain == nil || (prevNode != nil && prevNode.hash.IsEqual(b.bestChain.hash)) {
txStore := fetchTxStoreMain(b.db, txSet, false)
return txStore, nil return txStore, nil
} }
// 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)
// The requested node is either on a side chain or is a node on the main // 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 // chain before the end of it. In either case, we need to undo the
// transactions and spend information for the blocks which would be // transactions and spend information for the blocks which would be
@ -310,7 +322,9 @@ func (b *BlockChain) FetchTransactionStore(tx *btcwire.MsgTx) (TxStore, error) {
} }
// Request the input transactions from the point of view of the end of // Request the input transactions from the point of view of the end of
// the main chain. // the main chain without including fully spent trasactions in the
txStore := fetchTxStoreMain(b.db, txNeededSet) // results. Fully spent transactions are only needed for chain
// reorganization which does not apply here.
txStore := fetchTxStoreMain(b.db, txNeededSet, false)
return txStore, nil return txStore, nil
} }