net

package
v0.0.3-chain Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2018 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HandshakeCode cmd = iota
	StatusCode
	ForkCode // tell peer it has forked, use for respond GetSnapshotBlocksCode
	GetSubLedgerCode
	GetSnapshotBlocksCode // get snapshotblocks without content
	GetSnapshotBlocksContentCode
	GetFullSnapshotBlocksCode   // get snapshotblocks with content
	GetSnapshotBlocksByHashCode // a batch of hash
	GetSnapshotBlocksContentByHashCode
	GetFullSnapshotBlocksByHashCode
	GetAccountBlocksCode       // query single AccountChain
	GetMultiAccountBlocksCode  // query multi AccountChain
	GetAccountBlocksByHashCode // query accountBlocks by hashList
	GetFilesCode
	GetChunkCode
	SubLedgerCode
	FileListCode
	SnapshotBlocksCode
	SnapshotBlocksContentCode
	FullSnapshotBlocksCode
	AccountBlocksCode
	NewSnapshotBlockCode

	ExceptionCode = 127
)
View Source
const CmdSet = 2
View Source
const CmdSetName = "vite"

@section Cmd

View Source
const INIT_ID uint64 = 1

@section requestPool

Variables

View Source
var NULL_ADDRESS = types.Address{}

Functions

func NewPeerSet

func NewPeerSet() *peerSet

Types

type AccountblockCallback

type AccountblockCallback func(addr types.Address, block *ledger.AccountBlock)

type Broadcaster

type Broadcaster interface {
	BroadcastSnapshotBlock(block *ledger.SnapshotBlock)
	BroadcastAccountBlock(addr types.Address, block *ledger.AccountBlock)
	BroadcastAccountBlocks(addr types.Address, blocks []*ledger.AccountBlock)
}

type Chain

type Chain interface {
	// the second return value mean chunk befor/after file
	GetSubLedgerByHeight(start, count uint64, forward bool) ([]*ledger.CompressedFileMeta, [][2]uint64)
	GetSubLedgerByHash(origin *types.Hash, count uint64, forward bool) ([]*ledger.CompressedFileMeta, [][2]uint64, error)

	// query chunk
	GetConfirmSubLedger(start, end uint64) ([]*ledger.SnapshotBlock, map[types.Address][]*ledger.AccountBlock, error)

	GetSnapshotBlocksByHash(origin *types.Hash, count uint64, forward, content bool) ([]*ledger.SnapshotBlock, error)
	GetSnapshotBlocksByHeight(height, count uint64, forward, content bool) ([]*ledger.SnapshotBlock, error)

	GetAccountBlocksByHash(addr types.Address, origin *types.Hash, count uint64, forward bool) ([]*ledger.AccountBlock, error)
	GetAccountBlocksByHeight(addr types.Address, start, count uint64, forward bool) ([]*ledger.AccountBlock, error)

	GetAccountBlockByHash(blockHash *types.Hash) (*ledger.AccountBlock, error)
	GetAccountBlockByHeight(addr *types.Address, height uint64) (*ledger.AccountBlock, error)

	GetLatestSnapshotBlock() *ledger.SnapshotBlock
	GetGenesisSnapshotBlock() *ledger.SnapshotBlock

	Compressor() *compress.Compressor
}

all query include from block

type Config

type Config struct {
	Port     uint16
	Chain    Chain
	Verifier Verifier
}

type Fetcher

type Fetcher interface {
	// from is required, because we need from + count to find appropriate peer
	FetchSnapshotBlocks(start uint64, count uint64, hash *types.Hash)

	// address is optional
	FetchAccountBlocks(start types.Hash, count uint64, address *types.Address)
}

type Filter

type Filter interface {
}

type MsgHandler

type MsgHandler interface {
	ID() string
	Cmds() []cmd
	Handle(msg *p2p.Msg, sender *Peer) error
}

type Net

type Net struct {
	*Config

	Protocols []*p2p.Protocol // mount to p2p.Server
	// contains filtered or unexported fields
}

func New

func New(cfg *Config) (*Net, error)

auto from

func (*Net) AddHandler

func (n *Net) AddHandler(handler MsgHandler)

func (*Net) BroadcastAccountBlock

func (n *Net) BroadcastAccountBlock(addr types.Address, block *ledger.AccountBlock)

func (*Net) BroadcastAccountBlocks

func (n *Net) BroadcastAccountBlocks(addr types.Address, blocks []*ledger.AccountBlock)

func (*Net) BroadcastSnapshotBlock

func (n *Net) BroadcastSnapshotBlock(block *ledger.SnapshotBlock)

func (*Net) FetchAccountBlocks

func (n *Net) FetchAccountBlocks(start types.Hash, count uint64, address *types.Address)

func (*Net) FetchSnapshotBlocks

func (n *Net) FetchSnapshotBlocks(start uint64, count uint64, hash *types.Hash)

func (*Net) HandlePeer

func (n *Net) HandlePeer(p *Peer) error

will be called by p2p.Server, run as goroutine

func (*Net) Status

func (n *Net) Status() *NetStatus

get current netInfo (peers, syncStatus, ...)

func (*Net) Stop

func (n *Net) Stop()

func (*Net) SubscribeAccountBlock

