Clear disconnected transactions.

Rather than removing disconnected transactions from the node viewpoint
transaction store, clear the entry instead.  This is needed for the
connect code to update transactions that were removed on the other side of
a chain fork.
This commit is contained in:
Dave Collins 2013-08-29 08:56:27 -05:00
parent 117765ba7c
commit 4ac899e26f

View file

@ -60,22 +60,31 @@ func connectTransactions(txStore map[btcwire.ShaHash]*txData, block *btcutil.Blo
// transactions in the passed map are updated. // transactions in the passed map are updated.
func disconnectTransactions(txStore map[btcwire.ShaHash]*txData, block *btcutil.Block) error { func disconnectTransactions(txStore map[btcwire.ShaHash]*txData, block *btcutil.Block) error {
// Loop through all of the transactions in the block to see if any of // Loop through all of the transactions in the block to see if any of
// them are ones were need to undo based on the results map. // them are ones that need to be undone based on the transaction store.
for i, tx := range block.MsgBlock().Transactions { for i, tx := range block.MsgBlock().Transactions {
txHash, err := block.TxSha(i) txHash, err := block.TxSha(i)
if err != nil { if err != nil {
return err return err
} }
// Remove this transaction from the transaction store (this is a // Clear this transaction from the transaction store if needed.
// no-op if it's not there). // Only clear it rather than deleting it because the transaction
delete(txStore, *txHash) // 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.
if txD, exists := txStore[*txHash]; exists {
txD.tx = nil
txD.blockHeight = 0
txD.spent = nil
txD.err = btcdb.TxShaMissing
}
// Unspend the origin transaction output. // Unspend the origin transaction output.
for _, txIn := range tx.TxIn { for _, txIn := range tx.TxIn {
originHash := &txIn.PreviousOutpoint.Hash originHash := &txIn.PreviousOutpoint.Hash
originIndex := txIn.PreviousOutpoint.Index originIndex := txIn.PreviousOutpoint.Index
if originTx, exists := txStore[*originHash]; exists { originTx, exists := txStore[*originHash]
if exists && originTx.tx != nil && originTx.err == nil {
originTx.spent[originIndex] = false originTx.spent[originIndex] = false
} }
} }