From 9d7ae40003e0620d74be6e85680e1aad92f6fa17 Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Sun, 21 Sep 2014 23:34:42 -0700 Subject: [PATCH] feat(bitswap) expose ability to toggle "niceness" true -> always send to peer false -> use ledger-based strategy described in IPFS paper draft 3 --- core/core.go | 3 ++- exchange/bitswap/bitswap.go | 4 ++-- exchange/bitswap/bitswap_test.go | 3 ++- exchange/bitswap/strategy/math.go | 3 +++ exchange/bitswap/strategy/strategy.go | 13 +++++++++++-- exchange/bitswap/strategy/strategy_test.go | 2 +- 6 files changed, 21 insertions(+), 7 deletions(-) diff --git a/core/core.go b/core/core.go index 423dffe81..d925405dd 100644 --- a/core/core.go +++ b/core/core.go @@ -118,7 +118,8 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) { // TODO(brian): perform this inside NewDHT factory method dhtService.Handler = route // wire the handler to the service. - exchangeSession = bitswap.NetMessageSession(ctx, local, exchangeService, route, d) + const alwaysSendToPeer = true // use YesManStrategy + exchangeSession = bitswap.NetMessageSession(ctx, local, exchangeService, route, d, alwaysSendToPeer) // TODO(brian): pass a context to initConnections go initConnections(ctx, cfg, peerstore, route) diff --git a/exchange/bitswap/bitswap.go b/exchange/bitswap/bitswap.go index fcc558a2c..4f5bb45e7 100644 --- a/exchange/bitswap/bitswap.go +++ b/exchange/bitswap/bitswap.go @@ -19,13 +19,13 @@ import ( // NetMessageSession initializes a BitSwap session that communicates over the // provided NetMessage service -func NetMessageSession(parent context.Context, p *peer.Peer, s bsnet.NetMessageService, directory bsnet.Routing, d ds.Datastore) exchange.Interface { +func NetMessageSession(parent context.Context, p *peer.Peer, s bsnet.NetMessageService, directory bsnet.Routing, d ds.Datastore, nice bool) exchange.Interface { networkAdapter := bsnet.NetMessageAdapter(s, nil) bs := &bitswap{ blockstore: blockstore.NewBlockstore(d), notifications: notifications.New(), - strategy: strategy.New(), + strategy: strategy.New(nice), routing: directory, sender: networkAdapter, wantlist: u.NewKeySet(), diff --git a/exchange/bitswap/bitswap_test.go b/exchange/bitswap/bitswap_test.go index 2173fb57f..107180af7 100644 --- a/exchange/bitswap/bitswap_test.go +++ b/exchange/bitswap/bitswap_test.go @@ -283,10 +283,11 @@ func session(net tn.Network, rs tn.RoutingServer, id peer.ID) instance { htc := rs.Client(p) blockstore := bstore.NewBlockstore(ds.NewMapDatastore()) + const alwaysSendToPeer = true bs := &bitswap{ blockstore: blockstore, notifications: notifications.New(), - strategy: strategy.New(), + strategy: strategy.New(alwaysSendToPeer), routing: htc, sender: adapter, wantlist: util.NewKeySet(), diff --git a/exchange/bitswap/strategy/math.go b/exchange/bitswap/strategy/math.go index 21b1ff163..c5339e5b3 100644 --- a/exchange/bitswap/strategy/math.go +++ b/exchange/bitswap/strategy/math.go @@ -7,6 +7,9 @@ import ( type strategyFunc func(*ledger) bool +// TODO avoid using rand.Float64 method. it uses a singleton lock and may cause +// performance issues. Instead, instantiate a rand struct and use that to call +// Float64() func standardStrategy(l *ledger) bool { return rand.Float64() <= probabilitySend(l.Accounting.Value()) } diff --git a/exchange/bitswap/strategy/strategy.go b/exchange/bitswap/strategy/strategy.go index dc7a8e1b3..1cd4a021f 100644 --- a/exchange/bitswap/strategy/strategy.go +++ b/exchange/bitswap/strategy/strategy.go @@ -9,10 +9,19 @@ import ( ) // TODO declare thread-safe datastore -func New() Strategy { +// TODO niceness should be on a per-peer basis. Use-case: Certain peers are +// "trusted" and/or controlled by a single human user. The user may want for +// these peers to exchange data freely +func New(nice bool) Strategy { + var stratFunc strategyFunc + if nice { + stratFunc = yesManStrategy + } else { + stratFunc = standardStrategy + } return &strategist{ ledgerMap: ledgerMap{}, - strategyFunc: yesManStrategy, + strategyFunc: stratFunc, } } diff --git a/exchange/bitswap/strategy/strategy_test.go b/exchange/bitswap/strategy/strategy_test.go index e90bcd4ec..21f293c1c 100644 --- a/exchange/bitswap/strategy/strategy_test.go +++ b/exchange/bitswap/strategy/strategy_test.go @@ -17,7 +17,7 @@ type peerAndStrategist struct { func newPeerAndStrategist(idStr string) peerAndStrategist { return peerAndStrategist{ Peer: &peer.Peer{ID: peer.ID(idStr)}, - Strategy: New(), + Strategy: New(true), } }