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