Commit cb5edc30 authored by Hector Sanjuan's avatar Hector Sanjuan

Make Golint happy in the blocks submodule.

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: default avatarHector Sanjuan <hector@protocol.ai>
parent 5029bc3e
// package blocks contains the lowest level of IPFS data structures, // Package blocks contains the lowest level of IPFS data structures.
// the raw block with a checksum. // A block is raw data accompanied by a CID. The CID contains the multihash
// corresponding to the block.
package blocks package blocks
import ( import (
...@@ -11,8 +12,11 @@ import ( ...@@ -11,8 +12,11 @@ import (
mh "gx/ipfs/QmbZ6Cee2uHjG7hf19qLHppgKDRtaG4CVtMzdmK9VCVqLu/go-multihash" mh "gx/ipfs/QmbZ6Cee2uHjG7hf19qLHppgKDRtaG4CVtMzdmK9VCVqLu/go-multihash"
) )
var ErrWrongHash = errors.New("data did not match given hash!") // ErrWrongHash is returned when the Cid of a block is not the expected
// 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 { type Block interface {
RawData() []byte RawData() []byte
Cid() *cid.Cid Cid() *cid.Cid
...@@ -20,7 +24,8 @@ type Block interface { ...@@ -20,7 +24,8 @@ type Block interface {
Loggable() map[string]interface{} Loggable() map[string]interface{}
} }
// Block is a singular block of data in ipfs // A BasicBlock is a singular block of data in ipfs. It implements the Block
// interface.
type BasicBlock struct { type BasicBlock struct {
cid *cid.Cid cid *cid.Cid
data []byte data []byte
...@@ -32,9 +37,9 @@ func NewBlock(data []byte) *BasicBlock { ...@@ -32,9 +37,9 @@ func NewBlock(data []byte) *BasicBlock {
return &BasicBlock{data: data, cid: cid.NewCidV0(u.Hash(data))} return &BasicBlock{data: data, cid: cid.NewCidV0(u.Hash(data))}
} }
// NewBlockWithHash creates a new block when the hash of the data // NewBlockWithCid creates a new block when the hash of the data
// is already known, this is used to save time in situations where // is already known, this is used to save time in situations where
// we are able to be confident that the data is correct // 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) (*BasicBlock, error) {
if u.Debug { if u.Debug {
chkc, err := c.Prefix().Sum(data) chkc, err := c.Prefix().Sum(data)
...@@ -49,22 +54,27 @@ func NewBlockWithCid(data []byte, c *cid.Cid) (*BasicBlock, error) { ...@@ -49,22 +54,27 @@ func NewBlockWithCid(data []byte, c *cid.Cid) (*BasicBlock, error) {
return &BasicBlock{data: data, cid: c}, nil return &BasicBlock{data: data, cid: c}, nil
} }
// Multihash returns the hash contained in the block CID.
func (b *BasicBlock) Multihash() mh.Multihash { func (b *BasicBlock) Multihash() mh.Multihash {
return b.cid.Hash() return b.cid.Hash()
} }
// RawData returns the block raw contents as a byte slice.
func (b *BasicBlock) RawData() []byte { func (b *BasicBlock) RawData() []byte {
return b.data return b.data
} }
// Cid returns the content identifier of the block.
func (b *BasicBlock) Cid() *cid.Cid { func (b *BasicBlock) Cid() *cid.Cid {
return b.cid return b.cid
} }
// String provides a human-readable representation of the block CID.
func (b *BasicBlock) String() string { func (b *BasicBlock) String() string {
return fmt.Sprintf("[Block %s]", b.Cid()) return fmt.Sprintf("[Block %s]", b.Cid())
} }
// Loggable returns a go-log loggable item.
func (b *BasicBlock) Loggable() map[string]interface{} { func (b *BasicBlock) Loggable() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"block": b.Cid().String(), "block": b.Cid().String(),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment