syncer

package
v0.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2025 License: MIT Imports: 12 Imported by: 18

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// AnagamiBootstrapPeers is a list of peers for the Anagami network.
	AnagamiBootstrapPeers = []string{
		"15.204.160.10:9781",
		"141.95.66.81:9781",
		"51.81.242.140:9781",
		"15.204.101.216:9781",
		"40.160.12.62:9781",
	}

	// MainnetBootstrapPeers is a list of peers for the Mainnet network.
	MainnetBootstrapPeers = []string{
		"40.160.12.62:9881",
		"108.227.62.195:9981",
		"139.162.81.190:9991",
		"144.217.7.188:9981",
		"147.182.196.252:9981",
		"15.235.85.30:9981",
		"167.235.234.84:9981",
		"173.235.144.230:9981",
		"198.98.53.144:7791",
		"199.27.255.169:9981",
		"2.136.192.200:9981",
		"213.159.50.43:9981",
		"24.253.116.61:9981",
		"46.249.226.103:9981",
		"5.165.236.113:9981",
		"5.252.226.131:9981",
		"54.38.120.222:9981",
		"62.210.136.25:9981",
		"63.135.62.123:9981",
		"65.21.93.245:9981",
		"75.165.149.114:9981",
		"77.51.200.125:9981",
		"81.6.58.121:9981",
		"83.194.193.156:9981",
		"84.39.246.63:9981",
		"87.99.166.34:9981",
		"91.214.242.11:9981",
		"93.105.88.181:9981",
		"93.180.191.86:9981",
		"94.130.220.162:9981",
	}

	// ZenBootstrapPeers is a list of peers for the Zen network.
	ZenBootstrapPeers = []string{
		"40.160.12.62:9881",
		"141.94.161.198:9881",
		"15.204.141.235:9881",
		"51.79.16.87:9881",
		"141.94.161.198:9881",
		"31.208.123.47:9881",
		"62.30.63.90:9881",
		"162.19.232.38:9881",
		"15.204.160.10:10881",
		"51.81.56.133:9881",
	}
)
View Source
var (
	// ErrPeerBanned is returned when a peer is banned.
	ErrPeerBanned = errors.New("peer is banned")
	// ErrPeerNotFound is returned when the peer is not found.
	ErrPeerNotFound = errors.New("peer not found")
)

Functions

func Subnet

func Subnet(addr, mask string) string

Subnet normalizes the provided CIDR subnet string.

Types

type ChainManager

type ChainManager interface {
	History() ([32]types.BlockID, error)
	BlocksForHistory(history []types.BlockID, max uint64) ([]types.Block, uint64, error)
	Block(id types.BlockID) (types.Block, bool)
	State(id types.BlockID) (consensus.State, bool)
	AddBlocks(blocks []types.Block) error
	Tip() types.ChainIndex
	TipState() consensus.State

	PoolTransaction(txid types.TransactionID) (types.Transaction, bool)
	AddPoolTransactions(txns []types.Transaction) (bool, error)
	V2PoolTransaction(txid types.TransactionID) (types.V2Transaction, bool)
	AddV2PoolTransactions(basis types.ChainIndex, txns []types.V2Transaction) (bool, error)
	TransactionsForPartialBlock(missing []types.Hash256) ([]types.Transaction, []types.V2Transaction)
}

A ChainManager manages blockchain state.

type Option

type Option func(*config)

An Option modifies a Syncer's configuration.

func WithBanDuration

func WithBanDuration(d time.Duration) Option

WithBanDuration sets the duration for which a peer is banned when misbehaving.

func WithConnectTimeout

func WithConnectTimeout(d time.Duration) Option

WithConnectTimeout sets the timeout when connecting to a peer. The default is 5 seconds.

func WithLogger

func WithLogger(l *zap.Logger) Option

WithLogger sets the logger used by a Syncer. The default is a logger that outputs to io.Discard.

func WithMaxInboundPeers

func WithMaxInboundPeers(n int) Option

WithMaxInboundPeers sets the maximum number of inbound connections. The default is 8.

func WithMaxInflightRPCs

func WithMaxInflightRPCs(n int) Option

WithMaxInflightRPCs sets the maximum number of concurrent RPCs per peer. The default is 3.

func WithMaxOutboundPeers

func WithMaxOutboundPeers(n int) Option

WithMaxOutboundPeers sets the maximum number of outbound connections. The default is 8.

func WithMaxSendBlocks

func WithMaxSendBlocks(n uint64) Option

WithMaxSendBlocks sets the maximum number of blocks requested per SendBlocks RPC. The default is 10.

func WithPeerDiscoveryInterval