func (n *Net) SubscribeAccountBlock(fn AccountblockCallback) (subId int)

func (*Net) SubscribeSnapshotBlock

func (n *Net) SubscribeSnapshotBlock(fn SnapshotBlockCallback) (subId int)

func (*Net) SubscribeSyncStatus

func (n *Net) SubscribeSyncStatus(fn func(SyncState)) (subId int)

func (*Net) SyncState

func (n *Net) SyncState() SyncState

func (*Net) Syncing

func (n *Net) Syncing() bool

func (*Net) UnsubscribeAccountBlock

func (n *Net) UnsubscribeAccountBlock(subId int)

func (*Net) UnsubscribeSnapshotBlock

func (n *Net) UnsubscribeSnapshotBlock(subId int)

func (*Net) UnsubscribeSyncStatus

func (n *Net) UnsubscribeSyncStatus(subId int)

type NetStatus

type NetStatus struct {
	Peers     []*PeerInfo
	SyncState SyncState
	Running   bool
}

type Peer

type Peer struct {
	*p2p.Peer

	ID string

	CmdSet      uint64 // which cmdSet it belongs
	KnownBlocks *cuckoofilter.CuckooFilter
	Log         log15.Logger
	// contains filtered or unexported fields
}

func (*Peer) FileAddress

func (p *Peer) FileAddress() *net.TCPAddr

func (*Peer) Handshake

func (p *Peer) Handshake(our *message.HandShake) error

func (*Peer) Info added in v1.3.2

func (p *Peer) Info() *PeerInfo

func (*Peer) ReadHandshake

func (p *Peer) ReadHandshake() (their *message.HandShake, err error)

func (*Peer) SeeBlock

func (p *Peer) SeeBlock(hash types.Hash)

func (*Peer) Send

func (p *Peer) Send(code cmd, msgId uint64, payload p2p.Serializable) error

func (*Peer) SendAccountBlocks

func (p *Peer) SendAccountBlocks(s *message.AccountBlocks, msgId uint64) (err error)

func (*Peer) SendNewSnapshotBlock

func (p *Peer) SendNewSnapshotBlock(b *ledger.SnapshotBlock) (err error)

func (*Peer) SendSnapshotBlocks

func (p *Peer) SendSnapshotBlocks(bs []*ledger.SnapshotBlock, msgId uint64) (err error)

func (*Peer) SendSubLedger

func (p *Peer) SendSubLedger(s *message.SubLedger, msgId uint64) (err error)

response

func (*Peer) SetHead

func (p *Peer) SetHead(head types.Hash, height uint64)

type PeerInfo

type PeerInfo struct {
	Addr   string
	Flag   int
	Head   string
	Height uint64
}

type Peers

type Peers []*Peer

@implementation sort.Interface

func (Peers) Len

func (s Peers) Len() int

func (Peers) Less

func (s Peers) Less(i, j int) bool

func (Peers) Swap

func (s Peers) Swap(i, j int)

type Receiver

type Receiver interface {
	ReceiveSnapshotBlocks(blocks []*ledger.SnapshotBlock)
	ReceiveAccountBlocks(mblocks map[types.Address][]*ledger.AccountBlock)
	ReceiveNewSnapshotBlock(block *ledger.SnapshotBlock)
}

type Request

type Request interface {
	Handle(ctx *context, msg *p2p.Msg, peer *Peer)
	ID() uint64
	Run(ctx *context)
	Done(err error)
	Expired() bool
	State() reqState
}

type RequestPool

type RequestPool interface {
	Add(r Request) bool // can add request or not
	Retry(r uint64) bool
	Del(r uint64) bool
	MsgID() uint64
}

type SnapshotBlockCallback

type SnapshotBlockCallback func(block *ledger.SnapshotBlock)

type Subscriber

type Subscriber interface {
	// return the subId, use to unsubscibe
	// subId is always larger than 0
	SubscribeAccountBlock(fn AccountblockCallback) (subId int)
	// if subId is 0, then ignore
	UnsubscribeAccountBlock(subId int)

	// return the subId, use to unsubscibe
	// subId is always larger than 0
	SubscribeSnapshotBlock(fn SnapshotBlockCallback) (subId int)
	// if subId is 0, then ignore
	UnsubscribeSnapshotBlock(subId int)

	// return the subId, use to unsubscibe
	// subId is always larger than 0
	SubscribeSyncStatus(fn SyncStateCallback) (subId int)
	// if subId is 0, then ignore
	UnsubscribeSyncStatus(subId int)
}

type SyncState

type SyncState int32
const (
	SyncNotStart SyncState = iota
	Syncing
	Syncdone
	Syncerr
	SyncCancel
	SyncDownloaded
)

func (SyncState) String

func (s SyncState) String() string

type SyncStateCallback

type SyncStateCallback func(SyncState)

type SyncStateFeed

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

func (*SyncStateFeed) Notify

func (s *SyncStateFeed) Notify(st SyncState)

func (*SyncStateFeed) Sub

func (s *SyncStateFeed) Sub(fn SyncStateCallback) int

func (*SyncStateFeed) Unsub

func (s *SyncStateFeed) Unsub(subId int)

type Verifier

type Verifier interface {
	VerifyforP2P(block *ledger.AccountBlock) bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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