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 ForkDigest Filter #206

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
126 changes: 115 additions & 11 deletions graph/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions graph/model/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions graph/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,18 @@ type HeatmapData {
country: String!
}

input PeerFilter {
forkDigest: String
}

type Query {
aggregateByAgentName: [AggregateData!]!
aggregateByCountry: [AggregateData!]!
aggregateByOperatingSystem: [AggregateData!]!
aggregateByNetwork: [AggregateData!]!
aggregateByHardforkSchedule: [NextHardforkAggregation!]!
aggregateByHardforkSchedule(peerFilter: PeerFilter): [NextHardforkAggregation!]!
sadiq1971 marked this conversation as resolved.
Show resolved Hide resolved
aggregateByClientVersion: [ClientVersionAggregation!]!
getHeatmapData: [HeatmapData!]!
getHeatmapData(peerFilter: PeerFilter): [HeatmapData!]!
getNodeStats: NodeStats!
getNodeStatsOverTime(start: Float!, end: Float!): [NodeStatsOverTime!]!
getRegionalStats: RegionalStats!
Expand Down
8 changes: 4 additions & 4 deletions graph/schema.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ func (r *queryResolver) AggregateByNetwork(ctx context.Context) ([]*model.Aggreg
}

// AggregateByHardforkSchedule is the resolver for the aggregateByHardforkSchedule field.
func (r *queryResolver) AggregateByHardforkSchedule(ctx context.Context) ([]*model.NextHardforkAggregation, error) {
allPeers, err := r.peerStore.ViewAll(ctx)
func (r *queryResolver) AggregateByHardforkSchedule(ctx context.Context, peerFilter *model.PeerFilter) ([]*model.NextHardforkAggregation, error) {
allPeers, err := r.peerStore.ViewAll(ctx, peerFilter)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -125,8 +125,8 @@ func (r *queryResolver) AggregateByClientVersion(ctx context.Context) ([]*model.
}

// GetHeatmapData is the resolver for the getHeatmapData field.
func (r *queryResolver) GetHeatmapData(ctx context.Context) ([]*model.HeatmapData, error) {
peers, err := r.peerStore.ViewAll(ctx)
func (r *queryResolver) GetHeatmapData(ctx context.Context, peerFilter *model.PeerFilter) ([]*model.HeatmapData, error) {
peers, err := r.peerStore.ViewAll(ctx, peerFilter)
if err != nil {
return nil, err
}
Expand Down
28 changes: 26 additions & 2 deletions store/peerstore/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package mongo

import (
"context"
"encoding/hex"
"errors"
"eth2-crawler/graph/model"
"eth2-crawler/store/peerstore"
"fmt"
"time"
Expand Down Expand Up @@ -91,9 +93,31 @@ func (s *mongoStore) View(ctx context.Context, peerID peer.ID) (*models.Peer, er
}

// Todo: accept filter and find options to get limited information
func (s *mongoStore) ViewAll(ctx context.Context) ([]*models.Peer, error) {
func (s *mongoStore) ViewAll(ctx context.Context, peerFilter *model.PeerFilter) ([]*models.Peer, error) {
var peers []*models.Peer
cursor, err := s.coll.Find(ctx, bson.D{{Key: "is_connectable", Value: true}})

query := mongo.Pipeline{
bson.D{
{Key: "$match", Value: bson.D{{Key: "is_connectable", Value: true}}},
},
}

if peerFilter != nil &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add the nil checking inside AddForkDigestFilterToQueryPipeline so we don't need to check it every places.

Copy link
Contributor Author

@spacesailor24 spacesailor24 Oct 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sadiq1971 Should AddForkDigestFilterToQueryPipeline error if peerFilter or peerFilter.forkDigest is nil?
Instead, I could refactor the method to AddPeerFilterToQueryPipeline and it would return an unmodified pipeline if peerFilter or peerFilter.forkDigest was nil or empty

This commit does the above

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that exactly what I said. Will just add to pipeline if not nil

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can merge it.

peerFilter.ForkDigest != nil {
forkDigest := *(peerFilter.ForkDigest)
if forkDigest[0:2] == "0x" {
forkDigest = forkDigest[2:]
}
forkDigestBytes, err := hex.DecodeString(forkDigest)
if err != nil {
return nil, err
}
query = append(query, bson.D{
{Key: "$match", Value: bson.D{{Key: "fork_digest", Value: forkDigestBytes}}},
})
}

cursor, err := s.coll.Aggregate(ctx, query)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion store/peerstore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"time"

"eth2-crawler/graph/model"
"eth2-crawler/models"

"github.com/libp2p/go-libp2p-core/peer"
Expand All @@ -21,7 +22,7 @@ type Provider interface {
View(ctx context.Context, peerID peer.ID) (*models.Peer, error)
Delete(ctx context.Context, peer *models.Peer) error
// Todo: accept filter and find options to get limited information
ViewAll(ctx context.Context) ([]*models.Peer, error)
ViewAll(ctx context.Context, peerFilter *model.PeerFilter) ([]*models.Peer, error)
ListForJob(ctx context.Context, lastUpdated time.Duration, limit int) ([]*models.Peer, error)
AggregateByAgentName(ctx context.Context) ([]*models.AggregateData, error)
AggregateByOperatingSystem(ctx context.Context) ([]*models.AggregateData, error)
Expand Down