Repository: ipfs/go-bitswap
Pull request #308 (2020-03-18 23:26:45)

feat: add a custom CID type
This allows us to marshal/unmarshal/size protobufs without copying CID around.

Stebalien: > This should be backwards compatible right?

Should be. But let me add a quick test.

Patch:
diff --git a/message/message.go b/message/message.go
index 6668e7c..7c53148 100644
--- a/message/message.go
+++ b/message/message.go
@@ -2,7 +2,7 @@ package message
 
 import (
 	"encoding/binary"
-	"fmt"
+	"errors"
 	"io"
 
 	pb "github.com/ipfs/go-bitswap/message/pb"
@@ -117,14 +117,15 @@ type Entry struct {
 	SendDontHave bool
 }
 
+var errCidMissing = errors.New("missing cid")
+
 func newMessageFromProto(pbm pb.Message) (BitSwapMessage, error) {
 	m := newMsg(pbm.Wantlist.Full)
 	for _, e := range pbm.Wantlist.Entries {
-		c, err := cid.Cast([]byte(e.Block))
-		if err != nil {
-			return nil, fmt.Errorf("incorrectly formatted cid in wantlist: %s", err)
+		if !e.Block.Cid.Defined() {
+			return nil, errCidMissing
 		}
-		m.addEntry(c, e.Priority, e.Cancel, e.WantType, e.SendDontHave)
+		m.addEntry(e.Block.Cid, e.Priority, e.Cancel, e.WantType, e.SendDontHave)
 	}
 
 	// deprecated
@@ -155,13 +156,10 @@ func newMessageFromProto(pbm pb.Message) (BitSwapMessage, error) {
 	}
 
 	for _, bi := range pbm.GetBlockPresences() {
-		c, err := cid.Cast(bi.GetCid())
-		if err != nil {
-			return nil, err
+		if !bi.Cid.Cid.Defined() {
+			return nil, errCidMissing
 		}
-
-		t := bi.GetType()
-		m.AddBlockPresence(c, t)
+		m.AddBlockPresence(bi.Cid.Cid, bi.Type)
 	}
 
 	m.pendingBytes = pbm.PendingBytes
@@ -311,7 +309,7 @@ func (m *impl) Size() int {
 
 func BlockPresenceSize(c cid.Cid) int {
 	return (&pb.Message_BlockPresence{
-		Cid:  c.Bytes(),
+		Cid:  pb.Cid{Cid: c},
 		Type: pb.Message_Have,
 	}).Size()
 }
@@ -341,7 +339,7 @@ func FromMsgReader(r msgio.Reader) (BitSwapMessage, error) {
 
 func entryToPB(e *Entry) pb.Message_Wantlist_Entry {
 	return pb.Message_Wantlist_Entry{
-		Block:        e.Cid.Bytes(),
+		Block:        pb.Cid{Cid: e.Cid},
 		Priority:     int32(e.Priority),
 		Cancel:       e.Cancel,
 		WantType:     e.WantType,
@@ -385,7 +383,7 @@ func (m *impl) ToProtoV1() *pb.Message {
 	pbm.BlockPresences = make([]pb.Message_BlockPresence, 0, len(m.blockPresences))
 	for c, t := range m.blockPresences {
 		pbm.BlockPresences = append(pbm.BlockPresences, pb.Message_BlockPresence{
-			Cid:  c.Bytes(),
+			Cid:  pb.Cid{Cid: c},
 			Type: t,
 		})
 	}
diff --git a/message/pb/cid.go b/message/pb/cid.go
new file mode 100644
index 0000000..59e32bb
--- /dev/null
+++ b/message/pb/cid.go
@@ -0,0 +1,43 @@
+package bitswap_message_pb
+
+import (
+	"github.com/ipfs/go-cid"
+)
+
+// NOTE: Don't "embed" the cid, wrap it like we're doing here. Otherwise, gogo
+// will try to use the Bytes() function.
+
+// Cid is a custom type for CIDs in protobufs, that allows us to avoid
+// reallocating.
+type Cid struct {
+	Cid cid.Cid
+}
+
+func (c Cid) Marshal() ([]byte, error) {
+	return c.Cid.Bytes(), nil
+}
+
+func (c *Cid) MarshalTo(data []byte) (int, error) {
+	return copy(data[:c.Size()], c.Cid.Bytes()), nil
+}
+
+func (c *Cid) Unmarshal(data []byte) (err error) {
+	c.Cid, err = cid.Cast(data)
+	return err
+}
+
+func (c *Cid) Size() int {
+	return len(c.Cid.KeyString())
+}
+
+func (c Cid) MarshalJSON() ([]byte, error) {
+	return c.Cid.MarshalJSON()
+}
+
+func (c *Cid) UnmarshalJSON(data []byte) error {
+	return c.Cid.UnmarshalJSON(data)
+}
+
+func (c Cid) Equal(other Cid) bool {
+	return c.Cid.Equals(c.Cid)
+}
diff --git a/message/pb/message.pb.go b/message/pb/message.pb.go
index b64e308..c1effb8 100644
--- a/message/pb/message.pb.go
+++ b/message/pb/message.pb.go
@@ -21,7 +21,7 @@ var _ = math.Inf
 // is compatible with the proto package it is being compiled against.
 // A compilation error at this line likely means your copy of the
 // proto package needs to be updated.
-const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package
+const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package
 
 type Message_BlockPresenceType int32
 
@@ -202,7 +202,7 @@ func (m *Message_Wantlist) GetFull() bool {
 }
 
 type Message_Wantlist_Entry struct {
-	Block        []byte                    `protobuf:"bytes,1,opt,name=block,proto3" json:"block,omitempty"`
+	Block        Cid                       `protobuf:"bytes,1,opt,name=block,proto3,customtype=Cid" json:"block"`
 	Priority     int32                     `protobuf:"varint,2,opt,name=priority,proto3" json:"priority,omitempty"`
 	Cancel       bool                      `protobuf:"varint,3,opt,name=cancel,proto3" json:"cancel,omitempty"`
 	WantType     Message_Wantlist_WantType `protobuf:"varint,4,opt,name=wantType,proto3,enum=bitswap.message.pb.Message_Wantlist_WantType" json:"wantType,omitempty"`
@@ -242,13 +242,6 @@ func (m *Message_Wantlist_Entry) XXX_DiscardUnknown() {
 
 var xxx_messageInfo_Message_Wantlist_Entry proto.InternalMessageInfo
 
-func (m *Message_Wantlist_Entry) GetBlock() []byte {
-	if m != nil {
-		return m.Block
-	}
-	return nil
-}
-
 func (m *Message_Wantlist_Entry) GetPriority() int32 {
 	if m != nil {
 		return m.Priority
@@ -330,7 +323,7 @@ func (m *Message_Block) GetData() []byte {
 }
 
 type Message_BlockPresence struct {
-	Cid  []byte                    `protobuf:"bytes,1,opt,name=cid,proto3" json:"cid,omitempty"`
+	Cid  Cid                       `protobuf:"bytes,1,opt,name=cid,proto3,customtype=Cid" json:"cid"`
 	Type Message_BlockPresenceType `protobuf:"varint,2,opt,name=type,proto3,enum=bitswap.message.pb.Message_BlockPresenceType" json:"type,omitempty"`
 }
 
@@ -367,13 +360,6 @@ func (m *Message_BlockPresence) XXX_DiscardUnknown() {
 
 var xxx_messageInfo_Message_BlockPresence proto.InternalMessageInfo
 
-func (m *Message_BlockPresence) GetCid() []byte {
-	if m != nil {
-		return m.Cid
-	}
-	return nil
-}
-
 func (m *Message_BlockPresence) GetType() Message_BlockPresenceType {
 	if m != nil {
 		return m.Type
@@ -394,38 +380,39 @@ func init() {
 func init() { proto.RegisterFile("message.proto", fileDescriptor_33c57e4bae7b9afd) }
 
 var fileDescriptor_33c57e4bae7b9afd = []byte{
-	// 483 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x53, 0x4d, 0x6b, 0xd4, 0x50,
-	0x14, 0xcd, 0x9b, 0x24, 0x9d, 0x78, 0x9b, 0x96, 0xf1, 0x21, 0xf2, 0xc8, 0x22, 0x8d, 0x83, 0x8b,
-	0xa8, 0x34, 0x85, 0xe9, 0x2f, 0xe8, 0xa0, 0xa2, 0x82, 0x20, 0x41, 0x98, 0x75, 0x3e, 0xde, 0xc4,
-	0x60, 0x9a, 0x84, 0xbc, 0x37, 0xd6, 0xfc, 0x0b, 0x7f, 0x92, 0xb8, 0xea, 0x4a, 0xba, 0x74, 0x25,
-	0x32, 0xf3, 0x47, 0x24, 0x37, 0x2f, 0x81, 0xb1, 0x60, 0xbb, 0xbb, 0xe7, 0xbe, 0x7b, 0x4e, 0xee,
-	0xb9, 0x87, 0xc0, 0xd1, 0x25, 0x17, 0x22, 0xca, 0x78, 0x50, 0x37, 0x95, 0xac, 0x28, 0x8d, 0x73,
-	0x29, 0xae, 0xa2, 0x3a, 0x18, 0xdb, 0xb1, 0x73, 0x9a, 0xe5, 0xf2, 0xd3, 0x26, 0x0e, 0x92, 0xea,
-	0xf2, 0x2c, 0xab, 0xb2, 0xea, 0x0c, 0x47, 0xe3, 0xcd, 0x1a, 0x11, 0x02, 0xac, 0x7a, 0x89, 0xf9,
-	0x8f, 0x03, 0x98, 0xbe, 0xef, 0xd9, 0xf4, 0x35, 0x58, 0x57, 0x51, 0x29, 0x8b, 0x5c, 0x48, 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xa2, 0x6f, 0xfb, 0x80, 0x3f, 0xb6, 0x35, 0x67, 0x86, 0x47, 0xfc, 0xe3, 0xc5,
-	0xe9, 0xbd, 0x96, 0x5e, 0x29, 0x52, 0x38, 0xd2, 0xbb, 0x7b, 0x09, 0x5e, 0xa6, 0x2f, 0xab, 0x52,
-	0xbe, 0x89, 0xbe, 0x70, 0xbc, 0x97, 0x15, 0xee, 0xf5, 0xe6, 0x27, 0xfd, 0xb9, 0x70, 0xfe, 0x01,
-	0x98, 0x18, 0xc3, 0x4c, 0xa3, 0x16, 0x18, 0xdd, 0xf3, 0x8c, 0x38, 0xe7, 0xaa, 0xd9, 0x2d, 0x5c,
-	0x37, 0x7c, 0x9d, 0x7f, 0x55, 0x1e, 0x15, 0xea, 0x0e, 0x93, 0x46, 0x32, 0x42, 0x83, 0x76, 0x88,
-	0xb5, 0x93, 0xc2, 0xd1, 0x5e, 0xa0, 0x74, 0x06, 0x7a, 0x92, 0xa7, 0x8a, 0xd9, 0x95, 0xf4, 0x02,
-	0x0c, 0xd9, 0x79, 0x9c, 0xdc, 0xed, 0x71, 0x4f, 0x0a, 0x3d, 0x22, 0x75, 0xfe, 0x02, 0x1e, 0xde,
-	0x7a, 0x1a, 0x37, 0xd7, 0xa8, 0x0d, 0xd6, 0x60, 0x73, 0x46, 0x96, 0xec, 0x7a, 0xeb, 0x92, 0x9b,
-	0xad, 0x4b, 0xfe, 0x6c, 0x5d, 0xf2, 0x6d, 0xe7, 0x6a, 0x37, 0x3b, 0x57, 0xfb, 0xb5, 0x73, 0xb5,
-	0xf8, 0x00, 0xff, 0xb2, 0xf3, 0xbf, 0x01, 0x00, 0x00, 0xff, 0xff, 0xac, 0xa9, 0xf7, 0xab, 0xb9,
-	0x03, 0x00, 0x00,
+	// 497 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x93, 0xdf, 0x8a, 0xd3, 0x40,
+	0x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xec, 0x70, 0xfe, 0x1e, 0xd8, 0xb8, 0x93, 0xa1, 0x41,
+	0x1d, 0xb0, 0x9a, 0xeb, 0x21, 0xf1, 0x4e, 0x75, 0xb3, 0x79, 0x70, 0x55, 0xf3, 0xb9, 0xf8, 0xd2,
+	0x1a, 0x66, 0xba, 0x6a, 0x52, 0xca, 0x62, 0x15, 0xa3, 0xc1, 0x01, 0xc3, 0xb3, 0xf7, 0x09, 0x0e,
+	0xf6, 0xb6, 0x4b, 0x1f, 0x82, 0x99, 0x8a, 0xec, 0x5f, 0x51, 0x35, 0x7d, 0x7a, 0x06, 0x96, 0x6a,
+	0x0c, 0x77, 0x6e, 0x37, 0xbc, 0xc7, 0x45, 0xc3, 0x28, 0x1d, 0x3f, 0x85, 0xfb, 0x37, 0xae, 0x76,
+	0x36, 0x0c, 0x3a, 0x00, 0x67, 0xeb, 0x79, 0x48, 0xa6, 0xee, 0xe5, 0xda, 0x27, 0x57, 0x6b, 0x9f,
+	0xfc, 0x5e, 0xfb, 0xe4, 0xeb, 0xc6, 0x37, 0xae, 0x36, 0xbe, 0xf1, 0x7d, 0xe3, 0x1b, 0x49, 0x17,
+	0xff, 0xbf, 0xd3, 0x3f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x8a, 0x8a, 0xaf, 0x83, 0xd3, 0x03, 0x00,
+	0x00,
 }
 
 func (m *Message) Marshal() (dAtA []byte, err error) {
@@ -600,13 +587,16 @@ func (m *Message_Wantlist_Entry) MarshalToSizedBuffer(dAtA []byte) (int, error)
 		i--
 		dAtA[i] = 0x10
 	}
-	if len(m.Block) > 0 {
-		i -= len(m.Block)
-		copy(dAtA[i:], m.Block)
-		i = encodeVarintMessage(dAtA, i, uint64(len(m.Block)))
-		i--
-		dAtA[i] = 0xa
+	{
+		size := m.Block.Size()
+		i -= size
+		if _, err := m.Block.MarshalTo(dAtA[i:]); err != nil {
+			return 0, err
+		}
+		i = encodeVarintMessage(dAtA, i, uint64(size))
 	}
+	i--
+	dAtA[i] = 0xa
 	return len(dAtA) - i, nil
 }
 
@@ -672,13 +662,16 @@ func (m *Message_BlockPresence) MarshalToSizedBuffer(dAtA []byte) (int, error) {
 		i--
 		dAtA[i] = 0x10
 	}
-	if len(m.Cid) > 0 {
-		i -= len(m.Cid)
-		copy(dAtA[i:], m.Cid)
-		i = encodeVarintMessage(dAtA, i, uint64(len(m.Cid)))
-		i--
-		dAtA[i] = 0xa
+	{
+		size := m.Cid.Size()
+		i -= size
+		if _, err := m.Cid.MarshalTo(dAtA[i:]); err != nil {
+			return 0, err
+		}
+		i = encodeVarintMessage(dAtA, i, uint64(size))
 	}
+	i--
+	dAtA[i] = 0xa
 	return len(dAtA) - i, nil
 }
 
@@ -749,10 +742,8 @@ func (m *Message_Wantlist_Entry) Size() (n int) {
 	}
 	var l int
 	_ = l
-	l = len(m.Block)
-	if l > 0 {
-		n += 1 + l + sovMessage(uint64(l))
-	}
+	l = m.Block.Size()
+	n += 1 + l + sovMessage(uint64(l))
 	if m.Priority != 0 {
 		n += 1 + sovMessage(uint64(m.Priority))
 	}
@@ -791,10 +782,8 @@ func (m *Message_BlockPresence) Size() (n int) {
 	}
 	var l int
 	_ = l
-	l = len(m.Cid)
-	if l > 0 {
-		n += 1 + l + sovMessage(uint64(l))
-	}
+	l = m.Cid.Size()
+	n += 1 + l + sovMessage(uint64(l))
 	if m.Type != 0 {
 		n += 1 + sovMessage(uint64(m.Type))
 	}
@@ -1177,9 +1166,8 @@ func (m *Message_Wantlist_Entry) Unmarshal(dAtA []byte) error {
 			if postIndex > l {
 				return io.ErrUnexpectedEOF
 			}
-			m.Block = append(m.Block[:0], dAtA[iNdEx:postIndex]...)
-			if m.Block == nil {
-				m.Block = []byte{}
+			if err := m.Block.Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
+				return err
 			}
 			iNdEx = postIndex
 		case 2:
@@ -1463,9 +1451,8 @@ func (m *Message_BlockPresence) Unmarshal(dAtA []byte) error {
 			if postIndex > l {
 				return io.ErrUnexpectedEOF
 			}
-			m.Cid = append(m.Cid[:0], dAtA[iNdEx:postIndex]...)
-			if m.Cid == nil {
-				m.Cid = []byte{}
+			if err := m.Cid.Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
+				return err
 			}
 			iNdEx = postIndex
 		case 2:
@@ -1514,6 +1501,7 @@ func (m *Message_BlockPresence) Unmarshal(dAtA []byte) error {
 func skipMessage(dAtA []byte) (n int, err error) {
 	l := len(dAtA)
 	iNdEx := 0
+	depth := 0
 	for iNdEx < l {
 		var wire uint64
 		for shift := uint(0); ; shift += 7 {
@@ -1545,10 +1533,8 @@ func skipMessage(dAtA []byte) (n int, err error) {
 					break
 				}
 			}
-			return iNdEx, nil
 		case 1:
 			iNdEx += 8
-			return iNdEx, nil
 		case 2:
 			var length int
 			for shift := uint(0); ; shift += 7 {
@@ -1569,55 +1555,30 @@ func skipMessage(dAtA []byte) (n int, err error) {
 				return 0, ErrInvalidLengthMessage
 			}
 			iNdEx += length
-			if iNdEx < 0 {
-				return 0, ErrInvalidLengthMessage
-			}
-			return iNdEx, nil
 		case 3:
-			for {
-				var innerWire uint64
-				var start int = iNdEx
-				for shift := uint(0); ; shift += 7 {
-					if shift >= 64 {
-						return 0, ErrIntOverflowMessage
-					}
-					if iNdEx >= l {
-						return 0, io.ErrUnexpectedEOF
-					}
-					b := dAtA[iNdEx]
-					iNdEx++
-					innerWire |= (uint64(b) & 0x7F) << shift
-					if b < 0x80 {
-						break
-					}
-				}
-				innerWireType := int(innerWire & 0x7)
-				if innerWireType == 4 {
-					break
-				}
-				next, err := skipMessage(dAtA[start:])
-				if err != nil {
-					return 0, err
-				}
-				iNdEx = start + next
-				if iNdEx < 0 {
-					return 0, ErrInvalidLengthMessage
-				}
-			}
-			return iNdEx, nil
+			depth++
 		case 4:
-			return iNdEx, nil
+			if depth == 0 {
+				return 0, ErrUnexpectedEndOfGroupMessage
+			}
+			depth--
 		case 5:
 			iNdEx += 4
-			return iNdEx, nil
 		default:
 			return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
 		}
+		if iNdEx < 0 {
+			return 0, ErrInvalidLengthMessage
+		}
+		if depth == 0 {
+			return iNdEx, nil
+		}
 	}
-	panic("unreachable")
+	return 0, io.ErrUnexpectedEOF
 }
 
 var (
-	ErrInvalidLengthMessage = fmt.Errorf("proto: negative length found during unmarshaling")
-	ErrIntOverflowMessage   = fmt.Errorf("proto: integer overflow")
+	ErrInvalidLengthMessage        = fmt.Errorf("proto: negative length found during unmarshaling")
+	ErrIntOverflowMessage          = fmt.Errorf("proto: integer overflow")
+	ErrUnexpectedEndOfGroupMessage = fmt.Errorf("proto: unexpected end of group")
 )
diff --git a/message/pb/message.proto b/message/pb/message.proto
index f7afdb1..e6c271c 100644
--- a/message/pb/message.proto
+++ b/message/pb/message.proto
@@ -13,7 +13,7 @@ message Message {
     }
 
     message Entry {
-			bytes block = 1;		// the block cid (cidV0 in bitswap 1.0.0, cidV1 in bitswap 1.1.0)
+			bytes block = 1 [(gogoproto.customtype) = "Cid", (gogoproto.nullable) = false];		// the block cid (cidV0 in bitswap 1.0.0, cidV1 in bitswap 1.1.0)
 			int32 priority = 2;	// the priority (normalized). default to 1
 			bool cancel = 3;		// whether this revokes an entry
       WantType wantType = 4; // Note: defaults to enum 0, ie Block
@@ -34,7 +34,7 @@ message Message {
     DontHave = 1;
   }
   message BlockPresence {
-    bytes cid = 1;
+    bytes cid = 1 [(gogoproto.customtype) = "Cid", (gogoproto.nullable) = false];
     BlockPresenceType type = 2;
   }
 


Test patch:
diff --git a/message/message_test.go b/message/message_test.go
index 4b51a3c..aa58fa0 100644
--- a/message/message_test.go
+++ b/message/message_test.go
@@ -29,7 +29,7 @@ func TestNewMessageFromProto(t *testing.T) {
 	str := mkFakeCid("a_key")
 	protoMessage := new(pb.Message)
 	protoMessage.Wantlist.Entries = []pb.Message_Wantlist_Entry{
-		{Block: str.Bytes()},
+		{Block: pb.Cid{Cid: str}},
 	}
 	if !wantlistContains(&protoMessage.Wantlist, str) {
 		t.Fail()
@@ -164,7 +164,7 @@ func TestToAndFromNetMessage(t *testing.T) {
 
 func wantlistContains(wantlist *pb.Message_Wantlist, c cid.Cid) bool {
 	for _, e := range wantlist.GetEntries() {
-		if bytes.Equal(e.GetBlock(), c.Bytes()) {
+		if e.Block.Cid.Defined() && c.Equals(e.Block.Cid) {
 			return true
 		}
 	}