net

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2019 License: GPL-3.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const CmdSet = 2
View Source
const DefaultPort uint16 = 8484
View Source
const Vite = "vite"

Variables

View Source
var NULL_ADDRESS = types.Address{}

Functions

This section is empty.

Types

type AccountblockCallback

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

type BlockSubscriber added in v1.3.0

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

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

A BlockSubscriber implementation can be subscribed and Unsubscribed, when got a block, should notify subscribers

type Broadcaster

type Broadcaster interface {
	BroadcastSnapshotBlock(block *ledger.SnapshotBlock)
	BroadcastSnapshotBlocks(blocks []*ledger.SnapshotBlock)
	BroadcastAccountBlock(block *ledger.AccountBlock)
	BroadcastAccountBlocks(blocks []*ledger.AccountBlock)
}

A Broadcaster implementation can send blocks to the peers connected

type Chain

type Chain interface {
	GetSubLedgerByHeight(start, count uint64, forward bool) ([]*ledger.CompressedFileMeta, [][2]uint64)
	GetSubLedgerByHash(origin *types.Hash, count uint64, forward bool) ([]*ledger.CompressedFileMeta, [][2]uint64, error)

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

	GetSnapshotBlockByHeight(height uint64) (*ledger.SnapshotBlock, error)
	GetSnapshotBlockByHash(hash *types.Hash) (*ledger.SnapshotBlock, error)

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

	GetAccountBlockByHash(blockHash *types.Hash) (*ledger.AccountBlock, error)
	GetAccountBlockByHeight(addr *types.Address, height uint64) (*ledger.AccountBlock, 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)

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

	// Compressor could compress/uncompress a file
	Compressor() *compress.Compressor
}

type ChunkPoolStatus added in v1.3.0

type ChunkPoolStatus struct {
	Request []ChunkReqStatus
}

type ChunkReqStatus added in v1.3.0

type ChunkReqStatus struct {
	From, To uint64
	Done     bool
	Pieces   chunkResponses
	Deadline string
	Target   string
}

type ChunkResponse added in v1.3.0

type ChunkResponse struct {
	From, To uint64
}

type Config

type Config struct {
	Single      bool // for test
	FileAddress string
	Chain       Chain
	Verifier    Verifier
}

type ExecutorStatus added in v1.3.0

type ExecutorStatus struct {
	Tasks []string
}

type Fetcher

type Fetcher interface {
	FetchSnapshotBlocks(start types.Hash, count uint64)

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

	// FetchAccountBlocksWithHeight, add snapshot height
	FetchAccountBlocksWithHeight(start types.Hash, count uint64, address *types.Address, sHeight uint64)
}

A Fetcher implementation can request the wanted blocks to peers

type File added in v1.3.0

type File = *ledger.CompressedFileMeta

type FileClientStatus added in v1.3.0

type FileClientStatus struct {
	FPool FilePoolStatus
	Queue []File
}

type FileConnStatus added in v1.3.0

type FileConnStatus struct {
	Id    string
	Addr  string
	Speed float64
}

type FilePoolStatus added in v1.3.0

type FilePoolStatus struct {
	Conns []FileConnStatus
}

type FileServerStatus added in v1.3.0

type FileServerStatus struct {
	Conns []string
}

type Files added in v1.3.0

type Files []File

func (Files) Len added in v1.3.0

func (f Files) Len() int

func (Files) Less added in v1.3.0

func (f Files) Less(i, j int) bool

func (Files) Swap added in v1.3.0

func (f Files) Swap(i, j int)

type Handler added in v1.3.0

type Handler func(msgId uint64, payload p2p.Serializable)

type InvalidHash added in v1.3.0

type InvalidHash struct {
	HashList []string `json:"hashList"`
}

type MockPeer added in v1.3.0

type MockPeer struct {
	Handlers map[ViteCmd]Handler
	// contains filtered or unexported fields
}

func NewMockPeer added in v1.3.0

func NewMockPeer() *MockPeer

func (*MockPeer) Disconnect added in v1.3.0

func (mp *MockPeer) Disconnect(reason p2p.DiscReason)

func (*MockPeer) FileAddress added in v1.3.0

func (mp *MockPeer) FileAddress() *net2.TCPAddr

func (*MockPeer) HasBlock added in v1.3.2

func (mp *MockPeer) HasBlock(hash types.Hash) bool

func (*MockPeer) Head added in v1.3.0

func (mp *MockPeer) Head() types.Hash

func (*MockPeer) Height added in v1.3.0

func (mp *MockPeer) Height() uint64

func (*MockPeer) ID added in v1.3.0

func (mp *MockPeer) ID() string

func (*MockPeer) Info added in v1.3.2

func (mp *MockPeer) Info() PeerInfo

func (*MockPeer) RemoteAddr added in v1.3.0

func (mp *MockPeer) RemoteAddr() *net2.TCPAddr

func (*MockPeer) Report added in v1.3.0

func (mp *MockPeer) Report(err error)

