func (m *impl) ToProto() *pb.Message { pbm := new(pb.Message) pbm.Wantlist = new(pb.Message_Wantlist) for _, e := range m.wantlist { pbm.Wantlist.Entries = append(pbm.Wantlist.Entries, &pb.Message_Wantlist_Entry{ Block: proto.String(string(e.Key)), Priority: proto.Int32(int32(e.Priority)), Cancel: proto.Bool(e.Cancel), }) } for _, b := range m.Blocks() { pbm.Blocks = append(pbm.Blocks, b.Data) } return pbm }
func TestNewMessageFromProto(t *testing.T) { const str = "a_key" protoMessage := new(pb.Message) protoMessage.Wantlist = new(pb.Message_Wantlist) protoMessage.Wantlist.Entries = []*pb.Message_Wantlist_Entry{ {Block: proto.String(str)}, } if !wantlistContains(protoMessage.Wantlist, str) { t.Fail() } m := newMessageFromProto(*protoMessage) if !wantlistContains(m.ToProto().GetWantlist(), str) { t.Fail() } }
func newMessageFromProto(pbm pb.Message) BitSwapMessage { m := newMsg(pbm.GetWantlist().GetFull()) for _, e := range pbm.GetWantlist().GetEntries() { m.addEntry(key.Key(e.GetBlock()), int(e.GetPriority()), e.GetCancel()) } for _, d := range pbm.GetBlocks() { b := blocks.NewBlock(d) m.AddBlock(b) } return m }