more fleshed out bitswap interface
Juan Batiz-Benet committed
Aug 16, 2014 at 18:00 UTC
0c1a20278385c5615a02ad5c7ebe83a3f62b230b
1 file changed
+77
-11
bitswap/bitswap.go
+77
-11
@@ -1,25 +1,91 @@
1
package bitswap
2
3
import (
4
- blocks "github.com/jbenet/go-ipfs/blocks"
4
+ peer "github.com/jbenet/go-ipfs/peer"
5
u "github.com/jbenet/go-ipfs/util"
6
- mh "github.com/jbenet/go-multihash"
6
+
7
+ ds "github.com/jbenet/datastore.go"
8
+
9
+ "errors"
10
"time"
11
)
12
10
-// aliases
13
+// PartnerWantListMax is the bound for the number of keys we'll store per
14
+// partner. These are usually taken from the top of the Partner's WantList
15
+// advertisements. WantLists are sorted in terms of priority.
16
+const PartnerWantListMax = 10
17
+
18
+// KeySet is just a convenient alias for maps of keys, where we only care
19
+// access/lookups.
20
+type KeySet map[u.Key]struct{}
21
22
+// Ledger stores the data exchange relationship between two peers.
23
type Ledger struct {
13
- Owner mh.Multihash
14
- Partner mh.Multihash
24
+
25
+ // Partner is the ID of the remote Peer.
26
+ Partner peer.ID
27
+
28
+ // BytesSent counts the number of bytes the local peer sent to Partner
29
BytesSent uint64
16
- BytesRecv uint64
17
- Timestamp *time.Time
30
+
31
+ // BytesReceived counts the number of bytes local peer received from Partner
32
+ BytesReceived uint64
33
+
34
+ // FirstExchnage is the time of the first data exchange.
35
+ LastExchange *time.Time
36
+
37
+ // LastExchange is the time of the last data exchange.
38
+ LastExchange *time.Time
39
+
40
+ // WantList is a (bounded, small) set of keys that Partner desires.
41
+ WantList KeySet
42
}
43
44
+// LedgerMap lists Ledgers by their Partner key.
45
+type LedgerMap map[u.Key]*Ledger
46
+
47
type BitSwap struct {
21
- Ledgers map[u.Key]*Ledger // key is peer.ID
22
- HaveList map[u.Key]*blocks.Block // key is multihash
23
- WantList []*mh.Multihash
24
- // todo
48
+ // peer is the identity of this (local) node.
49
+ peer peer.Peer
50
+
51
+ // net holds the connections to all peers.
52
+ net swarm.Network
53
+
54
+ // datastore is the local database
55
+ // Ledgers of known
56
+ datastore ds.Datastore
57
+
58
+ // partners is a map of currently active bitswap relationships.
59
+ // The Ledger has the peer.ID, and the peer connection works through net.
60
+ // Ledgers of known relationships (active or inactive) stored in datastore.
61
+ // Changes to the Ledger should be committed to the datastore.
62
+ partners map[u.Key]*Ledger
63
+
64
+ // haveList is the set of keys we have values for. a map for fast lookups.
65
+ // haveList KeySet -- not needed. all values in datastore?
66
+
67
+ // wantList is the set of keys we want values for. a map for fast lookups.
68
+ wantList KeySet
69
+}
70
+
71
+func NewBitSwap(p *peer.Peer, net swarm.Network, d ds.Datastore) *BitSwap {
72
+ return &BitSwap{
73
+ peer: p,
74
+ net: net,
75
+ datastore: d,
76
+ ledgers: &LedgerMap{},
77
+ haveList: &KeySet{},
78
+ wantList: &KeySet{},
79
+ }
80
+}
81
+
82
+// GetBlock attempts to retrieve a particular block from peers, within timeout.
83
+func (s *BitSwap) GetBlock(k u.Key, timeout time.Time) (*Block, error) {
84
+ return nil, errors.New("not implemented")
85
+}
86
+
87
+// Announce the existance of a block to BitSwap, potentially sending it to
88
+// peers (Partners) whose WantLists include it.
89
+func (s *BitSwap) HaveBlock(k u.Key) (*Block, error) {
90
+ return nil, errors.New("not implemented")
91
}