Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
ff9f68a
feat(pushsync): remember first hop results per neighborhood
gacevicljubisa Nov 10, 2026
27c37ec
fix(pushsync): expire idle shallow records and wait when all peers ar…
gacevicljubisa Nov 16, 2026
844c4b0
chore: merge master into feat/pushsync-first-hop-memory-mainnet
gacevicljubisa Nov 16, 2026
85f1d65
fix(retrieval): ask closer peers before far healthy peers at the origin
gacevicljubisa Nov 16, 2026
19b84d4
fix(retrieval): ask closer peers before far healthy peers at forwarders
gacevicljubisa Nov 18, 2026
74bcfd6
chore: merge fix/pushsync-out-of-depth-chunk-loss into feat/pushsync-…
gacevicljubisa Nov 18, 2026
ff11ee8
chore(pushsync): shorten comments
gacevicljubisa Nov 18, 2026
649c1bc
fix(pushsync): send through a paused first hop and ignore late results
gacevicljubisa Nov 19, 2026
b3e76f3
fix(retrieval): select the closest reachable peer without the health …
gacevicljubisa Nov 19, 2026
180495a
fix(pushsync): remove idle first-hop records of unused neighborhoods
gacevicljubisa Nov 19, 2026
f81c595
fix(pushsync): use one radius per attempt and ignore late results aft…
gacevicljubisa Nov 20, 2026
b9ed8cf
refactor(pushsync): select the paused first hop in one pass over conn…
gacevicljubisa Nov 20, 2026
1c0bc9a
refactor(pushsync): build the first-hop skip list only where it is used
gacevicljubisa Nov 20, 2026
3977015
feat(pushsync): add gauges for the size of the first-hop memory
gacevicljubisa Nov 20, 2026
a7e7364
test(pushsync): remove the paused first hop test that passes without …
gacevicljubisa Nov 20, 2026
40cf109
refactor(pushsync): add the internal firsthop package for the first-h…
gacevicljubisa Nov 22, 2026
788a276
refactor(pushsync): use the firsthop package and move the first hop s…
gacevicljubisa Nov 22, 2026
52c3c28
feat(pushsync): limit the first-hop memory to 14 address bits per nei…
gacevicljubisa Nov 22, 2026
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
29 changes: 27 additions & 2 deletions pkg/pushsync/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

package pushsync

import "github.com/ethersphere/bee/v2/pkg/pushsync/pb"
import (
"github.com/ethersphere/bee/v2/pkg/pushsync/internal/firsthop"
"github.com/ethersphere/bee/v2/pkg/pushsync/pb"
"github.com/ethersphere/bee/v2/pkg/swarm"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
)

