Skip to content

Commit

Permalink
fix block height and hash for unconfirmed transations in store
Browse files Browse the repository at this point in the history
The Transaction Store sets block hash to `0...0` for unconfirmed transaction instead of `nil`. This code change makes sure not to override the nil value of `blockHash` in that case.
  • Loading branch information
bjarnemagnussen committed Nov 29, 2021
1 parent 82c95a7 commit 0eb3f9c
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2441,7 +2441,7 @@ func (w *Wallet) GetTransaction(txHash *chainhash.Hash) (*GetTransactionResult,
// because not all block headers are saved but when they are for SPV the
// db can be queried directly without this.
var blockHash *chainhash.Hash
var height int32 = -1
var endHeight int32 = -1
if txResult != nil && txResult.BlockHash != "" {
blockHash, err = chainhash.NewHashFromStr(txResult.BlockHash)
if err != nil {
Expand All @@ -2455,17 +2455,17 @@ func (w *Wallet) GetTransaction(txHash *chainhash.Hash) (*GetTransactionResult,
if err != nil {
return nil, false, err
}
height = header.Height
bestHeight = header.Height
case *chain.BitcoindClient:
height, err = client.GetBlockHeight(blockHash)
bestHeight, err = client.GetBlockHeight(blockHash)
if err != nil {
return nil, false, err
}
}

// We know that the transaction definitively is in a specific
// block height and set both start and end height to it.
bestHeight = height
endHeight = bestHeight
}

// Populate with additional data if this transaction exists in the
Expand All @@ -2489,16 +2489,18 @@ func (w *Wallet) GetTransaction(txHash *chainhash.Hash) (*GetTransactionResult,
// transaction and can return early.
if txHash.IsEqual(dbTx.Hash) {
summary, inDB = &dbTx, true
height = details[i].Block.Height
blockHash = &details[i].Block.Hash
if details[i].Block.Height != -1 {
bestHeight = details[i].Block.Height
blockHash = &details[i].Block.Hash
}
return true, nil
}
}
return false, nil
}

return w.TxStore.RangeTransactions(
txmgrNs, bestHeight, height, rangeFn,
txmgrNs, bestHeight, endHeight, rangeFn,
)
})
if err != nil {
Expand Down Expand Up @@ -2535,7 +2537,7 @@ func (w *Wallet) GetTransaction(txHash *chainhash.Hash) (*GetTransactionResult,
default:
res.MinedTransaction = &Block{
Hash: blockHash,
Height: height,
Height: bestHeight,
Timestamp: summary.Timestamp,
Transactions: []TransactionSummary{*summary},
}
Expand Down

0 comments on commit 0eb3f9c

Please sign in to comment.