Commit d2e4bad4 authored by Brian Tiger Chow's avatar Brian Tiger Chow

fix(bitswap:message) don't use proto internally

parent c1873b89
...@@ -26,45 +26,45 @@ type Exportable interface { ...@@ -26,45 +26,45 @@ type Exportable interface {
// message wraps a proto message for convenience // message wraps a proto message for convenience
type message struct { type message struct {
pb PBMessage wantlist []u.Key
} blocks []blocks.Block
func newMessageFromProto(pb PBMessage) *message {
return &message{pb: pb}
} }
func New() *message { func New() *message {
return new(message) return new(message)
} }
func newMessageFromProto(pbm PBMessage) (BitSwapMessage, error) {
m := New()
for _, s := range pbm.GetWantlist() {
m.AppendWanted(u.Key(s))
}
for _, d := range pbm.GetBlocks() {
b, err := blocks.NewBlock(d)
if err != nil {
return nil, err
}
m.AppendBlock(*b)
}
return m, nil
}
// TODO(brian): convert these into keys // TODO(brian): convert these into keys
func (m *message) Wantlist() []u.Key { func (m *message) Wantlist() []u.Key {
wl := make([]u.Key, len(m.pb.Wantlist)) return m.wantlist
for _, str := range m.pb.Wantlist {
wl = append(wl, u.Key(str))
}
return wl
} }
// TODO(brian): convert these into blocks // TODO(brian): convert these into blocks
func (m *message) Blocks() []blocks.Block { func (m *message) Blocks() []blocks.Block {
bs := make([]blocks.Block, len(m.pb.Blocks)) return m.blocks
for _, data := range m.pb.Blocks {
b, err := blocks.NewBlock(data)
if err != nil {
continue
}
bs = append(bs, *b)
}
return bs
} }
func (m *message) AppendWanted(k u.Key) { func (m *message) AppendWanted(k u.Key) {
m.pb.Wantlist = append(m.pb.Wantlist, string(k)) m.wantlist = append(m.wantlist, k)
} }
func (m *message) AppendBlock(b blocks.Block) { func (m *message) AppendBlock(b blocks.Block) {
m.pb.Blocks = append(m.pb.Blocks, b.Data) m.blocks = append(m.blocks, b)
} }
func FromNet(nmsg netmsg.NetMessage) (BitSwapMessage, error) { func FromNet(nmsg netmsg.NetMessage) (BitSwapMessage, error) {
...@@ -72,8 +72,14 @@ func FromNet(nmsg netmsg.NetMessage) (BitSwapMessage, error) { ...@@ -72,8 +72,14 @@ func FromNet(nmsg netmsg.NetMessage) (BitSwapMessage, error) {
} }
func (m *message) ToProto() *PBMessage { func (m *message) ToProto() *PBMessage {
cp := m.pb pb := new(PBMessage)
return &cp for _, k := range m.Wantlist() {
pb.Wantlist = append(pb.Wantlist, string(k))
}
for _, b := range m.Blocks() {
pb.Blocks = append(pb.Blocks, b.Data)
}
return pb
} }
func (m *message) ToNet(p *peer.Peer) (nm.NetMessage, error) { func (m *message) ToNet(p *peer.Peer) (nm.NetMessage, error) {
......
...@@ -25,7 +25,10 @@ func TestNewMessageFromProto(t *testing.T) { ...@@ -25,7 +25,10 @@ func TestNewMessageFromProto(t *testing.T) {
if !contains(protoMessage.Wantlist, str) { if !contains(protoMessage.Wantlist, str) {
t.Fail() t.Fail()
} }
m := newMessageFromProto(*protoMessage) m, err := newMessageFromProto(*protoMessage)
if err != nil {
t.Fatal(err)
}
if !contains(m.ToProto().GetWantlist(), str) { if !contains(m.ToProto().GetWantlist(), str) {
t.Fail() t.Fail()
} }
...@@ -52,6 +55,29 @@ func TestAppendBlock(t *testing.T) { ...@@ -52,6 +55,29 @@ func TestAppendBlock(t *testing.T) {
} }
} }
func TestWantlist(t *testing.T) {
keystrs := []string{"foo", "bar", "baz", "bat"}
m := New()
for _, s := range keystrs {
m.AppendWanted(u.Key(s))
}
exported := m.Wantlist()
for _, k := range exported {
present := false
for _, s := range keystrs {
if s == string(k) {
present = true
}
}
if !present {
t.Logf("%v isn't in original list", string(k))
t.Fail()
}
}
}
func TestCopyProtoByValue(t *testing.T) { func TestCopyProtoByValue(t *testing.T) {
const str = "foo" const str = "foo"
m := New() m := New()
......
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