-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SelectUnspent JSON-RPC method #2079
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did a first pass review. TBH, I'm not super sure this should live in the wallet package, since you can effectively do this selection by calling the existing ListUnspent
, then using whatever logic you want to select desired outputs.
Do you have a specific reasoning for why this needs to be in the wallet itself?
wallet/wallet.go
Outdated
@@ -3716,6 +3717,276 @@ func (w *Wallet) ListUnspent(ctx context.Context, minconf, maxconf int32, addres | |||
return results, nil | |||
} | |||
|
|||
// SelectUnspent returns a slice of objects representing the unspent wallet | |||
// transactions for the given criteria that are enough to pay the target amount. | |||
// The transaction amount and confirmations will be greater than the amount |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"The output amount [...] will be greater than the minAmount"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minAmount refers to the minimum amount output value of transaction output should have before it is considered
wallet/wallet.go
Outdated
case stake.TxTypeSStx: | ||
// Ticket commitment, only spendable after ticket maturity. | ||
if output.Index == 0 { | ||
if !ticketMatured(w.chainParams, details.Height(), tipHeight) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, if you're going to use the new selectunspent
api to build regular txs, using this output without client-side filtering will break your code (because you can't arbitrarily spend a ticket submission - only votes and revocations can).
Any thoughts on how to handle this (whether clients need to be warned to verify this or have some flag to toggle inclusion of ticket submission outputs)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ticket submissions will be removed by default, I don't think this API will be used to build votes or revocation transactions.
if !coinbaseMatured(w.chainParams, details.Height(), tipHeight) { | ||
continue | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to also check if it's a tspend. OP_GEN outputs also need to pass maturity before being spent.
wallet/wallet.go
Outdated
acct, err := w.manager.AddrAccount( | ||
addrmgrNs, addrs[0]) | ||
if err == nil { | ||
s, err := w.manager.AccountName( | ||
addrmgrNs, acct) | ||
if err == nil { | ||
acctName = s | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Silencing errors in general is not a great idea. Seems you want to specifically handle the case of the address not existing (which as you noted in the comment earlier, shouldn't happen), so you could check for errors.NotExist
directly:
acct, err := w.manager.AddrAccount(addrmgrNs, addrs[0])
if err == nil {
var s string
s, err = w.manager.AccountName(addrmgrNs, acct)
if err == nil {
acctName = s
}
}
if err != nil && !errors.Is(err, errors.NotExist) {
return err
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I also make this change here https://github.com/decred/dcrwallet/blob/master/wallet/wallet.go#L3633-L3641
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd do that as a separate PR.
@matheusd the |
- Shuffle transactions using wallet package's shuffle() and remove redundant sort. - Change log level to trace
This adds the SelectUnspent method to the JSON-RPC server. It is used for fetching unspent wallet transactions that are enough to pay a specified amount. The returned transactions can be filtered using the
minAmount
,minconf
&accountName
parameters. ThetargetAmount
parameter specifies the minimum total amount that should be returned and it'll be ignored if thespendAll
parameter is true.Inputs can be selected using the different methods specified by the
InputSelectionMethod
parameter. These options consist ofRandomInputSelection
that indicates any random utxo can be selected.RandomAddressInputSelection
that indicates only utxos matching a randomly selected address should be selected.OneUTXOInputSelection
indicates only one utxo should be selected(and should be enough to pay targetAmount). As well asUniqueTxInputSelection
that indicates only utxos with a unique address and hash should be selected.