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

Commit

Permalink
fix: notify wanted blocks from the client to the server
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo committed Aug 5, 2022
1 parent 392251e commit 5a3d01a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
19 changes: 19 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ func WithTracer(tap tracer.Tracer) Option {
}
}

func WithBlockReceivedNotifier(brn BlockReceivedNotifier) Option {
return func(bs *Client) {
bs.blockReceivedNotifier = brn
}
}

type BlockReceivedNotifier interface {
// ReceivedBlocks notify the decision engine that a peer is well behaving
// and gave us usefull data, potentially increasing it's score and making us
// send them more data in exchange.
ReceivedBlocks(peer.ID, []blocks.Block)
}

// New initializes a BitSwap instance that communicates over the provided
// BitSwapNetwork. This function registers the returned instance as the network
// delegate. Runs until context is cancelled or bitswap.Close is called.
Expand Down Expand Up @@ -210,6 +223,8 @@ type Client struct {
// how often to rebroadcast providing requests to find more optimized providers
rebroadcastDelay delay.D

blockReceivedNotifier BlockReceivedNotifier

// whether we should actually simulate dont haves on request timeout
simulateDontHavesOnTimeout bool
}
Expand Down Expand Up @@ -302,6 +317,10 @@ func (bs *Client) receiveBlocksFrom(ctx context.Context, from peer.ID, blks []bl
// Send all block keys (including duplicates) to any sessions that want them for accounting purpose.
bs.sm.ReceiveFrom(ctx, from, allKs, haves, dontHaves)

if bs.blockReceivedNotifier != nil {
bs.blockReceivedNotifier.ReceivedBlocks(from, wanted)
}

// Publish the block to any Bitswap clients that had requested blocks.
// (the sessions use this pubsub mechanism to inform clients of incoming
// blocks)
Expand Down
4 changes: 2 additions & 2 deletions polyfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func New(ctx context.Context, net network.BitSwapNetwork, bstore blockstore.Bloc
}

stats := metrics.New(ctx)
bs.Client = client.New(ctx, net, bstore, stats, clientOptions...)
bs.Server = server.New(ctx, net, bstore, stats, serverOptions...)
net.Start(bs) // use the polyfill receiver to log receive errors only once
bs.Client = client.New(ctx, net, bstore, stats, append(clientOptions, client.WithBlockReceivedNotifier(bs.Server))...)
net.Start(bs) // use the polyfill receiver to log received errors and trace messages only once

return bs
}
Expand Down
7 changes: 7 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,13 @@ func (bs *Server) ReceiveMessage(ctx context.Context, p peer.ID, incoming messag
}
}

// ReceivedBlocks notify the decision engine that a peer is well behaving
// and gave us usefull data, potentially increasing it's score and making us
// send them more data in exchange.
func (bs *Server) ReceivedBlocks(from peer.ID, blks []blocks.Block) {
bs.engine.ReceivedBlocks(from, blks)
}

func (*Server) ReceiveError(err error) {
log.Infof("Bitswap Client ReceiveError: %s", err)
// TODO log the network error
Expand Down

0 comments on commit 5a3d01a

Please sign in to comment.