peermgr

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2022 License: Apache-2.0, MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Opts

type Opts struct {
	// The datastore path of the peer manager.
	Path string
}

Opts is the options for peer manager.

type PeerManager

type PeerManager interface {
	// AddPeer adds a peer. If there is an existing peer presented, overwrite the peer addr.
	//
	// @input - context, currency id, peer wallet address, peer p2p address.
	//
	// @output - error.
	AddPeer(ctx context.Context, currencyID byte, toAddr string, pi peer.AddrInfo) error

	// HasPeer checks if a given peer exists.
	//
	// @input - context, currency id, peer wallet address.
	//
	// @output - if exists, error.
	HasPeer(ctx context.Context, currencyID byte, toAddr string) (bool, error)

	// RemovePeer removes a peer from the manager.
	//
	// @input - context, currency id, peer wallet address.
	//
	// @output - error.
	RemovePeer(ctx context.Context, currencyID byte, toAddr string) error

	// PeerAddr gets the peer addr by given wallet address.
	//
	// @input - context, currency id, peer wallet address.
	//
	// @output - peer p2p address, error.
	PeerAddr(ctx context.Context, currencyID byte, toAddr string) (peer.AddrInfo, error)

	// IsBlocked checks if given peer is currently blocked.
	//
	// @input - context, currency id, peer wallet address.
	//
	// @output - boolean indicating whether it is blocked, error.
	IsBlocked(ctx context.Context, currencyID byte, toAddr string) (bool, error)

	// BlockPeer blocks a peer.
	//
	// @input - context, currency id, peer wallet address.
	//
	// @output - error.
	BlockPeer(ctx context.Context, currencyID byte, toAddr string) error

	// UnblockPeer unblocks a peer.
	//
	// @input - context, currency id, peer wallet address.
	//
	// @output - error.
	UnblockPeer(ctx context.Context, currencyID byte, toAddr string) error

	// ListCurrencyIDs lists all currencies.
	//
	// @input - context.
	//
	// @output - currency chan out, error chan out.
	ListCurrencyIDs(ctx context.Context) (<-chan byte, <-chan error)

	// ListPeers lists all peers stored by the manager.
	//
	// @input - context, currency id.
	//
	// @output - peer wallet address channel out, error channel out.
	ListPeers(ctx context.Context, currencyID byte) (<-chan string, <-chan error)

	// AddToHistory adds a record to a given peer's history.
	//
	// @input - context, currency id, peer wallet address, record.
	//
	// @output - error.
	AddToHistory(ctx context.Context, currencyID byte, toAddr string, rec Record) error

	// RemoveRecord removes a record from a given peer's history.
	//
	// @input - context, currency id, peer wallet address, record id.
	//
	// @output - error.
	RemoveRecord(ctx context.Context, currencyID byte, toAddr string, recID *big.Int) error

	// SetRecID sets the next record id to use for a given peer's history.
	//
	// @input - context, currency id, peer wallet address, record id.
	//
	// @output - error.
	SetRecID(ctx context.Context, currencyID byte, toAddr string, recID *big.Int) error

	// ListHistory lists all recorded history of given peer. The result will be given from
	// latest to oldest.
	//
	// @input - context, currency id, peer wallet address.
	//
	// @output - record id channel out, record channel out, error channel out.
	ListHistory(ctx context.Context, currencyID byte, toAddr string) (<-chan *big.Int, <-chan Record, <-chan error)
}

PeerManager is an interface for a peer manager that not only tracks each peer by maintaining a map of (toAddr -> peer addr), also tracks the history of each peer, where history is a collection of record. It can also be used to block/unblock a peer.

type PeerManagerImpl

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

PeerManagerImpl is the implementation of PeerManager interface.

func NewPeerManagerImpl

func NewPeerManagerImpl(ctx context.Context, opts Opts) (*PeerManagerImpl, error)

NewPeerManagerImpl creates a new PeerManager.

@input - context, options.

@output - peer manager, error.

func (*PeerManagerImpl) AddPeer

func (mgr *PeerManagerImpl) AddPeer(ctx context.Context, currencyID byte, toAddr string, pi peer.AddrInfo) error

AddPeer adds a peer. If there is an existing peer presented, overwrite the peer addr.

@input - context, currency id, peer wallet address, peer p2p address.

