github.com/celestiaorg/celestia-node@v0.15.0-beta.1/nodebuilder/p2p/bitswap.go (about)

     1  package p2p
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"github.com/ipfs/boxo/bitswap/client"
     9  	"github.com/ipfs/boxo/bitswap/network"
    10  	"github.com/ipfs/boxo/bitswap/server"
    11  	"github.com/ipfs/boxo/blockstore"
    12  	"github.com/ipfs/boxo/exchange"
    13  	"github.com/ipfs/go-datastore"
    14  	routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
    15  	hst "github.com/libp2p/go-libp2p/core/host"
    16  	"github.com/libp2p/go-libp2p/core/protocol"
    17  	"go.uber.org/fx"
    18  
    19  	"github.com/celestiaorg/celestia-node/share/eds"
    20  )
    21  
    22  const (
    23  	// default size of bloom filter in blockStore
    24  	defaultBloomFilterSize = 512 << 10
    25  	// default amount of hash functions defined for bloom filter
    26  	defaultBloomFilterHashes = 7
    27  	// default size of arc cache in blockStore
    28  	defaultARCCacheSize = 64 << 10
    29  )
    30  
    31  // dataExchange provides a constructor for IPFS block's DataExchange over BitSwap.
    32  func dataExchange(params bitSwapParams) exchange.Interface {
    33  	prefix := protocolID(params.Net)
    34  	net := network.NewFromIpfsHost(params.Host, &routinghelpers.Null{}, network.Prefix(prefix))
    35  	srvr := server.New(
    36  		params.Ctx,
    37  		net,
    38  		params.Bs,
    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),
    43  	)
    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
    65  }
    66  
    67  func blockstoreFromDatastore(ctx context.Context, ds datastore.Batching) (blockstore.Blockstore, error) {
    68  	return blockstore.CachedBlockstore(
    69  		ctx,
    70  		blockstore.NewBlockstore(ds),
    71  		blockstore.CacheOpts{
    72  			HasBloomFilterSize:   defaultBloomFilterSize,
    73  			HasBloomFilterHashes: defaultBloomFilterHashes,
    74  			HasTwoQueueCacheSize: defaultARCCacheSize,
    75  		},
    76  	)
    77  }
    78  
    79  func blockstoreFromEDSStore(ctx context.Context, store *eds.Store) (blockstore.Blockstore, error) {
    80  	return blockstore.CachedBlockstore(
    81  		ctx,
    82  		store.Blockstore(),
    83  		blockstore.CacheOpts{
    84  			HasTwoQueueCacheSize: defaultARCCacheSize,
    85  		},
    86  	)
    87  }
    88  
    89  type bitSwapParams struct {
    90  	fx.In
    91  
    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))
   101  }