block_generator.go 956 Bytes
Newer Older
1 2
// Package blocksutil provides utility functions for working
// with Blocks.
3 4
package blocksutil

Steven Allen's avatar
Steven Allen committed
5
import "gx/ipfs/QmYsEQydGrsxNZfAiskvQ76N2xE9hDQtSAkRSynwMiUK3c/go-block-format"
6

7 8
// NewBlockGenerator returns an object capable of
// producing blocks.
9 10 11 12
func NewBlockGenerator() BlockGenerator {
	return BlockGenerator{}
}

13 14 15 16
// BlockGenerator generates BasicBlocks on demand.
// For each instace of BlockGenerator,
// each new block is different from the previous,
// although two different instances will produce the same.
17 18 19 20
type BlockGenerator struct {
	seq int
}

21
// Next generates a new BasicBlock.
22
func (bg *BlockGenerator) Next() *blocks.BasicBlock {
23 24 25 26
	bg.seq++
	return blocks.NewBlock([]byte(string(bg.seq)))
}

27
// Blocks generates as many BasicBlocks as specified by n.
Jeromy's avatar
Jeromy committed
28 29
func (bg *BlockGenerator) Blocks(n int) []blocks.Block {
	blocks := make([]blocks.Block, 0, n)
30 31 32 33 34 35
	for i := 0; i < n; i++ {
		b := bg.Next()
		blocks = append(blocks, b)
	}
	return blocks
}