rpcserver: style related changes to listunspent

This commit is contained in:
Olaoluwa Osuntokun committed 2018-12-11 16:29:11 -08:00
1 parent 2352918abb
commit dc7c59746e
1 file changed
+47 -18
+47 -18
View File
@@ -684,48 +684,67 @@ func determineFeePerKw(feeEstimator lnwallet.FeeEstimator, targetConf int32,
} }
} }
// ListUnspent returns useful information about each unspent output // ListUnspent returns useful information about each unspent output owned by
// owned by the wallet, as reported by the underlying `ListUnspentWitness`; // the wallet, as reported by the underlying `ListUnspentWitness`; the
// the information returned is: outpoint, amount in satoshis, address, // information returned is: outpoint, amount in satoshis, address, address
// address type, scriptPubKey in hex and number of confirmations. // type, scriptPubKey in hex and number of confirmations. The result is
// The result is filtered to contain outputs whose number of confirmations // filtered to contain outputs whose number of confirmations is between a
// is between a minimum and maximum number of confirmations specified by the // minimum and maximum number of confirmations specified by the user, with 0
// user, with 0 meaning unconfirmed. // meaning unconfirmed.
func (r *rpcServer) ListUnspent(ctx context.Context, func (r *rpcServer) ListUnspent(ctx context.Context,
in *lnrpc.ListUnspentRequest) (*lnrpc.ListUnspentResponse, error) { in *lnrpc.ListUnspentRequest) (*lnrpc.ListUnspentResponse, error) {
minConfs := in.MinConfs minConfs := in.MinConfs
maxConfs := in.MaxConfs maxConfs := in.MaxConfs
if minConfs < 0 { switch {
// Ensure that the user didn't attempt to specify a negative number of
// confirmations, as that isn't possible.
case minConfs < 0:
return nil, fmt.Errorf("min confirmations must be >= 0") return nil, fmt.Errorf("min confirmations must be >= 0")
}
if minConfs > maxConfs { // We'll also ensure that the min number of confs is strictly less than
// or equal to the max number of confs for sanity.
case minConfs > maxConfs:
return nil, fmt.Errorf("max confirmations must be >= min " + return nil, fmt.Errorf("max confirmations must be >= min " +
"confirmations") "confirmations")
} }
// With our arguments validated, we'll query the internal wallet for
// the set of UTXOs that match our query.
utxos, err := r.server.cc.wallet.ListUnspentWitness(minConfs, maxConfs) utxos, err := r.server.cc.wallet.ListUnspentWitness(minConfs, maxConfs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
resp := &lnrpc.ListUnspentResponse{Utxos: []*lnrpc.Utxo{}} resp := &lnrpc.ListUnspentResponse{
Utxos: make([]*lnrpc.Utxo, 0, len(utxos)),
}
for _, utxo := range utxos { for _, utxo := range utxos {
// Translate lnwallet address type to gRPC proto address type: // Translate lnwallet address type to the proper gRPC proto
// address type.
var addrType lnrpc.AddressType var addrType lnrpc.AddressType
switch utxo.AddressType { switch utxo.AddressType {
case lnwallet.WitnessPubKey: case lnwallet.WitnessPubKey:
addrType = lnrpc.AddressType_WITNESS_PUBKEY_HASH addrType = lnrpc.AddressType_WITNESS_PUBKEY_HASH
case lnwallet.NestedWitnessPubKey: case lnwallet.NestedWitnessPubKey:
addrType = lnrpc.AddressType_NESTED_PUBKEY_HASH addrType = lnrpc.AddressType_NESTED_PUBKEY_HASH
case lnwallet.UnknownAddressType: case lnwallet.UnknownAddressType:
rpcsLog.Warnf("[listunspent] utxo with address of unknown "+ rpcsLog.Warnf("[listunspent] utxo with address of "+
"type ignored: %v", utxo.OutPoint.String()) "unknown type ignored: %v",
utxo.OutPoint.String())
continue continue
default: default:
return nil, fmt.Errorf("invalid utxo address type") return nil, fmt.Errorf("invalid utxo address type")
} }
// Now that we know we have a proper mapping to an address,
// we'll convert the regular outpoint to an lnrpc variant.
outpoint := &lnrpc.ChannelPoint{ outpoint := &lnrpc.ChannelPoint{
FundingTxid: &lnrpc.ChannelPoint_FundingTxidStr{ FundingTxid: &lnrpc.ChannelPoint_FundingTxidStr{
FundingTxidStr: utxo.OutPoint.Hash.String(), FundingTxidStr: utxo.OutPoint.Hash.String(),
@@ -741,17 +760,26 @@ func (r *rpcServer) ListUnspent(ctx context.Context,
Confirmations: utxo.Confirmations, Confirmations: utxo.Confirmations,
} }
// Finally, we'll attempt to extract the raw address from the
// script so we can display a human friendly address to the end
// user.
_, outAddresses, _, err := txscript.ExtractPkScriptAddrs( _, outAddresses, _, err := txscript.ExtractPkScriptAddrs(
utxo.PkScript, activeNetParams.Params) utxo.PkScript, activeNetParams.Params,
)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// If we can't properly locate a single address, then this was
// an error in our mapping, and we'll return an error back to
// the user.
if len(outAddresses) != 1 { if len(outAddresses) != 1 {
return nil, fmt.Errorf("an output was unexpectedly multisig") return nil, fmt.Errorf("an output was unexpectedly " +
"multisig")
} }
utxoResp.Address = outAddresses[0].String() utxoResp.Address = outAddresses[0].String()
resp.Utxos = append(resp.Utxos, &utxoResp) resp.Utxos = append(resp.Utxos, &utxoResp)
} }
@@ -759,8 +787,9 @@ func (r *rpcServer) ListUnspent(ctx context.Context,
if maxConfs != math.MaxInt32 { if maxConfs != math.MaxInt32 {
maxStr = " max=" + fmt.Sprintf("%d", maxConfs) maxStr = " max=" + fmt.Sprintf("%d", maxConfs)
} }
rpcsLog.Debugf("[listunspent] min=%v%v, generated utxos: %v",
minConfs, maxStr, utxos) rpcsLog.Debugf("[listunspent] min=%v%v, generated utxos: %v", minConfs,
maxStr, utxos)
return resp, nil return resp, nil
} }