dcrd/blockchain/chaingen/example_test.go
Dave Collins be7ab82cf9
chaincfg: Start v3 module dev cycle.
Upcoming changes constitute breaking public API changes to the chaincfg
module, therefore, this follows the process for introducing major API
breaks which consists of:

- Bump the major version in the go.mod of the affected module if not
  already done since the last release tag
- Add a replacement to the go.mod in the main module if not already done
  since the last release tag
- Update all imports in the repo to use the new major version as
  necessary
- Make necessary modifications to allow all other modules to use the
  new version in the same commit
  - Repeat the process for any other modules the require a new major as a
    result of consuming the new major(s)
2020-01-29 13:24:14 -06:00

72 lines
1.7 KiB
Go

// Copyright (c) 2017-2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package chaingen_test
import (
"fmt"
"github.com/decred/dcrd/blockchain/v3/chaingen"
"github.com/decred/dcrd/chaincfg/v3"
)
// This example demonstrates creating a new generator instance and using it to
// generate the required first block and enough blocks to have mature coinbase
// outputs to work with along with asserting the generator state along the way.
func Example_basicUsage() {
params := chaincfg.RegNetParams()
g, err := chaingen.MakeGenerator(params)
if err != nil {
fmt.Println(err)
return
}
// Shorter versions of useful params for convenience.
coinbaseMaturity := params.CoinbaseMaturity
// ---------------------------------------------------------------------
// First block.
// ---------------------------------------------------------------------
// Add the required first block.
//
// genesis -> bfb
g.CreateBlockOne("bfb", 0)
g.AssertTipHeight(1)
fmt.Println(g.TipName())
// ---------------------------------------------------------------------
// Generate enough blocks to have mature coinbase outputs to work with.
//
// genesis -> bp -> bm0 -> bm1 -> ... -> bm#
// ---------------------------------------------------------------------
for i := uint16(0); i < coinbaseMaturity; i++ {
blockName := fmt.Sprintf("bm%d", i)
g.NextBlock(blockName, nil, nil)
g.SaveTipCoinbaseOuts()
fmt.Println(g.TipName())
}
g.AssertTipHeight(uint32(coinbaseMaturity) + 1)
// Output:
// bfb
// bm0
// bm1
// bm2
// bm3
// bm4
// bm5
// bm6
// bm7
// bm8
// bm9
// bm10
// bm11
// bm12
// bm13
// bm14
// bm15
}