Skip to content

Commit

Permalink
Update to use the new string representation of a Multihash.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevina committed Aug 29, 2018
1 parent 193039c commit 70b07e9
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 72 deletions.
8 changes: 2 additions & 6 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (p Prefix) WithCodec(c uint64) Builder {
}

func (p V0Builder) Sum(data []byte) (Cid, error) {
hash, err := mh.Sum(data, mh.SHA2_256, -1)
hash, err := mh.Builder{Type: mh.SHA2_256}.Sum(data)
if err != nil {
return Nil, err
}
Expand All @@ -53,11 +53,7 @@ func (p V0Builder) WithCodec(c uint64) Builder {
}

func (p V1Builder) Sum(data []byte) (Cid, error) {
mhLen := p.MhLength
if mhLen <= 0 {
mhLen = -1
}
hash, err := mh.Sum(data, p.MhType, mhLen)
hash, err := mh.Builder{Type: p.MhType, Length: p.MhLength}.Sum(data)
if err != nil {
return Nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestV0Builder(t *testing.T) {
}

// Construct c2
hash, err := mh.Sum(data, mh.SHA2_256, -1)
hash, err := mh.Builder{Type: mh.SHA2_256}.Sum(data)
if err != nil {
t.Fatal(err)
}
Expand All @@ -42,7 +42,7 @@ func TestV1Builder(t *testing.T) {
}

// Construct c2
hash, err := mh.Sum(data, mh.SHA2_256, -1)
hash, err := mh.Builder{Type: mh.SHA2_256}.Sum(data)
if err != nil {
t.Fatal(err)
}
Expand Down
36 changes: 17 additions & 19 deletions cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

mbase "github.com/multiformats/go-multibase"
mh "github.com/multiformats/go-multihash"
strbinary "github.com/multiformats/go-multihash/strbinary"
)

// UnsupportedVersionString just holds an error message
Expand Down Expand Up @@ -133,18 +134,18 @@ var CodecToStr = map[uint64]string{
// compatibility with the plain-multihash format used used in IPFS.
// NewCidV1 should be used preferentially.
func NewCidV0(mhash mh.Multihash) Cid {
return Cid{string(mhash)}
return Cid{mhash.Binary()}
}

// NewCidV1 returns a new Cid using the given multicodec-packed
// content type.
func NewCidV1(codecType uint64, mhash mh.Multihash) Cid {
hashlen := len(mhash)
hashlen := len(mhash.Binary())
// two 8 bytes (max) numbers plus hash
buf := make([]byte, 2*binary.MaxVarintLen64+hashlen)
n := binary.PutUvarint(buf, 1)
n += binary.PutUvarint(buf[n:], codecType)
cn := copy(buf[n:], mhash)
cn := copy(buf[n:], mhash.Binary())
if cn != hashlen {
panic("copy hash length is inconsistent")
}
Expand Down Expand Up @@ -215,7 +216,7 @@ func Decode(v string) (Cid, error) {
return Nil, err
}

return NewCidV0(hash), nil
return NewCidV0(hash.Cast()), nil
}

_, data, err := mbase.Decode(v)
Expand Down Expand Up @@ -277,7 +278,7 @@ func Cast(data []byte) (Cid, error) {
return Nil, err
}

return NewCidV0(h), nil
return NewCidV0(h.Cast()), nil
}

vers, n := binary.Uvarint(data)
Expand Down Expand Up @@ -316,8 +317,7 @@ func (c Cid) Type() uint64 {
if c.Version() == 0 {
return DagProtobuf
}
_, n := uvarint(c.str)
codec, _ := uvarint(c.str[n:])
codec, _ := strbinary.Uvarint(c.str[1:])
return codec
}

Expand Down Expand Up @@ -358,18 +358,16 @@ func (c Cid) StringOfBase(base mbase.Encoding) (string, error) {

// Hash returns the multihash contained by a Cid.
func (c Cid) Hash() mh.Multihash {
bytes := c.Bytes()

if c.Version() == 0 {
return mh.Multihash(bytes)
return mh.FromBinary(c.str)
}

// skip version length
_, n1 := binary.Uvarint(bytes)
// skip codec length
_, n2 := binary.Uvarint(bytes[n1:])
// Skip 1 byte for the version prefix
i := 1
// Skip the Codec prefix
i += strbinary.UvarintLen(c.str[i:])

return mh.Multihash(bytes[n1+n2:])
return mh.FromBinary(c.str[i:])
}

// Bytes returns the byte representation of a Cid.
Expand Down Expand Up @@ -438,10 +436,10 @@ func (c Cid) Loggable() map[string]interface{} {

// Prefix builds and returns a Prefix out of a Cid.
func (c Cid) Prefix() Prefix {
dec, _ := mh.Decode(c.Hash()) // assuming we got a valid multiaddr, this will not error
mhType, mhLength, _ := c.Hash().Parts()
return Prefix{
MhType: dec.Code,
MhLength: dec.Length,
MhType: mhType,
MhLength: mhLength,
Version: c.Version(),
Codec: c.Type(),
}
Expand All @@ -463,7 +461,7 @@ type Prefix struct {
// Sum uses the information in a prefix to perform a multihash.Sum()
// and return a newly constructed Cid with the resulting multihash.
func (p Prefix) Sum(data []byte) (Cid, error) {
hash, err := mh.Sum(data, p.MhType, p.MhLength)
hash, err := mh.Builder{Type: p.MhType, Length: p.MhLength}.Sum(data)
if err != nil {
return Nil, err
}
Expand Down
20 changes: 10 additions & 10 deletions cid_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cid

import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
Expand Down Expand Up @@ -46,7 +45,7 @@ func assertEqual(t *testing.T, a, b Cid) {
t.Fatal("mismatch on version")
}

if !bytes.Equal(a.Hash(), b.Hash()) {
if a.Hash() != b.Hash() {
t.Fatal("multihash mismatch")
}
}
Expand All @@ -72,7 +71,7 @@ func TestTableForV0(t *testing.T) {
}

func TestBasicMarshaling(t *testing.T) {
h, err := mh.Sum([]byte("TEST"), mh.SHA3, 4)
h, err := mh.Builder{Type: mh.SHA3}.Sum([]byte("TEST"))
if err != nil {
t.Fatal(err)
}
Expand All @@ -98,7 +97,7 @@ func TestBasicMarshaling(t *testing.T) {
}

func TestBasesMarshaling(t *testing.T) {
h, err := mh.Sum([]byte("TEST"), mh.SHA3, 4)
h, err := mh.Builder{Type: mh.SHA3}.Sum([]byte("TEST"))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -198,7 +197,7 @@ func TestNewPrefixV1(t *testing.T) {
}

// Construct c2
hash, err := mh.Sum(data, mh.SHA2_256, -1)
hash, err := mh.Builder{Type: mh.SHA2_256}.Sum(data)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -227,7 +226,7 @@ func TestNewPrefixV0(t *testing.T) {
}

// Construct c2
hash, err := mh.Sum(data, mh.SHA2_256, -1)
hash, err := mh.Builder{Type: mh.SHA2_256}.Sum(data)
if err != nil {
t.Fatal(err)
}
Expand All @@ -243,7 +242,7 @@ func TestNewPrefixV0(t *testing.T) {

func TestPrefixRoundtrip(t *testing.T) {
data := []byte("this is some test content")
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
hash, _ := mh.Builder{Type: mh.SHA2_256}.Sum(data)
c := NewCidV1(DagCBOR, hash)

pref := c.Prefix()
Expand Down Expand Up @@ -272,7 +271,7 @@ func TestPrefixRoundtrip(t *testing.T) {

func Test16BytesVarint(t *testing.T) {
data := []byte("this is some test content")
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
hash, _ := mh.Builder{Type: mh.SHA2_256}.Sum(data)
c := NewCidV1(1<<63, hash)
_ = c.Bytes()
}
Expand All @@ -296,10 +295,11 @@ func TestParse(t *testing.T) {
}

theHash := "QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n"
h, err := mh.FromB58String(theHash)
h0, err := mh.FromB58String(theHash)
if err != nil {
t.Fatal(err)
}
h := h0.Cast()

assertions := [][]interface{}{
[]interface{}{NewCidV0(h), theHash},
Expand Down Expand Up @@ -407,7 +407,7 @@ func TestJsonRoundTrip(t *testing.T) {

func BenchmarkStringV1(b *testing.B) {
data := []byte("this is some test content")
hash, _ := mh.Sum(data, mh.SHA2_256, -1)
hash, _ := mh.Builder{Type: mh.SHA2_256}.Sum(data)
cid := NewCidV1(Raw, hash)

b.ReportAllocs()
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"gxDependencies": [
{
"author": "whyrusleeping",
"hash": "QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8",
"hash": "QmNNAKBZusgdz5GpZh5D5RvBaSYgqfXtwB7YLsjyemkcPU",
"name": "go-multihash",
"version": "1.0.8"
"version": "1.2.0"
},
{
"author": "whyrusleeping",
Expand Down
2 changes: 1 addition & 1 deletion set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func makeRandomCid(t *testing.T) Cid {
t.Fatal(err)
}

h, err := mh.Sum(p, mh.SHA3, 4)
h, err := mh.Builder{Type: mh.SHA3, Length: 4}.Sum(p)
if err != nil {
t.Fatal(err)
}
Expand Down
32 changes: 0 additions & 32 deletions varint.go

This file was deleted.

0 comments on commit 70b07e9

Please sign in to comment.