github.com/decred/dcrd/blockchain@v1.2.1/chaingen/example_test.go (about) 1 // Copyright (c) 2017 The Decred developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package chaingen_test 6 7 import ( 8 "fmt" 9 10 "github.com/decred/dcrd/blockchain/chaingen" 11 "github.com/decred/dcrd/chaincfg" 12 ) 13 14 // This example demonstrates creating a new generator instance and using it to 15 // generate the required premine block and enough blocks to have mature coinbase 16 // outputs to work with along with asserting the generator state along the way. 17 func Example_basicUsage() { 18 params := &chaincfg.RegNetParams 19 g, err := chaingen.MakeGenerator(params) 20 if err != nil { 21 fmt.Println(err) 22 return 23 } 24 25 // Shorter versions of useful params for convenience. 26 coinbaseMaturity := params.CoinbaseMaturity 27 28 // --------------------------------------------------------------------- 29 // Premine. 30 // --------------------------------------------------------------------- 31 32 // Add the required premine block. 33 // 34 // genesis -> bp 35 g.CreatePremineBlock("bp", 0) 36 g.AssertTipHeight(1) 37 fmt.Println(g.TipName()) 38 39 // --------------------------------------------------------------------- 40 // Generate enough blocks to have mature coinbase outputs to work with. 41 // 42 // genesis -> bp -> bm0 -> bm1 -> ... -> bm# 43 // --------------------------------------------------------------------- 44 45 for i := uint16(0); i < coinbaseMaturity; i++ { 46 blockName := fmt.Sprintf("bm%d", i) 47 g.NextBlock(blockName, nil, nil) 48 g.SaveTipCoinbaseOuts() 49 fmt.Println(g.TipName()) 50 } 51 g.AssertTipHeight(uint32(coinbaseMaturity) + 1) 52 53 // Output: 54 // bp 55 // bm0 56 // bm1 57 // bm2 58 // bm3 59 // bm4 60 // bm5 61 // bm6 62 // bm7 63 // bm8 64 // bm9 65 // bm10 66 // bm11 67 // bm12 68 // bm13 69 // bm14 70 // bm15 71 }