Skip to content
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

legacyrpc: add fundrawtransaction command #1195

Closed
wants to merge 13 commits into from

Conversation

vctt94
Copy link
Member

@vctt94 vctt94 commented Jun 27, 2018

closes #1181

@vctt94 vctt94 changed the title add fundrawtransaction command legacyrpc: add fundrawtransaction command Jun 27, 2018
@vctt94 vctt94 force-pushed the addFundrawtransaction branch 2 times, most recently from bd95579 to 7401a62 Compare July 12, 2018 11:41
@vctt94 vctt94 changed the title legacyrpc: add fundrawtransaction command [wip] legacyrpc: add fundrawtransaction command Jul 12, 2018
@vctt94 vctt94 changed the title [wip] legacyrpc: add fundrawtransaction command legacyrpc: add fundrawtransaction command Jul 12, 2018
@vctt94 vctt94 force-pushed the addFundrawtransaction branch from 6855f06 to 84751bc Compare July 12, 2018 14:33
@@ -31,6 +31,8 @@ import (
"github.com/decred/dcrwallet/wallet"
"github.com/decred/dcrwallet/wallet/txrules"
"github.com/decred/dcrwallet/wallet/udb"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there should be no grpc imports in the json-rpc implementation (and likewise, no json-rpc code in the grpc server)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@vctt94 vctt94 force-pushed the addFundrawtransaction branch 2 times, most recently from 66220b2 to 2d5208e Compare August 1, 2018 15:50
@vctt94
Copy link
Member Author

vctt94 commented Aug 6, 2018

This PR is ready for another review

@vctt94 vctt94 force-pushed the addFundrawtransaction branch 2 times, most recently from e34ff63 to 5f82c26 Compare September 13, 2018 13:31
if err != nil {
return nil, rpcError(dcrjson.ErrRPCDeserialization, err)
}
err = mtx.Deserialize(bytes.NewReader(decodedTx))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the hex decoding may be done in a single step by using mtx.Deserialize(hex.NewDecoder(strings.NewReader(cmd.HexString)))

}
resp := &dcrjson.FundRawTransactionResult{
Hex: hex.EncodeToString(buf.Bytes()),
Fee: float64(fundedTx.EstimatedSignedSerializeSize) / 1e8,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the correct fee calculation. The fee is totalInputValue - totalOutputValue

}
feePerKb := w.RelayFee()
requiredConfs := int32(1)
addr := ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be of type dcrutil.Address. Address decoding should occur in this RPC handler and not in the wallet method.

wallet/addresses.go Outdated Show resolved Hide resolved
@@ -168,6 +168,70 @@ func (w *Wallet) NewUnsignedTransaction(outputs []*wire.TxOut, relayFeePerKb dcr
return authoredTx, nil
}

func (w *Wallet) FundRawTransaction(inputs []*wire.TxIn, outputs []*wire.TxOut, relayFeePerKb dcrutil.Amount, account uint32, minConf int32,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other fields besides the inputs and outputs that must be preserved by fundrawtransaction (e.g. sertype, version, locktime, and expiry), so we can not just pass in the inputs and outputs of the decoded input and then return an entirely new transaction.

Use a dcrutil.Address for the change address.

@@ -154,6 +154,82 @@ func NewUnsignedTransaction(outputs []*wire.TxOut, relayFeePerKb dcrutil.Amount,
}
}

