Skip to content

Commit 9fee656

Browse files
committed
chainntnfs+lntest: fix TestInterfaces
This commit upgrades the test to always use a segwit v0 witness program when creating testing txns.
1 parent f27f9f2 commit 9fee656

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

chainntnfs/test_utils.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/btcsuite/btcd/integration/rpctest"
1717
"github.com/btcsuite/btcd/txscript"
1818
"github.com/btcsuite/btcd/wire"
19+
"github.com/lightningnetwork/lnd/input"
1920
"github.com/lightningnetwork/lnd/lntest/unittest"
2021
"github.com/stretchr/testify/require"
2122
)
@@ -36,7 +37,7 @@ func randPubKeyHashScript() ([]byte, *btcec.PrivateKey, error) {
3637
}
3738

3839
pubKeyHash := btcutil.Hash160(privKey.PubKey().SerializeCompressed())
39-
addrScript, err := btcutil.NewAddressPubKeyHash(
40+
addrScript, err := btcutil.NewAddressWitnessPubKeyHash(
4041
pubKeyHash, unittest.NetParams,
4142
)
4243
if err != nil {
@@ -139,16 +140,26 @@ func CreateSpendTx(t *testing.T, prevOutPoint *wire.OutPoint,
139140

140141
t.Helper()
141142

142-
spendingTx := wire.NewMsgTx(1)
143-
spendingTx.AddTxIn(&wire.TxIn{PreviousOutPoint: *prevOutPoint})
144-
spendingTx.AddTxOut(&wire.TxOut{Value: 1e8, PkScript: prevOutput.PkScript})
145-
146-
sigScript, err := txscript.SignatureScript(
147-
spendingTx, 0, prevOutput.PkScript, txscript.SigHashAll,
148-
privKey, true,
143+
// Create a new output.
144+
outputAmt := int64(1e8)
145+
witnessProgram, _, err := randPubKeyHashScript()
146+
require.NoError(t, err, "unable to generate pkScript")
147+
output := wire.NewTxOut(outputAmt, witnessProgram)
148+
149+
// Create a new tx.
150+
tx := wire.NewMsgTx(2)
151+
tx.AddTxIn(wire.NewTxIn(prevOutPoint, nil, nil))
152+
tx.AddTxOut(output)
153+
154+
// Generate the witness.
155+
sigHashes := input.NewTxSigHashesV0Only(tx)
156+
witnessScript, err := txscript.WitnessSignature(
157+
tx, sigHashes, 0, prevOutput.Value, prevOutput.PkScript,
158+
txscript.SigHashAll, privKey, true,
149159
)
150160
require.NoError(t, err, "unable to sign tx")
151-
spendingTx.TxIn[0].SignatureScript = sigScript
152161

153-
return spendingTx
162+
tx.TxIn[0].Witness = witnessScript
163+
164+
return tx
154165
}

lntest/unittest/backend.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ func NewMiner(t *testing.T, netParams *chaincfg.Params, extraArgs []string,
5959
t.Fatalf("unable to set up backend node: %v", err)
6060
}
6161

62+
// Next mine enough blocks in order for segwit and the CSV package
63+
// soft-fork to activate.
64+
numBlocks := netParams.MinerConfirmationWindow*2 + 17
65+
_, err = node.Client.Generate(numBlocks)
66+
require.NoError(t, err, "failed to generate blocks")
67+
6268
return node
6369
}
6470

0 commit comments

Comments
 (0)