func (*MockPeer) SeeBlock added in v1.3.0

func (mp *MockPeer) SeeBlock(hash types.Hash)

func (*MockPeer) Send added in v1.3.0

func (mp *MockPeer) Send(code ViteCmd, msgId uint64, payload p2p.Serializable) (err error)

func (*MockPeer) SendAccountBlocks added in v1.3.0

func (mp *MockPeer) SendAccountBlocks(bs []*ledger.AccountBlock, msgId uint64) (err error)

func (*MockPeer) SendMsg added in v1.3.0

func (mp *MockPeer) SendMsg(msg *p2p.Msg) (err error)

func (*MockPeer) SendNewAccountBlock added in v1.3.0

func (mp *MockPeer) SendNewAccountBlock(b *ledger.AccountBlock) (err error)

func (*MockPeer) SendNewSnapshotBlock added in v1.3.0

func (mp *MockPeer) SendNewSnapshotBlock(b *ledger.SnapshotBlock) (err error)

func (*MockPeer) SendSnapshotBlocks added in v1.3.0

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

func (*MockPeer) SetHead added in v1.3.0

func (mp *MockPeer) SetHead(head types.Hash, height uint64)

type MsgHandler

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

type MsgIder

type MsgIder interface {
	MsgID() uint64
}

type Net

type Net interface {
	Syncer
	Fetcher
	Broadcaster
	BlockSubscriber
	Protocols() []*p2p.Protocol
	Start(svr p2p.Server) error
	Stop()
	Info() NodeInfo
	AddPlugin(plugin p2p.Plugin)
}

func New

func New(cfg *Config) Net

type NodeInfo

type NodeInfo struct {
	PeerCount int           `json:"peerCount"`
	Peers     []PeerInfo    `json:"peers"`
	Latency   []int64       `json:"latency"` // [0,1,12,24]
	Plugins   []interface{} `json:"plugins"`
}

type Peer

type Peer interface {
	RemoteAddr() *net2.TCPAddr
	FileAddress() *net2.TCPAddr
	SetHead(head types.Hash, height uint64)
	SeeBlock(hash types.Hash)
	SendSnapshotBlocks(bs []*ledger.SnapshotBlock, msgId uint64) (err error)
	SendAccountBlocks(bs []*ledger.AccountBlock, msgId uint64) (err error)
	SendNewSnapshotBlock(b *ledger.SnapshotBlock) (err error)
	SendNewAccountBlock(b *ledger.AccountBlock) (err error)
	Send(code ViteCmd, msgId uint64, payload p2p.Serializable) (err error)
	SendMsg(msg *p2p.Msg) (err error)
	Report(err error)
	ID() string
	Height() uint64
	Head() types.Hash
	Disconnect(reason p2p.DiscReason)
	Info() PeerInfo
	HasBlock(hash types.Hash) bool
}

Peer for protocol handle, not p2p Peer.

type PeerInfo

type PeerInfo struct {
	ID      string `json:"id"`
	Addr    string `json:"addr"`
	Head    string `json:"head"`
	Height  uint64 `json:"height"`
	Created string `json:"created"`
}

func (*PeerInfo) String

func (p *PeerInfo) String() string

type SnapshotBlockCallback

type SnapshotBlockCallback = func(block *ledger.SnapshotBlock, source types.BlockSource)

source is use to statistic where the block come from, broadcast, sync or fetch

type Subscriber

type Subscriber interface {
	BlockSubscriber
	SyncStateSubscriber
}

type SyncDetail added in v1.3.0

type SyncState

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

func (SyncState) String

func (s SyncState) String() string

type SyncStateCallback

type SyncStateCallback = func(SyncState)

type SyncStateSubscriber added in v1.3.0

type SyncStateSubscriber interface {
	// SubscribeSyncStatus return the subId, always larger than 0, use to unsubscribe
	SubscribeSyncStatus(fn SyncStateCallback) (subId int)
	// UnsubscribeSyncStatus, if subId is 0, then ignore
	UnsubscribeSyncStatus(subId int)

	// SyncState return the latest sync state
	SyncState() SyncState
}

type SyncStatus

type SyncStatus struct {
	From     uint64
	To       uint64
	Current  uint64
	Received uint64
	State    SyncState
}

type Syncer

type Syncer interface {
	SyncStateSubscriber
	Status() SyncStatus
	Detail() SyncDetail
}

A Syncer implementation can synchronise blocks to peers

type Task

type Task struct {
	Msg    string `json:"Msg"`
	Sender string `json:"Sender"`
}

type Verifier

type Verifier interface {
	VerifyNetSb(block *ledger.SnapshotBlock) error
	VerifyNetAb(block *ledger.AccountBlock) error
}

type ViteCmd

type ViteCmd p2p.Cmd
const (
	HandshakeCode ViteCmd = 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
	NewAccountBlockCode

	ExceptionCode = 127
)

func (ViteCmd) String

func (t ViteCmd) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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