Skip to content

Commit 31f0e25

Browse files
authored
refactor(modp2p): decouple Bitswap (#2962)
Decouples Bitswap into Client and Server. This is necessary to provide new custom options to the Client. In particular, this brings client.WithoutDuplicatedBlockStats() option to the table as nice little optimization for LNs. It disables one metrics we don't care much about atm, but avoids expensive Has call to DB as profiles show.
1 parent fa6a137 commit 31f0e25

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

nodebuilder/p2p/bitswap.go

+41-13
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package p2p
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

7-
"github.com/ipfs/boxo/bitswap"
8+
"github.com/ipfs/boxo/bitswap/client"
89
"github.com/ipfs/boxo/bitswap/network"
10+
"github.com/ipfs/boxo/bitswap/server"
911
"github.com/ipfs/boxo/blockstore"
1012
"github.com/ipfs/boxo/exchange"
1113
"github.com/ipfs/go-datastore"
@@ -28,17 +30,38 @@ const (
2830

2931
// dataExchange provides a constructor for IPFS block's DataExchange over BitSwap.
3032
func dataExchange(params bitSwapParams) exchange.Interface {
31-
prefix := protocol.ID(fmt.Sprintf("/celestia/%s", params.Net))
32-
return bitswap.New(
33+
prefix := protocolID(params.Net)
34+
net := network.NewFromIpfsHost(params.Host, &routinghelpers.Null{}, network.Prefix(prefix))
35+
srvr := server.New(
3336
params.Ctx,
34-
network.NewFromIpfsHost(params.Host, &routinghelpers.Null{}, network.Prefix(prefix)),
37+
net,
3538
params.Bs,
36-
bitswap.ProvideEnabled(false),
37-
// NOTE: These below ar required for our protocol to work reliably.
38-
// See https://github.com/celestiaorg/celestia-node/issues/732
39-
bitswap.SetSendDontHaves(false),
40-
bitswap.SetSimulateDontHavesOnTimeout(false),
39+
server.ProvideEnabled(false), // we don't provide blocks over DHT
40+
// NOTE: These below are required for our protocol to work reliably.
41+
// // See https://github.com/celestiaorg/celestia-node/issues/732
42+
server.SetSendDontHaves(false),
4143
)
44+
45+
clnt := client.New(
46+
params.Ctx,
47+
net,
48+
params.Bs,
49+
client.WithBlockReceivedNotifier(srvr),
50+
client.SetSimulateDontHavesOnTimeout(false),
51+
client.WithoutDuplicatedBlockStats(),
52+
)
53+
net.Start(srvr, clnt) // starting with hook does not work
54+
55+
params.Lifecycle.Append(fx.Hook{
56+
OnStop: func(ctx context.Context) (err error) {
57+
err = errors.Join(err, clnt.Close())
58+
err = errors.Join(err, srvr.Close())
59+
net.Stop()
60+
return err
61+
},
62+
})
63+
64+
return clnt
4265
}
4366

4467
func blockstoreFromDatastore(ctx context.Context, ds datastore.Batching) (blockstore.Blockstore, error) {
@@ -66,8 +89,13 @@ func blockstoreFromEDSStore(ctx context.Context, store *eds.Store) (blockstore.B
6689
type bitSwapParams struct {
6790
fx.In
6891

69-
Ctx context.Context
70-
Net Network
71-
Host hst.Host
72-
Bs blockstore.Blockstore
92+
Lifecycle fx.Lifecycle
93+
Ctx context.Context
94+
Net Network
95+
Host hst.Host
96+
Bs blockstore.Blockstore
97+
}
98+
99+
func protocolID(network Network) protocol.ID {
100+
return protocol.ID(fmt.Sprintf("/celestia/%s", network))
73101
}

0 commit comments

Comments
 (0)