func WithPeerDiscoveryInterval(d time.Duration) Option

WithPeerDiscoveryInterval sets the frequency at which the syncer attempts to discover and connect to new peers. The default is 5 seconds.

func WithRelayBlockOutlineTimeout

func WithRelayBlockOutlineTimeout(d time.Duration) Option

WithRelayBlockOutlineTimeout sets the timeout for the RelayV2BlockOutline RPC. The default is 60 seconds.

func WithRelayHeaderTimeout

func WithRelayHeaderTimeout(d time.Duration) Option

WithRelayHeaderTimeout sets the timeout for the RelayHeader and RelayV2Header RPCs. The default is 5 seconds.

func WithRelayTransactionSetTimeout

func WithRelayTransactionSetTimeout(d time.Duration) Option

WithRelayTransactionSetTimeout sets the timeout for the RelayTransactionSet RPC. The default is 60 seconds.

func WithSendBlockTimeout

func WithSendBlockTimeout(d time.Duration) Option

WithSendBlockTimeout sets the timeout for the SendBlock RPC. The default is 60 seconds.

func WithSendBlocksTimeout

func WithSendBlocksTimeout(d time.Duration) Option

WithSendBlocksTimeout sets the timeout for the SendBlocks RPC. The default is 120 seconds.

func WithSendTransactionsTimeout

func WithSendTransactionsTimeout(d time.Duration) Option

WithSendTransactionsTimeout sets the timeout for the SendTransactions RPC. The default is 60 seconds.

func WithShareNodesTimeout

func WithShareNodesTimeout(d time.Duration) Option

WithShareNodesTimeout sets the timeout for the ShareNodes RPC. The default is 5 seconds.

func WithSyncInterval

func WithSyncInterval(d time.Duration) Option

WithSyncInterval sets the frequency at which the syncer attempts to sync with peers. The default is 5 seconds.

type Peer

type Peer struct {
	ConnAddr string
	Inbound  bool
	// contains filtered or unexported fields
}

A Peer is a connected gateway peer.

func (*Peer) Addr

func (p *Peer) Addr() string

Addr returns the peer's reported dialback address.

func (*Peer) Close

func (p *Peer) Close() error

Close closes the peer's connection.

func (*Peer) DiscoverIP

func (p *Peer) DiscoverIP(timeout time.Duration) (string, error)

DiscoverIP requests our external IP as seen by the peer.

func (*Peer) Err

func (p *Peer) Err() error

Err returns the error that caused the peer to disconnect, if any.

func (*Peer) RelayHeader

func (p *Peer) RelayHeader(h types.BlockHeader, timeout time.Duration) error

RelayHeader relays a header to the peer.

func (*Peer) RelayTransactionSet

func (p *Peer) RelayTransactionSet(txns []types.Transaction, timeout time.Duration) error

RelayTransactionSet relays a transaction set to the peer.

func (*Peer) RelayV2BlockOutline

func (p *Peer) RelayV2BlockOutline(b gateway.V2BlockOutline, timeout time.Duration) error

RelayV2BlockOutline relays a v2 block outline to the peer.

func (*Peer) RelayV2Header

func (p *Peer) RelayV2Header(bh types.BlockHeader, timeout time.Duration) error

RelayV2Header relays a v2 block header to the peer.

func (*Peer) RelayV2TransactionSet

func (p *Peer) RelayV2TransactionSet(index types.ChainIndex, txns []types.V2Transaction, timeout time.Duration) error

RelayV2TransactionSet relays a v2 transaction set to the peer.

func (*Peer) SendBlock

func (p *Peer) SendBlock(id types.BlockID, timeout time.Duration) (types.Block, error)

SendBlock requests a single block from the peer.

func (*Peer) SendBlocks

func (p *Peer) SendBlocks(history [32]types.BlockID, timeout time.Duration, fn func([]types.Block) error) error

SendBlocks downloads blocks from p, starting from the most recent element of history known to p. The blocks are sent in batches, and fn is called on each batch.

func (*Peer) SendCheckpoint

func (p *Peer) SendCheckpoint(index types.ChainIndex, timeout time.Duration) (types.Block, consensus.State, error)

SendCheckpoint requests a checkpoint from the peer. The checkpoint is validated.

func (*Peer) SendTransactions

func (p *Peer) SendTransactions(index types.ChainIndex, txnHashes []types.Hash256, timeout time.Duration) ([]types.Transaction, []types.V2Transaction, error)

SendTransactions requests a subset of a block's transactions from the peer.

func (*Peer) SendV2Blocks

