Skip to content

Commit

Permalink
changes for review from guggero
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarnemagnussen committed Nov 24, 2021
1 parent 03046ed commit 82c95a7
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2385,15 +2385,18 @@ type GetTransactionResult struct {
// transaction is returned in a Block structure which records properties about
// the block. A bool is also returned to denote if the transaction previously
// existed in the database or not.
// The backend must have a transaction index to poll a confirmed transaction
// that does not exist in the internal wallet.
func (w *Wallet) GetTransaction(txHash *chainhash.Hash) (*GetTransactionResult,
bool, error) {

var res GetTransactionResult
chainClient := w.chainClient
if chainClient == nil {
return nil, false, errors.New("no chain server client")
chainClient, err := w.requireChainClient()
if err != nil {
return nil, false, err
}

var res GetTransactionResult

// In case the transaction is unconfirmed it could due to race condition
// confirm during the time we receive it from the backend and check it
// in the database.
Expand All @@ -2406,17 +2409,26 @@ func (w *Wallet) GetTransaction(txHash *chainhash.Hash) (*GetTransactionResult,

// Get the transaction information from directly calling the backend
// endpoint.
var backendErr error
var txResult *btcjson.TxRawResult
switch client := chainClient.(type) {
case *chain.RPCClient:
txResult, err = client.GetRawTransactionVerbose(txHash)
if err != nil {
return nil, false, err
// If the backend returned an error we will still try to
// find the transaction in the database, but return the
// error if it was also not found there.
backendErr = err
bestHeight = 0
}
case *chain.BitcoindClient:
txResult, err = client.GetRawTransactionVerbose(txHash)
if err != nil {
return nil, false, err
// If the backend returned an error we will still try to
// find the transaction in the database, but return the
// error if it was also not found there.
backendErr = err
bestHeight = 0
}
case *chain.NeutrinoClient:
return nil, false, errors.New("not supported with neutrino client")
Expand All @@ -2430,20 +2442,8 @@ func (w *Wallet) GetTransaction(txHash *chainhash.Hash) (*GetTransactionResult,
// db can be queried directly without this.
var blockHash *chainhash.Hash
var height int32 = -1
if txResult.BlockHash != "" {
blockHashBytes, err := hex.DecodeString(txResult.BlockHash)
if err != nil {
return nil, false, err
}

// The byte is reversed due to different endianness in response
// and call.
reversed := make([]byte, len(blockHashBytes))
for i, n := range blockHashBytes {
j := len(blockHashBytes) - i - 1
reversed[j] = n
}
blockHash, err = chainhash.NewHash(reversed)
if txResult != nil && txResult.BlockHash != "" {
blockHash, err = chainhash.NewHashFromStr(txResult.BlockHash)
if err != nil {
return nil, false, err
}
Expand All @@ -2461,11 +2461,6 @@ func (w *Wallet) GetTransaction(txHash *chainhash.Hash) (*GetTransactionResult,
if err != nil {
return nil, false, err
}
case *chain.NeutrinoClient:
height, err = client.GetBlockHeight(blockHash)
if err != nil {
return nil, false, err
}
}

// We know that the transaction definitively is in a specific
Expand Down Expand Up @@ -2502,8 +2497,9 @@ func (w *Wallet) GetTransaction(txHash *chainhash.Hash) (*GetTransactionResult,
return false, nil
}

return w.TxStore.RangeTransactions(txmgrNs, bestHeight,
height, rangeFn)
return w.TxStore.RangeTransactions(
txmgrNs, bestHeight, height, rangeFn,
)
})
if err != nil {
return nil, false, err
Expand All @@ -2512,6 +2508,12 @@ func (w *Wallet) GetTransaction(txHash *chainhash.Hash) (*GetTransactionResult,
// We need to build the transaction to return as the response if it was
// not found in the database.
if summary == nil {
// Return the backend error if the transaction was not found in
// the database.
if backendErr != nil {
return nil, false, backendErr
}

txRaw, err := hex.DecodeString(txResult.Hex)
if err != nil {
return nil, false, err
Expand Down

0 comments on commit 82c95a7

Please sign in to comment.