@cryptotaxi247 / kubo / commits / 9d7ae4000

feat(bitswap) expose ability to toggle "niceness"

true -> always send to peer false -> use ledger-based strategy described in IPFS paper draft 3

Brian Tiger Chow committed Sep 21, 2014 at 23:34 UTC 9d7ae40003e0620d74be6e85680e1aad92f6fa17
6 files changed +21 -7
core/core.go
+2 -1
@@ -118,7 +118,8 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
118 // TODO(brian): perform this inside NewDHT factory method
119 dhtService.Handler = route // wire the handler to the service.
120
121 - exchangeSession = bitswap.NetMessageSession(ctx, local, exchangeService, route, d)
121 + const alwaysSendToPeer = true // use YesManStrategy
122 + exchangeSession = bitswap.NetMessageSession(ctx, local, exchangeService, route, d, alwaysSendToPeer)
123
124 // TODO(brian): pass a context to initConnections
125 go initConnections(ctx, cfg, peerstore, route)
exchange/bitswap/bitswap.go
+2 -2
@@ -19,13 +19,13 @@ import (
19
20 // NetMessageSession initializes a BitSwap session that communicates over the
21 // provided NetMessage service
22 -func NetMessageSession(parent context.Context, p *peer.Peer, s bsnet.NetMessageService, directory bsnet.Routing, d ds.Datastore) exchange.Interface {
22 +func NetMessageSession(parent context.Context, p *peer.Peer, s bsnet.NetMessageService, directory bsnet.Routing, d ds.Datastore, nice bool) exchange.Interface {
23
24 networkAdapter := bsnet.NetMessageAdapter(s, nil)
25 bs := &bitswap{
26 blockstore: blockstore.NewBlockstore(d),
27 notifications: notifications.New(),
28 - strategy: strategy.New(),
28 + strategy: strategy.New(nice),
29 routing: directory,
30 sender: networkAdapter,
31 wantlist: u.NewKeySet(),
exchange/bitswap/bitswap_test.go
+2 -1
@@ -283,10 +283,11 @@ func session(net tn.Network, rs tn.RoutingServer, id peer.ID) instance {
283 htc := rs.Client(p)
284
285 blockstore := bstore.NewBlockstore(ds.NewMapDatastore())
286 + const alwaysSendToPeer = true
287 bs := &bitswap{
288 blockstore: blockstore,
289 notifications: notifications.New(),
289 - strategy: strategy.New(),
290 + strategy: strategy.New(alwaysSendToPeer),
291 routing: htc,
292 sender: adapter,
293 wantlist: util.NewKeySet(),
exchange/bitswap/strategy/math.go
+3
@@ -7,6 +7,9 @@ import (
7
8 type strategyFunc func(*ledger) bool
9
10 +// TODO avoid using rand.Float64 method. it uses a singleton lock and may cause
11 +// performance issues. Instead, instantiate a rand struct and use that to call
12 +// Float64()
13 func standardStrategy(l *ledger) bool {
14 return rand.Float64() <= probabilitySend(l.Accounting.Value())
15 }
exchange/bitswap/strategy/strategy.go
+11 -2
@@ -9,10 +9,19 @@ import (
9 )
10
11 // TODO declare thread-safe datastore
12 -func New() Strategy {
12 +// TODO niceness should be on a per-peer basis. Use-case: Certain peers are
13 +// "trusted" and/or controlled by a single human user. The user may want for
14 +// these peers to exchange data freely
15 +func New(nice bool) Strategy {
16 + var stratFunc strategyFunc
17 + if nice {
18 + stratFunc = yesManStrategy
19 + } else {
20 + stratFunc = standardStrategy
21 + }
22 return &strategist{
23 ledgerMap: ledgerMap{},
15 - strategyFunc: yesManStrategy,
24 + strategyFunc: stratFunc,
25 }
26 }
27
exchange/bitswap/strategy/strategy_test.go
+1 -1
@@ -17,7 +17,7 @@ type peerAndStrategist struct {
17 func newPeerAndStrategist(idStr string) peerAndStrategist {
18 return peerAndStrategist{
19 Peer: &peer.Peer{ID: peer.ID(idStr)},
20 - Strategy: New(),
20 + Strategy: New(true),
21 }
22 }
23