Commit fe040f76 authored by Brian Tiger Chow's avatar Brian Tiger Chow Committed by Juan Batiz-Benet

refactor() message API

performing CR in the form of a PR. Let me know what you think.

License: MIT
Signed-off-by: default avatarBrian Tiger Chow <brian@perfmode.com>
parent 17d40121
...@@ -169,7 +169,7 @@ func (bs *bitswap) sendWantListTo(ctx context.Context, peers <-chan peer.Peer) e ...@@ -169,7 +169,7 @@ func (bs *bitswap) sendWantListTo(ctx context.Context, peers <-chan peer.Peer) e
} }
message := bsmsg.New() message := bsmsg.New()
for _, wanted := range bs.wantlist.Entries() { for _, wanted := range bs.wantlist.Entries() {
message.AddEntry(wanted.Value, wanted.Priority, false) message.AddEntry(wanted.Value, wanted.Priority)
} }
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
for peerToQuery := range peers { for peerToQuery := range peers {
...@@ -207,7 +207,7 @@ func (bs *bitswap) sendWantlistToProviders(ctx context.Context, wantlist *wl.Wan ...@@ -207,7 +207,7 @@ func (bs *bitswap) sendWantlistToProviders(ctx context.Context, wantlist *wl.Wan
message := bsmsg.New() message := bsmsg.New()
message.SetFull(true) message.SetFull(true)
for _, e := range bs.wantlist.Entries() { for _, e := range bs.wantlist.Entries() {
message.AddEntry(e.Value, e.Priority, false) message.AddEntry(e.Value, e.Priority)
} }
ps := pset.NewPeerSet() ps := pset.NewPeerSet()
...@@ -335,7 +335,7 @@ func (bs *bitswap) cancelBlocks(ctx context.Context, bkeys []u.Key) { ...@@ -335,7 +335,7 @@ func (bs *bitswap) cancelBlocks(ctx context.Context, bkeys []u.Key) {
message := bsmsg.New() message := bsmsg.New()
message.SetFull(false) message.SetFull(false)
for _, k := range bkeys { for _, k := range bkeys {
message.AddEntry(k, 0, true) message.Cancel(k)
} }
for _, p := range bs.ledgermanager.Peers() { for _, p := range bs.ledgermanager.Peers() {
err := bs.send(ctx, p, message) err := bs.send(ctx, p, message)
......
...@@ -24,7 +24,9 @@ type BitSwapMessage interface { ...@@ -24,7 +24,9 @@ type BitSwapMessage interface {
Blocks() []*blocks.Block Blocks() []*blocks.Block
// AddEntry adds an entry to the Wantlist. // AddEntry adds an entry to the Wantlist.
AddEntry(key u.Key, priority int, cancel bool) AddEntry(key u.Key, priority int)
Cancel(key u.Key)
// Sets whether or not the contained wantlist represents the entire wantlist // Sets whether or not the contained wantlist represents the entire wantlist
// true = full wantlist // true = full wantlist
...@@ -50,6 +52,10 @@ type impl struct { ...@@ -50,6 +52,10 @@ type impl struct {
} }
func New() BitSwapMessage { func New() BitSwapMessage {
return newMsg()
}
func newMsg() *impl {
return &impl{ return &impl{
blocks: make(map[u.Key]*blocks.Block), blocks: make(map[u.Key]*blocks.Block),
wantlist: make(map[u.Key]*Entry), wantlist: make(map[u.Key]*Entry),
...@@ -64,10 +70,10 @@ type Entry struct { ...@@ -64,10 +70,10 @@ type Entry struct {
} }
func newMessageFromProto(pbm pb.Message) BitSwapMessage { func newMessageFromProto(pbm pb.Message) BitSwapMessage {
m := New() m := newMsg()
m.SetFull(pbm.GetWantlist().GetFull()) m.SetFull(pbm.GetWantlist().GetFull())
for _, e := range pbm.GetWantlist().GetEntries() { for _, e := range pbm.GetWantlist().GetEntries() {
m.AddEntry(u.Key(e.GetBlock()), int(e.GetPriority()), e.GetCancel()) m.addEntry(u.Key(e.GetBlock()), int(e.GetPriority()), e.GetCancel())
} }
for _, d := range pbm.GetBlocks() { for _, d := range pbm.GetBlocks() {
b := blocks.NewBlock(d) b := blocks.NewBlock(d)
...@@ -100,7 +106,15 @@ func (m *impl) Blocks() []*blocks.Block { ...@@ -100,7 +106,15 @@ func (m *impl) Blocks() []*blocks.Block {
return bs return bs
} }
func (m *impl) AddEntry(k u.Key, priority int, cancel bool) { func (m *impl) Cancel(k u.Key) {
m.addEntry(k, 0, true)
}
func (m *impl) AddEntry(k u.Key, priority int) {
m.addEntry(k, priority, false)
}
func (m *impl) addEntry(k u.Key, priority int, cancel bool) {
e, exists := m.wantlist[k] e, exists := m.wantlist[k]
if exists { if exists {
e.Priority = priority e.Priority = priority
......
...@@ -14,7 +14,7 @@ import ( ...@@ -14,7 +14,7 @@ import (
func TestAppendWanted(t *testing.T) { func TestAppendWanted(t *testing.T) {
const str = "foo" const str = "foo"
m := New() m := New()
m.AddEntry(u.Key(str), 1, false) m.AddEntry(u.Key(str), 1)
if !wantlistContains(m.ToProto().GetWantlist(), str) { if !wantlistContains(m.ToProto().GetWantlist(), str) {
t.Fail() t.Fail()
...@@ -63,7 +63,7 @@ func TestWantlist(t *testing.T) { ...@@ -63,7 +63,7 @@ func TestWantlist(t *testing.T) {
keystrs := []string{"foo", "bar", "baz", "bat"} keystrs := []string{"foo", "bar", "baz", "bat"}
m := New() m := New()
for _, s := range keystrs { for _, s := range keystrs {
m.AddEntry(u.Key(s), 1, false) m.AddEntry(u.Key(s), 1)
} }
exported := m.Wantlist() exported := m.Wantlist()
...@@ -86,7 +86,7 @@ func TestCopyProtoByValue(t *testing.T) { ...@@ -86,7 +86,7 @@ func TestCopyProtoByValue(t *testing.T) {
const str = "foo" const str = "foo"
m := New() m := New()
protoBeforeAppend := m.ToProto() protoBeforeAppend := m.ToProto()
m.AddEntry(u.Key(str), 1, false) m.AddEntry(u.Key(str), 1)
if wantlistContains(protoBeforeAppend.GetWantlist(), str) { if wantlistContains(protoBeforeAppend.GetWantlist(), str) {
t.Fail() t.Fail()
} }
...@@ -94,11 +94,11 @@ func TestCopyProtoByValue(t *testing.T) { ...@@ -94,11 +94,11 @@ func TestCopyProtoByValue(t *testing.T) {
func TestToNetFromNetPreservesWantList(t *testing.T) { func TestToNetFromNetPreservesWantList(t *testing.T) {
original := New() original := New()
original.AddEntry(u.Key("M"), 1, false) original.AddEntry(u.Key("M"), 1)
original.AddEntry(u.Key("B"), 1, false) original.AddEntry(u.Key("B"), 1)
original.AddEntry(u.Key("D"), 1, false) original.AddEntry(u.Key("D"), 1)
original.AddEntry(u.Key("T"), 1, false) original.AddEntry(u.Key("T"), 1)
original.AddEntry(u.Key("F"), 1, false) original.AddEntry(u.Key("F"), 1)
var buf bytes.Buffer var buf bytes.Buffer
if err := original.ToNet(&buf); err != nil { if err := original.ToNet(&buf); err != nil {
...@@ -174,8 +174,8 @@ func TestDuplicates(t *testing.T) { ...@@ -174,8 +174,8 @@ func TestDuplicates(t *testing.T) {
b := blocks.NewBlock([]byte("foo")) b := blocks.NewBlock([]byte("foo"))
msg := New() msg := New()
msg.AddEntry(b.Key(), 1, false) msg.AddEntry(b.Key(), 1)
msg.AddEntry(b.Key(), 1, false) msg.AddEntry(b.Key(), 1)
if len(msg.Wantlist()) != 1 { if len(msg.Wantlist()) != 1 {
t.Fatal("Duplicate in BitSwapMessage") t.Fatal("Duplicate in BitSwapMessage")
} }
......
...@@ -64,7 +64,7 @@ func TestBlockRecordedAsWantedAfterMessageReceived(t *testing.T) { ...@@ -64,7 +64,7 @@ func TestBlockRecordedAsWantedAfterMessageReceived(t *testing.T) {
block := blocks.NewBlock([]byte("data wanted by beggar")) block := blocks.NewBlock([]byte("data wanted by beggar"))
messageFromBeggarToChooser := message.New() messageFromBeggarToChooser := message.New()
messageFromBeggarToChooser.AddEntry(block.Key(), 1, false) messageFromBeggarToChooser.AddEntry(block.Key(), 1)
chooser.ls.MessageReceived(beggar.Peer, messageFromBeggarToChooser) chooser.ls.MessageReceived(beggar.Peer, messageFromBeggarToChooser)
// for this test, doesn't matter if you record that beggar sent // for this test, doesn't matter if you record that beggar sent
......
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