Commit 99db07c3 authored by Brian Tiger Chow's avatar Brian Tiger Chow

hide ledger wantlist manipulation

parent 182604c6
...@@ -137,7 +137,7 @@ func (bs *BitSwap) getBlock(k u.Key, p *peer.Peer, timeout time.Duration) ([]byt ...@@ -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 { func (bs *BitSwap) HaveBlock(blk *blocks.Block) error {
go func() { go func() {
for _, ledger := range bs.partners { for _, ledger := range bs.partners {
if _, ok := ledger.WantList[blk.Key()]; ok { if ledger.WantListContains(blk.Key()) {
//send block to node //send block to node
if ledger.ShouldSend() { if ledger.ShouldSend() {
bs.SendBlock(ledger.Partner, blk) bs.SendBlock(ledger.Partner, blk)
...@@ -192,14 +192,13 @@ func (bs *BitSwap) handleMessages() { ...@@ -192,14 +192,13 @@ func (bs *BitSwap) handleMessages() {
// and then if we do, check the ledger for whether or not we should send it. // 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) { func (bs *BitSwap) peerWantsBlock(p *peer.Peer, want string) {
u.DOut("peer [%s] wants block [%s]\n", p.ID.Pretty(), u.Key(want).Pretty()) 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) dsk := ds.NewKey(want)
blk_i, err := bs.datastore.Get(dsk) blk_i, err := bs.datastore.Get(dsk)
if err != nil { if err != nil {
if err == ds.ErrNotFound { if err == ds.ErrNotFound {
// TODO: this needs to be different. We need timeouts. ledger.Wants(u.Key(want))
ledg.WantList[u.Key(want)] = struct{}{}
} }
u.PErr("datastore get error: %v\n", err) u.PErr("datastore get error: %v\n", err)
return return
...@@ -211,7 +210,7 @@ func (bs *BitSwap) peerWantsBlock(p *peer.Peer, want string) { ...@@ -211,7 +210,7 @@ func (bs *BitSwap) peerWantsBlock(p *peer.Peer, want string) {
return return
} }
if ledg.ShouldSend() { if ledger.ShouldSend() {
u.DOut("Sending block to peer.\n") u.DOut("Sending block to peer.\n")
bblk, err := blocks.NewBlock(blk) bblk, err := blocks.NewBlock(blk)
if err != nil { if err != nil {
...@@ -219,7 +218,7 @@ func (bs *BitSwap) peerWantsBlock(p *peer.Peer, want string) { ...@@ -219,7 +218,7 @@ func (bs *BitSwap) peerWantsBlock(p *peer.Peer, want string) {
return return
} }
bs.SendBlock(p, bblk) bs.SendBlock(p, bblk)
ledg.SentBytes(len(blk)) ledger.SentBytes(len(blk))
} else { } else {
u.DOut("Decided not to send block.") u.DOut("Decided not to send block.")
} }
...@@ -276,7 +275,7 @@ func (bs *BitSwap) Halt() { ...@@ -276,7 +275,7 @@ func (bs *BitSwap) Halt() {
func (bs *BitSwap) SetStrategy(sf StrategyFunc) { func (bs *BitSwap) SetStrategy(sf StrategyFunc) {
bs.strategy = sf bs.strategy = sf
for _, ledg := range bs.partners { for _, ledger := range bs.partners {
ledg.Strategy = sf ledger.Strategy = sf
} }
} }
...@@ -49,3 +49,13 @@ func (l *Ledger) ReceivedBytes(n int) { ...@@ -49,3 +49,13 @@ func (l *Ledger) ReceivedBytes(n int) {
l.LastExchange = time.Now() l.LastExchange = time.Now()
l.Accounting.BytesRecv += uint64(n) 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
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment