Skip to content

Commit

Permalink
Support using a custom Blockstore
Browse files Browse the repository at this point in the history
Usually (in Kubo), the Blockstore is just built on top of the datastore.

There are, however, blockstore implementations (i.e StoreTheHash) that are
optimized blockstores and not general purpose datastores.

This commit allows providing a custom blockstore, but initializes everything
as before when set to nil. The signature of New() changes accordingly. A new
option UncachedBlockstore, allows disabling the CachedBlockstore-wrapping that
also happens by default, since some datastores/blockstores include their own
caching and bloom-filters, so this can potentially be making things slower.
  • Loading branch information
hsanjuan committed Dec 2, 2022
1 parent f27f1d2 commit 2ec3699
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion examples/litepeer/litepeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {
panic(err)
}

lite, err := ipfslite.New(ctx, ds, h, dht, nil)
lite, err := ipfslite.New(ctx, ds, nil, h, dht, nil)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/walker/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func main() {
panic(err)
}

lite, err := ipfslite.New(ctx, ds, h, dht, nil)
lite, err := ipfslite.New(ctx, ds, nil, h, dht, nil)
if err != nil {
panic(err)
}
Expand Down
40 changes: 27 additions & 13 deletions ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ type Config struct {
Offline bool
// ReprovideInterval sets how often to reprovide records to the DHT
ReprovideInterval time.Duration
// Disables wrapping the blockstore in an ARC cache + Bloomfilter. Use
// when the given blockstore or datastore already has caching, or when
// caching is not needed.
UncachedBlockstore bool
}

func (cfg *Config) setDefaults() {
Expand All @@ -81,13 +85,15 @@ type Peer struct {
reprovider provider.System
}

// New creates an IPFS-Lite Peer. It uses the given datastore, libp2p Host and
// Routing (usuall the DHT). The Host and the Routing may be nil if
// config.Offline is set to true, as they are not used in that case. Peer
// implements the ipld.DAGService interface.
// New creates an IPFS-Lite Peer. It uses the given datastore, blockstore,
// libp2p Host and Routing (usuall the DHT). If the blockstore is nil, the
// given datastore will be wrapped to create one. The Host and the Routing may
// be nil if config.Offline is set to true, as they are not used in that
// case. Peer implements the ipld.DAGService interface.
func New(
ctx context.Context,
store datastore.Batching,
datastore datastore.Batching,
blockstore blockstore.Blockstore,
host host.Host,
dht routing.Routing,
cfg *Config,
Expand All @@ -104,10 +110,10 @@ func New(
cfg: cfg,
host: host,
dht: dht,
store: store,
store: datastore,
}

err := p.setupBlockstore()
err := p.setupBlockstore(blockstore)
if err != nil {
return nil, err
}
Expand All @@ -131,14 +137,22 @@ func New(
return p, nil
}

func (p *Peer) setupBlockstore() error {
bs := blockstore.NewBlockstore(p.store)
func (p *Peer) setupBlockstore(bs blockstore.Blockstore) error {
var err error
if bs == nil {
bs = blockstore.NewBlockstore(p.store)
}

// Support Identity multihashes.
bs = blockstore.NewIdStore(bs)
cachedbs, err := blockstore.CachedBlockstore(p.ctx, bs, blockstore.DefaultCacheOpts())
if err != nil {
return err

if !p.cfg.UncachedBlockstore {
bs, err = blockstore.CachedBlockstore(p.ctx, bs, blockstore.DefaultCacheOpts())
if err != nil {
return err
}
}
p.bstore = cachedbs
p.bstore = bs
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions ipfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ func setupPeers(t *testing.T) (p1, p2 *Peer, closer func(t *testing.T)) {
}
}
}
p1, err = New(ctx, ds1, h1, dht1, nil)
p1, err = New(ctx, ds1, nil, h1, dht1, nil)
if err != nil {
closer(t)
t.Fatal(err)
}
p2, err = New(ctx, ds2, h2, dht2, nil)
p2, err = New(ctx, ds2, nil, h2, dht2, nil)
if err != nil {
closer(t)
t.Fatal(err)
Expand Down

0 comments on commit 2ec3699

Please sign in to comment.