From ca5e14da6a376e706ac25aefb2f2144d9fa992e8 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sat, 12 Nov 2016 19:41:08 -0800 Subject: [PATCH] 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. --- rpctest/blockgen.go | 29 ++++++++++++++++++++++++----- rpctest/rpc_harness.go | 2 +- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/rpctest/blockgen.go b/rpctest/blockgen.go index 9f50dc84..fd3bfb8b 100644 --- a/rpctest/blockgen.go +++ b/rpctest/blockgen.go @@ -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) diff --git a/rpctest/rpc_harness.go b/rpctest/rpc_harness.go index 16ba96d4..16789731 100644 --- a/rpctest/rpc_harness.go +++ b/rpctest/rpc_harness.go @@ -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