mirror of
https://github.com/btcsuite/btcd.git
synced 2024-11-19 09:50:08 +01:00
rpctest: publicly export the CreateBlock funciton
This commit publicly exports the CreateBlock function as it can be very useful for generating blocks for tests. Additionally, the behavior of the function has been modified slightly to build off of the genesis block for the specified chain if the `prevBlock` paramter is nil.
This commit is contained in:
parent
528cc07a00
commit
ca5e14da6a
@ -112,13 +112,32 @@ func createCoinbaseTx(coinbaseScript []byte, nextBlockHeight int32,
|
||||
return btcutil.NewTx(tx), nil
|
||||
}
|
||||
|
||||
// createBlock creates a new block building from the previous block.
|
||||
func createBlock(prevBlock *btcutil.Block, inclusionTxs []*btcutil.Tx,
|
||||
// CreateBlock creates a new block building from the previous block with a
|
||||
// specified blockversion and timestamp. If the timestamp passed is zero (not
|
||||
// initialized), then the timestamp of the previous block will be used plus 1
|
||||
// second is used. Passing nil for the previous block results in a block that
|
||||
// builds off of the genesis block for the specified chain.
|
||||
func CreateBlock(prevBlock *btcutil.Block, inclusionTxs []*btcutil.Tx,
|
||||
blockVersion int32, blockTime time.Time,
|
||||
miningAddr btcutil.Address, net *chaincfg.Params) (*btcutil.Block, error) {
|
||||
|
||||
prevHash := prevBlock.Hash()
|
||||
blockHeight := prevBlock.Height() + 1
|
||||
var (
|
||||
prevHash *chainhash.Hash
|
||||
blockHeight int32
|
||||
prevBlockTime time.Time
|
||||
)
|
||||
|
||||
// If the previous block isn't specified, then we'll construct a block
|
||||
// that builds off of the genesis block for the chain.
|
||||
if prevBlock == nil {
|
||||
prevHash = net.GenesisHash
|
||||
blockHeight = 1
|
||||
prevBlockTime = net.GenesisBlock.Header.Timestamp.Add(time.Minute)
|
||||
} else {
|
||||
prevHash = prevBlock.Hash()
|
||||
blockHeight = prevBlock.Height() + 1
|
||||
prevBlockTime = prevBlock.MsgBlock().Header.Timestamp
|
||||
}
|
||||
|
||||
// If a target block time was specified, then use that as the header's
|
||||
// timestamp. Otherwise, add one second to the previous block unless
|
||||
@ -128,7 +147,7 @@ func createBlock(prevBlock *btcutil.Block, inclusionTxs []*btcutil.Tx,
|
||||
case !blockTime.IsZero():
|
||||
ts = blockTime
|
||||
default:
|
||||
ts = prevBlock.MsgBlock().Header.Timestamp.Add(time.Second)
|
||||
ts = prevBlockTime.Add(time.Second)
|
||||
}
|
||||
|
||||
extraNonce := uint64(0)
|
||||
|
@ -421,7 +421,7 @@ func (h *Harness) GenerateAndSubmitBlock(txns []*btcutil.Tx, blockVersion int32,
|
||||
prevBlock.SetHeight(prevBlockHeight)
|
||||
|
||||
// Create a new block including the specified transactions
|
||||
newBlock, err := createBlock(prevBlock, txns, blockVersion,
|
||||
newBlock, err := CreateBlock(prevBlock, txns, blockVersion,
|
||||
blockTime, h.wallet.coinbaseAddr, h.ActiveNet)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
Loading…
Reference in New Issue
Block a user