Commit 09c41e5b authored by Jeromy's avatar Jeromy

cid: integrate cid into bitswap and blockstores

License: MIT
Signed-off-by: default avatarJeromy <why@ipfs.io>
parent dd1aa730
...@@ -6,8 +6,6 @@ import ( ...@@ -6,8 +6,6 @@ import (
"errors" "errors"
"fmt" "fmt"
key "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key"
mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash" mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid" cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util" u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
...@@ -18,37 +16,39 @@ var ErrWrongHash = errors.New("data did not match given hash!") ...@@ -18,37 +16,39 @@ var ErrWrongHash = errors.New("data did not match given hash!")
type Block interface { type Block interface {
Multihash() mh.Multihash Multihash() mh.Multihash
RawData() []byte RawData() []byte
Key() key.Key Cid() *cid.Cid
String() string String() string
Loggable() map[string]interface{} Loggable() map[string]interface{}
} }
// Block is a singular block of data in ipfs // Block is a singular block of data in ipfs
type BasicBlock struct { type BasicBlock struct {
multihash mh.Multihash cid *cid.Cid
data []byte data []byte
} }
// NewBlock creates a Block object from opaque data. It will hash the data. // NewBlock creates a Block object from opaque data. It will hash the data.
func NewBlock(data []byte) *BasicBlock { func NewBlock(data []byte) *BasicBlock {
return &BasicBlock{data: data, multihash: u.Hash(data)} // TODO: fix assumptions
return &BasicBlock{data: data, cid: cid.NewCidV0(u.Hash(data))}
} }
// NewBlockWithHash creates a new block when the hash of the data // NewBlockWithHash 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 NewBlockWithHash(data []byte, h mh.Multihash) (*BasicBlock, error) { func NewBlockWithCid(data []byte, c *cid.Cid) (*BasicBlock, error) {
if u.Debug { if u.Debug {
chk := u.Hash(data) // TODO: fix assumptions
if string(chk) != string(h) { chkc := cid.NewCidV0(u.Hash(data))
if !chkc.Equals(c) {
return nil, ErrWrongHash return nil, ErrWrongHash
} }
} }
return &BasicBlock{data: data, multihash: h}, nil return &BasicBlock{data: data, cid: c}, nil
} }
func (b *BasicBlock) Multihash() mh.Multihash { func (b *BasicBlock) Multihash() mh.Multihash {
return b.multihash return b.cid.Hash()
} }
func (b *BasicBlock) RawData() []byte { func (b *BasicBlock) RawData() []byte {
...@@ -56,20 +56,15 @@ func (b *BasicBlock) RawData() []byte { ...@@ -56,20 +56,15 @@ func (b *BasicBlock) RawData() []byte {
} }
func (b *BasicBlock) Cid() *cid.Cid { func (b *BasicBlock) Cid() *cid.Cid {
return cid.NewCidV0(b.multihash) return b.cid
}
// Key returns the block's Multihash as a Key value.
func (b *BasicBlock) Key() key.Key {
return key.Key(b.multihash)
} }
func (b *BasicBlock) String() string { func (b *BasicBlock) String() string {
return fmt.Sprintf("[Block %s]", b.Key()) return fmt.Sprintf("[Block %s]", b.Cid())
} }
func (b *BasicBlock) Loggable() map[string]interface{} { func (b *BasicBlock) Loggable() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"block": b.Key().String(), "block": b.Cid().String(),
} }
} }
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"testing" "testing"
mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash" mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util" u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
) )
...@@ -44,12 +45,12 @@ func TestHash(t *testing.T) { ...@@ -44,12 +45,12 @@ func TestHash(t *testing.T) {
} }
} }
func TestKey(t *testing.T) { func TestCid(t *testing.T) {
data := []byte("yet another data") data := []byte("yet another data")
block := NewBlock(data) block := NewBlock(data)
key := block.Key() c := block.Cid()
if !bytes.Equal(block.Multihash(), key.ToMultihash()) { if !bytes.Equal(block.Multihash(), c.Hash()) {
t.Error("key contains wrong data") t.Error("key contains wrong data")
} }
} }
...@@ -66,8 +67,10 @@ func TestManualHash(t *testing.T) { ...@@ -66,8 +67,10 @@ func TestManualHash(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
c := cid.NewCidV0(hash)
u.Debug = false u.Debug = false
block, err := NewBlockWithHash(data, hash) block, err := NewBlockWithCid(data, c)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -77,7 +80,7 @@ func TestManualHash(t *testing.T) { ...@@ -77,7 +80,7 @@ func TestManualHash(t *testing.T) {
} }
data[5] = byte((uint32(data[5]) + 5) % 256) // Transfrom hash to be different data[5] = byte((uint32(data[5]) + 5) % 256) // Transfrom hash to be different
block, err = NewBlockWithHash(data, hash) block, err = NewBlockWithCid(data, c)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
...@@ -88,7 +91,7 @@ func TestManualHash(t *testing.T) { ...@@ -88,7 +91,7 @@ func TestManualHash(t *testing.T) {
u.Debug = true u.Debug = true
block, err = NewBlockWithHash(data, hash) block, err = NewBlockWithCid(data, c)
if err != ErrWrongHash { if err != ErrWrongHash {
t.Fatal(err) t.Fatal(err)
} }
......
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