-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitswap.go
181 lines (153 loc) · 4.83 KB
/
bitswap.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package bitswap
import (
"context"
"fmt"
"github.com/ipfs/go-bitswap/client"
"github.com/ipfs/go-bitswap/internal/defaults"
"github.com/ipfs/go-bitswap/message"
"github.com/ipfs/go-bitswap/network"
"github.com/ipfs/go-bitswap/server"
"github.com/ipfs/go-bitswap/tracer"
"github.com/ipfs/go-metrics-interface"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
blockstore "github.com/ipfs/go-ipfs-blockstore"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p/core/peer"
"go.uber.org/multierr"
)
var log = logging.Logger("bitswap")
// old interface we are targeting
type bitswap interface {
Close() error
GetBlock(ctx context.Context, k cid.Cid) (blocks.Block, error)
GetBlocks(ctx context.Context, keys []cid.Cid) (<-chan blocks.Block, error)
GetWantBlocks() []cid.Cid
GetWantHaves() []cid.Cid
GetWantlist() []cid.Cid
IsOnline() bool
LedgerForPeer(p peer.ID) *server.Receipt
NewSession(ctx context.Context) exchange.Fetcher
NotifyNewBlocks(ctx context.Context, blks ...blocks.Block) error
PeerConnected(p peer.ID)
PeerDisconnected(p peer.ID)
ReceiveError(err error)
ReceiveMessage(ctx context.Context, p peer.ID, incoming message.BitSwapMessage)
Stat() (*Stat, error)
WantlistForPeer(p peer.ID) []cid.Cid
}
var _ exchange.SessionExchange = (*Bitswap)(nil)
var _ bitswap = (*Bitswap)(nil)
var HasBlockBufferSize = defaults.HasBlockBufferSize
type Bitswap struct {
*client.Client
*server.Server
tracer tracer.Tracer
net network.BitSwapNetwork
}
func New(ctx context.Context, net network.BitSwapNetwork, bstore blockstore.Blockstore, options ...Option) *Bitswap {
bs := &Bitswap{
net: net,
}
var serverOptions []server.Option
var clientOptions []client.Option
for _, o := range options {
switch typedOption := o.v.(type) {
case server.Option:
serverOptions = append(serverOptions, typedOption)
case client.Option:
clientOptions = append(clientOptions, typedOption)
case option:
typedOption(bs)
default:
panic(fmt.Errorf("unknown option type passed to bitswap.New, got: %T, %v; expected: %T, %T or %T", typedOption, typedOption, server.Option(nil), client.Option(nil), option(nil)))
}
}
if bs.tracer != nil {
var tracer tracer.Tracer = nopReceiveTracer{bs.tracer}
clientOptions = append(clientOptions, client.WithTracer(tracer))
serverOptions = append(serverOptions, server.WithTracer(tracer))
}
if HasBlockBufferSize != defaults.HasBlockBufferSize {
serverOptions = append(serverOptions, server.HasBlockBufferSize(HasBlockBufferSize))
}
ctx = metrics.CtxSubScope(ctx, "bitswap")
bs.Server = server.New(ctx, net, bstore, serverOptions...)
bs.Client = client.New(ctx, net, bstore, append(clientOptions, client.WithBlockReceivedNotifier(bs.Server))...)
net.Start(bs) // use the polyfill receiver to log received errors and trace messages only once
return bs
}
func (bs *Bitswap) NotifyNewBlocks(ctx context.Context, blks ...blocks.Block) error {
return multierr.Combine(
bs.Client.NotifyNewBlocks(ctx, blks...),
bs.Server.NotifyNewBlocks(ctx, blks...),
)
}
type Stat struct {
Wantlist []cid.Cid
Peers []string
BlocksReceived uint64
DataReceived uint64
DupBlksReceived uint64
DupDataReceived uint64
MessagesReceived uint64
BlocksSent uint64
DataSent uint64
ProvideBufLen int
}
func (bs *Bitswap) Stat() (*Stat, error) {
cs, err := bs.Client.Stat()
if err != nil {
return nil, err
}
ss, err := bs.Server.Stat()
if err != nil {
return nil, err
}
return &Stat{
Wantlist: cs.Wantlist,
BlocksReceived: cs.BlocksReceived,
DataReceived: cs.DataReceived,
DupBlksReceived: cs.DupBlksReceived,
DupDataReceived: cs.DupDataReceived,
MessagesReceived: cs.MessagesReceived,
Peers: ss.Peers,
BlocksSent: ss.BlocksSent,
DataSent: ss.DataSent,
ProvideBufLen: ss.ProvideBufLen,
}, nil
}
func (bs *Bitswap) Close() error {
bs.net.Stop()
return multierr.Combine(
bs.Client.Close(),
bs.Server.Close(),
)
}
func (bs *Bitswap) WantlistForPeer(p peer.ID) []cid.Cid {
if p == bs.net.Self() {
return bs.Client.GetWantlist()
}
return bs.Server.WantlistForPeer(p)
}
func (bs *Bitswap) PeerConnected(p peer.ID) {
bs.Client.PeerConnected(p)
bs.Server.PeerConnected(p)
}
func (bs *Bitswap) PeerDisconnected(p peer.ID) {
bs.Client.PeerDisconnected(p)
bs.Server.PeerDisconnected(p)
}
func (bs *Bitswap) ReceiveError(err error) {
log.Infof("Bitswap Client ReceiveError: %s", err)
// TODO log the network error
// TODO bubble the network error up to the parent context/error logger
}
func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming message.BitSwapMessage) {
if bs.tracer != nil {
bs.tracer.MessageReceived(p, incoming)
}
bs.Client.ReceiveMessage(ctx, p, incoming)
bs.Server.ReceiveMessage(ctx, p, incoming)
}