ledger.go 1.95 KB
Newer Older
1
package decision
2 3

import (
Jeromy's avatar
Jeromy committed
4
	"sync"
5 6
	"time"

7
	wl "github.com/ipfs/go-ipfs/exchange/bitswap/wantlist"
8

9
	cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
10
	peer "gx/ipfs/QmfMmLGoKzCHDN7cGgk64PJr4iipzidDRME8HABSJqvmhC/go-libp2p-peer"
11 12
)

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

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

	// 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
40
	wantList *wl.Wantlist
41

42 43
	// sentToPeer is a set of keys to ensure we dont send duplicate blocks
	// to a given peer
44
	sentToPeer map[string]time.Time
Jeromy's avatar
Jeromy committed
45 46

	lk sync.Mutex
47 48
}

49 50 51 52 53 54 55 56
type Receipt struct {
	Peer      string
	Value     float64
	Sent      uint64
	Recv      uint64
	Exchanged uint64
}

Brian Tiger Chow's avatar
Brian Tiger Chow committed
57 58 59 60 61 62 63 64 65
type debtRatio struct {
	BytesSent uint64
	BytesRecv uint64
}

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

66 67 68 69 70 71 72 73 74 75 76 77
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)
}

78
func (l *ledger) Wants(k *cid.Cid, priority int) {
79
	log.Debugf("peer %s wants %s", l.Partner, k)
80
	l.wantList.Add(k, priority)
Jeromy's avatar
Jeromy committed
81 82
}

83
func (l *ledger) CancelWant(k *cid.Cid) {
Jeromy's avatar
Jeromy committed
84
	l.wantList.Remove(k)
85 86
}

87
func (l *ledger) WantListContains(k *cid.Cid) (*wl.Entry, bool) {
Jeromy's avatar
Jeromy committed
88
	return l.wantList.Contains(k)
89 90 91 92 93
}

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