var (
ProtocolName = protocolName
Expand All @@ -15,6 +21,25 @@ var (
// CheckReceipt exposes the unexported receipt verification (signature recovery,
// overlay derivation, proximity and shallow-receipt logic) for fuzzing.
func (ps *PushSync) CheckReceipt(receipt *pb.Receipt) error {
_, err := ps.checkReceipt(receipt)
rad, err := ps.radius()
if err != nil {
return err
}
_, err = ps.checkReceipt(receipt, swarm.ZeroAddress, rad)
return err
}

// FirstHops returns the first-hop memory.
func (ps *PushSync) FirstHops() *firsthop.Memory { return ps.firstHops }

func (ps *PushSync) FirstHopCount(source, result string) float64 {
return counterValue(ps.metrics.FirstHop.WithLabelValues(source, result))
}

func counterValue(c prometheus.Counter) float64 {
var m dto.Metric
if err := c.Write(&m); err != nil {
return -1
}
return m.GetCounter().GetValue()
}
159 changes: 159 additions & 0 deletions pkg/pushsync/firsthop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Copyright 2026 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package pushsync

import (
"errors"
"slices"

"github.com/ethersphere/bee/v2/pkg/swarm"
"github.com/ethersphere/bee/v2/pkg/topology"
)

// firstHopSource says why an origin picked a first hop.
type firstHopSource string

const (
firstHopSourceProbe firstHopSource = "probe"
firstHopSourceValid firstHopSource = "known_valid"
firstHopSourceUnknown firstHopSource = "unknown"
firstHopSourcePaused firstHopSource = "paused"
)

// originPush picks the first hops of one push of an origin and records
// their results in the first-hop memory.
type originPush struct {
ps *PushSync
chunk swarm.Address
probed bool // the push made its probe
sources map[string]firstHopSource // why each first hop was picked
}

func (ps *PushSync) newOriginPush(chunk swarm.Address) *originPush {
return &originPush{
ps: ps,
chunk: chunk,
sources: make(map[string]firstHopSource),
}
}

// next picks the next first hop that is not in skip:
// 1. a probe, at most once per push and per probe interval of the
// neighborhood: a peer closer to the chunk than every peer with a valid
// record;
// 2. the closest peer with a valid record;
// 3. the closest peer that is not in a shallow pause;
// 4. the peer whose shallow pause ends first, unless the origin can store
// the chunk itself.
func (o *originPush) next(radius uint8, skip []swarm.Address) (swarm.Address, error) {
peer, source, err := o.pick(radius, skip)
if err != nil {
return swarm.ZeroAddress, err
}
o.probed = o.probed || source == firstHopSourceProbe
o.sources[peer.ByteString()] = source
return peer, nil
}

func (o *originPush) pick(radius uint8, skip []swarm.Address) (swarm.Address, firstHopSource, error) {
ps := o.ps
view := ps.firstHops.View(radius, o.chunk)

valid := o.connectedValid(radius, view.Valid)
if known := closestOf(o.chunk, valid, skip); !known.IsZero() {
if view.ProbeDue && !o.probed {
candidate, err := ps.closestPeer(o.chunk, true, slices.Concat(skip, view.Paused, valid))
if err == nil {
if closer, _ := candidate.Closer(o.chunk, known); closer && ps.firstHops.ClaimProbe(radius, o.chunk) {
return candidate, firstHopSourceProbe, nil
}
}
}
return known, firstHopSourceValid, nil
}

peer, err := ps.closestPeer(o.chunk, true, slices.Concat(skip, view.Paused))
if !errors.Is(err, topology.ErrNotFound) || len(view.Paused) == 0 {
return peer, firstHopSourceUnknown, err
}
if ps.fullNode && swarm.Proximity(ps.address.Bytes(), o.chunk.Bytes()) >= radius {
return peer, firstHopSourceUnknown, err
}

// Every peer left is in a shallow pause. Waiting would hold a push slot
// of the pusher, so send through the connected peer whose pause ends first.
connected := ps.connectedOf(view.Paused)
for _, p := range view.Paused {
if swarm.ContainsAddress(connected, p) && !swarm.ContainsAddress(skip, p) {
return p, firstHopSourcePaused, nil
}
}

return peer, firstHopSourceUnknown, err
}

// connectedValid returns the peers with a valid record that are connected,
// and removes the valid records of the others.
func (o *originPush) connectedValid(radius uint8, valid []swarm.Address) []swarm.Address {
if len(valid) == 0 {
return nil
}
connected := o.ps.connectedOf(valid)
for _, p := range valid {
if !swarm.ContainsAddress(connected, p) {
o.ps.firstHops.Forget(radius, o.chunk, p)
}
}
return connected
}

// closestOf returns the peer closest to the chunk among the peers that are
// not in skip, or the zero address.
func closestOf(chunk swarm.Address, peers, skip []swarm.Address) swarm.Address {
closest := swarm.ZeroAddress
for _, p := range peers {
if swarm.ContainsAddress(skip, p) {
continue
}
if closest.IsZero() {
closest = p
continue
}
if closer, _ := p.Closer(chunk, closest); closer {
closest = p
}
}
return closest
}

// record stores the result of a push to a first hop: a valid receipt (err is
// nil), a shallow receipt, or an error, which removes the record of the peer.
func (o *originPush) record(radius uint8, peer swarm.Address, err error) {
result := "error"
switch {
case err == nil:
result = "valid"
o.ps.firstHops.Valid(radius, o.chunk, peer)
case errors.Is(err, ErrShallowReceipt):
result = "shallow"
o.ps.firstHops.Shallow(radius, o.chunk, peer)
default:
o.ps.firstHops.Forget(radius, o.chunk, peer)
}
o.ps.metrics.FirstHop.WithLabelValues(string(o.sources[peer.ByteString()]), result).Inc()
}

// connectedOf returns the peers that are connected, in one pass over the
// connected peers.
func (ps *PushSync) connectedOf(peers []swarm.Address) []swarm.Address {
var connected []swarm.Address
_ = ps.topologyDriver.EachConnectedPeer(func(p swarm.Address, _ uint8) (bool, bool, error) {
if swarm.ContainsAddress(peers, p) {
connected = append(connected, p)
}
return len(connected) == len(peers), false, nil
}, topology.Select{})
return connected
}
Loading
Loading