ledger.go 1.91 KB
Newer Older
1
package decision
2 3 4 5

import (
	"time"

6
	key "github.com/ipfs/go-ipfs/blocks/key"
7
	wl "github.com/ipfs/go-ipfs/exchange/bitswap/wantlist"
8
	peer "gx/ipfs/QmZwZjMVGss5rqYsJVGy18gNbkTJffFyq2x1uJ4e4p3ZAt/go-libp2p-peer"
9 10 11 12
)

// keySet is just a convenient alias for maps of keys, where we only care
// access/lookups.
13
type keySet map[key.Key]struct{}
14

15
func newLedger(p peer.ID) *ledger {
16
	return &ledger{
17
		wantList:   wl.New(),
18
		Partner:    p,
19
		sentToPeer: make(map[key.Key]time.Time),
20 21 22 23
	}
}

// ledger stores the data exchange relationship between two peers.
24
// NOT threadsafe
25 26
type ledger struct {
	// Partner is the remote Peer.
27
	Partner peer.ID
28 29 30 31 32 33 34 35 36 37 38 39 40 41

	// Accounting tracks bytes sent and recieved.
	Accounting debtRatio

	// firstExchnage is the time of the first data exchange.
	firstExchange time.Time

	// lastExchange is the time of the last data exchange.
	lastExchange time.Time

	// exchangeCount is the number of exchanges with this peer
	exchangeCount uint64

	// wantList is a (bounded, small) set of keys that Partner desires.
Jeromy's avatar
Jeromy committed
42
	wantList *wl.Wantlist
43

44 45
	// sentToPeer is a set of keys to ensure we dont send duplicate blocks
	// to a given peer
46
	sentToPeer map[key.Key]time.Time
47 48
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
49 50 51 52 53 54 55 56 57
type debtRatio struct {
	BytesSent uint64
	BytesRecv uint64
}

func (dr *debtRatio) Value() float64 {
	return float64(dr.BytesSent) / float64(dr.BytesRecv+1)
}

58 59 60 61 62 63 64 65 66 67 68 69
func (l *ledger) SentBytes(n int) {
	l.exchangeCount++
	l.lastExchange = time.Now()
	l.Accounting.BytesSent += uint64(n)
}

func (l *ledger) ReceivedBytes(n int) {
	l.exchangeCount++
	l.lastExchange = time.Now()
	l.Accounting.BytesRecv += uint64(n)
}

70
func (l *ledger) Wants(k key.Key, priority int) {
71
	log.Debugf("peer %s wants %s", l.Partner, k)
72
	l.wantList.Add(k, priority)
Jeromy's avatar
Jeromy committed
73 74
}

75
func (l *ledger) CancelWant(k key.Key) {
Jeromy's avatar
Jeromy committed
76
	l.wantList.Remove(k)
77 78
}

79
func (l *ledger) WantListContains(k key.Key) (wl.Entry, bool) {
Jeromy's avatar
Jeromy committed
80
	return l.wantList.Contains(k)
81 82 83 84 85
}

func (l *ledger) ExchangeCount() uint64 {
	return l.exchangeCount
}