func (p *Peer) SendV2Blocks(history []types.BlockID, maxBlocks uint64, timeout time.Duration) ([]types.Block, uint64, error)

SendV2Blocks requests up to n blocks from p, starting from the most recent element of history known to p. The peer also returns the number of remaining blocks left to sync.

func (*Peer) ShareNodes

func (p *Peer) ShareNodes(timeout time.Duration) ([]string, error)

ShareNodes requests a list of potential peers from the peer.

func (*Peer) String

func (p *Peer) String() string

String implements fmt.Stringer.

func (*Peer) Synced

func (p *Peer) Synced() bool

Synced returns the peer's sync status.

func (*Peer) UniqueID

func (p *Peer) UniqueID() gateway.UniqueID

UniqueID returns the peer's reported UniqueID.

func (*Peer) Version

func (p *Peer) Version() string

Version returns the peer's reported version.

type PeerInfo

type PeerInfo struct {
	Address      string        `json:"address"`
	FirstSeen    time.Time     `json:"firstSeen"`
	LastConnect  time.Time     `json:"lastConnect,omitempty"`
	SyncedBlocks uint64        `json:"syncedBlocks,omitempty"`
	SyncDuration time.Duration `json:"syncDuration,omitempty"`
}

PeerInfo contains metadata about a peer.

type PeerStore

type PeerStore interface {
	// AddPeer adds a peer to the store. If the peer already exists, nil should
	// be returned.
	AddPeer(addr string) error
	// Peers returns the set of known peers.
	Peers() ([]PeerInfo, error)
	// PeerInfo returns the metadata for the specified peer or ErrPeerNotFound
	// if the peer wasn't found in the store.
	PeerInfo(addr string) (PeerInfo, error)
	// UpdatePeerInfo updates the metadata for the specified peer. If the peer
	// is not found, the error should be ErrPeerNotFound.
	UpdatePeerInfo(addr string, fn func(*PeerInfo)) error
	// Ban temporarily bans one or more IPs. The addr should either be a single
	// IP with port (e.g. 1.2.3.4:5678) or a CIDR subnet (e.g. 1.2.3.4/16).
	Ban(addr string, duration time.Duration, reason string) error

	// Banned returns true, nil if the peer is banned.
	Banned(addr string) (bool, error)
}

A PeerStore stores peers and bans.

type Syncer

type Syncer struct {
	// contains filtered or unexported fields
}

A Syncer synchronizes blockchain data with peers.

func New

func New(l net.Listener, cm ChainManager, pm PeerStore, header gateway.Header, opts ...Option) *Syncer

New returns a new Syncer.

func (*Syncer) Addr

func (s *Syncer) Addr() string

Addr returns the address of the Syncer.

func (*Syncer) BroadcastHeader

func (s *Syncer) BroadcastHeader(bh types.BlockHeader)

BroadcastHeader broadcasts a header to all peers.

func (*Syncer) BroadcastTransactionSet

func (s *Syncer) BroadcastTransactionSet(txns []types.Transaction)

BroadcastTransactionSet broadcasts a transaction set to all peers.

func (*Syncer) BroadcastV2BlockOutline

func (s *Syncer) BroadcastV2BlockOutline(b gateway.V2BlockOutline)

BroadcastV2BlockOutline broadcasts a v2 block outline to all peers.

func (*Syncer) BroadcastV2Header

func (s *Syncer) BroadcastV2Header(bh types.BlockHeader)

BroadcastV2Header broadcasts a v2 header to all peers.

func (*Syncer) BroadcastV2TransactionSet

func (s *Syncer) BroadcastV2TransactionSet(index types.ChainIndex, txns []types.V2Transaction)

BroadcastV2TransactionSet broadcasts a v2 transaction set to all peers.

func (*Syncer) Close added in v0.2.2

func (s *Syncer) Close() error

Close closes the Syncer's net.Listener.

func (*Syncer) Connect

func (s *Syncer) Connect(ctx context.Context, addr string) (*Peer, error)

Connect forms an outbound connection to a peer.

func (*Syncer) PeerInfo added in v0.0.4

func (s *Syncer) PeerInfo(addr string) (PeerInfo, error)

PeerInfo returns the metadata for the specified peer or ErrPeerNotFound if the peer wasn't found in the store.

func (*Syncer) Peers

func (s *Syncer) Peers() []*Peer

Peers returns the set of currently-connected peers.

func (*Syncer) Run

func (s *Syncer) Run(ctx context.Context) error

Run spawns goroutines for accepting inbound connections, forming outbound connections, and syncing the blockchain from active peers. It blocks until an error occurs, upon which all connections are closed and goroutines are terminated.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL