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

Memory optimization in selector validator #97

Merged
merged 1 commit into from
Sep 23, 2020
Merged
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
37 changes: 19 additions & 18 deletions selectorvalidator/selectorvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,15 @@ var (
ErrInvalidLimit = errors.New("unsupported recursive selector limit")
)

// SelectorValidator returns an OnRequestReceivedHook that only validates
// requests if their selector only has no recursions that are greater than
// maxAcceptedDepth
func SelectorValidator(maxAcceptedDepth int) graphsync.OnIncomingRequestHook {
return func(p peer.ID, request graphsync.RequestData, hookActions graphsync.IncomingRequestHookActions) {
err := ValidateMaxRecursionDepth(request.Selector(), maxAcceptedDepth)
if err == nil {
hookActions.ValidateRequest()
}
}
}
var maxDepthSelector selector.Selector

// ValidateMaxRecursionDepth examines the given selector node and verifies
// recursive selectors are limited to the given fixed depth
func ValidateMaxRecursionDepth(node ipld.Node, maxAcceptedDepth int) error {
func init() {
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Map)

// this selector is a selector for traversing selectors...
// it traverses the various selector types looking for recursion limit fields
// and matches them
s, err := ssb.ExploreRecursive(selector.RecursionLimitNone(), ssb.ExploreFields(func(efsb builder.ExploreFieldsSpecBuilder) {
maxDepthSelector, _ = ssb.ExploreRecursive(selector.RecursionLimitNone(), ssb.ExploreFields(func(efsb builder.ExploreFieldsSpecBuilder) {
efsb.Insert(selector.SelectorKey_ExploreRecursive, ssb.ExploreFields(func(efsb builder.ExploreFieldsSpecBuilder) {
efsb.Insert(selector.SelectorKey_Limit, ssb.Matcher())
efsb.Insert(selector.SelectorKey_Sequence, ssb.ExploreRecursiveEdge())
Expand All @@ -61,12 +49,25 @@ func ValidateMaxRecursionDepth(node ipld.Node, maxAcceptedDepth int) error {
efsb.Insert(selector.SelectorKey_Next, ssb.ExploreRecursiveEdge())
}))
})).Selector()
}

if err != nil {
return err
// SelectorValidator returns an OnRequestReceivedHook that only validates
// requests if their selector only has no recursions that are greater than
// maxAcceptedDepth
func SelectorValidator(maxAcceptedDepth int) graphsync.OnIncomingRequestHook {
return func(p peer.ID, request graphsync.RequestData, hookActions graphsync.IncomingRequestHookActions) {
err := ValidateMaxRecursionDepth(request.Selector(), maxAcceptedDepth)
if err == nil {
hookActions.ValidateRequest()
}
}
}

// ValidateMaxRecursionDepth examines the given selector node and verifies
// recursive selectors are limited to the given fixed depth
func ValidateMaxRecursionDepth(node ipld.Node, maxAcceptedDepth int) error {

return traversal.WalkMatching(node, s, func(progress traversal.Progress, visited ipld.Node) error {
return traversal.WalkMatching(node, maxDepthSelector, func(progress traversal.Progress, visited ipld.Node) error {
if visited.ReprKind() != ipld.ReprKind_Map || visited.Length() != 1 {
return ErrInvalidLimit
}
Expand Down