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:
Olaoluwa Osuntokun 2016-11-12 19:41:08 -08:00
parent 528cc07a00
commit ca5e14da6a
No known key found for this signature in database
GPG key ID: 9CC5B105D03521A2
2 changed files with 25 additions and 6 deletions

View file

@ -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)

View file

@ -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