Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
itswisdomagain committed May 7, 2021
1 parent 816795e commit 946c23b
Showing 1 changed file with 13 additions and 34 deletions.
47 changes: 13 additions & 34 deletions internal/rpc/jsonrpc/methods.go
Expand Up @@ -2138,76 +2138,55 @@ func (s *Server) getTxOut(ctx context.Context, icmd interface{}) (interface{}, e
return nil, rpcErrorf(dcrjson.ErrRPCInvalidParameter, "Tx tree must be regular or stake")
}

// Check if the transaction is known to wallet.
walletTx, err := wallet.UnstableAPI(w).TxDetails(ctx, txHash)
// Attempt to read the unspent txout info from wallet.
outpoint := wire.OutPoint{Hash: *txHash, Index: cmd.Vout, Tree: cmd.Tree}
utxo, err := w.UnspentOutput(ctx, outpoint, *cmd.IncludeMempool)
if err != nil && !errors.Is(err, errors.NotExist) {
return nil, err
}
if walletTx == nil {
return nil, nil // Tx not found in wallet.
}
if len(walletTx.MsgTx.TxOut) <= int(cmd.Vout) {
return nil, rpcErrorf(dcrjson.ErrRPCInvalidTxVout, "invalid vout %d", cmd.Vout)
if utxo == nil {
return nil, nil // output is spent or does not exist.
}

tx := &walletTx.MsgTx
txTree := wire.TxTreeRegular
if stake.DetermineTxType(tx, true) != stake.TxTypeRegular {
txTree = wire.TxTreeStake
}
if txTree != cmd.Tree {
if utxo.Tree != cmd.Tree {
// Not an error because it is technically possible (though extremely unlikely)
// that the required tx (same hash, different tree) exists on the blockchain.
return nil, nil
}

// Attempt to read the unspent txout info from wallet.
outpoint := wire.OutPoint{Hash: *txHash, Index: cmd.Vout, Tree: cmd.Tree}
walletUnspent, err := w.UnspentOutput(ctx, outpoint, *cmd.IncludeMempool)
if err != nil && !errors.Is(err, errors.NotExist) {
return nil, err
}
if walletUnspent == nil {
return nil, nil // output is spent
}

txout := tx.TxOut[cmd.Vout]
pkScript := txout.PkScript
scriptVersion := txout.Version

// Disassemble script into single line printable format. The
// disassembled string will contain [error] inline if the script
// doesn't fully parse, so ignore the error here.
disbuf, _ := txscript.DisasmString(pkScript)
disbuf, _ := txscript.DisasmString(utxo.PkScript)

// Get further info about the script. Ignore the error here since an
// error means the script couldn't parse and there is no additional
// information about it anyways.
scriptClass, addrs, reqSigs, _ := txscript.ExtractPkScriptAddrs(
scriptVersion, pkScript, s.activeNet, true) // Yes treasury
0, utxo.PkScript, s.activeNet, true) // Yes treasury
addresses := make([]string, len(addrs))
for i, addr := range addrs {
addresses[i] = addr.Address()
}

bestHash, bestHeight := w.MainChainTip(ctx)
var confirmations int64
if walletTx.Block.Height != -1 {
confirmations = int64(1 + bestHeight - walletTx.Block.Height)
if utxo.Block.Height != -1 {
confirmations = int64(confirms(utxo.Block.Height, bestHeight))
}

return &dcrdtypes.GetTxOutResult{
BestBlock: bestHash.String(),
Confirmations: confirmations,
Value: dcrutil.Amount(txout.Value).ToUnit(dcrutil.AmountCoin),
Value: utxo.Amount.ToCoin(),
ScriptPubKey: dcrdtypes.ScriptPubKeyResult{
Asm: disbuf,
Hex: hex.EncodeToString(pkScript),
Hex: hex.EncodeToString(utxo.PkScript),
ReqSigs: int32(reqSigs),
Type: scriptClass.String(),
Addresses: addresses,
},
Coinbase: blockchain.IsCoinBaseTx(tx, false) || blockchain.IsCoinBaseTx(tx, true),
Coinbase: utxo.FromCoinBase,
}, nil
}

Expand Down

0 comments on commit 946c23b

Please sign in to comment.