diff --git a/core/core.go b/core/core.go index f641e8b2d93..60d0598cd46 100644 --- a/core/core.go +++ b/core/core.go @@ -127,7 +127,9 @@ 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. + //Clarification- this is here because the bitswap strategy still has to be completely worked out const alwaysSendToPeer = true // use YesManStrategy + exchangeSession = bitswap.NetMessageSession(ctx, local, exchangeService, route, d, alwaysSendToPeer) // TODO(brian): pass a context to initConnections diff --git a/exchange/bitswap/bitswap.go b/exchange/bitswap/bitswap.go index 4ba9e179fa4..a9760b94240 100644 --- a/exchange/bitswap/bitswap.go +++ b/exchange/bitswap/bitswap.go @@ -15,24 +15,6 @@ import ( u "github.com/jbenet/go-ipfs/util" ) -// 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, nice bool) exchange.Interface { - - networkAdapter := bsnet.NetMessageAdapter(s, nil) - bs := &bitswap{ - blockstore: blockstore.NewBlockstore(d), - notifications: notifications.New(), - strategy: strategy.New(nice), - routing: directory, - sender: networkAdapter, - wantlist: u.NewKeySet(), - } - networkAdapter.SetDelegate(bs) - - return bs -} - // bitswap instances implement the bitswap protocol. type bitswap struct { @@ -56,6 +38,24 @@ type bitswap struct { wantlist u.KeySet } +// 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, trusted bool) exchange.Interface { + + networkAdapter := bsnet.NetMessageAdapter(s, nil) + bs := &bitswap{ + blockstore: blockstore.NewBlockstore(d), + notifications: notifications.New(), + strategy: strategy.New(trusted), + routing: directory, + sender: networkAdapter, + wantlist: u.NewKeySet(), + } + networkAdapter.SetDelegate(bs) + + return bs +} + // GetBlock attempts to retrieve a particular block from peers within the // deadline enforced by the context // diff --git a/exchange/bitswap/strategy/ledger.go b/exchange/bitswap/strategy/ledger.go index 34f3010552f..b05f64dc08b 100644 --- a/exchange/bitswap/strategy/ledger.go +++ b/exchange/bitswap/strategy/ledger.go @@ -12,14 +12,6 @@ import ( // access/lookups. type keySet map[u.Key]struct{} -func newLedger(p *peer.Peer, strategy strategyFunc) *ledger { - return &ledger{ - wantList: keySet{}, - Strategy: strategy, - Partner: p, - } -} - // ledger stores the data exchange relationship between two peers. type ledger struct { lock sync.RWMutex @@ -45,6 +37,14 @@ type ledger struct { Strategy strategyFunc } +func newLedger(p *peer.Peer, strategy strategyFunc) *ledger { + return &ledger{ + wantList: keySet{}, + Strategy: strategy, + Partner: p, + } +} + func (l *ledger) ShouldSend() bool { l.lock.Lock() defer l.lock.Unlock() diff --git a/exchange/bitswap/strategy/math.go b/exchange/bitswap/strategy/math.go index c5339e5b357..cea896f46f1 100644 --- a/exchange/bitswap/strategy/math.go +++ b/exchange/bitswap/strategy/math.go @@ -7,11 +7,14 @@ 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() +type debtRatio struct { + BytesSent uint64 + BytesRecv uint64 +} + func standardStrategy(l *ledger) bool { - return rand.Float64() <= probabilitySend(l.Accounting.Value()) + r := rand.New(rand.NewSource(1234)) + return r.Float64() <= probabilitySend(l.Accounting.Value()) } func yesManStrategy(l *ledger) bool { @@ -24,11 +27,6 @@ func probabilitySend(ratio float64) float64 { return 1 - y } -type debtRatio struct { - BytesSent uint64 - BytesRecv uint64 -} - func (dr *debtRatio) Value() float64 { return float64(dr.BytesSent) / float64(dr.BytesRecv+1) } diff --git a/exchange/bitswap/strategy/strategy.go b/exchange/bitswap/strategy/strategy.go index 5d09f30b540..7b5a166d4bd 100644 --- a/exchange/bitswap/strategy/strategy.go +++ b/exchange/bitswap/strategy/strategy.go @@ -8,13 +8,24 @@ import ( u "github.com/jbenet/go-ipfs/util" ) +type strategist struct { + ledgerMap + strategyFunc +} + +// LedgerMap lists Ledgers by their Partner key. +type ledgerMap map[peerKey]*ledger + +// FIXME share this externally +type peerKey u.Key + // TODO declare thread-safe datastore // 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 { +func New(trusted bool) Strategy { var stratFunc strategyFunc - if nice { + if trusted { stratFunc = yesManStrategy } else { stratFunc = standardStrategy @@ -25,17 +36,6 @@ func New(nice bool) Strategy { } } -type strategist struct { - ledgerMap - strategyFunc -} - -// LedgerMap lists Ledgers by their Partner key. -type ledgerMap map[peerKey]*ledger - -// FIXME share this externally -type peerKey u.Key - // Peers returns a list of peers func (s *strategist) Peers() []*peer.Peer { response := make([]*peer.Peer, 0) @@ -89,7 +89,6 @@ func (s *strategist) MessageSent(p *peer.Peer, m bsmsg.BitSwapMessage) error { for _, block := range m.Blocks() { l.SentBytes(len(block.Data)) } - // TODO remove these blocks from peer's want list return nil