Skip to content

Commit

Permalink
createtransaction should sign the txn but just not bcast it
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdelisle committed Oct 19, 2021
1 parent 0e93511 commit f9bad7a
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 26 deletions.
3 changes: 2 additions & 1 deletion lnd/lntest/mock/walletcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/pkt-cash/pktd/btcutil/util"
"github.com/pkt-cash/pktd/chaincfg"
"github.com/pkt-cash/pktd/chaincfg/chainhash"
"github.com/pkt-cash/pktd/pktwallet/wallet"
"github.com/pkt-cash/pktd/pktwallet/wallet/txauthor"
"github.com/pkt-cash/pktd/pktwallet/wtxmgr"
"github.com/pkt-cash/pktd/wire"
Expand Down Expand Up @@ -87,7 +88,7 @@ func (w *WalletController) SendOutputs(outputs []*wire.TxOut,

// CreateSimpleTx currently returns dummy values.
func (w *WalletController) CreateSimpleTx(outputs []*wire.TxOut,
_ chainfee.SatPerKWeight, _ bool) (*txauthor.AuthoredTx, er.R) {
_ chainfee.SatPerKWeight, _ wallet.SendMode) (*txauthor.AuthoredTx, er.R) {

return nil, nil
}
Expand Down
10 changes: 4 additions & 6 deletions lnd/lnwallet/btcwallet/btcwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/pkt-cash/pktd/lnd/lnwallet/chainfee"
"github.com/pkt-cash/pktd/pktwallet/chain"
"github.com/pkt-cash/pktd/pktwallet/waddrmgr"
"github.com/pkt-cash/pktd/pktwallet/wallet"
base "github.com/pkt-cash/pktd/pktwallet/wallet"
"github.com/pkt-cash/pktd/pktwallet/wallet/txauthor"
"github.com/pkt-cash/pktd/pktwallet/wallet/txrules"
Expand Down Expand Up @@ -315,7 +316,7 @@ func (b *BtcWallet) SendOutputs(outputs []*wire.TxOut,
Outputs: outputs,
Minconf: minconf,
FeeSatPerKB: feeSatPerKB,
DryRun: false,
SendMode: base.SendModeBcasted,
Label: label,

// TODO(cjd): Maybe change the defaults ?
Expand All @@ -337,14 +338,11 @@ func (b *BtcWallet) SendOutputs(outputs []*wire.TxOut,
// returned. This method also takes the target fee expressed in sat/kw that
// should be used when crafting the transaction.
//
// NOTE: The dryRun argument can be set true to create a tx that doesn't alter
// the database. A tx created with this set to true SHOULD NOT be broadcasted.
//
// NOTE: This method requires the global coin selection lock to be held.
//
// This is a part of the WalletController interface.
func (b *BtcWallet) CreateSimpleTx(outputs []*wire.TxOut,
feeRate chainfee.SatPerKWeight, dryRun bool) (*txauthor.AuthoredTx, er.R) {
feeRate chainfee.SatPerKWeight, sendMode wallet.SendMode) (*txauthor.AuthoredTx, er.R) {

// The fee rate is passed in using units of sat/kw, so we'll convert
// this to sat/KB as the CreateSimpleTx method requires this unit.
Expand Down Expand Up @@ -372,7 +370,7 @@ func (b *BtcWallet) CreateSimpleTx(outputs []*wire.TxOut,
Outputs: outputs,
Minconf: 1,
FeeSatPerKB: feeSatPerKB,
DryRun: dryRun,
SendMode: sendMode,
Label: "",

// TODO(cjd): Maybe change the defaults ?
Expand Down
7 changes: 2 additions & 5 deletions lnd/lnwallet/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/pkt-cash/pktd/chaincfg/chainhash"
"github.com/pkt-cash/pktd/lnd/input"
"github.com/pkt-cash/pktd/lnd/lnwallet/chainfee"
"github.com/pkt-cash/pktd/pktwallet/wallet"
"github.com/pkt-cash/pktd/pktwallet/wallet/txauthor"
"github.com/pkt-cash/pktd/pktwallet/wtxmgr"
"github.com/pkt-cash/pktd/wire"
Expand Down Expand Up @@ -195,13 +196,9 @@ type WalletController interface {
// the target fee expressed in sat/kw that should be used when crafting
// the transaction.
//
// NOTE: The dryRun argument can be set true to create a tx that
// doesn't alter the database. A tx created with this set to true
// SHOULD NOT be broadcasted.
//
// NOTE: This method requires the global coin selection lock to be held.
CreateSimpleTx(outputs []*wire.TxOut, feeRate chainfee.SatPerKWeight,
dryRun bool) (*txauthor.AuthoredTx, er.R)
sendMode wallet.SendMode) (*txauthor.AuthoredTx, er.R)

// ListUnspentWitness returns all unspent outputs which are version 0
// witness programs. The 'minconfirms' and 'maxconfirms' parameters
Expand Down
8 changes: 4 additions & 4 deletions lnd/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1154,9 +1154,9 @@ func (r *rpcServer) EstimateFee(ctx context.Context,
// We will ask the wallet to create a tx using this fee rate. We set
// dryRun=true to avoid inflating the change addresses in the db.
var tx *txauthor.AuthoredTx
wallet := r.server.cc.Wallet
err = wallet.WithCoinSelectLock(func() er.R {
tx, err = wallet.CreateSimpleTx(outputs, feePerKw, true)
walletx := r.server.cc.Wallet
err = walletx.WithCoinSelectLock(func() er.R {
tx, err = walletx.CreateSimpleTx(outputs, feePerKw, wallet.SendModeUnsigned)
return err
})
if err != nil {
Expand Down Expand Up @@ -6899,4 +6899,4 @@ func (r *rpcServer) StopReSync(ctx context.Context, req *lnrpc.StopReSyncRequest
return &lnrpc.StopReSyncResponse{
Value: msg,
}, nil
}
}
8 changes: 4 additions & 4 deletions pktwallet/rpc/legacyrpc/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,15 +1190,15 @@ func sendOutputs(
fromAddressses *[]string,
minconf int32,
feeSatPerKb btcutil.Amount,
dryRun bool,
sendMode wallet.SendMode,
changeAddress *string,
inputMinHeight int,
maxInputs int,
) (*txauthor.AuthoredTx, er.R) {
req := wallet.CreateTxReq{
Minconf: minconf,
FeeSatPerKB: feeSatPerKb,
DryRun: dryRun,
SendMode: sendMode,
InputMinHeight: inputMinHeight,
MaxInputs: maxInputs,
Label: "",
Expand Down Expand Up @@ -1261,7 +1261,7 @@ func sendPairs(w *wallet.Wallet, amounts map[string]btcutil.Amount,
return "", err
}

tx, err := sendOutputs(w, amounts, vote, fromAddressses, minconf, feeSatPerKb, false, nil, inputMinHeight, maxInputs)
tx, err := sendOutputs(w, amounts, vote, fromAddressses, minconf, feeSatPerKb, wallet.SendModeBcasted, nil, inputMinHeight, maxInputs)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -1361,7 +1361,7 @@ func createTransaction(icmd interface{}, w *wallet.Wallet) (interface{}, er.R) {
}

tx, err := sendOutputs(w, amounts, vote, cmd.FromAddresses, minconf,
feeSatPerKb, true, cmd.ChangeAddress, inputMinHeight, maxInputs)
feeSatPerKb, wallet.SendModeSigned, cmd.ChangeAddress, inputMinHeight, maxInputs)
if err != nil {
return "", err
}
Expand Down
6 changes: 5 additions & 1 deletion pktwallet/wallet/createtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (w *Wallet) txToOutputs(txr CreateTxReq) (tx *txauthor.AuthoredTx, err er.R
// scripts, and don't commit the database transaction. The DB will be
// rolled back when this method returns to ensure the dry run didn't
// alter the DB in any way.
if txr.DryRun {
if txr.SendMode == SendModeUnsigned {
return tx, nil
}

Expand All @@ -245,6 +245,10 @@ func (w *Wallet) txToOutputs(txr CreateTxReq) (tx *txauthor.AuthoredTx, err er.R
return nil, err
}

if txr.SendMode != SendModeBcasted {
return tx, nil
}

if err := dbtx.Commit(); err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pktwallet/wallet/createtx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestTxToOutputsDryRun(t *testing.T) {
Outputs: txOuts,
Minconf: 1,
FeeSatPerKB: 1000,
DryRun: true,
SendMode: SendModeSigned,
ChangeAddress: nil,
InputMinHeight: 0,
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestTxToOutputsDryRun(t *testing.T) {

// Now we do a proper, non-dry run. This should add a change address
// to the database.
txr.DryRun = false
txr.SendMode = SendModeBcasted
tx, err := w.txToOutputs(txr)
if err != nil {
t.Fatalf("unable to author tx: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion pktwallet/wallet/psbt.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (w *Wallet) FundPsbt(packet *psbt.Packet, account uint32,
FeeSatPerKB: feeSatPerKB,
Minconf: 1,
Outputs: packet.UnsignedTx.TxOut,
DryRun: false,
SendMode: SendModeBcasted,
})
if err != nil {
return 0, er.Errorf("error creating funding TX: %v",
Expand Down
11 changes: 9 additions & 2 deletions pktwallet/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,12 +619,13 @@ func getBlockStamp(chainClient chain.Interface, height int32) (*waddrmgr.BlockSt
}

type (
SendMode uint8
CreateTxReq struct {
InputAddresses *[]btcutil.Address
Outputs []*wire.TxOut
Minconf int32
FeeSatPerKB btcutil.Amount
DryRun bool
SendMode SendMode
ChangeAddress *btcutil.Address
InputMinHeight int
InputComparator utils.Comparator
Expand All @@ -641,6 +642,12 @@ type (
}
)

const (
SendModeUnsigned SendMode = 0
SendModeSigned SendMode = 1
SendModeBcasted SendMode = 2
)

// txCreator is responsible for the input selection and creation of
// transactions. These functions are the responsibility of this method
// (designed to be run as its own goroutine) since input selection must be
Expand Down Expand Up @@ -2217,7 +2224,7 @@ func (w *Wallet) SendOutputs(txr CreateTxReq) (*txauthor.AuthoredTx, er.R) {
if err != nil {
return nil, err
}
if txr.DryRun {
if txr.SendMode != SendModeBcasted {
return createdTx, nil
}

Expand Down

0 comments on commit f9bad7a

Please sign in to comment.