@cryptotaxi247 / kubo / commits / caedb6469

fix(bitswap/message) impl with map to ensure no duplicate blocks

comes at the cost of O(n) Blocks() method.

Brian Tiger Chow committed Oct 27, 2014 at 06:16 UTC caedb64697fe9f37a72640957784f8425e675f5b
1 file changed +8 -4
exchange/bitswap/message/message.go
+8 -4
@@ -26,15 +26,15 @@ type Exportable interface {
26 ToNet(p peer.Peer) (nm.NetMessage, error)
27 }
28
29 -// message wraps a proto message for convenience
29 type impl struct {
30 wantlist map[u.Key]struct{}
32 - blocks []blocks.Block
31 + blocks map[u.Key]blocks.Block
32 }
33
34 func New() BitSwapMessage {
35 return &impl{
36 wantlist: make(map[u.Key]struct{}),
37 + blocks: make(map[u.Key]blocks.Block),
38 }
39 }
40
@@ -61,7 +61,11 @@ func (m *impl) Wantlist() []u.Key {
61
62 // TODO(brian): convert these into blocks
63 func (m *impl) Blocks() []blocks.Block {
64 - return m.blocks
64 + bs := make([]blocks.Block, 0)
65 + for _, block := range m.blocks {
66 + bs = append(bs, block)
67 + }
68 + return bs
69 }
70
71 func (m *impl) AddWanted(k u.Key) {
@@ -69,7 +73,7 @@ func (m *impl) AddWanted(k u.Key) {
73 }
74
75 func (m *impl) AppendBlock(b blocks.Block) {
72 - m.blocks = append(m.blocks, b)
76 + m.blocks[b.Key()] = b
77 }
78
79 func FromNet(nmsg netmsg.NetMessage) (BitSwapMessage, error) {