From 99db07c3a4fa77a367431333680fc671950105e1 Mon Sep 17 00:00:00 2001 From: Brian Tiger Chow Date: Wed, 10 Sep 2014 20:23:39 -0700 Subject: [PATCH] hide ledger wantlist manipulation --- bitswap/bitswap.go | 15 +++++++-------- bitswap/ledger.go | 10 ++++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/bitswap/bitswap.go b/bitswap/bitswap.go index d118c3687..a2012c3de 100644 --- a/bitswap/bitswap.go +++ b/bitswap/bitswap.go @@ -137,7 +137,7 @@ func (bs *BitSwap) getBlock(k u.Key, p *peer.Peer, timeout time.Duration) ([]byt func (bs *BitSwap) HaveBlock(blk *blocks.Block) error { go func() { for _, ledger := range bs.partners { - if _, ok := ledger.WantList[blk.Key()]; ok { + if ledger.WantListContains(blk.Key()) { //send block to node if ledger.ShouldSend() { bs.SendBlock(ledger.Partner, blk) @@ -192,14 +192,13 @@ func (bs *BitSwap) handleMessages() { // and then if we do, check the ledger for whether or not we should send it. func (bs *BitSwap) peerWantsBlock(p *peer.Peer, want string) { u.DOut("peer [%s] wants block [%s]\n", p.ID.Pretty(), u.Key(want).Pretty()) - ledg := bs.getLedger(p) + ledger := bs.getLedger(p) dsk := ds.NewKey(want) blk_i, err := bs.datastore.Get(dsk) if err != nil { if err == ds.ErrNotFound { - // TODO: this needs to be different. We need timeouts. - ledg.WantList[u.Key(want)] = struct{}{} + ledger.Wants(u.Key(want)) } u.PErr("datastore get error: %v\n", err) return @@ -211,7 +210,7 @@ func (bs *BitSwap) peerWantsBlock(p *peer.Peer, want string) { return } - if ledg.ShouldSend() { + if ledger.ShouldSend() { u.DOut("Sending block to peer.\n") bblk, err := blocks.NewBlock(blk) if err != nil { @@ -219,7 +218,7 @@ func (bs *BitSwap) peerWantsBlock(p *peer.Peer, want string) { return } bs.SendBlock(p, bblk) - ledg.SentBytes(len(blk)) + ledger.SentBytes(len(blk)) } else { u.DOut("Decided not to send block.") } @@ -276,7 +275,7 @@ func (bs *BitSwap) Halt() { func (bs *BitSwap) SetStrategy(sf StrategyFunc) { bs.strategy = sf - for _, ledg := range bs.partners { - ledg.Strategy = sf + for _, ledger := range bs.partners { + ledger.Strategy = sf } } diff --git a/bitswap/ledger.go b/bitswap/ledger.go index 2a197f057..2e99d2ec7 100644 --- a/bitswap/ledger.go +++ b/bitswap/ledger.go @@ -49,3 +49,13 @@ func (l *Ledger) ReceivedBytes(n int) { l.LastExchange = time.Now() l.Accounting.BytesRecv += uint64(n) } + +// TODO: this needs to be different. We need timeouts. +func (l *Ledger) Wants(k u.Key) { + l.WantList[k] = struct{}{} +} + +func (l *Ledger) WantListContains(k u.Key) bool { + _, ok := l.WantList[k] + return ok +}