btcjson+rpcclient: support new bitcoind sendrawtransaction request

This commit is contained in:
Wilmer Paulino 2019-10-29 18:51:59 -07:00
parent 93a84aa014
commit e2e5cc694d
No known key found for this signature in database
GPG key ID: 6DF57B9F9514972F
3 changed files with 44 additions and 1 deletions

View file

@ -638,6 +638,7 @@ func NewSearchRawTransactionsCmd(address string, verbose, skip, count *int, vinE
type SendRawTransactionCmd struct { type SendRawTransactionCmd struct {
HexTx string HexTx string
AllowHighFees *bool `jsonrpcdefault:"false"` AllowHighFees *bool `jsonrpcdefault:"false"`
MaxFeeRate *int32
} }
// NewSendRawTransactionCmd returns a new instance which can be used to issue a // NewSendRawTransactionCmd returns a new instance which can be used to issue a
@ -652,6 +653,17 @@ func NewSendRawTransactionCmd(hexTx string, allowHighFees *bool) *SendRawTransac
} }
} }
// NewSendRawTransactionCmd returns a new instance which can be used to issue a
// sendrawtransaction JSON-RPC command to a bitcoind node.
//
// A 0 maxFeeRate indicates that a maximum fee rate won't be enforced.
func NewBitcoindSendRawTransactionCmd(hexTx string, maxFeeRate int32) *SendRawTransactionCmd {
return &SendRawTransactionCmd{
HexTx: hexTx,
MaxFeeRate: &maxFeeRate,
}
}
// SetGenerateCmd defines the setgenerate JSON-RPC command. // SetGenerateCmd defines the setgenerate JSON-RPC command.
type SetGenerateCmd struct { type SetGenerateCmd struct {
Generate bool Generate bool

View file

@ -15,6 +15,12 @@ import (
"github.com/btcsuite/btcutil" "github.com/btcsuite/btcutil"
) )
const (
// defaultMaxFeeRate is the default maximum fee rate in sat/KB enforced
// by bitcoind v0.19.0 or after for transaction broadcast.
defaultMaxFeeRate = btcutil.SatoshiPerBitcoin / 10
)
// SigHashType enumerates the available signature hashing types that the // SigHashType enumerates the available signature hashing types that the
// SignRawTransaction function accepts. // SignRawTransaction function accepts.
type SigHashType string type SigHashType string
@ -296,7 +302,31 @@ func (c *Client) SendRawTransactionAsync(tx *wire.MsgTx, allowHighFees bool) Fut
txHex = hex.EncodeToString(buf.Bytes()) txHex = hex.EncodeToString(buf.Bytes())
} }
cmd := btcjson.NewSendRawTransactionCmd(txHex, &allowHighFees) // Due to differences in the sendrawtransaction API for different
// backends, we'll need to inspect our version and construct the
// appropriate request.
version, err := c.BackendVersion()
if err != nil {
return newFutureError(err)
}
var cmd *btcjson.SendRawTransactionCmd
switch version {
// Starting from bitcoind v0.19.0, the MaxFeeRate field should be used.
case BitcoindPost19:
// Using a 0 MaxFeeRate is interpreted as a maximum fee rate not
// being enforced by bitcoind.
var maxFeeRate int32
if !allowHighFees {
maxFeeRate = defaultMaxFeeRate
}
cmd = btcjson.NewBitcoindSendRawTransactionCmd(txHex, maxFeeRate)
// Otherwise, use the AllowHighFees field.
default:
cmd = btcjson.NewSendRawTransactionCmd(txHex, &allowHighFees)
}
return c.sendCmd(cmd) return c.sendCmd(cmd)
} }

View file

@ -552,6 +552,7 @@ var helpDescsEnUS = map[string]string{
"sendrawtransaction--synopsis": "Submits the serialized, hex-encoded transaction to the local peer and relays it to the network.", "sendrawtransaction--synopsis": "Submits the serialized, hex-encoded transaction to the local peer and relays it to the network.",
"sendrawtransaction-hextx": "Serialized, hex-encoded signed transaction", "sendrawtransaction-hextx": "Serialized, hex-encoded signed transaction",
"sendrawtransaction-allowhighfees": "Whether or not to allow insanely high fees (btcd does not yet implement this parameter, so it has no effect)", "sendrawtransaction-allowhighfees": "Whether or not to allow insanely high fees (btcd does not yet implement this parameter, so it has no effect)",
"sendrawtransaction-maxfeerate": "Used by bitcoind on or after v0.19.0",
"sendrawtransaction--result0": "The hash of the transaction", "sendrawtransaction--result0": "The hash of the transaction",
// SetGenerateCmd help. // SetGenerateCmd help.