generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Will be fine performance wise once #57 is fixed.
- Loading branch information
Showing
17 changed files
with
297 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Package blocks contains the lowest level of IPLD data structures. | ||
// A block is raw data accompanied by a CID. The CID contains the multihash | ||
// corresponding to the block. | ||
package blocks | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
cid "github.com/ipfs/go-cid" | ||
u "github.com/ipfs/go-ipfs-util" | ||
mh "github.com/multiformats/go-multihash" | ||
) | ||
|
||
// 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 { | ||
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 { | ||
cid cid.Cid | ||
data []byte | ||
} | ||
|
||
// NewBlock creates a Block object from opaque data. It will hash the data. | ||
func NewBlock(data []byte) *BasicBlock { | ||
// TODO: fix assumptions | ||
return &BasicBlock{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) { | ||
if u.Debug { | ||
chkc, err := c.Prefix().Sum(data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !chkc.Equals(c) { | ||
return nil, ErrWrongHash | ||
} | ||
} | ||
return &BasicBlock{data: data, cid: c}, nil | ||
} | ||
|
||
// Multihash returns the hash contained in the block CID. | ||
func (b *BasicBlock) Multihash() mh.Multihash { | ||
return b.cid.Hash() | ||
} | ||
|
||
// RawData returns the block raw contents as a byte slice. | ||
func (b *BasicBlock) RawData() []byte { | ||
return b.data | ||
} | ||
|
||
// Cid returns the content identifier of the block. | ||
func (b *BasicBlock) Cid() cid.Cid { | ||
return b.cid | ||
} | ||
|
||
// String provides a human-readable representation of the block CID. | ||
func (b *BasicBlock) String() string { | ||
return fmt.Sprintf("[Block %s]", b.Cid()) | ||
} | ||
|
||
// Loggable returns a go-log loggable item. | ||
func (b *BasicBlock) Loggable() map[string]interface{} { | ||
return map[string]interface{}{ | ||
"block": b.Cid().String(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package blocks | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
cid "github.com/ipfs/go-cid" | ||
u "github.com/ipfs/go-ipfs-util" | ||
mh "github.com/multiformats/go-multihash" | ||
) | ||
|
||
func TestBlocksBasic(t *testing.T) { | ||
|
||
// Test empty data | ||
empty := []byte{} | ||
NewBlock(empty) | ||
|
||
// Test nil case | ||
NewBlock(nil) | ||
|
||
// Test some data | ||
NewBlock([]byte("Hello world!")) | ||
} | ||
|
||
func TestData(t *testing.T) { | ||
data := []byte("some data") | ||
block := NewBlock(data) | ||
|
||
if !bytes.Equal(block.RawData(), data) { | ||
t.Error("data is wrong") | ||
} | ||
} | ||
|
||
func TestHash(t *testing.T) { | ||
data := []byte("some other data") | ||
block := NewBlock(data) | ||
|
||
hash, err := mh.Sum(data, mh.SHA2_256, -1) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !bytes.Equal(block.Multihash(), hash) { | ||
t.Error("wrong multihash") | ||
} | ||
} | ||
|
||
func TestCid(t *testing.T) { | ||
data := []byte("yet another data") | ||
block := NewBlock(data) | ||
c := block.Cid() | ||
|
||
if !bytes.Equal(block.Multihash(), c.Hash()) { | ||
t.Error("key contains wrong data") | ||
} | ||
} | ||
|
||
func TestManualHash(t *testing.T) { | ||
oldDebugState := u.Debug | ||
defer (func() { | ||
u.Debug = oldDebugState | ||
})() | ||
|
||
data := []byte("I can't figure out more names .. data") | ||
hash, err := mh.Sum(data, mh.SHA2_256, -1) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
c := cid.NewCidV0(hash) | ||
|
||
u.Debug = false | ||
block, err := NewBlockWithCid(data, c) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !bytes.Equal(block.Multihash(), hash) { | ||
t.Error("wrong multihash") | ||
} | ||
|
||
data[5] = byte((uint32(data[5]) + 5) % 256) // Transfrom hash to be different | ||
block, err = NewBlockWithCid(data, c) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !bytes.Equal(block.Multihash(), hash) { | ||
t.Error("wrong multihash") | ||
} | ||
|
||
u.Debug = true | ||
|
||
_, err = NewBlockWithCid(data, c) | ||
if err != ErrWrongHash { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
comment: | ||
layout: "reach, diff, files" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.