Skip to content

Commit

Permalink
mempool: Accept test mungers for create signed tx.
Browse files Browse the repository at this point in the history
This modifies CreateSignedTx in the mempool test harness to accept munge
functions similar to how NextBlock in chaingen works and updates the
callers accordingly.  This allows the test code to more easily mutate
the transactions as desired prior to them being signed which provides
greater flexibility.

While here, it also sets the input amount to the proper value in the
generated transaction.  While there is not currently anything in the
tests that relies on that value being accurate, it is more desirable to
create transactions in the test code which are fully valid in case they
end up being used where it does matter in future tests.
  • Loading branch information
davecgh committed Jan 26, 2019
1 parent 6088b18 commit df84fbd
Showing 1 changed file with 22 additions and 7 deletions.
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

0 comments on commit df84fbd

Please sign in to comment.