From 2ec369960c1e537a5d703f2c7d033e241ab161ce Mon Sep 17 00:00:00 2001 From: Hector Sanjuan Date: Fri, 2 Dec 2022 15:55:45 +0100 Subject: [PATCH] Support using a custom Blockstore 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. --- examples/litepeer/litepeer.go | 2 +- examples/walker/walker.go | 2 +- ipfs.go | 40 +++++++++++++++++++++++------------ ipfs_test.go | 4 ++-- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/examples/litepeer/litepeer.go b/examples/litepeer/litepeer.go index 2f5d97b..2a2b074 100644 --- a/examples/litepeer/litepeer.go +++ b/examples/litepeer/litepeer.go @@ -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) } diff --git a/examples/walker/walker.go b/examples/walker/walker.go index 11ccb29..060e4fa 100644 --- a/examples/walker/walker.go +++ b/examples/walker/walker.go @@ -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) } diff --git a/ipfs.go b/ipfs.go index 3188849..52dbe67 100644 --- a/ipfs.go +++ b/ipfs.go @@ -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() { @@ -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, @@ -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 } @@ -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 } diff --git a/ipfs_test.go b/ipfs_test.go index 0de8c9a..8b296e3 100644 --- a/ipfs_test.go +++ b/ipfs_test.go @@ -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)