Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a hook to fix multihash mismatch errors #423

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion engine/linksystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ var (
// Creates the main engine linksystem.
func (e *Engine) mkLinkSystem() ipld.LinkSystem {
lsys := cidlink.DefaultLinkSystem()
lsys.StorageReadOpener = func(lctx ipld.LinkContext, lnk ipld.Link) (io.Reader, error) {

storageReadOpener := func(lctx ipld.LinkContext, lnk ipld.Link) (io.Reader, error) {
// If link corresponds to schema.NoEntries return error immediately.
if lnk == schema.NoEntries {
return nil, errNoEntries
Expand Down Expand Up @@ -149,6 +150,21 @@ func (e *Engine) mkLinkSystem() ipld.LinkSystem {

return bytes.NewBuffer(val), nil
}

// If error hook provided, call error hook function on storageReadOpener
// error. Otherwise, only call storageReadOpener.
if e.options.storageReadOpenerErrorHook != nil {
lsys.StorageReadOpener = func(lctx ipld.LinkContext, lnk ipld.Link) (io.Reader, error) {
r, err := storageReadOpener(lctx, lnk)
if err != nil {
return r, e.options.storageReadOpenerErrorHook(lctx, lnk, err)
}
return r, nil
}
} else {
lsys.StorageReadOpener = storageReadOpener
}

lsys.StorageWriteOpener = func(lctx ipld.LinkContext) (io.Writer, ipld.BlockWriteCommitter, error) {
buf := bytes.NewBuffer(nil)
return buf, func(lnk ipld.Link) error {
Expand Down
13 changes: 13 additions & 0 deletions engine/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
datatransfer "github.com/filecoin-project/go-data-transfer/v2"
"github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
"github.com/ipld/go-ipld-prime"
_ "github.com/ipni/go-libipni/maurl"
"github.com/ipni/index-provider/engine/chunker"
"github.com/ipni/index-provider/engine/policy"
Expand Down Expand Up @@ -102,6 +103,8 @@ type (
chunker chunker.NewChunkerFunc

syncPolicy *policy.Policy

storageReadOpenerErrorHook func(lctx ipld.LinkContext, lnk ipld.Link, err error) error
}
)

Expand Down Expand Up @@ -442,3 +445,13 @@ func WithExtraGossipData(extraData []byte) Option {
return nil
}
}

// WithStorageReadOpenerErrorHook allows the calling applicaiton to invoke a custom piece logic whenever a storage read opener error occurs.
// For example the calling application can delete corrupted / create a new advertisement if the datastore was corrupted for some reason.
// The calling application can return ipld.ErrNotFound{} to indicate IPNI that this advertisement should be skipped without halting processing of the rest of the chain.
func WithStorageReadOpenerErrorHook(hook func(ipld.LinkContext, ipld.Link, error) error) Option {
return func(o *options) error {
o.storageReadOpenerErrorHook = hook
return nil
}
}
Loading