From 4363cae413b631b4b262ee47b47828daf37c08da Mon Sep 17 00:00:00 2001 From: Giulio Date: Sat, 6 Jul 2024 02:37:24 +0200 Subject: [PATCH 1/3] save --- cl/phase1/core/state/raw/hashing.go | 2 +- cl/phase1/stages/stage_history_download.go | 2 +- cl/sentinel/discovery.go | 14 ++-- cl/sentinel/sentinel.go | 87 ++++++++++++++++++++++ cl/sentinel/service/start.go | 9 +-- 5 files changed, 102 insertions(+), 12 deletions(-) diff --git a/cl/phase1/core/state/raw/hashing.go b/cl/phase1/core/state/raw/hashing.go index fb8ddb5732a..bdd7d40abd6 100644 --- a/cl/phase1/core/state/raw/hashing.go +++ b/cl/phase1/core/state/raw/hashing.go @@ -199,7 +199,7 @@ func (b *BeaconState) computeDirtyLeaves() error { } b.updateLeaf(BalancesLeafIndex, root) } - log.Trace("Balances hashing", "elapsed", time.Since(begin)) + log.Debug("Balances hashing", "elapsed", time.Since(begin)) begin = time.Now() // Field(13): RandaoMixes diff --git a/cl/phase1/stages/stage_history_download.go b/cl/phase1/stages/stage_history_download.go index ad737973ec6..b2b4039d84f 100644 --- a/cl/phase1/stages/stage_history_download.go +++ b/cl/phase1/stages/stage_history_download.go @@ -144,8 +144,8 @@ func SpawnStageHistoryDownload(cfg StageHistoryReconstructionCfg, ctx context.Co } } isInElSnapshots := true - frozenBlocksInEL := cfg.engine.FrozenBlocks(ctx) if blk.Version() >= clparams.BellatrixVersion && cfg.engine != nil && cfg.engine.SupportInsertion() { + frozenBlocksInEL := cfg.engine.FrozenBlocks(ctx) isInElSnapshots = blk.Block.Body.ExecutionPayload.BlockNumber < frozenBlocksInEL if cfg.engine.HasGapInSnapshots(ctx) && frozenBlocksInEL > 0 { destinationSlotForEL = frozenBlocksInEL - 1 diff --git a/cl/sentinel/discovery.go b/cl/sentinel/discovery.go index 2fd0e43197e..1f73ff59502 100644 --- a/cl/sentinel/discovery.go +++ b/cl/sentinel/discovery.go @@ -32,6 +32,8 @@ import ( "github.com/ledgerwatch/erigon/p2p/enr" ) +const peerSubnetTarget = 4 + // ConnectWithPeer is used to attempt to connect and add the peer to our pool // it errors when if fail to connect with the peer, for instance, if it fails the handshake // if it does not return an error, the peer is attempted to be added to the pool @@ -96,16 +98,18 @@ func (s *Sentinel) listenForPeers() { break } - if s.HasTooManyPeers() { - log.Trace("[Sentinel] Not looking for peers, at peer limit") - time.Sleep(100 * time.Millisecond) - continue - } exists := iterator.Next() if !exists { continue } node := iterator.Node() + + needsPeersForSubnets := s.isPeerUsefulForAnySubnet(node) + if s.HasTooManyPeers() && !needsPeersForSubnets { + log.Trace("[Sentinel] Not looking for peers, at peer limit") + time.Sleep(100 * time.Millisecond) + continue + } peerInfo, _, err := convertToAddrInfo(node) if err != nil { log.Error("[Sentinel] Could not convert to peer info", "err", err) diff --git a/cl/sentinel/sentinel.go b/cl/sentinel/sentinel.go index 5b77c60b322..f2a7bfa6a28 100644 --- a/cl/sentinel/sentinel.go +++ b/cl/sentinel/sentinel.go @@ -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) + 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) + return true + } + return false +} + func (s *Sentinel) GetPeersCount() (active int, connected int, disconnected int) { peers := s.host.Network().Peers() diff --git a/cl/sentinel/service/start.go b/cl/sentinel/service/start.go index e2fc2a7a5f9..04bafd9e527 100644 --- a/cl/sentinel/service/start.go +++ b/cl/sentinel/service/start.go @@ -20,7 +20,6 @@ import ( "context" "fmt" "net" - "strings" "time" "github.com/ledgerwatch/erigon/cl/gossip" @@ -61,10 +60,10 @@ func generateSubnetsTopics(template string, maxIds int) []sentinel.GossipTopic { } func getExpirationForTopic(topic string) time.Time { - if strings.Contains(topic, "beacon_attestation") || - (strings.Contains(topic, "sync_committee_") && !strings.Contains(topic, gossip.TopicNameSyncCommitteeContributionAndProof)) { - return time.Unix(0, 0) - } + // if strings.Contains(topic, "beacon_attestation") || + // (strings.Contains(topic, "sync_committee_") && !strings.Contains(topic, gossip.TopicNameSyncCommitteeContributionAndProof)) { + // return time.Unix(0, 0) + // } return time.Unix(0, math.MaxInt64) } From cbb65f537e3638343f5ef12585165aec1fbf3a49 Mon Sep 17 00:00:00 2001 From: Giulio Date: Sat, 6 Jul 2024 02:38:36 +0200 Subject: [PATCH 2/3] save --- cl/phase1/core/state/raw/hashing.go | 2 +- cl/sentinel/service/start.go | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/cl/phase1/core/state/raw/hashing.go b/cl/phase1/core/state/raw/hashing.go index bdd7d40abd6..fb8ddb5732a 100644 --- a/cl/phase1/core/state/raw/hashing.go +++ b/cl/phase1/core/state/raw/hashing.go @@ -199,7 +199,7 @@ func (b *BeaconState) computeDirtyLeaves() error { } b.updateLeaf(BalancesLeafIndex, root) } - log.Debug("Balances hashing", "elapsed", time.Since(begin)) + log.Trace("Balances hashing", "elapsed", time.Since(begin)) begin = time.Now() // Field(13): RandaoMixes diff --git a/cl/sentinel/service/start.go b/cl/sentinel/service/start.go index 04bafd9e527..e2fc2a7a5f9 100644 --- a/cl/sentinel/service/start.go +++ b/cl/sentinel/service/start.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "net" + "strings" "time" "github.com/ledgerwatch/erigon/cl/gossip" @@ -60,10 +61,10 @@ func generateSubnetsTopics(template string, maxIds int) []sentinel.GossipTopic { } func getExpirationForTopic(topic string) time.Time { - // if strings.Contains(topic, "beacon_attestation") || - // (strings.Contains(topic, "sync_committee_") && !strings.Contains(topic, gossip.TopicNameSyncCommitteeContributionAndProof)) { - // return time.Unix(0, 0) - // } + if strings.Contains(topic, "beacon_attestation") || + (strings.Contains(topic, "sync_committee_") && !strings.Contains(topic, gossip.TopicNameSyncCommitteeContributionAndProof)) { + return time.Unix(0, 0) + } return time.Unix(0, math.MaxInt64) } From 4351ba4041d4676f4b056a1b077177d53c04ef27 Mon Sep 17 00:00:00 2001 From: Kewei Date: Wed, 17 Jul 2024 00:33:38 +0800 Subject: [PATCH 3/3] Update cl/sentinel/sentinel.go. Remove println --- cl/sentinel/sentinel.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cl/sentinel/sentinel.go b/cl/sentinel/sentinel.go index f2a7bfa6a28..5dc4e747757 100644 --- a/cl/sentinel/sentinel.go +++ b/cl/sentinel/sentinel.go @@ -406,7 +406,6 @@ func (s *Sentinel) isPeerUsefulForSyncNet(sub *GossipSubscription, nodeSyncnets } // check if syncnetIdth bit is set in nodeSyncnets if nodeSyncnets.BitAt(uint64(syncnetId)) { - fmt.Println("syncnet", syncnetId) return true } return false