topology.go 1.46 KB
Newer Older
acud's avatar
acud committed
1 2 3 4
// Copyright 2020 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.

5 6
// Package topology exposes abstractions needed in
// topology-aware components.
acud's avatar
acud committed
7 8 9
package topology

import (
10
	"context"
acud's avatar
acud committed
11
	"errors"
12
	"io"
acud's avatar
acud committed
13 14 15 16

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

17 18 19 20
var (
	ErrNotFound = errors.New("no peer found")
	ErrWantSelf = errors.New("node wants self")
)
acud's avatar
acud committed
21 22

type Driver interface {
23
	PeerAdder
24
	ClosestPeerer
25 26 27
	EachPeerer
	NeighborhoodDepth() uint8
	SubscribePeersChange() (c <-chan struct{}, unsubscribe func())
28
	io.Closer
acud's avatar
acud committed
29 30
}

31
type PeerAdder interface {
32 33
	// AddPeers is called when peers are added to the topology backlog
	AddPeers(ctx context.Context, addr ...swarm.Address) error
34 35
}

36
type ClosestPeerer interface {
37 38 39 40 41
	// ClosestPeer returns the closest connected peer we have in relation to a
	// given chunk address.
	// This function will ignore peers with addresses provided in skipPeers.
	// Returns topology.ErrWantSelf in case base is the closest to the address.
	ClosestPeer(addr swarm.Address, skipPeers ...swarm.Address) (peerAddr swarm.Address, err error)
42
}
43

44 45 46 47 48 49 50
type EachPeerer interface {
	// EachPeer iterates from closest bin to farthest
	EachPeer(EachPeerFunc) error
	// EachPeerRev iterates from farthest bin to closest
	EachPeerRev(EachPeerFunc) error
}

51 52
// EachPeerFunc is a callback that is called with a peer and its PO
type EachPeerFunc func(swarm.Address, uint8) (stop, jumpToNext bool, err error)