@output - error.

func (*PeerManagerImpl) AddToHistory

func (mgr *PeerManagerImpl) AddToHistory(ctx context.Context, currencyID byte, toAddr string, rec Record) error

AddToHistory adds a record to a given peer's history.

@input - context, currency id, peer wallet address, record.

@output - error.

func (*PeerManagerImpl) BlockPeer

func (mgr *PeerManagerImpl) BlockPeer(ctx context.Context, currencyID byte, toAddr string) error

BlockPeer blocks a peer.

@input - context, currency id, peer wallet address.

@output - error.

func (*PeerManagerImpl) HasPeer

func (mgr *PeerManagerImpl) HasPeer(ctx context.Context, currencyID byte, toAddr string) (bool, error)

HasPeer checks if a given peer exists.

@input - context, currency id, peer wallet address.

@output - if exists, error.

func (*PeerManagerImpl) IsBlocked

func (mgr *PeerManagerImpl) IsBlocked(ctx context.Context, currencyID byte, toAddr string) (bool, error)

IsBlocked checks if given peer is currently blocked.

@input - context, currency id, peer wallet address.

@output - boolean indicating whether it is blocked, error.

func (*PeerManagerImpl) ListCurrencyIDs

func (mgr *PeerManagerImpl) ListCurrencyIDs(ctx context.Context) (<-chan byte, <-chan error)

ListCurrencyIDs lists all currencies.

@input - context.

@output - currency chan out, error chan out.

func (*PeerManagerImpl) ListHistory

func (mgr *PeerManagerImpl) ListHistory(ctx context.Context, currencyID byte, toAddr string) (<-chan *big.Int, <-chan Record, <-chan error)

ListHistory lists all recorded history of given peer. The result will be given from latest to oldest.

@input - context, currency id, peer wallet address.

@output - record id channel out, record channel out, error channel out.

func (*PeerManagerImpl) ListPeers

func (mgr *PeerManagerImpl) ListPeers(ctx context.Context, currencyID byte) (<-chan string, <-chan error)

ListPeers lists all peers stored by the manager.

@input - context, currency id.

@output - peer wallet address channel out, error channel out.

func (*PeerManagerImpl) PeerAddr

func (mgr *PeerManagerImpl) PeerAddr(ctx context.Context, currencyID byte, toAddr string) (peer.AddrInfo, error)

PeerAddr gets the peer addr by given wallet address.

@input - context, currency id, peer wallet address.

@output - peer p2p address, error.

func (*PeerManagerImpl) RemovePeer

func (mgr *PeerManagerImpl) RemovePeer(ctx context.Context, currencyID byte, toAddr string) error

RemovePeer removes a peer from the manager.

@input - context, currency id, peer wallet address.

@output - error.

func (*PeerManagerImpl) RemoveRecord

func (mgr *PeerManagerImpl) RemoveRecord(ctx context.Context, currencyID byte, toAddr string, recID *big.Int) error

RemoveRecord removes a record from a given peer's history.

@input - context, currency id, peer wallet address, record id.

@output - error.

func (*PeerManagerImpl) SetRecID

func (mgr *PeerManagerImpl) SetRecID(ctx context.Context, currencyID byte, toAddr string, recID *big.Int) error

SetRecID sets the next record id to use for a given peer's history.

@input - context, currency id, peer wallet address, record id.

@output - error.

func (*PeerManagerImpl) Shutdown

func (mgr *PeerManagerImpl) Shutdown()

Shutdown safely shuts down the component.

func (*PeerManagerImpl) UnblockPeer

func (mgr *PeerManagerImpl) UnblockPeer(ctx context.Context, currencyID byte, toAddr string) error

UnblockPeer unblocks a peer.

@input - context, currency id, peer wallet address.

@output - error.

type Record

type Record struct {
	// Description describes the record.
	Description string

	// CreatedAt is the time when the record is created.
	CreatedAt time.Time
}

Record is a record of activity of a peer.

func DecodeRecord

func DecodeRecord(data []byte) (Record, error)

DecodeRecord decodes a record data bytes.

@input - data.

@output - record, error.

func (Record) Encode

func (r Record) Encode() ([]byte, error)

Encode encodes a record.

@output - data, error.

Jump to

Keyboard shortcuts

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