Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mempool: Accept test mungers for create signed tx. #1576

Merged
merged 1 commit into from
Jan 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions mempool/mempool_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) 2016 The btcsuite developers
// Copyright (c) 2017 The Decred developers
// Copyright (c) 2017-2019 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

Expand Down Expand Up @@ -283,7 +283,12 @@ func (p *poolHarness) CreateCoinbaseTx(blockHeight int64, numOutputs uint32) (*d
// inputs and generates the provided number of outputs by evenly splitting the
// total input amount. All outputs will be to the payment script associated
// with the harness and all inputs are assumed to do the same.
func (p *poolHarness) CreateSignedTx(inputs []spendableOutput, numOutputs uint32, expiry uint32) (*dcrutil.Tx, error) {
//
// Additionally, if one or more munge functions are specified, they will be
// invoked with the transaction prior to signing it. This provides callers with
// the opportunity to modify the transaction which is especially useful for
// testing.
func (p *poolHarness) CreateSignedTx(inputs []spendableOutput, numOutputs uint32, mungers ...func(*wire.MsgTx)) (*dcrutil.Tx, error) {
// Calculate the total input amount and split it amongst the requested
// number of outputs.
var totalInput dcrutil.Amount
Expand All @@ -294,12 +299,13 @@ func (p *poolHarness) CreateSignedTx(inputs []spendableOutput, numOutputs uint32
remainder := int64(totalInput) - amountPerOutput*int64(numOutputs)

tx := wire.NewMsgTx()
tx.Expiry = expiry
tx.Expiry = wire.NoExpiryValue
for _, input := range inputs {
tx.AddTxIn(&wire.TxIn{
PreviousOutPoint: input.outPoint,
SignatureScript: nil,
Sequence: wire.MaxTxInSequenceNum,
ValueIn: int64(input.amount),
})
}
for i := uint32(0); i < numOutputs; i++ {
Expand All @@ -315,6 +321,11 @@ func (p *poolHarness) CreateSignedTx(inputs []spendableOutput, numOutputs uint32
})
}

// Perform any transaction munging just before signing.
for _, f := range mungers {
f(tx)
}

// Sign the new transaction.
for i := range tx.TxIn {
sigScript, err := txscript.SignatureScript(tx, i, p.payScript,
Expand Down Expand Up @@ -1066,7 +1077,7 @@ func TestExpirationPruning(t *testing.T) {
// These outputs will be used as inputs to transactions with expirations.
const numTxns = 5
multiOutputTx, err := harness.CreateSignedTx([]spendableOutput{outputs[0]},
numTxns, wire.NoExpiryValue)
numTxns)
if err != nil {
t.Fatalf("unable to create signed tx: %v", err)
}
Expand All @@ -1089,7 +1100,9 @@ func TestExpirationPruning(t *testing.T) {
for i := 0; i < numTxns; i++ {
tx, err := harness.CreateSignedTx([]spendableOutput{
txOutToSpendableOut(multiOutputTx, uint32(i), wire.TxTreeRegular),
}, 1, uint32(nextBlockHeight+int64(i)+1))
}, 1, func(tx *wire.MsgTx) {
tx.Expiry = uint32(nextBlockHeight + int64(i) + 1)
})
if err != nil {
t.Fatalf("unable to create signed tx: %v", err)
}
Expand Down Expand Up @@ -1175,7 +1188,9 @@ func TestBasicOrphanRemoval(t *testing.T) {
nonChainedOrphanTx, err := harness.CreateSignedTx([]spendableOutput{{
amount: dcrutil.Amount(5000000000),
outPoint: wire.OutPoint{Hash: chainhash.Hash{}, Index: 0},
}}, 1, uint32(harness.chain.BestHeight()+1))
}}, 1, func(tx *wire.MsgTx) {
tx.Expiry = uint32(harness.chain.BestHeight() + 1)
})
if err != nil {
t.Fatalf("unable to create signed tx: %v", err)
}
Expand Down Expand Up @@ -1311,7 +1326,7 @@ func TestMultiInputOrphanDoubleSpend(t *testing.T) {
doubleSpendTx, err := harness.CreateSignedTx([]spendableOutput{
txOutToSpendableOut(chainedTxns[1], 0, wire.TxTreeRegular),
txOutToSpendableOut(chainedTxns[maxOrphans], 0, wire.TxTreeRegular),
}, 1, wire.NoExpiryValue)
}, 1)
if err != nil {
t.Fatalf("unable to create signed tx: %v", err)
}
Expand Down