forked from ipfs/go-bitswap
/
ledger.go
46 lines (35 loc) · 965 Bytes
/
ledger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package decision
import (
"sync"
wl "github.com/onflow/go-bitswap/client/wantlist"
pb "github.com/onflow/go-bitswap/message/pb"
"github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p/core/peer"
)
func newLedger(p peer.ID) *ledger {
return &ledger{
wantList: wl.New(),
Partner: p,
}
}
// Keeps the wantlist for the partner. NOT threadsafe!
type ledger struct {
// Partner is the remote Peer.
Partner peer.ID
// wantList is a (bounded, small) set of keys that Partner desires.
wantList *wl.Wantlist
lk sync.RWMutex
}
func (l *ledger) Wants(k cid.Cid, priority int32, wantType pb.Message_Wantlist_WantType) {
log.Debugf("peer %s wants %s", l.Partner, k)
l.wantList.Add(k, priority, wantType)
}
func (l *ledger) CancelWant(k cid.Cid) bool {
return l.wantList.Remove(k)
}
func (l *ledger) WantListContains(k cid.Cid) (wl.Entry, bool) {
return l.wantList.Contains(k)
}
func (l *ledger) Entries() []wl.Entry {
return l.wantList.Entries()
}