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 tickets. #2721

Merged
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
45 changes: 28 additions & 17 deletions internal/mempool/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,40 +527,44 @@ func (p *poolHarness) CreateTx(out spendableOutput) (*dcrutil.Tx, error) {
return txns[0], err
}

// CreateTicketPurchase creates a ticket purchase spending the first output of
// the provided transaction.
func (p *poolHarness) CreateTicketPurchase(sourceTx *dcrutil.Tx, cost int64) (*dcrutil.Tx, error) {
// CreateTicketPurchase creates a ticket purchase from the provided spendable
// output.
func (p *poolHarness) CreateTicketPurchase(input spendableOutput, cost int64, mungers ...func(*wire.MsgTx)) (*dcrutil.Tx, error) {
ticketFee := singleInputTicketSize
ticketPrice := cost

// Generate the voting rights, commitment, and change scripts of the ticket.
voteScriptVer, voteScript := p.payAddr.VotingRightsScript()
commitScriptVer, commitScript := p.payAddr.RewardCommitmentScript(
ticketPrice+ticketFee, 0, ticketPrice)
change := sourceTx.MsgTx().TxOut[0].Value - ticketPrice - ticketFee
change := int64(input.amount) - ticketPrice - ticketFee
changeScriptVer, changeScript := p.payAddr.StakeChangeScript()

// Generate the ticket purchase.
tx := wire.NewMsgTx()
tx.AddTxIn(&wire.TxIn{
PreviousOutPoint: wire.OutPoint{
Hash: *sourceTx.Hash(),
Hash: input.outPoint.Hash,
Index: 0,
Tree: wire.TxTreeRegular,
},
Sequence: wire.MaxTxInSequenceNum,
ValueIn: sourceTx.MsgTx().TxOut[0].Value,
ValueIn: int64(input.amount),
BlockHeight: uint32(p.chain.BestHeight()),
})

tx.AddTxOut(newTxOut(ticketPrice, voteScriptVer, voteScript))
tx.AddTxOut(newTxOut(0, commitScriptVer, commitScript))
tx.AddTxOut(newTxOut(change, changeScriptVer, changeScript))

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

// Sign the ticket purchase.
sigScript, err := sign.SignatureScript(tx, 0,
sourceTx.MsgTx().TxOut[0].PkScript, txscript.SigHashAll, p.signKey,
dcrec.STEcdsaSecp256k1, true)
sigScript, err := sign.SignatureScript(tx, 0, p.payScript,
txscript.SigHashAll, p.signKey, dcrec.STEcdsaSecp256k1, true)
if err != nil {
return nil, err
}
Expand All @@ -569,6 +573,13 @@ func (p *poolHarness) CreateTicketPurchase(sourceTx *dcrutil.Tx, cost int64) (*d
return dcrutil.NewTx(tx), nil
}

// CreateTicketPurchaseFromTx creates a ticket purchase spending the first
// output of the provided transaction.
func (p *poolHarness) CreateTicketPurchaseFromTx(sourceTx *dcrutil.Tx, cost int64) (*dcrutil.Tx, error) {
spend := txOutToSpendableOut(sourceTx, 0, wire.TxTreeRegular)
return p.CreateTicketPurchase(spend, cost)
}

// newVoteScript generates a voting script from the passed VoteBits, for
// use in a vote.
func newVoteScript(voteBits stake.VoteBits) ([]byte, error) {
Expand Down Expand Up @@ -973,7 +984,7 @@ func TestTicketPurchaseOrphan(t *testing.T) {

// Create a ticket purchase transaction spending the outputs of the
// prior regular transaction.
ticket, err := harness.CreateTicketPurchase(tx, 40000)
ticket, err := harness.CreateTicketPurchaseFromTx(tx, 40000)
if err != nil {
t.Fatalf("unable to create ticket purchase transaction %v", err)
}
Expand Down Expand Up @@ -1047,7 +1058,7 @@ func TestVoteOrphan(t *testing.T) {

// Create a ticket purchase transaction spending the outputs of the
// prior regular transaction.
ticket, err := harness.CreateTicketPurchase(tx, 40000)
ticket, err := harness.CreateTicketPurchaseFromTx(tx, 40000)
if err != nil {
t.Fatalf("unable to create ticket purchase transaction: %v", err)
}
Expand Down Expand Up @@ -1117,7 +1128,7 @@ func TestRevocationOrphan(t *testing.T) {

// Create a ticket purchase transaction spending the outputs of the
// prior regular transaction.
ticket, err := harness.CreateTicketPurchase(tx, 40000)
ticket, err := harness.CreateTicketPurchaseFromTx(tx, 40000)
if err != nil {
t.Fatalf("unable to create ticket purchase transaction: %v", err)
}
Expand Down Expand Up @@ -1863,7 +1874,7 @@ func TestMaxVoteDoubleSpendRejection(t *testing.T) {

// Create a ticket purchase transaction spending the outputs of the prior
// regular transaction.
ticket, err := harness.CreateTicketPurchase(tx, 40000)
ticket, err := harness.CreateTicketPurchaseFromTx(tx, 40000)
if err != nil {
t.Fatalf("unable to create ticket purchase transaction: %v", err)
}
Expand Down Expand Up @@ -1988,7 +1999,7 @@ func TestDuplicateVoteRejection(t *testing.T) {

// Create a ticket purchase transaction spending the outputs of the prior
// regular transaction.
ticket, err := harness.CreateTicketPurchase(tx, 40000)
ticket, err := harness.CreateTicketPurchaseFromTx(tx, 40000)
if err != nil {
t.Fatalf("unable to create ticket purchase transaction: %v", err)
}
Expand Down Expand Up @@ -2186,7 +2197,7 @@ func TestFetchTransaction(t *testing.T) {

// Create a ticket purchase transaction spending the outputs of the
// prior regular transaction.
ticket, err := harness.CreateTicketPurchase(tx, 40000)
ticket, err := harness.CreateTicketPurchaseFromTx(tx, 40000)
if err != nil {
t.Fatalf("unable to create ticket purchase transaction %v", err)
}
Expand Down Expand Up @@ -2248,7 +2259,7 @@ func TestRemoveDoubleSpends(t *testing.T) {

// Create a ticket purchase transaction spending the outputs of the
// base regular transaction.
ticket, err := harness.CreateTicketPurchase(baseTx, 40000)
ticket, err := harness.CreateTicketPurchaseFromTx(baseTx, 40000)
if err != nil {
t.Fatalf("unable to create ticket purchase transaction %v", err)
}
Expand Down Expand Up @@ -2683,7 +2694,7 @@ func TestStagedTransactionHeight(t *testing.T) {
spendableOuts[0],
}, 1)

ticket, err := harness.CreateTicketPurchase(txA, 40000)
ticket, err := harness.CreateTicketPurchaseFromTx(txA, 40000)
if err != nil {
t.Fatalf("unable to create ticket purchase transaction %v", err)
}
Expand Down