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 function to create general dagsync blockhook function #144

Merged
merged 6 commits into from
Jan 10, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
MakeGeneralBlockHook takes func that returns prev ad cid
  • Loading branch information
gammazero committed Jan 9, 2024
commit 156837810bb137ba723f1c9eeebb24c61451216c
14 changes: 5 additions & 9 deletions dagsync/option.go
Original file line number Diff line number Diff line change
@@ -6,9 +6,7 @@ import (
"time"

"github.com/ipfs/go-cid"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipni/go-libipni/announce"
"github.com/ipni/go-libipni/ingest/schema"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/peer"
)
@@ -318,9 +316,9 @@ func ScopedBlockHook(hook BlockHookFunc) SyncOption {
//
// Use this when segmented sync is enabled and no other blockhook is defined.
//
// The supplied loadAd function loads an advertisement, generally stored by the
// subscriber's LinkSystem.
func MakeGeneralBlockHook(loadAd func(c cid.Cid) (schema.Advertisement, error)) BlockHookFunc {
// The supplied prevAdCid function takes the CID of the current advertisement
// and returns the CID of the previous advertisement in the chain.
func MakeGeneralBlockHook(prevAdCid func(adCid cid.Cid) (cid.Cid, error)) BlockHookFunc {
return func(_ peer.ID, adCid cid.Cid, actions SegmentSyncActions) {
// The only kind of block we should get by loading CIDs here should be
// Advertisement.
@@ -333,13 +331,11 @@ func MakeGeneralBlockHook(loadAd func(c cid.Cid) (schema.Advertisement, error))
//
// Therefore, we only attempt to load advertisements here and signal
// failure if the load fails.
ad, err := loadAd(adCid)
prevCid, err := prevAdCid(adCid)
if err != nil {
actions.FailSync(err)
} else if ad.PreviousID != nil {
actions.SetNextSyncCid(ad.PreviousID.(cidlink.Link).Cid)
} else {
actions.SetNextSyncCid(cid.Undef)
actions.SetNextSyncCid(prevCid)
}
}
}