Commit 2c7761fa authored by Brian Tiger Chow's avatar Brian Tiger Chow

fix(bitswap) preserve ordering in bitswap message

parent f758e76d
...@@ -27,14 +27,16 @@ type Exportable interface { ...@@ -27,14 +27,16 @@ type Exportable interface {
} }
type impl struct { type impl struct {
wantlist map[u.Key]struct{} existsInWantlist map[u.Key]struct{} // map to detect duplicates
blocks map[u.Key]blocks.Block wantlist []u.Key // slice to preserve ordering
blocks map[u.Key]blocks.Block // map to detect duplicates
} }
func New() BitSwapMessage { func New() BitSwapMessage {
return &impl{ return &impl{
wantlist: make(map[u.Key]struct{}),
blocks: make(map[u.Key]blocks.Block), blocks: make(map[u.Key]blocks.Block),
existsInWantlist: make(map[u.Key]struct{}),
wantlist: make([]u.Key, 0),
} }
} }
...@@ -50,16 +52,10 @@ func newMessageFromProto(pbm pb.Message) BitSwapMessage { ...@@ -50,16 +52,10 @@ func newMessageFromProto(pbm pb.Message) BitSwapMessage {
return m return m
} }
// TODO(brian): convert these into keys
func (m *impl) Wantlist() []u.Key { func (m *impl) Wantlist() []u.Key {
wl := make([]u.Key, 0) return m.wantlist
for k, _ := range m.wantlist {
wl = append(wl, k)
}
return wl
} }
// TODO(brian): convert these into blocks
func (m *impl) Blocks() []blocks.Block { func (m *impl) Blocks() []blocks.Block {
bs := make([]blocks.Block, 0) bs := make([]blocks.Block, 0)
for _, block := range m.blocks { for _, block := range m.blocks {
...@@ -69,7 +65,12 @@ func (m *impl) Blocks() []blocks.Block { ...@@ -69,7 +65,12 @@ func (m *impl) Blocks() []blocks.Block {
} }
func (m *impl) AddWanted(k u.Key) { func (m *impl) AddWanted(k u.Key) {
m.wantlist[k] = struct{}{} _, exists := m.existsInWantlist[k]
if exists {
return
}
m.existsInWantlist[k] = struct{}{}
m.wantlist = append(m.wantlist, k)
} }
func (m *impl) AddBlock(b blocks.Block) { func (m *impl) AddBlock(b blocks.Block) {
......
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