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 (
"errors"
"fmt"
key "gx/ipfs/QmYEoKZXHoAToWfhGF3vryhMn3WWhE1o2MasQ8uzY5iDi9/go-key"
mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
......@@ -18,37 +16,39 @@ var ErrWrongHash = errors.New("data did not match given hash!")
type Block interface {
Multihash() mh.Multihash
RawData() []byte
Key() key.Key
Cid() *cid.Cid
String() string
Loggable() map[string]interface{}
}
// Block is a singular block of data in ipfs
type BasicBlock struct {
multihash mh.Multihash
data []byte
cid *cid.Cid
data []byte
}
// NewBlock creates a Block object from opaque data. It will hash the data.
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
// is already known, this is used to save time in situations where
// 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 {
chk := u.Hash(data)
if string(chk) != string(h) {
// TODO: fix assumptions
chkc := cid.NewCidV0(u.Hash(data))
if !chkc.Equals(c) {
return nil, ErrWrongHash
}
}
return &BasicBlock{data: data, multihash: h}, nil
return &BasicBlock{data: data, cid: c}, nil
}
func (b *BasicBlock) Multihash() mh.Multihash {
return b.multihash
return b.cid.Hash()
}
func (b *BasicBlock) RawData() []byte {
......@@ -56,20 +56,15 @@ func (b *BasicBlock) RawData() []byte {
}
func (b *BasicBlock) Cid() *cid.Cid {
return cid.NewCidV0(b.multihash)
}
// Key returns the block's Multihash as a Key value.
func (b *BasicBlock) Key() key.Key {
return key.Key(b.multihash)
return b.cid
}
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{} {
return map[string]interface{}{
"block": b.Key().String(),
"block": b.Cid().String(),
}
}
......@@ -5,6 +5,7 @@ import (
"testing"
mh "gx/ipfs/QmYDds3421prZgqKbLpEK7T9Aa2eVdQ7o3YarX1LVLdP2J/go-multihash"
cid "gx/ipfs/QmakyCk6Vnn16WEKjbkxieZmM2YLTzkFWizbmGowoYPjro/go-cid"
u "gx/ipfs/Qmb912gdngC1UWwTkhuW8knyRbcWeu5kqkxBpveLmW8bSr/go-ipfs-util"
)
......@@ -44,12 +45,12 @@ func TestHash(t *testing.T) {
}
}
func TestKey(t *testing.T) {
func TestCid(t *testing.T) {
data := []byte("yet another 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")
}
}
......@@ -66,8 +67,10 @@ func TestManualHash(t *testing.T) {
t.Fatal(err)
}
c := cid.NewCidV0(hash)
u.Debug = false
block, err := NewBlockWithHash(data, hash)
block, err := NewBlockWithCid(data, c)
if err != nil {
t.Fatal(err)
}
......@@ -77,7 +80,7 @@ func TestManualHash(t *testing.T) {
}
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 {
t.Fatal(err)
}
......@@ -88,7 +91,7 @@ func TestManualHash(t *testing.T) {
u.Debug = true
block, err = NewBlockWithHash(data, hash)
block, err = NewBlockWithCid(data, c)
if err != ErrWrongHash {
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