fullblocktests, testhelper: move createCoinbaseTx to testhelper

createCoinbaseTx's code is refactored out and placed in testhelper
package and is exported so that callers in package blockchain can reuse
the code without introducing import cycles.  The test code for
invalidateblock and reconsiderblock that'll be added in later commits
make use of this code.
This commit is contained in:
Calvin Kim 2024-04-02 16:32:28 +09:00
parent 59c7d10507
commit 8ab27b9245
2 changed files with 29 additions and 20 deletions

View File

@ -224,26 +224,8 @@ func pushDataScript(items ...[]byte) []byte {
// subsidy based on the passed block height. The coinbase signature script
// conforms to the requirements of version 2 blocks.
func (g *testGenerator) createCoinbaseTx(blockHeight int32) *wire.MsgTx {
extraNonce := uint64(0)
coinbaseScript, err := testhelper.StandardCoinbaseScript(blockHeight, extraNonce)
if err != nil {
panic(err)
}
tx := wire.NewMsgTx(1)
tx.AddTxIn(&wire.TxIn{
// Coinbase transactions have no inputs, so previous outpoint is
// zero hash and max index.
PreviousOutPoint: *wire.NewOutPoint(&chainhash.Hash{},
wire.MaxPrevOutIndex),
Sequence: wire.MaxTxInSequenceNum,
SignatureScript: coinbaseScript,
})
tx.AddTxOut(&wire.TxOut{
Value: blockchain.CalcBlockSubsidy(blockHeight, g.params),
PkScript: testhelper.OpTrueScript,
})
return tx
return testhelper.CreateCoinbaseTx(
blockHeight, blockchain.CalcBlockSubsidy(blockHeight, g.params))
}
// calcMerkleRoot creates a merkle tree from the slice of transactions and

View File

@ -7,6 +7,7 @@ import (
"github.com/btcsuite/btcd/blockchain/internal/workmath"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
)
@ -21,6 +22,32 @@ var (
LowFee = btcutil.Amount(1)
)
// CreateCoinbaseTx returns a coinbase transaction paying an appropriate
// subsidy based on the passed block height and the block subsidy. The
// coinbase signature script conforms to the requirements of version 2 blocks.
func CreateCoinbaseTx(blockHeight int32, blockSubsidy int64) *wire.MsgTx {
extraNonce := uint64(0)
coinbaseScript, err := StandardCoinbaseScript(blockHeight, extraNonce)
if err != nil {
panic(err)
}
tx := wire.NewMsgTx(1)
tx.AddTxIn(&wire.TxIn{
// Coinbase transactions have no inputs, so previous outpoint is
// zero hash and max index.
PreviousOutPoint: *wire.NewOutPoint(&chainhash.Hash{},
wire.MaxPrevOutIndex),
Sequence: wire.MaxTxInSequenceNum,
SignatureScript: coinbaseScript,
})
tx.AddTxOut(&wire.TxOut{
Value: blockSubsidy,
PkScript: OpTrueScript,
})
return tx
}
// StandardCoinbaseScript returns a standard script suitable for use as the
// signature script of the coinbase transaction of a new block. In particular,
// it starts with the block height that is required by version 2 blocks.