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
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
}
}
......@@ -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
}
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