Skip to content

Commit

Permalink
improve gettxout usages
Browse files Browse the repository at this point in the history
  • Loading branch information
itswisdomagain committed Mar 5, 2021
1 parent e2fee6f commit 49aa398
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions client/asset/dcr/dcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ func (dcr *ExchangeWallet) feeRate(confTarget uint64) (uint64, error) {
if err != nil {
return 0, translateRPCCancelErr(err)
}
atomsPerKB, err := dcrutil.NewAmount(estimateFeeResult.FeeRate) // satPerKB is 0 when err != nil
atomsPerKB, err := dcrutil.NewAmount(estimateFeeResult.FeeRate) // atomsPerKB is 0 when err != nil
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -1230,7 +1230,7 @@ func (dcr *ExchangeWallet) FundingCoins(ids []dex.Bytes) (asset.Coins, error) {
if !notFound[pt] {
continue
}
txOut, err := dcr.getTxOut(txHash, output.Vout, true)
txOut, err := dcr.node.GetTxOut(dcr.ctx, txHash, output.Vout, output.Tree, true)
if err != nil {
return nil, fmt.Errorf("gettxout error for locked output %v: %w", pt.String(), translateRPCCancelErr(err))
}
Expand Down Expand Up @@ -1499,7 +1499,7 @@ func (dcr *ExchangeWallet) SignMessage(coin asset.Coin, msg dex.Bytes) (pubkeys,
addr = fCoin.addr
} else {
// Check if we can get the address from gettxout.
txOut, err := dcr.getTxOut(op.txHash(), op.vout(), true)
txOut, err := dcr.node.GetTxOut(dcr.ctx, op.txHash(), op.vout(), op.tree, true)
if err == nil && txOut != nil {
addrs := txOut.ScriptPubKey.Addresses
if len(addrs) != 1 {
Expand Down Expand Up @@ -1547,7 +1547,7 @@ func (dcr *ExchangeWallet) AuditContract(coinID, contract dex.Bytes) (*asset.Aud
return nil, fmt.Errorf("error extracting swap addresses: %w", err)
}
// Get the contracts P2SH address from the tx output's pubkey script.
txOut, err := dcr.getTxOut(txHash, vout, true)
txOut, txTree, err := dcr.getTxOut(txHash, vout, true)
if err != nil {
return nil, fmt.Errorf("error finding unspent contract: %w", translateRPCCancelErr(err))
}
Expand Down Expand Up @@ -1581,7 +1581,7 @@ func (dcr *ExchangeWallet) AuditContract(coinID, contract dex.Bytes) (*asset.Aud
contractHash, addr.ScriptAddress())
}
return &asset.AuditInfo{
Coin: newOutput(txHash, vout, toAtoms(txOut.Value), wire.TxTreeRegular),
Coin: newOutput(txHash, vout, toAtoms(txOut.Value), txTree),
Contract: contract,
SecretHash: secretHash,
Recipient: receiver.String(),
Expand Down Expand Up @@ -1957,7 +1957,7 @@ func (dcr *ExchangeWallet) Refund(coinID, contract dex.Bytes) (dex.Bytes, error)
return nil, err
}
// Grab the unspent output to make sure it's good and to get the value.
utxo, err := dcr.getTxOut(txHash, vout, true)
utxo, err := dcr.node.GetTxOut(dcr.ctx, txHash, vout, wire.TxTreeRegular, true)
if err != nil {
return nil, fmt.Errorf("error finding unspent contract: %w", translateRPCCancelErr(err))
}
Expand Down Expand Up @@ -2104,15 +2104,12 @@ func (dcr *ExchangeWallet) ValidateSecret(secret, secretHash []byte) bool {
// first checking for a unspent output, and if not found, searching indexed
// wallet transactions.
func (dcr *ExchangeWallet) Confirmations(ctx context.Context, id dex.Bytes) (confs uint32, spent bool, err error) {
// Could check with gettransaction first, figure out the tree, and look for a
// redeem script with listscripts, but the listunspent entry has all the
// necessary fields already.
txHash, vout, err := decodeCoinID(id)
if err != nil {
return 0, false, err
}
// Check for an unspent output.
txOut, err := dcr.getTxOut(txHash, vout, true)
txOut, _, err := dcr.getTxOut(txHash, vout, true)
if err == nil && txOut != nil {
return uint32(txOut.Confirmations), false, nil
}
Expand Down Expand Up @@ -2246,13 +2243,16 @@ func (dcr *ExchangeWallet) lockedAtoms() (uint64, error) {
}

// getTxOut attempts to find the specified txout from the regular tree and if
// not found in the regular tree, checks the stake tree.
func (dcr *ExchangeWallet) getTxOut(txHash *chainhash.Hash, index uint32, mempool bool) (*chainjson.GetTxOutResult, error) {
txout, err := dcr.node.GetTxOut(dcr.ctx, txHash, index, wire.TxTreeRegular, mempool) // check regular tree first
// not found in the regular tree, checks the stake tree. Also returns the tree
// where the output is found.
func (dcr *ExchangeWallet) getTxOut(txHash *chainhash.Hash, index uint32, mempool bool) (*chainjson.GetTxOutResult, int8, error) {
tree := wire.TxTreeRegular
txout, err := dcr.node.GetTxOut(dcr.ctx, txHash, index, tree, mempool) // check regular tree first
if err == nil && txout == nil {
txout, err = dcr.node.GetTxOut(dcr.ctx, txHash, index, wire.TxTreeStake, mempool) // check stake tree
tree = wire.TxTreeStake
txout, err = dcr.node.GetTxOut(dcr.ctx, txHash, index, tree, mempool) // check stake tree
}
return txout, err
return txout, tree, err
}

// convertCoin converts the asset.Coin to an unspent output.
Expand All @@ -2265,20 +2265,12 @@ func (dcr *ExchangeWallet) convertCoin(coin asset.Coin) (*output, error) {
if err != nil {
return nil, err
}
txOut, err := dcr.getTxOut(txHash, vout, true)
txOut, tree, err := dcr.getTxOut(txHash, vout, true)
if err != nil {
return nil, fmt.Errorf("error finding unspent output %s:%d: %w", txHash, vout, translateRPCCancelErr(err))
}
if txOut == nil {
return nil, asset.CoinNotFoundError
}
pkScript, err := hex.DecodeString(txOut.ScriptPubKey.Hex)
if err != nil {
return nil, err
}
tree := wire.TxTreeRegular
if dexdcr.IsStakePubkeyHashScript(pkScript) || dexdcr.IsStakeScriptHashScript(pkScript) {
tree = wire.TxTreeStake
return nil, asset.CoinNotFoundError // maybe spent
}
return newOutput(txHash, vout, coin.Value(), tree), nil
}
Expand Down

0 comments on commit 49aa398

Please sign in to comment.