Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cece493

Browse files
committedMay 12, 2015
revert changes to bitswap message, they werent necessary
1 parent b9063b4 commit cece493

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed
 

‎exchange/bitswap/message/message.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type Exportable interface {
5353
type impl struct {
5454
full bool
5555
wantlist map[u.Key]Entry
56-
blocks []*blocks.Block
56+
blocks map[u.Key]*blocks.Block
5757
}
5858

5959
func New() BitSwapMessage {
@@ -62,6 +62,7 @@ func New() BitSwapMessage {
6262

6363
func newMsg() *impl {
6464
return &impl{
65+
blocks: make(map[u.Key]*blocks.Block),
6566
wantlist: make(map[u.Key]Entry),
6667
full: true,
6768
}
@@ -106,7 +107,11 @@ func (m *impl) Wantlist() []Entry {
106107
}
107108

108109
func (m *impl) Blocks() []*blocks.Block {
109-
return m.blocks
110+
bs := make([]*blocks.Block, 0, len(m.blocks))
111+
for _, block := range m.blocks {
112+
bs = append(bs, block)
113+
}
114+
return bs
110115
}
111116

112117
func (m *impl) Cancel(k u.Key) {
@@ -135,7 +140,7 @@ func (m *impl) addEntry(k u.Key, priority int, cancel bool) {
135140
}
136141

137142
func (m *impl) AddBlock(b *blocks.Block) {
138-
m.blocks = append(m.blocks, b)
143+
m.blocks[b.Key()] = b
139144
}
140145

141146
func FromNet(r io.Reader) (BitSwapMessage, error) {

‎exchange/bitswap/message/message_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,20 @@ func contains(strs []string, x string) bool {
169169
}
170170
return false
171171
}
172+
173+
func TestDuplicates(t *testing.T) {
174+
b := blocks.NewBlock([]byte("foo"))
175+
msg := New()
176+
177+
msg.AddEntry(b.Key(), 1)
178+
msg.AddEntry(b.Key(), 1)
179+
if len(msg.Wantlist()) != 1 {
180+
t.Fatal("Duplicate in BitSwapMessage")
181+
}
182+
183+
msg.AddBlock(b)
184+
msg.AddBlock(b)
185+
if len(msg.Blocks()) != 1 {
186+
t.Fatal("Duplicate in BitSwapMessage")
187+
}
188+
}

0 commit comments

Comments
 (0)
Please sign in to comment.