Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change Block to a value struct #53

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,66 +16,57 @@
// according to the contents. It is currently used only when debugging.
var ErrWrongHash = errors.New("data did not match given hash")

// Block provides abstraction for blocks implementations.
type Block interface {
RawData() []byte
Cid() cid.Cid
String() string
Loggable() map[string]interface{}
}

// A BasicBlock is a singular block of data in ipfs. It implements the Block
// interface.
type BasicBlock struct {
// A Block is a singular block of data in ipfs. This is some bytes addressed by a hash.
type Block struct {
cid cid.Cid
data []byte
}

// NewBlock creates a Block object from opaque data. It will hash the data.
func NewBlock(data []byte) *BasicBlock {
func NewBlock(data []byte) Block {
// TODO: fix assumptions
return &BasicBlock{data: data, cid: cid.NewCidV0(u.Hash(data))}
return Block{data: data, cid: cid.NewCidV0(u.Hash(data))}
}

// NewBlockWithCid creates a new block when the hash of the data
// is already known, this is used to save time in situations where
// we are able to be confident that the data is correct.
func NewBlockWithCid(data []byte, c cid.Cid) (*BasicBlock, error) {
func NewBlockWithCid(data []byte, c cid.Cid) (Block, error) {
if u.Debug {
chkc, err := c.Prefix().Sum(data)
if err != nil {
return nil, err
return Block{}, err

Check warning on line 38 in blocks.go

View check run for this annotation

Codecov / codecov/patch

blocks.go#L38

Added line #L38 was not covered by tests
}

if !chkc.Equals(c) {
return nil, ErrWrongHash
return Block{}, ErrWrongHash
}
}
return &BasicBlock{data: data, cid: c}, nil
return Block{data: data, cid: c}, nil
}

// Multihash returns the hash contained in the block CID.
func (b *BasicBlock) Multihash() mh.Multihash {
func (b Block) Multihash() mh.Multihash {
return b.cid.Hash()
}

// RawData returns the block raw contents as a byte slice.
func (b *BasicBlock) RawData() []byte {
func (b Block) RawData() []byte {
return b.data
}

// Cid returns the content identifier of the block.
func (b *BasicBlock) Cid() cid.Cid {
func (b Block) Cid() cid.Cid {
return b.cid
}

// String provides a human-readable representation of the block CID.
func (b *BasicBlock) String() string {
func (b Block) String() string {

Check warning on line 64 in blocks.go

View check run for this annotation

Codecov / codecov/patch

blocks.go#L64

Added line #L64 was not covered by tests
return fmt.Sprintf("[Block %s]", b.Cid())
}

// Loggable returns a go-log loggable item.
func (b *BasicBlock) Loggable() map[string]interface{} {
func (b Block) Loggable() map[string]interface{} {

Check warning on line 69 in blocks.go

View check run for this annotation

Codecov / codecov/patch

blocks.go#L69

Added line #L69 was not covered by tests
return map[string]interface{}{
"block": b.Cid().String(),
}
Expand Down