From 5d8ad3eb9c2003f0819a6d2c021c5a475fb7cdd2 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Thu, 28 Jun 2018 22:48:56 -0400 Subject: [PATCH 1/9] Create new Format interface for creating CIDs. --- cid.go | 3 +++ format.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++ format_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 format.go create mode 100644 format_test.go diff --git a/cid.go b/cid.go index 99a26a6..7461836 100644 --- a/cid.go +++ b/cid.go @@ -145,6 +145,7 @@ func NewCidV1(codecType uint64, mhash mh.Multihash) *Cid { } // NewPrefixV0 returns a CIDv0 prefix with the specified multihash type. +// DEPRECATED: Use FormatV0 func NewPrefixV0(mhType uint64) Prefix { return Prefix{ MhType: mhType, @@ -156,6 +157,7 @@ func NewPrefixV0(mhType uint64) Prefix { // NewPrefixV1 returns a CIDv1 prefix with the specified codec and multihash // type. +// DEPRECATED: Use FormatV1 func NewPrefixV1(codecType uint64, mhType uint64) Prefix { return Prefix{ MhType: mhType, @@ -451,6 +453,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. +// DEPRECATED: Use PrefixToFormat(p).Sum(data) func (p Prefix) Sum(data []byte) (*Cid, error) { hash, err := mh.Sum(data, p.MhType, p.MhLength) if err != nil { diff --git a/format.go b/format.go new file mode 100644 index 0000000..eda37a4 --- /dev/null +++ b/format.go @@ -0,0 +1,55 @@ +package cid + +import ( + mh "github.com/multiformats/go-multihash" +) + +type Format interface { + Sum(data []byte) (*Cid, error) +} + +type FormatV0 struct{} + +type FormatV1 struct { + Codec uint64 + HashFun uint64 + HashLen int // HashLen <= 0 means the default length +} + +func PrefixToFormat(p Prefix) Format { + if p.Version == 0 { + return FormatV0{} + } + mhLen := p.MhLength + if p.MhType == mh.ID { + mhLen = 0 + } + if mhLen < 0 { + mhLen = 0 + } + return FormatV1{ + Codec: p.Codec, + HashFun: p.MhType, + HashLen: mhLen, + } +} + +func (p FormatV0) Sum(data []byte) (*Cid, error) { + hash, err := mh.Sum(data, mh.SHA2_256, -1) + if err != nil { + return nil, err + } + return NewCidV0(hash), nil +} + +func (p FormatV1) Sum(data []byte) (*Cid, error) { + mhLen := p.HashLen + if mhLen <= 0 { + mhLen = -1 + } + hash, err := mh.Sum(data, p.HashFun, mhLen) + if err != nil { + return nil, err + } + return NewCidV1(p.Codec, hash), nil +} diff --git a/format_test.go b/format_test.go new file mode 100644 index 0000000..3f74989 --- /dev/null +++ b/format_test.go @@ -0,0 +1,58 @@ +package cid + +import ( + "testing" + + mh "github.com/multiformats/go-multihash" +) + +func TestFormatV1(t *testing.T) { + data := []byte("this is some test content") + + // Construct c1 + format := FormatV1{Codec: DagCBOR, HashFun: mh.SHA2_256} + c1, err := format.Sum(data) + if err != nil { + t.Fatal(err) + } + + // Construct c2 + hash, err := mh.Sum(data, mh.SHA2_256, -1) + if err != nil { + t.Fatal(err) + } + c2 := NewCidV1(DagCBOR, hash) + + if !c1.Equals(c2) { + t.Fatal("cids mismatch") + } + if c1.Prefix() != c2.Prefix() { + t.Fatal("prefixes mismatch") + } +} + +func TestFormatV0(t *testing.T) { + data := []byte("this is some test content") + + // Construct c1 + format := FormatV0{} + c1, err := format.Sum(data) + if err != nil { + t.Fatal(err) + } + + // Construct c2 + hash, err := mh.Sum(data, mh.SHA2_256, -1) + if err != nil { + t.Fatal(err) + } + c2 := NewCidV0(hash) + + if !c1.Equals(c2) { + t.Fatal("cids mismatch") + } + if c1.Prefix() != c2.Prefix() { + t.Fatal("prefixes mismatch") + } +} + From 6f951560f534e88d28de8c5e8ced17dfc44db549 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Thu, 28 Jun 2018 23:08:17 -0400 Subject: [PATCH 2/9] Update deprecated note to reflect code review. --- cid.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cid.go b/cid.go index 7461836..6a1f36e 100644 --- a/cid.go +++ b/cid.go @@ -444,6 +444,8 @@ func (c *Cid) Prefix() Prefix { // that is, the Version, the Codec, the Multihash type // and the Multihash length. It does not contains // any actual content information. +// NOTE: The use -1 in MhLength to mean default length is deprecated, +// use the PrefixV0 or PrefixV1 structures instead type Prefix struct { Version uint64 Codec uint64 @@ -453,7 +455,6 @@ 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. -// DEPRECATED: Use PrefixToFormat(p).Sum(data) func (p Prefix) Sum(data []byte) (*Cid, error) { hash, err := mh.Sum(data, p.MhType, p.MhLength) if err != nil { From 88cd5dcebfa9635d5eb2ada44a08252e88f7f79d Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Tue, 24 Jul 2018 22:28:19 -0400 Subject: [PATCH 3/9] Add WithCodec and GetCodec methods to format interface. --- format.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/format.go b/format.go index eda37a4..331c613 100644 --- a/format.go +++ b/format.go @@ -6,6 +6,8 @@ import ( type Format interface { Sum(data []byte) (*Cid, error) + GetCodec() uint64 + WithCodec(uint64) Format } type FormatV0 struct{} @@ -34,6 +36,21 @@ func PrefixToFormat(p Prefix) Format { } } +func (p Prefix) GetCodec() uint64 { + return p.Codec +} + +func (p Prefix) WithCodec(c uint64) Format { + if c == p.Codec { + return p + } + p.Codec = c + if c != DagProtobuf { + p.Version = 1 + } + return p +} + func (p FormatV0) Sum(data []byte) (*Cid, error) { hash, err := mh.Sum(data, mh.SHA2_256, -1) if err != nil { @@ -42,6 +59,17 @@ func (p FormatV0) Sum(data []byte) (*Cid, error) { return NewCidV0(hash), nil } +func (p FormatV0) GetCodec() uint64 { + return DagProtobuf +} + +func (p FormatV0) WithCodec(c uint64) Format { + if c == DagProtobuf { + return p + } + return FormatV1{Codec: c, HashFun: mh.SHA2_256} +} + func (p FormatV1) Sum(data []byte) (*Cid, error) { mhLen := p.HashLen if mhLen <= 0 { @@ -53,3 +81,12 @@ func (p FormatV1) Sum(data []byte) (*Cid, error) { } return NewCidV1(p.Codec, hash), nil } + +func (p FormatV1) GetCodec() uint64 { + return p.Codec +} + +func (p FormatV1) WithCodec(c uint64) Format { + p.Codec = c + return p +} From ad88cb11c56eec286fd740c948247f25fade1109 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Thu, 9 Aug 2018 00:04:26 -0400 Subject: [PATCH 4/9] Rename Format to Builder. --- format.go => builder.go | 30 +++++++++++++++--------------- format_test.go => builder_test.go | 5 ++--- cid.go | 2 +- 3 files changed, 18 insertions(+), 19 deletions(-) rename format.go => builder.go (62%) rename format_test.go => builder_test.go (91%) diff --git a/format.go b/builder.go similarity index 62% rename from format.go rename to builder.go index 331c613..524c03d 100644 --- a/format.go +++ b/builder.go @@ -4,23 +4,23 @@ import ( mh "github.com/multiformats/go-multihash" ) -type Format interface { +type Builder interface { Sum(data []byte) (*Cid, error) GetCodec() uint64 - WithCodec(uint64) Format + WithCodec(uint64) Builder } -type FormatV0 struct{} +type V0Builder struct{} -type FormatV1 struct { +type V1Builder struct { Codec uint64 HashFun uint64 HashLen int // HashLen <= 0 means the default length } -func PrefixToFormat(p Prefix) Format { +func PrefixToBuilder(p Prefix) Builder { if p.Version == 0 { - return FormatV0{} + return V0Builder{} } mhLen := p.MhLength if p.MhType == mh.ID { @@ -29,7 +29,7 @@ func PrefixToFormat(p Prefix) Format { if mhLen < 0 { mhLen = 0 } - return FormatV1{ + return V1Builder{ Codec: p.Codec, HashFun: p.MhType, HashLen: mhLen, @@ -40,7 +40,7 @@ func (p Prefix) GetCodec() uint64 { return p.Codec } -func (p Prefix) WithCodec(c uint64) Format { +func (p Prefix) WithCodec(c uint64) Builder { if c == p.Codec { return p } @@ -51,7 +51,7 @@ func (p Prefix) WithCodec(c uint64) Format { return p } -func (p FormatV0) Sum(data []byte) (*Cid, error) { +func (p V0Builder) Sum(data []byte) (*Cid, error) { hash, err := mh.Sum(data, mh.SHA2_256, -1) if err != nil { return nil, err @@ -59,18 +59,18 @@ func (p FormatV0) Sum(data []byte) (*Cid, error) { return NewCidV0(hash), nil } -func (p FormatV0) GetCodec() uint64 { +func (p V0Builder) GetCodec() uint64 { return DagProtobuf } -func (p FormatV0) WithCodec(c uint64) Format { +func (p V0Builder) WithCodec(c uint64) Builder { if c == DagProtobuf { return p } - return FormatV1{Codec: c, HashFun: mh.SHA2_256} + return V1Builder{Codec: c, HashFun: mh.SHA2_256} } -func (p FormatV1) Sum(data []byte) (*Cid, error) { +func (p V1Builder) Sum(data []byte) (*Cid, error) { mhLen := p.HashLen if mhLen <= 0 { mhLen = -1 @@ -82,11 +82,11 @@ func (p FormatV1) Sum(data []byte) (*Cid, error) { return NewCidV1(p.Codec, hash), nil } -func (p FormatV1) GetCodec() uint64 { +func (p V1Builder) GetCodec() uint64 { return p.Codec } -func (p FormatV1) WithCodec(c uint64) Format { +func (p V1Builder) WithCodec(c uint64) Builder { p.Codec = c return p } diff --git a/format_test.go b/builder_test.go similarity index 91% rename from format_test.go rename to builder_test.go index 3f74989..7a021ee 100644 --- a/format_test.go +++ b/builder_test.go @@ -10,7 +10,7 @@ func TestFormatV1(t *testing.T) { data := []byte("this is some test content") // Construct c1 - format := FormatV1{Codec: DagCBOR, HashFun: mh.SHA2_256} + format := V1Builder{Codec: DagCBOR, HashFun: mh.SHA2_256} c1, err := format.Sum(data) if err != nil { t.Fatal(err) @@ -35,7 +35,7 @@ func TestFormatV0(t *testing.T) { data := []byte("this is some test content") // Construct c1 - format := FormatV0{} + format := V0Builder{} c1, err := format.Sum(data) if err != nil { t.Fatal(err) @@ -55,4 +55,3 @@ func TestFormatV0(t *testing.T) { t.Fatal("prefixes mismatch") } } - diff --git a/cid.go b/cid.go index 6a1f36e..b9e0de4 100644 --- a/cid.go +++ b/cid.go @@ -445,7 +445,7 @@ func (c *Cid) Prefix() Prefix { // and the Multihash length. It does not contains // any actual content information. // NOTE: The use -1 in MhLength to mean default length is deprecated, -// use the PrefixV0 or PrefixV1 structures instead +// use the V0Builder or V1Builder structures instead type Prefix struct { Version uint64 Codec uint64 From 67951e2c090082000abd8644c01859dadc7aa3bd Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Thu, 9 Aug 2018 00:09:33 -0400 Subject: [PATCH 5/9] Move deprecated function to there own file. --- cid.go | 23 ----------------------- deprecated.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 23 deletions(-) create mode 100644 deprecated.go diff --git a/cid.go b/cid.go index b9e0de4..4c91344 100644 --- a/cid.go +++ b/cid.go @@ -144,29 +144,6 @@ func NewCidV1(codecType uint64, mhash mh.Multihash) *Cid { } } -// NewPrefixV0 returns a CIDv0 prefix with the specified multihash type. -// DEPRECATED: Use FormatV0 -func NewPrefixV0(mhType uint64) Prefix { - return Prefix{ - MhType: mhType, - MhLength: mh.DefaultLengths[mhType], - Version: 0, - Codec: DagProtobuf, - } -} - -// NewPrefixV1 returns a CIDv1 prefix with the specified codec and multihash -// type. -// DEPRECATED: Use FormatV1 -func NewPrefixV1(codecType uint64, mhType uint64) Prefix { - return Prefix{ - MhType: mhType, - MhLength: mh.DefaultLengths[mhType], - Version: 1, - Codec: codecType, - } -} - // Cid represents a self-describing content adressed // identifier. It is formed by a Version, a Codec (which indicates // a multicodec-packed content type) and a Multihash. diff --git a/deprecated.go b/deprecated.go new file mode 100644 index 0000000..611ca09 --- /dev/null +++ b/deprecated.go @@ -0,0 +1,28 @@ +package cid + +import ( + mh "github.com/multiformats/go-multihash" +) + +// NewPrefixV0 returns a CIDv0 prefix with the specified multihash type. +// DEPRECATED: Use FormatV0 +func NewPrefixV0(mhType uint64) Prefix { + return Prefix{ + MhType: mhType, + MhLength: mh.DefaultLengths[mhType], + Version: 0, + Codec: DagProtobuf, + } +} + +// NewPrefixV1 returns a CIDv1 prefix with the specified codec and multihash +// type. +// DEPRECATED: Use FormatV1 +func NewPrefixV1(codecType uint64, mhType uint64) Prefix { + return Prefix{ + MhType: mhType, + MhLength: mh.DefaultLengths[mhType], + Version: 1, + Codec: codecType, + } +} From ae25e25d1a8edbd26650ffafaf46946dbdedcfda Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Thu, 9 Aug 2018 23:14:37 -0400 Subject: [PATCH 6/9] Remove PrefixToBuilder as it is needed right now. --- builder.go | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/builder.go b/builder.go index 524c03d..fec6590 100644 --- a/builder.go +++ b/builder.go @@ -18,24 +18,6 @@ type V1Builder struct { HashLen int // HashLen <= 0 means the default length } -func PrefixToBuilder(p Prefix) Builder { - if p.Version == 0 { - return V0Builder{} - } - mhLen := p.MhLength - if p.MhType == mh.ID { - mhLen = 0 - } - if mhLen < 0 { - mhLen = 0 - } - return V1Builder{ - Codec: p.Codec, - HashFun: p.MhType, - HashLen: mhLen, - } -} - func (p Prefix) GetCodec() uint64 { return p.Codec } From 8f7ba15bfbf88177acc08ef5ea5de28736cf7c72 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Thu, 9 Aug 2018 23:55:33 -0400 Subject: [PATCH 7/9] Documentation Cleanups. --- deprecated.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/deprecated.go b/deprecated.go index 611ca09..cd889f9 100644 --- a/deprecated.go +++ b/deprecated.go @@ -5,7 +5,7 @@ import ( ) // NewPrefixV0 returns a CIDv0 prefix with the specified multihash type. -// DEPRECATED: Use FormatV0 +// DEPRECATED: Use V0Builder func NewPrefixV0(mhType uint64) Prefix { return Prefix{ MhType: mhType, @@ -17,7 +17,7 @@ func NewPrefixV0(mhType uint64) Prefix { // NewPrefixV1 returns a CIDv1 prefix with the specified codec and multihash // type. -// DEPRECATED: Use FormatV1 +// DEPRECATED: Use V1Builder func NewPrefixV1(codecType uint64, mhType uint64) Prefix { return Prefix{ MhType: mhType, From f8683758253513b2cad8e61a67aa195c019ccb96 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Thu, 9 Aug 2018 23:55:45 -0400 Subject: [PATCH 8/9] Enhance Tests. --- builder_test.go | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/builder_test.go b/builder_test.go index 7a021ee..82c3b1c 100644 --- a/builder_test.go +++ b/builder_test.go @@ -6,11 +6,11 @@ import ( mh "github.com/multiformats/go-multihash" ) -func TestFormatV1(t *testing.T) { +func TestV0Builder(t *testing.T) { data := []byte("this is some test content") // Construct c1 - format := V1Builder{Codec: DagCBOR, HashFun: mh.SHA2_256} + format := V0Builder{} c1, err := format.Sum(data) if err != nil { t.Fatal(err) @@ -21,7 +21,7 @@ func TestFormatV1(t *testing.T) { if err != nil { t.Fatal(err) } - c2 := NewCidV1(DagCBOR, hash) + c2 := NewCidV0(hash) if !c1.Equals(c2) { t.Fatal("cids mismatch") @@ -31,11 +31,11 @@ func TestFormatV1(t *testing.T) { } } -func TestFormatV0(t *testing.T) { +func TestV1Builder(t *testing.T) { data := []byte("this is some test content") // Construct c1 - format := V0Builder{} + format := V1Builder{Codec: DagCBOR, HashFun: mh.SHA2_256} c1, err := format.Sum(data) if err != nil { t.Fatal(err) @@ -46,7 +46,7 @@ func TestFormatV0(t *testing.T) { if err != nil { t.Fatal(err) } - c2 := NewCidV0(hash) + c2 := NewCidV1(DagCBOR, hash) if !c1.Equals(c2) { t.Fatal("cids mismatch") @@ -55,3 +55,38 @@ func TestFormatV0(t *testing.T) { t.Fatal("prefixes mismatch") } } + +func TestCodecChange(t *testing.T) { + t.Run("Prefix-CidV0", func(t *testing.T) { + p := Prefix{Version: 0, Codec: DagProtobuf, MhType: mh.SHA2_256, MhLength: mh.DefaultLengths[mh.SHA2_256]} + testCodecChange(t, p) + }) + t.Run("Prefix-CidV1", func(t *testing.T) { + p := Prefix{Version: 1, Codec: DagProtobuf, MhType: mh.SHA2_256, MhLength: mh.DefaultLengths[mh.SHA2_256]} + testCodecChange(t, p) + }) + t.Run("V0Builder", func(t *testing.T) { + testCodecChange(t, V0Builder{}) + }) + t.Run("V1Builder", func(t *testing.T) { + testCodecChange(t, V1Builder{Codec: DagProtobuf, HashFun: mh.SHA2_256}) + }) +} + +func testCodecChange(t *testing.T, b Builder) { + data := []byte("this is some test content") + + if b.GetCodec() != DagProtobuf { + t.Fatal("original builder not using Protobuf codec") + } + + b = b.WithCodec(Raw) + c, err := b.Sum(data) + if err != nil { + t.Fatal(err) + } + + if c.Type() != Raw { + t.Fatal("new cid codec did not change to Raw") + } +} From 86805e711c0b28208aeb4ba3e8599ef7967a53c6 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Fri, 10 Aug 2018 00:13:49 -0400 Subject: [PATCH 9/9] Change field names in V1Builder to match Prefix. --- builder.go | 12 ++++++------ builder_test.go | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/builder.go b/builder.go index fec6590..af33aac 100644 --- a/builder.go +++ b/builder.go @@ -13,9 +13,9 @@ type Builder interface { type V0Builder struct{} type V1Builder struct { - Codec uint64 - HashFun uint64 - HashLen int // HashLen <= 0 means the default length + Codec uint64 + MhType uint64 + MhLength int // MhLength <= 0 means the default length } func (p Prefix) GetCodec() uint64 { @@ -49,15 +49,15 @@ func (p V0Builder) WithCodec(c uint64) Builder { if c == DagProtobuf { return p } - return V1Builder{Codec: c, HashFun: mh.SHA2_256} + return V1Builder{Codec: c, MhType: mh.SHA2_256} } func (p V1Builder) Sum(data []byte) (*Cid, error) { - mhLen := p.HashLen + mhLen := p.MhLength if mhLen <= 0 { mhLen = -1 } - hash, err := mh.Sum(data, p.HashFun, mhLen) + hash, err := mh.Sum(data, p.MhType, mhLen) if err != nil { return nil, err } diff --git a/builder_test.go b/builder_test.go index 82c3b1c..dc6f35c 100644 --- a/builder_test.go +++ b/builder_test.go @@ -35,7 +35,7 @@ func TestV1Builder(t *testing.T) { data := []byte("this is some test content") // Construct c1 - format := V1Builder{Codec: DagCBOR, HashFun: mh.SHA2_256} + format := V1Builder{Codec: DagCBOR, MhType: mh.SHA2_256} c1, err := format.Sum(data) if err != nil { t.Fatal(err) @@ -69,7 +69,7 @@ func TestCodecChange(t *testing.T) { testCodecChange(t, V0Builder{}) }) t.Run("V1Builder", func(t *testing.T) { - testCodecChange(t, V1Builder{Codec: DagProtobuf, HashFun: mh.SHA2_256}) + testCodecChange(t, V1Builder{Codec: DagProtobuf, MhType: mh.SHA2_256}) }) }