Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
fix: virtual net race
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc committed Mar 19, 2020
1 parent 6510eac commit a10ca03
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
23 changes: 21 additions & 2 deletions message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ type BitSwapMessage interface {

// Reset the values in the message back to defaults, so it can be reused
Reset(bool)

// Clone the message fields
Clone() BitSwapMessage
}

// Exportable is an interface for structures than can be
Expand Down Expand Up @@ -144,13 +147,29 @@ func New(full bool) BitSwapMessage {

func newMsg(full bool) *impl {
return &impl{
full: full,
wantlist: make(map[cid.Cid]*Entry),
blocks: make(map[cid.Cid]blocks.Block),
blockPresences: make(map[cid.Cid]pb.Message_BlockPresenceType),
wantlist: make(map[cid.Cid]*Entry),
full: full,
}
}

// Clone the message fields
func (m *impl) Clone() BitSwapMessage {
msg := newMsg(m.full)
for k := range m.wantlist {
msg.wantlist[k] = m.wantlist[k]
}
for k := range m.blocks {
msg.blocks[k] = m.blocks[k]
}
for k := range m.blockPresences {
msg.blockPresences[k] = m.blockPresences[k]
}
msg.pendingBytes = m.pendingBytes
return msg
}

// Reset the values in the message back to defaults, so it can be reused
func (m *impl) Reset(full bool) {
m.full = full
Expand Down
3 changes: 0 additions & 3 deletions message/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package message

import (
"bytes"
"fmt"
"testing"

pb "github.com/ipfs/go-bitswap/message/pb"
Expand Down Expand Up @@ -305,8 +304,6 @@ func TestEntrySize(t *testing.T) {
SendDontHave: true,
Cancel: false,
}
fmt.Println(len(c.Bytes()))
fmt.Println(len(c.KeyString()))
epb := e.ToPB()
if e.Size() != epb.Size() {
t.Fatal("entry size calculation incorrect", e.Size(), epb.Size())
Expand Down
2 changes: 2 additions & 0 deletions testnet/virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ func (n *network) SendMessage(
to peer.ID,
mes bsmsg.BitSwapMessage) error {

mes = mes.Clone()

n.mu.Lock()
defer n.mu.Unlock()

Expand Down

0 comments on commit a10ca03

Please sign in to comment.