mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-27 21:37:57 +08:00
This has required changing the order of some parameters and adding HashOnRead to the Blockstore interface (which I have in turn added to all the wrapper implementations). License: MIT Signed-off-by: Hector Sanjuan <hector@protocol.ai>
36 lines
925 B
Go
36 lines
925 B
Go
// Package blocksutil provides utility functions for working
|
|
// with Blocks.
|
|
package blocksutil
|
|
|
|
import "github.com/ipfs/go-ipfs/blocks"
|
|
|
|
// NewBlockGenerator returns an object capable of
|
|
// producing blocks.
|
|
func NewBlockGenerator() BlockGenerator {
|
|
return BlockGenerator{}
|
|
}
|
|
|
|
// 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.
|
|
type BlockGenerator struct {
|
|
seq int
|
|
}
|
|
|
|
// Next generates a new BasicBlock.
|
|
func (bg *BlockGenerator) Next() *blocks.BasicBlock {
|
|
bg.seq++
|
|
return blocks.NewBlock([]byte(string(bg.seq)))
|
|
}
|
|
|
|
// Blocks generates as many BasicBlocks as specified by n.
|
|
func (bg *BlockGenerator) Blocks(n int) []*blocks.BasicBlock {
|
|
blocks := make([]*blocks.BasicBlock, 0)
|
|
for i := 0; i < n; i++ {
|
|
b := bg.Next()
|
|
blocks = append(blocks, b)
|
|
}
|
|
return blocks
|
|
}
|