chainntnfs+lntest: fix TestInterfaces

This commit upgrades the test to always use a segwit v0 witness program
when creating testing txns.
This commit is contained in:
yyforyongyu 2024-07-19 14:05:28 +08:00
parent f27f9f2799
commit 9fee656d70
No known key found for this signature in database
GPG key ID: 9BCD95C4FF296868
2 changed files with 26 additions and 9 deletions

View file

@ -16,6 +16,7 @@ import (
"github.com/btcsuite/btcd/integration/rpctest"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lntest/unittest"
"github.com/stretchr/testify/require"
)
@ -36,7 +37,7 @@ func randPubKeyHashScript() ([]byte, *btcec.PrivateKey, error) {
}
pubKeyHash := btcutil.Hash160(privKey.PubKey().SerializeCompressed())
addrScript, err := btcutil.NewAddressPubKeyHash(
addrScript, err := btcutil.NewAddressWitnessPubKeyHash(
pubKeyHash, unittest.NetParams,
)
if err != nil {
@ -139,16 +140,26 @@ func CreateSpendTx(t *testing.T, prevOutPoint *wire.OutPoint,
t.Helper()
spendingTx := wire.NewMsgTx(1)
spendingTx.AddTxIn(&wire.TxIn{PreviousOutPoint: *prevOutPoint})
spendingTx.AddTxOut(&wire.TxOut{Value: 1e8, PkScript: prevOutput.PkScript})
// Create a new output.
outputAmt := int64(1e8)
witnessProgram, _, err := randPubKeyHashScript()
require.NoError(t, err, "unable to generate pkScript")
output := wire.NewTxOut(outputAmt, witnessProgram)
sigScript, err := txscript.SignatureScript(
spendingTx, 0, prevOutput.PkScript, txscript.SigHashAll,
privKey, true,
// Create a new tx.
tx := wire.NewMsgTx(2)
tx.AddTxIn(wire.NewTxIn(prevOutPoint, nil, nil))
tx.AddTxOut(output)
// Generate the witness.
sigHashes := input.NewTxSigHashesV0Only(tx)
witnessScript, err := txscript.WitnessSignature(
tx, sigHashes, 0, prevOutput.Value, prevOutput.PkScript,
txscript.SigHashAll, privKey, true,
)
require.NoError(t, err, "unable to sign tx")
spendingTx.TxIn[0].SignatureScript = sigScript
return spendingTx
tx.TxIn[0].Witness = witnessScript
return tx
}

View file

@ -59,6 +59,12 @@ func NewMiner(t *testing.T, netParams *chaincfg.Params, extraArgs []string,
t.Fatalf("unable to set up backend node: %v", err)
}
// Next mine enough blocks in order for segwit and the CSV package
// soft-fork to activate.
numBlocks := netParams.MinerConfirmationWindow*2 + 17
_, err = node.Client.Generate(numBlocks)
require.NoError(t, err, "failed to generate blocks")
return node
}