From 3c6274ec3c462a9fa120e90a35e32c155cc1acc0 Mon Sep 17 00:00:00 2001 From: Haiko Schol Date: Thu, 5 Dec 2024 20:47:23 +0700 Subject: [PATCH] fix(dot/network): recreate incompatible libp2p datastore --- dot/network/host.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/dot/network/host.go b/dot/network/host.go index f3fa3343c8..4e0f2282d9 100644 --- a/dot/network/host.go +++ b/dot/network/host.go @@ -8,6 +8,7 @@ import ( "fmt" "log" "net" + "os" "path" "strconv" "strings" @@ -177,9 +178,23 @@ func newHost(ctx context.Context, cfg *Config) (*host, error) { // format protocol id pid := protocol.ID(cfg.ProtocolID) - ds, err := badger.NewDatastore(path.Join(cfg.BasePath, "libp2p-datastore"), &badger.DefaultOptions) + dsPath := path.Join(cfg.BasePath, "libp2p-datastore") + ds, err := badger.NewDatastore(dsPath, &badger.DefaultOptions) if err != nil { - return nil, fmt.Errorf("failed to create libp2p datastore: %w", err) + // unfortunately we have to use this brittle hack instead of errors.Is() + // https://github.com/ipfs/go-ds-badger4/blob/v0.1.5/datastore.go#L188 + if strings.HasPrefix(err.Error(), "unsupported badger version") { + logger.Warn("unable to reuse libp2p datastore, recreating it") + if err := os.RemoveAll(dsPath); err != nil { + return nil, fmt.Errorf("removing libp2p datastore: %w", err) + } + + if ds, err = badger.NewDatastore(dsPath, &badger.DefaultOptions); err != nil { + return nil, fmt.Errorf("failed to create libp2p datastore: %w", err) + } + } else { + return nil, fmt.Errorf("failed to create libp2p datastore: %w", err) + } } ps, err := mempstore.NewPeerstore()