-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Caplin: Look for peers if not avaiable within subnet #11057
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,12 +22,16 @@ import ( | |
"fmt" | ||
"net" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/prysmaticlabs/go-bitfield" | ||
|
||
"github.com/ledgerwatch/erigon-lib/kv" | ||
"github.com/ledgerwatch/erigon/cl/gossip" | ||
"github.com/ledgerwatch/erigon/cl/persistence/blob_storage" | ||
"github.com/ledgerwatch/erigon/cl/phase1/forkchoice" | ||
"github.com/ledgerwatch/erigon/cl/sentinel/handlers" | ||
|
@@ -325,6 +329,89 @@ func (s *Sentinel) HasTooManyPeers() bool { | |
return active >= peers.DefaultMaxPeers | ||
} | ||
|
||
func (s *Sentinel) isPeerUsefulForAnySubnet(node *enode.Node) bool { | ||
ret := false | ||
|
||
nodeAttnets := bitfield.NewBitvector64() | ||
nodeSyncnets := bitfield.NewBitvector4() | ||
if err := node.Load(enr.WithEntry(s.cfg.NetworkConfig.AttSubnetKey, &nodeAttnets)); err != nil { | ||
log.Trace("Could not load att subnet", "err", err) | ||
return false | ||
} | ||
if err := node.Load(enr.WithEntry(s.cfg.NetworkConfig.SyncCommsSubnetKey, &nodeSyncnets)); err != nil { | ||
log.Trace("Could not load sync subnet", "err", err) | ||
return false | ||
} | ||
|
||
s.subManager.subscriptions.Range(func(key, value any) bool { | ||
sub := value.(*GossipSubscription) | ||
if sub.sub == nil { | ||
return true | ||
} | ||
|
||
if !sub.subscribed.Load() { | ||
return true | ||
} | ||
|
||
if len(sub.topic.ListPeers()) > peerSubnetTarget { | ||
return true | ||
} | ||
if gossip.IsTopicBeaconAttestation(sub.sub.Topic()) { | ||
ret = s.isPeerUsefulForAttNet(sub, nodeAttnets) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel the logic can be more explicit so the code turns to be readable, but feel free to see if you'd like to take my advise.
|
||
return !ret | ||
} | ||
|
||
if gossip.IsTopicSyncCommittee(sub.sub.Topic()) { | ||
ret = s.isPeerUsefulForSyncNet(sub, nodeSyncnets) | ||
return !ret | ||
} | ||
|
||
return true | ||
}) | ||
return ret | ||
} | ||
|
||
func (s *Sentinel) isPeerUsefulForAttNet(sub *GossipSubscription, nodeAttnets bitfield.Bitvector64) bool { | ||
splitTopic := strings.Split(sub.sub.Topic(), "/") | ||
if len(splitTopic) < 4 { | ||
return false | ||
} | ||
subnetIdStr, found := strings.CutPrefix(splitTopic[3], "beacon_attestation_") | ||
if !found { | ||
return false | ||
} | ||
subnetId, err := strconv.Atoi(subnetIdStr) | ||
if err != nil { | ||
log.Warn("Could not parse subnet id", "subnet", subnetIdStr, "err", err) | ||
return false | ||
} | ||
// check if subnetIdth bit is set in nodeAttnets | ||
return nodeAttnets.BitAt(uint64(subnetId)) | ||
|
||
} | ||
|
||
func (s *Sentinel) isPeerUsefulForSyncNet(sub *GossipSubscription, nodeSyncnets bitfield.Bitvector4) bool { | ||
splitTopic := strings.Split(sub.sub.Topic(), "/") | ||
if len(splitTopic) < 4 { | ||
return false | ||
} | ||
syncnetIdStr, found := strings.CutPrefix(splitTopic[3], "sync_committee_") | ||
if !found { | ||
return false | ||
} | ||
syncnetId, err := strconv.Atoi(syncnetIdStr) | ||
if err != nil { | ||
log.Warn("Could not parse syncnet id", "syncnet", syncnetIdStr, "err", err) | ||
return false | ||
} | ||
// check if syncnetIdth bit is set in nodeSyncnets | ||
if nodeSyncnets.BitAt(uint64(syncnetId)) { | ||
fmt.Println("syncnet", syncnetId) | ||
domiwei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return true | ||
} | ||
return false | ||
} | ||
|
||
func (s *Sentinel) GetPeersCount() (active int, connected int, disconnected int) { | ||
peers := s.host.Network().Peers() | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer a more meaningful naming here, like
peerUseful