diff --git a/bitswap.go b/bitswap.go index 03694302..af648972 100644 --- a/bitswap.go +++ b/bitswap.go @@ -335,7 +335,7 @@ type Bitswap struct { sendTimeHistogram metrics.Histogram // External statistics interface - wiretap WireTap + tracer Tracer // the SessionManager routes requests to interested sessions sm *bssm.SessionManager @@ -527,8 +527,8 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg // TODO: this is bad, and could be easily abused. // Should only track *useful* messages in ledger - if bs.wiretap != nil { - bs.wiretap.MessageReceived(p, incoming) + if bs.tracer != nil { + bs.tracer.MessageReceived(p, incoming) } iblocks := incoming.Blocks() diff --git a/bitswap_test.go b/bitswap_test.go index 0da62dd3..33032137 100644 --- a/bitswap_test.go +++ b/bitswap_test.go @@ -870,29 +870,29 @@ type logItem struct { pid peer.ID msg bsmsg.BitSwapMessage } -type mockWireTap struct { +type mockTracer struct { mu sync.Mutex log []logItem } -func (m *mockWireTap) MessageReceived(p peer.ID, msg bsmsg.BitSwapMessage) { +func (m *mockTracer) MessageReceived(p peer.ID, msg bsmsg.BitSwapMessage) { m.mu.Lock() defer m.mu.Unlock() m.log = append(m.log, logItem{'r', p, msg}) } -func (m *mockWireTap) MessageSent(p peer.ID, msg bsmsg.BitSwapMessage) { +func (m *mockTracer) MessageSent(p peer.ID, msg bsmsg.BitSwapMessage) { m.mu.Lock() defer m.mu.Unlock() m.log = append(m.log, logItem{'s', p, msg}) } -func (m *mockWireTap) getLog() []logItem { +func (m *mockTracer) getLog() []logItem { m.mu.Lock() defer m.mu.Unlock() return m.log[:len(m.log):len(m.log)] } -func TestWireTap(t *testing.T) { +func TestTracer(t *testing.T) { net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay)) ig := testinstance.NewTestInstanceGenerator(net, nil, nil) defer ig.Close() @@ -901,9 +901,9 @@ func TestWireTap(t *testing.T) { instances := ig.Instances(3) blocks := bg.Blocks(2) - // Install WireTap - wiretap := new(mockWireTap) - bitswap.EnableWireTap(wiretap)(instances[0].Exchange) + // Install Tracer + wiretap := new(mockTracer) + bitswap.WithTracer(wiretap)(instances[0].Exchange) // First peer has block err := instances[0].Exchange.HasBlock(blocks[0]) @@ -937,9 +937,9 @@ func TestWireTap(t *testing.T) { log := wiretap.getLog() - // After communication, 3 messages should be logged via WireTap + // After communication, 3 messages should be logged via Tracer if l := len(log); l != 3 { - t.Fatal("expected 3 items logged via WireTap, found", l) + t.Fatal("expected 3 items logged via Tracer, found", l) } // Received: 'Have' @@ -988,7 +988,7 @@ func TestWireTap(t *testing.T) { } // After disabling WireTap, no new messages are logged - bitswap.DisableWireTap()(instances[0].Exchange) + bitswap.WithTracer(nil)(instances[0].Exchange) err = instances[0].Exchange.HasBlock(blocks[1]) if err != nil { diff --git a/tracer.go b/tracer.go new file mode 100644 index 00000000..dc977abd --- /dev/null +++ b/tracer.go @@ -0,0 +1,20 @@ +package bitswap + +import ( + bsmsg "github.com/ipfs/go-bitswap/message" + peer "github.com/libp2p/go-libp2p-core/peer" +) + +// Tracer provides methods to access all messages sent and received by Bitswap. +// This interface can be used to implement various statistics (this is original intent). +type Tracer interface { + MessageReceived(peer.ID, bsmsg.BitSwapMessage) + MessageSent(peer.ID, bsmsg.BitSwapMessage) +} + +// Configures Bitswap to use given tracer. +func WithTracer(tap Tracer) Option { + return func(bs *Bitswap) { + bs.tracer = tap + } +} diff --git a/wiretap.go b/wiretap.go deleted file mode 100644 index 55cb21d3..00000000 --- a/wiretap.go +++ /dev/null @@ -1,27 +0,0 @@ -package bitswap - -import ( - bsmsg "github.com/ipfs/go-bitswap/message" - peer "github.com/libp2p/go-libp2p-core/peer" -) - -// WireTap provides methods to access all messages sent and received by Bitswap. -// This interface can be used to implement various statistics (this is original intent). -type WireTap interface { - MessageReceived(peer.ID, bsmsg.BitSwapMessage) - MessageSent(peer.ID, bsmsg.BitSwapMessage) -} - -// Configures Bitswap to use given wiretap. -func EnableWireTap(tap WireTap) Option { - return func(bs *Bitswap) { - bs.wiretap = tap - } -} - -// Configures Bitswap not to use any wiretap. -func DisableWireTap() Option { - return func(bs *Bitswap) { - bs.wiretap = nil - } -} diff --git a/workers.go b/workers.go index c5b62d25..af4531ad 100644 --- a/workers.go +++ b/workers.go @@ -56,8 +56,8 @@ func (bs *Bitswap) taskWorker(ctx context.Context, id int) { // Ideally, yes. But we'd need some way to trigger a retry and/or drop // the peer. bs.engine.MessageSent(envelope.Peer, envelope.Message) - if bs.wiretap != nil { - bs.wiretap.MessageSent(envelope.Peer, envelope.Message) + if bs.tracer != nil { + bs.tracer.MessageSent(envelope.Peer, envelope.Message) } bs.sendBlocks(ctx, envelope)