Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Marcin Rataj <[email protected]>
  • Loading branch information
2color and lidel authored Sep 27, 2024
1 parent f13c862 commit 137d34f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 37 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The following emojis are used to highlight certain changes:

### Added

- Added address and protocol filtering to the delegated routing server [IPIP-484](https://github.com/ipfs/specs/pull/484)
- `routing/http`: added support for address and protocol filtering to the delegated routing server ([IPIP-484](https://github.com/ipfs/specs/pull/484)) [#671](https://github.com/ipfs/boxo/pull/671)

### Changed

Expand Down
40 changes: 4 additions & 36 deletions routing/http/server/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func applyFiltersToIter(recordsIter iter.ResultIter[types.Record], filterAddrs,
record, ok := v.Val.(*types.PeerRecord)
if !ok {
logger.Errorw("problem casting find providers record", "Schema", v.Val.GetSchema(), "Type", reflect.TypeOf(v).String())
// TODO: Do we want to let failed type assertions to pass through?
return v
// drop failed type assertion
return iter.Result[types.Record]{}
}

Check warning on line 44 in routing/http/server/filters.go

View check run for this annotation

Codecov / codecov/patch

routing/http/server/filters.go#L41-L44

Added lines #L41 - L44 were not covered by tests

record = applyFilters(record, filterAddrs, filterProtocols)
Expand All @@ -55,8 +55,8 @@ func applyFiltersToIter(recordsIter iter.ResultIter[types.Record], filterAddrs,
record, ok := v.Val.(*types.BitswapRecord)
if !ok {
logger.Errorw("problem casting find providers record", "Schema", v.Val.GetSchema(), "Type", reflect.TypeOf(v).String())
// TODO: Do we want to let failed type assertions to pass through?
return v
// drop failed type assertion
return iter.Result[types.Record]{}
}

Check warning on line 60 in routing/http/server/filters.go

View check run for this annotation

Codecov / codecov/patch

routing/http/server/filters.go#L57-L60

Added lines #L57 - L60 were not covered by tests
peerRecord := types.FromBitswapRecord(record)
peerRecord = applyFilters(peerRecord, filterAddrs, filterProtocols)
Expand All @@ -76,38 +76,6 @@ func applyFiltersToIter(recordsIter iter.ResultIter[types.Record], filterAddrs,
return filteredIter
}

//lint:ignore U1000 // ignore unused
func filterRecords(records []types.Record, filterAddrs, filterProtocols []string) []types.Record {
if len(filterAddrs) == 0 && len(filterProtocols) == 0 {
return records
}

filtered := make([]types.Record, 0, len(records))

for _, record := range records {
// TODO: Handle SchemaBitswap
if schema := record.GetSchema(); schema == types.SchemaPeer {
peer, ok := record.(*types.PeerRecord)
if !ok {
logger.Errorw("problem casting find providers result", "Schema", record.GetSchema(), "Type", reflect.TypeOf(record).String())
// if the type assertion fails, we exlude record from results
continue
}

record := applyFilters(peer, filterAddrs, filterProtocols)

if record != nil {
filtered = append(filtered, record)
}

} else {
// Will we ever encounter the SchemaBitswap type? Evidence seems to suggest that no longer
logger.Errorw("encountered unknown provider schema", "Schema", record.GetSchema(), "Type", reflect.TypeOf(record).String())
}
}
return filtered
}

// Applies the filters. Returns nil if the provider does not pass the protocols filter
// The address filter is more complicated because it potentially modifies the Addrs slice.
func applyFilters(provider *types.PeerRecord, filterAddrs, filterProtocols []string) *types.PeerRecord {
Expand Down

0 comments on commit 137d34f

Please sign in to comment.