From 03266223d35c3d66caa49aea8417ecf2ea4da0c1 Mon Sep 17 00:00:00 2001 From: Lucca Martins Date: Fri, 20 Mar 2026 00:17:49 -0300 Subject: [PATCH] flag to disable snap serving --- docs/cli/default_config.toml | 1 + docs/cli/server.md | 2 + eth/backend.go | 2 +- eth/backend_protocols_test.go | 91 +++++++++++++++++++++++++++++++++++ eth/ethconfig/config.go | 5 ++ internal/cli/server/config.go | 5 ++ internal/cli/server/flags.go | 7 +++ 7 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 eth/backend_protocols_test.go diff --git a/docs/cli/default_config.toml b/docs/cli/default_config.toml index 534b88999d..0c033c0d6d 100644 --- a/docs/cli/default_config.toml +++ b/docs/cli/default_config.toml @@ -44,6 +44,7 @@ devfakeauthor = false txarrivalwait = "500ms" txannouncementonly = false disable-tx-propagation = false + nosnap = false [p2p.discovery] v4disc = true v5disc = true diff --git a/docs/cli/server.md b/docs/cli/server.md index 93e1501d11..f609ae56e8 100644 --- a/docs/cli/server.md +++ b/docs/cli/server.md @@ -304,6 +304,8 @@ The ```bor server``` command runs the Bor client. - ```nodiscover```: Disables the peer discovery mechanism (manual peer addition) (default: false) +- ```p2p.nosnap```: Disable serving snap sync requests to peers (snap/1 protocol is not advertised) (default: false) + - ```port```: Network listening port (default: 30303) - ```relay.bp-rpc-endpoints```: Comma separated rpc endpoints of all block producers diff --git a/eth/backend.go b/eth/backend.go index 3705f72514..59d83b256c 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -693,7 +693,7 @@ func (s *Ethereum) SetAuthorized(authorized bool) { // network protocols to start. func (s *Ethereum) Protocols() []p2p.Protocol { protos := eth.MakeProtocols((*ethHandler)(s.handler), s.networkID, s.discmix) - if s.config.SnapshotCache > 0 { + if s.config.SnapshotCache > 0 && !s.config.NoSnapServing { protos = append(protos, snap.MakeProtocols((*snapHandler)(s.handler))...) } if s.config.WitnessProtocol { diff --git a/eth/backend_protocols_test.go b/eth/backend_protocols_test.go new file mode 100644 index 0000000000..03ab5eef23 --- /dev/null +++ b/eth/backend_protocols_test.go @@ -0,0 +1,91 @@ +// Copyright 2025 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package eth + +import ( + "testing" + + "github.com/ethereum/go-ethereum/eth/ethconfig" + "github.com/ethereum/go-ethereum/eth/protocols/snap" +) + +// TestProtocolsSnapServing verifies that the snap/1 protocol is included or +// excluded from the advertised protocol list based on SnapshotCache and +// NoSnapServing config fields. +func TestProtocolsSnapServing(t *testing.T) { + th := newTestHandlerWithBlocks(0) + defer th.close() + + tests := []struct { + name string + snapshotCache int + noSnapServing bool + wantSnap bool + }{ + { + name: "snap served when snapshots enabled", + snapshotCache: 100, + noSnapServing: false, + wantSnap: true, + }, + { + name: "snap not served when SnapshotCache is zero", + snapshotCache: 0, + noSnapServing: false, + wantSnap: false, + }, + { + name: "snap not served when NoSnapServing is set", + snapshotCache: 100, + noSnapServing: true, + wantSnap: false, + }, + { + name: "snap not served when both SnapshotCache zero and NoSnapServing set", + snapshotCache: 0, + noSnapServing: true, + wantSnap: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + eth := &Ethereum{ + handler: th.handler, + networkID: 1, + config: ðconfig.Config{ + SnapshotCache: tt.snapshotCache, + NoSnapServing: tt.noSnapServing, + }, + } + + protos := eth.Protocols() + + var hasSnap bool + for _, proto := range protos { + if proto.Name == snap.ProtocolName { + hasSnap = true + break + } + } + + if hasSnap != tt.wantSnap { + t.Errorf("snap protocol advertised = %v, want %v", hasSnap, tt.wantSnap) + } + }) + } +} diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index 4447437e60..00710fee19 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -268,6 +268,11 @@ type Config struct { // WitnessProtocol enabless the wit protocol WitnessProtocol bool + // NoSnapServing disables the snap/1 sub-protocol, preventing this node from + // serving snap sync state to peers. Useful for block producers that should + // not spend resources on snap sync serving. + NoSnapServing bool + // SyncWithWitnesses enables syncing blocks with witnesses SyncWithWitnesses bool diff --git a/internal/cli/server/config.go b/internal/cli/server/config.go index 745d35fd56..35ce0c8434 100644 --- a/internal/cli/server/config.go +++ b/internal/cli/server/config.go @@ -285,6 +285,9 @@ type P2PConfig struct { // DisableTxPropagation disables transaction broadcast and announcement completely to its peers DisableTxPropagation bool `hcl:"disable-tx-propagation,optional" toml:"disable-tx-propagation,optional"` + + // NoSnapServing disables serving snap sync requests to peers (snap/1 protocol is not advertised) + NoSnapServing bool `hcl:"nosnap,optional" toml:"nosnap,optional"` } type P2PDiscovery struct { @@ -837,6 +840,7 @@ func DefaultConfig() *Config { TxArrivalWait: 500 * time.Millisecond, TxAnnouncementOnly: false, DisableTxPropagation: false, + NoSnapServing: false, Discovery: &P2PDiscovery{ DiscoveryV4: true, DiscoveryV5: true, @@ -1657,6 +1661,7 @@ func (c *Config) buildEth(stack *node.Node, accountManager *accounts.Manager) (* n.ParallelEVM.SpeculativeProcesses = c.ParallelEVM.SpeculativeProcesses n.ParallelEVM.Enforce = c.ParallelEVM.Enforce + n.NoSnapServing = c.P2P.NoSnapServing n.WitnessProtocol = c.Witness.Enable if c.SyncMode == "stateless" { if !c.Witness.Enable { diff --git a/internal/cli/server/flags.go b/internal/cli/server/flags.go index 602bdb1c7f..c8ed239f0e 100644 --- a/internal/cli/server/flags.go +++ b/internal/cli/server/flags.go @@ -1059,6 +1059,13 @@ func (c *Command) Flags(config *Config) *flagset.Flagset { Default: c.cliConfig.P2P.DisableTxPropagation, Group: "P2P", }) + f.BoolFlag(&flagset.BoolFlag{ + Name: "p2p.nosnap", + Usage: "Disable serving snap sync requests to peers (snap/1 protocol is not advertised)", + Value: &c.cliConfig.P2P.NoSnapServing, + Default: c.cliConfig.P2P.NoSnapServing, + Group: "P2P", + }) f.SliceStringFlag(&flagset.SliceStringFlag{ Name: "discovery.dns", Usage: "Comma separated list of enrtree:// URLs which will be queried for nodes to connect to",