func FundRawTransaction(inputs []*wire.TxIn, outputs []*wire.TxOut, relayFeePerKb dcrutil.Amount,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, we must pass a MsgTx directly for all tx fields, not just inputs and outputs.

wallet/txauthor/author.go Outdated Show resolved Hide resolved

totalInTransaction := h.SumInputValues(inputs)
targetAmount := h.SumOutputValues(outputs) - totalInTransaction
scriptSizes := []int{txsizes.RedeemP2PKHSigScriptSize}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong. The initial scriptSizes slice should contain all of the sizes of transaction input scripts that were passed to the function. We can not remove any of these inputs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the initial scriptSizes to this plus each input the transaction might be, is that right now?

scriptSizes := []int{txsizes.RedeemP2PKHSigScriptSize}
for range mtx.TxIn {
	scriptSizes = append(scriptSizes, txsizes.RedeemP2PKHInputSize)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, if the inputs already contain scripts, then we should use those sizes. If they are missing, we're stuck since we don't really know what kinds of inputs are being redeemed and how large the input scripts will be once signed. We could guess it to be P2PKH, but that's still a guess and could result in fees being either too high or too low once the transaction is completely signed.

maxSignedSize = txsizes.EstimateSerializeSize(scriptSizes,
unsignedTransaction.TxOut, 0)
}
return &AuthoredTx{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AuthoredTx is the wrong type to return from this function. This is because PrevScripts is not possible to determine if the funded transaction already contained inputs that the wallet did not know about.

return nil, errors.E(op, errors.InsufficientBalance)
}

scriptSizes = []int{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what?

@vctt94 vctt94 force-pushed the addFundrawtransaction branch 2 times, most recently from 01b1872 to fbe0f48 Compare October 16, 2018 15:32
@vctt94
Copy link
Member Author

vctt94 commented Oct 16, 2018

Thanks for reviewing my PR again Jrick.

I fixed your comments and this PR is ready for another review.

"fundrawtransaction-options": "The optional inputs to fund a raw transaction",

// FundRawTransactionOptions
"fundrawtransactionoptions-changeaddress": "Address the change will be send to (default=\"default\")",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default is the empty string

var changeAddr dcrutil.Address

if cmd.Options != nil {
// use provided fee per Kb if specified
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's kilobyte, kB. You will sometimes see KB used in variable names due to camel case

requiredConfs = *cmd.Options.ConfTarget
}
// use provided change account if specified
// use default account otherwise
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change address, not account

type p2PKHAddrChangeSource struct {
persist persistReturnedChildFunc
changeAddress dcrutil.Address
wallet *Wallet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this represents a user-specified address, the persist and wallet fields aren't needed (there's no need to persist this address to the wallet's db).

@@ -160,6 +160,62 @@ func (w *Wallet) NewUnsignedTransaction(outputs []*wire.TxOut, relayFeePerKb dcr
return authoredTx, nil
}

func (w *Wallet) FundRawTransaction(tx wire.MsgTx, relayFeePerKb dcrutil.Amount, account uint32, minConf int32,
changeAddress dcrutil.Address) (*wire.MsgTx, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an exported public API and needs a documentation comment describing what it does. Be sure to document that changeAddress may be nil, and how the behavior changes depending on if it is nil or not.

tx should be passed by pointer.

changeAddress: changeAddress,
wallet: w,
}
fundedTx, err = txauthor.FundRawTransaction(tx, relayFeePerKb,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this call is identical in both branches of the if-else, so it can be moved out. Declare a change source interface variable above the branch and use the branches to set it to either the provided address or a wallet-derived one.

@@ -154,6 +154,75 @@ func NewUnsignedTransaction(outputs []*wire.TxOut, relayFeePerKb dcrutil.Amount,
}
}

func FundRawTransaction(mtx wire.MsgTx, relayFeePerKb dcrutil.Amount,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs doc comment

mtx should be passed by pointer.


inputs := mtx.TxIn
outputs := mtx.TxOut
totalInTransaction := h.SumInputValues(inputs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have to be careful here. If the input amount is not set in any of the inputs (using the sentinel value wire.NullValueIn) this won't be accurate. This has to be checked, and we have to error out if it's not set (it will eventually become required by consensus).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what this wire.NullValuIn is supposed to do. I added a verification checking if the TxIn array is not empty, is that right? if len(inputs) > 0 {


totalInTransaction := h.SumInputValues(inputs)
targetAmount := h.SumOutputValues(outputs) - totalInTransaction
scriptSizes := []int{txsizes.RedeemP2PKHSigScriptSize}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, if the inputs already contain scripts, then we should use those sizes. If they are missing, we're stuck since we don't really know what kinds of inputs are being redeemed and how large the input scripts will be once signed. We could guess it to be P2PKH, but that's still a guess and could result in fees being either too high or too low once the transaction is completely signed.

return nil, errors.E(op, errors.InsufficientBalance)
}

scriptSizes = append(scriptSizes, inputDetail.RedeemScriptSizes...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this appends the discovered inputs to the script sizes, but the code below may continue the loop without actually adding any of the chosen inputs to the transaction

@vctt94 vctt94 force-pushed the addFundrawtransaction branch from e64357d to 2f5d70a Compare October 24, 2018 17:17
@vctt94
Copy link
Member Author

vctt94 commented Oct 30, 2018

I added the fee estimation as we discussed and this PR is ready for another review.

@vctt94 vctt94 force-pushed the addFundrawtransaction branch 2 times, most recently from 8eb4f7b to bad05ae Compare February 26, 2019 14:29
@vctt94 vctt94 force-pushed the addFundrawtransaction branch from bad05ae to b9d5954 Compare February 26, 2019 14:46
@vctt94
Copy link
Member Author

vctt94 commented Feb 26, 2019

rebased and moved fundrawtransaction to jsonrpc/types modules

@jrick
Copy link
Member

jrick commented Mar 25, 2020

Implemented in #1706

@jrick jrick closed this Mar 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement fundrawtransaction JSON-RPC
2 participants