Skip to content

Commit 102e455

Browse files
authored
Merge pull request #747 from ipfs/http-retr2
httpnet: bitswap network for HTTP block retrieval over trustless gateway endpoints.
2 parents 83f1b59 + 51fe392 commit 102e455

39 files changed

+3224
-144
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ The following emojis are used to highlight certain changes:
1414

1515
## [Unreleased]
1616

17+
-`httpnet`: Transparent HTTP-block retrieval support over Trustless Gateways [#747]((https://github.com/ipfs/boxo/pull/747):
18+
- Complements Bitswap as a block-retrieval mechanism, implementing `bitswap/network`.
19+
- Understands peers found in provider records with `/.../http` endpoints (trustless gateway).
20+
- Treats them as "Bitswap" peers, except instead of using Bitswap it makes HTTP/2 requests to discover (`HEAD`) and retrieve (`GET`) individual blocks (`?format=raw`).
21+
- A `bitswap/network` proxy implementation allows co-existance with standard `bitswap/network/bsnet`.
22+
- `httpnet` is not enabled by default. Upstream implementations may use it by modifying how they create the Bitswap network and initialize bitswap.
23+
1724
### Added
1825

1926
### Changed

bitswap/benchmarks_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/ipfs/boxo/bitswap"
16-
bsnet "github.com/ipfs/boxo/bitswap/network"
16+
bsnet "github.com/ipfs/boxo/bitswap/network/bsnet"
1717
testinstance "github.com/ipfs/boxo/bitswap/testinstance"
1818
tn "github.com/ipfs/boxo/bitswap/testnet"
1919
mockrouting "github.com/ipfs/boxo/routing/mock"

bitswap/client/client.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Package bitswap implements the IPFS exchange interface with the BitSwap
1+
// Package client implements the IPFS exchange interface with the BitSwap
22
// bilateral exchange protocol.
33
package client
44

@@ -202,7 +202,7 @@ func New(parent context.Context, network bsnet.BitSwapNetwork, providerFinder ro
202202

203203
sim := bssim.New()
204204
bpm := bsbpm.New()
205-
pm := bspm.New(ctx, peerQueueFactory, network.Self())
205+
pm := bspm.New(ctx, peerQueueFactory)
206206

207207
if bs.providerFinder != nil && bs.defaultProviderQueryManager {
208208
// network can do dialing.
@@ -241,7 +241,7 @@ func New(parent context.Context, network bsnet.BitSwapNetwork, providerFinder ro
241241
return bssession.New(sessctx, sessmgr, id, spm, sessionProvFinder, sim, pm, bpm, notif, provSearchDelay, rebroadcastDelay, self)
242242
}
243243
sessionPeerManagerFactory := func(ctx context.Context, id uint64) bssession.SessionPeerManager {
244-
return bsspm.New(id, network.ConnectionManager())
244+
return bsspm.New(id, network)
245245
}
246246
notif := notifications.New()
247247
sm = bssm.New(ctx, sessionFactory, sim, sessionPeerManagerFactory, bpm, pm, notif, network.Self())

bitswap/client/internal/peermanager/peermanager.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,17 @@ type PeerManager struct {
4444
psLk sync.RWMutex
4545
sessions map[uint64]Session
4646
peerSessions map[peer.ID]map[uint64]struct{}
47-
48-
self peer.ID
4947
}
5048

5149
// New creates a new PeerManager, given a context and a peerQueueFactory.
52-
func New(ctx context.Context, createPeerQueue PeerQueueFactory, self peer.ID) *PeerManager {
50+
func New(ctx context.Context, createPeerQueue PeerQueueFactory) *PeerManager {
5351
wantGauge := metrics.NewCtx(ctx, "wantlist_total", "Number of items in wantlist.").Gauge()
5452
wantBlockGauge := metrics.NewCtx(ctx, "want_blocks_total", "Number of want-blocks in wantlist.").Gauge()
5553
return &PeerManager{
5654
peerQueues: make(map[peer.ID]PeerQueue),
5755
pwm: newPeerWantManager(wantGauge, wantBlockGauge),
5856
createPeerQueue: createPeerQueue,
5957
ctx: ctx,
60-
self: self,
6158

6259
sessions: make(map[uint64]Session),
6360
peerSessions: make(map[peer.ID]map[uint64]struct{}),

bitswap/client/internal/peermanager/peermanager_test.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ func TestAddingAndRemovingPeers(t *testing.T) {
8585
peerQueueFactory := makePeerQueueFactory(msgs)
8686

8787
tp := random.Peers(6)
88-
self, peer1, peer2, peer3, peer4, peer5 := tp[0], tp[1], tp[2], tp[3], tp[4], tp[5]
89-
peerManager := New(ctx, peerQueueFactory, self)
88+
peer1, peer2, peer3, peer4, peer5 := tp[0], tp[1], tp[2], tp[3], tp[4]
89+
peerManager := New(ctx, peerQueueFactory)
9090

9191
peerManager.Connected(peer1)
9292
peerManager.Connected(peer2)
@@ -128,8 +128,8 @@ func TestBroadcastOnConnect(t *testing.T) {
128128
msgs := make(chan msg, 16)
129129
peerQueueFactory := makePeerQueueFactory(msgs)
130130
tp := random.Peers(2)
131-
self, peer1 := tp[0], tp[1]
132-
peerManager := New(ctx, peerQueueFactory, self)
131+
peer1 := tp[0]
132+
peerManager := New(ctx, peerQueueFactory)
133133

134134
cids := random.Cids(2)
135135
peerManager.BroadcastWantHaves(ctx, cids)
@@ -149,8 +149,8 @@ func TestBroadcastWantHaves(t *testing.T) {
149149
msgs := make(chan msg, 16)
150150
peerQueueFactory := makePeerQueueFactory(msgs)
151151
tp := random.Peers(3)
152-
self, peer1, peer2 := tp[0], tp[1], tp[2]
153-
peerManager := New(ctx, peerQueueFactory, self)
152+
peer1, peer2 := tp[0], tp[1]
153+
peerManager := New(ctx, peerQueueFactory)
154154

155155
cids := random.Cids(3)
156156

@@ -190,8 +190,8 @@ func TestSendWants(t *testing.T) {
190190
msgs := make(chan msg, 16)
191191
peerQueueFactory := makePeerQueueFactory(msgs)
192192
tp := random.Peers(2)
193-
self, peer1 := tp[0], tp[1]
194-
peerManager := New(ctx, peerQueueFactory, self)
193+
peer1 := tp[0]
194+
peerManager := New(ctx, peerQueueFactory)
195195
cids := random.Cids(4)
196196

197197
peerManager.Connected(peer1)
@@ -224,8 +224,8 @@ func TestSendCancels(t *testing.T) {
224224
msgs := make(chan msg, 16)
225225
peerQueueFactory := makePeerQueueFactory(msgs)
226226
tp := random.Peers(3)
227-
self, peer1, peer2 := tp[0], tp[1], tp[2]
228-
peerManager := New(ctx, peerQueueFactory, self)
227+
peer1, peer2 := tp[0], tp[1]
228+
peerManager := New(ctx, peerQueueFactory)
229229
cids := random.Cids(4)
230230

231231
// Connect to peer1 and peer2
@@ -285,8 +285,8 @@ func TestSessionRegistration(t *testing.T) {
285285
peerQueueFactory := makePeerQueueFactory(msgs)
286286

287287
tp := random.Peers(3)
288-
self, p1, p2 := tp[0], tp[1], tp[2]
289-
peerManager := New(ctx, peerQueueFactory, self)
288+
p1, p2 := tp[0], tp[1]
289+
peerManager := New(ctx, peerQueueFactory)
290290

291291
id := uint64(1)
292292
s := newSess(id)
@@ -344,9 +344,8 @@ func BenchmarkPeerManager(b *testing.B) {
344344
return &benchPeerQueue{}
345345
}
346346

347-
self := random.Peers(1)[0]
348347
peers := random.Peers(500)
349-
peerManager := New(ctx, peerQueueFactory, self)
348+
peerManager := New(ctx, peerQueueFactory)
350349

351350
// Create a bunch of connections
352351
connected := 0

bitswap/message/message.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -416,26 +416,30 @@ func BlockPresenceSize(c cid.Cid) int {
416416
}
417417

418418
// FromNet generates a new BitswapMessage from incoming data on an io.Reader.
419-
func FromNet(r io.Reader) (BitSwapMessage, error) {
419+
func FromNet(r io.Reader) (BitSwapMessage, int, error) {
420420
reader := msgio.NewVarintReaderSize(r, network.MessageSizeMax)
421421
return FromMsgReader(reader)
422422
}
423423

424424
// FromPBReader generates a new Bitswap message from a protobuf reader.
425-
func FromMsgReader(r msgio.Reader) (BitSwapMessage, error) {
425+
func FromMsgReader(r msgio.Reader) (BitSwapMessage, int, error) {
426426
msg, err := r.ReadMsg()
427427
if err != nil {
428-
return nil, err
428+
return nil, 0, err
429429
}
430430

431431
pb := new(pb.Message)
432432
err = proto.Unmarshal(msg, pb)
433433
r.ReleaseMsg(msg)
434434
if err != nil {
435-
return nil, err
435+
return nil, 0, err
436436
}
437437

438-
return newMessageFromProto(pb)
438+
m, err := newMessageFromProto(pb)
439+
if err != nil {
440+
return nil, 0, err
441+
}
442+
return m, len(msg), nil
439443
}
440444

441445
func (m *impl) ToProtoV0() *pb.Message {

bitswap/message/message_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestToNetFromNetPreservesWantList(t *testing.T) {
116116
t.Fatal(err)
117117
}
118118

119-
copied, err := FromNet(buf)
119+
copied, _, err := FromNet(buf)
120120
if err != nil {
121121
t.Fatal(err)
122122
}
@@ -149,7 +149,7 @@ func TestToAndFromNetMessage(t *testing.T) {
149149
t.Fatal(err)
150150
}
151151

152-
m2, err := FromNet(buf)
152+
m2, _, err := FromNet(buf)
153153
if err != nil {
154154
t.Fatal(err)
155155
}

bitswap/network/bsnet/bsnet.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package bsnet
2+
3+
import "github.com/ipfs/boxo/bitswap/network/bsnet/internal"
4+
5+
var (
6+
// ProtocolBitswapNoVers is equivalent to the legacy bitswap protocol
7+
ProtocolBitswapNoVers = internal.ProtocolBitswapNoVers
8+
// ProtocolBitswapOneZero is the prefix for the legacy bitswap protocol
9+
ProtocolBitswapOneZero = internal.ProtocolBitswapOneZero
10+
// ProtocolBitswapOneOne is the prefix for version 1.1.0
11+
ProtocolBitswapOneOne = internal.ProtocolBitswapOneOne
12+
// ProtocolBitswap is the current version of the bitswap protocol: 1.2.0
13+
ProtocolBitswap = internal.ProtocolBitswap
14+
)
File renamed without changes.

0 commit comments

Comments
 (0)