chainntnfs: modify GetTestTxidAndScript to generate new P2PKH scripts
In this commit, we modify GetTestTxidAndScript to generate new P2PKH scripts. This is needed to properly test confirmations and spends of unique scripts on-chain within the set of interface-level test cases.
This commit is contained in:
parent
52db5ed682
commit
83c7f204cd
@ -197,7 +197,7 @@ func TestHistoricalConfDetailsTxIndex(t *testing.T) {
|
|||||||
// historical confirmation details using the set of fallback methods when the
|
// historical confirmation details using the set of fallback methods when the
|
||||||
// backend node's txindex is disabled.
|
// backend node's txindex is disabled.
|
||||||
func TestHistoricalConfDetailsNoTxIndex(t *testing.T) {
|
func TestHistoricalConfDetailsNoTxIndex(t *testing.T) {
|
||||||
miner, tearDown := chainntnfs.NewMiner(t, nil, true, 400)
|
miner, tearDown := chainntnfs.NewMiner(t, nil, true, 25)
|
||||||
defer tearDown()
|
defer tearDown()
|
||||||
|
|
||||||
bitcoindConn, cleanUp := chainntnfs.NewBitcoindBackend(
|
bitcoindConn, cleanUp := chainntnfs.NewBitcoindBackend(
|
||||||
|
@ -35,35 +35,44 @@ var (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
NetParams = &chaincfg.RegressionNetParams
|
NetParams = &chaincfg.RegressionNetParams
|
||||||
|
|
||||||
testPrivKey = []byte{
|
|
||||||
0x81, 0xb6, 0x37, 0xd8, 0xfc, 0xd2, 0xc6, 0xda,
|
|
||||||
0x63, 0x59, 0xe6, 0x96, 0x31, 0x13, 0xa1, 0x17,
|
|
||||||
0xd, 0xe7, 0x95, 0xe4, 0xb7, 0x25, 0xb8, 0x4d,
|
|
||||||
0x1e, 0xb, 0x4c, 0xfd, 0x9e, 0xc5, 0x8c, 0xe9,
|
|
||||||
}
|
|
||||||
privKey, pubKey = btcec.PrivKeyFromBytes(btcec.S256(), testPrivKey)
|
|
||||||
addrPk, _ = btcutil.NewAddressPubKey(
|
|
||||||
pubKey.SerializeCompressed(), NetParams,
|
|
||||||
)
|
|
||||||
testAddr = addrPk.AddressPubKeyHash()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetTestTxidAndScript generate a new test transaction and returns its txid and
|
// randPubKeyHashScript generates a P2PKH script that pays to the public key of
|
||||||
// the script of the output being generated.
|
// a randomly-generated private key.
|
||||||
func GetTestTxidAndScript(h *rpctest.Harness) (*chainhash.Hash, []byte, error) {
|
func randPubKeyHashScript() ([]byte, *btcec.PrivateKey, error) {
|
||||||
script, err := txscript.PayToAddrScript(testAddr)
|
privKey, err := btcec.NewPrivateKey(btcec.S256())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
output := &wire.TxOut{Value: 2e8, PkScript: script}
|
pubKeyHash := btcutil.Hash160(privKey.PubKey().SerializeCompressed())
|
||||||
|
addrScript, err := btcutil.NewAddressPubKeyHash(pubKeyHash, NetParams)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
pkScript, err := txscript.PayToAddrScript(addrScript)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return pkScript, privKey, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetTestTxidAndScript generate a new test transaction and returns its txid and
|
||||||
|
// the script of the output being generated.
|
||||||
|
func GetTestTxidAndScript(h *rpctest.Harness) (*chainhash.Hash, []byte, error) {
|
||||||
|
pkScript, _, err := randPubKeyHashScript()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("unable to generate pkScript: %v", err)
|
||||||
|
}
|
||||||
|
output := &wire.TxOut{Value: 2e8, PkScript: pkScript}
|
||||||
txid, err := h.SendOutputs([]*wire.TxOut{output}, 10)
|
txid, err := h.SendOutputs([]*wire.TxOut{output}, 10)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return txid, script, nil
|
return txid, pkScript, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WaitForMempoolTx waits for the txid to be seen in the miner's mempool.
|
// WaitForMempoolTx waits for the txid to be seen in the miner's mempool.
|
||||||
@ -107,16 +116,18 @@ func WaitForMempoolTx(miner *rpctest.Harness, txid *chainhash.Hash) error {
|
|||||||
|
|
||||||
// CreateSpendableOutput creates and returns an output that can be spent later
|
// CreateSpendableOutput creates and returns an output that can be spent later
|
||||||
// on.
|
// on.
|
||||||
func CreateSpendableOutput(t *testing.T, miner *rpctest.Harness) (*wire.OutPoint, []byte) {
|
func CreateSpendableOutput(t *testing.T,
|
||||||
|
miner *rpctest.Harness) (*wire.OutPoint, *wire.TxOut, *btcec.PrivateKey) {
|
||||||
|
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
// Create a transaction that only has one output, the one destined for
|
// Create a transaction that only has one output, the one destined for
|
||||||
// the recipient.
|
// the recipient.
|
||||||
script, err := txscript.PayToAddrScript(testAddr)
|
pkScript, privKey, err := randPubKeyHashScript()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create p2pkh script: %v", err)
|
t.Fatalf("unable to generate pkScript: %v", err)
|
||||||
}
|
}
|
||||||
output := &wire.TxOut{Value: 2e8, PkScript: script}
|
output := &wire.TxOut{Value: 2e8, PkScript: pkScript}
|
||||||
txid, err := miner.SendOutputsWithoutChange([]*wire.TxOut{output}, 10)
|
txid, err := miner.SendOutputsWithoutChange([]*wire.TxOut{output}, 10)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to create tx: %v", err)
|
t.Fatalf("unable to create tx: %v", err)
|
||||||
@ -130,19 +141,22 @@ func CreateSpendableOutput(t *testing.T, miner *rpctest.Harness) (*wire.OutPoint
|
|||||||
t.Fatalf("unable to generate single block: %v", err)
|
t.Fatalf("unable to generate single block: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return wire.NewOutPoint(txid, 0), script
|
return wire.NewOutPoint(txid, 0), output, privKey
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateSpendTx creates a transaction spending the specified output.
|
// CreateSpendTx creates a transaction spending the specified output.
|
||||||
func CreateSpendTx(t *testing.T, outpoint *wire.OutPoint, pkScript []byte) *wire.MsgTx {
|
func CreateSpendTx(t *testing.T, prevOutPoint *wire.OutPoint,
|
||||||
|
prevOutput *wire.TxOut, privKey *btcec.PrivateKey) *wire.MsgTx {
|
||||||
|
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
spendingTx := wire.NewMsgTx(1)
|
spendingTx := wire.NewMsgTx(1)
|
||||||
spendingTx.AddTxIn(&wire.TxIn{PreviousOutPoint: *outpoint})
|
spendingTx.AddTxIn(&wire.TxIn{PreviousOutPoint: *prevOutPoint})
|
||||||
spendingTx.AddTxOut(&wire.TxOut{Value: 1e8, PkScript: pkScript})
|
spendingTx.AddTxOut(&wire.TxOut{Value: 1e8, PkScript: prevOutput.PkScript})
|
||||||
|
|
||||||
sigScript, err := txscript.SignatureScript(
|
sigScript, err := txscript.SignatureScript(
|
||||||
spendingTx, 0, pkScript, txscript.SigHashAll, privKey, true,
|
spendingTx, 0, prevOutput.PkScript, txscript.SigHashAll,
|
||||||
|
privKey, true,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to sign tx: %v", err)
|
t.Fatalf("unable to sign tx: %v", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user