net

package
v2.6.3+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2019 License: GPL-3.0 Imports: 36 Imported by: 13

Documentation

Index

Examples

Constants

View Source
const DBDirName = "db"

Variables

View Source
var ZERO_ADDRESS types.Address

Functions

func Disconnect

func Disconnect(c MsgWriteCloser, err error) (e2 error)

func PutVarint

func PutVarint(buf []byte, n uint) (m byte)

func Varint

func Varint(buf []byte) (n uint)

Types

type AccountBlockCallback

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

AccountBlockCallback will be invoked when receive a block, source mark where the block come from: broadcast, sync or fetch

type AccountBlocks

type AccountBlocks struct {
	Blocks []*ledger.AccountBlock
	TTL    int32
}

AccountBlocks is batch of account blocks

func (*AccountBlocks) Deserialize

func (a *AccountBlocks) Deserialize(buf []byte) error

func (*AccountBlocks) Serialize

func (a *AccountBlocks) Serialize() ([]byte, error)

func (*AccountBlocks) String

func (a *AccountBlocks) String() string

type BlockSubscriber

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 chain 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 {
	// contains filtered or unexported methods
}

type Chunk

type Chunk struct {
	SnapshotChunks []ledger.SnapshotChunk
	SnapshotRange  [2]*ledger.HashHeight
	AccountRange   map[types.Address][2]*ledger.HashHeight
	HashMap        map[types.Hash]struct{}
	Source         types.BlockSource
	// contains filtered or unexported fields
}

Chunk means a chain chunk, contains snapshot blocks and dependent account blocks, SnapshotRange means the chunk range, the first HashHeight is the prevHash and prevHeight. eg. Chunk is from 101 to 200, SnapshotChunks is [101 ... 200], but SnapshotRange is [100, 200]. AccountRange like SnapshotRange but describe every account chain. HashMap record all blocks hash in Chunk, it can quickly tell if a block is in the chunk.

type ChunkReader

type ChunkReader interface {
	Peek() *Chunk
	Pop(endHash types.Hash)
}

type Chunks

type Chunks []*Chunk

func (Chunks) Len

func (cs Chunks) Len() int

func (Chunks) Less

func (cs Chunks) Less(i, j int) bool

func (Chunks) Swap

func (cs Chunks) Swap(i, j int)

type Code

type Code = byte
const (
	CodeDisconnect  Code = 1
	CodeHandshake   Code = 2
	CodeControlFlow Code = 3
	CodeHeartBeat   Code = 4

	CodeGetHashList       Code = 25
	CodeHashList          Code = 26
	CodeGetSnapshotBlocks Code = 27
	CodeSnapshotBlocks    Code = 28
	CodeGetAccountBlocks  Code = 29
	CodeAccountBlocks     Code = 30
	CodeNewSnapshotBlock  Code = 31
	CodeNewAccountBlock   Code = 32

	CodeSyncHandshake   Code = 60
	CodeSyncHandshakeOK Code = 61
	CodeSyncRequest     Code = 62
	CodeSyncReady       Code = 63

	CodeException Code = 127
	CodeTrace     Code = 128
)

type Codec

type Codec interface {
	MsgReadWriter
	Close() error
	SetReadTimeout(timeout time.Duration)
	SetWriteTimeout(timeout time.Duration)
	SetTimeout(timeout time.Duration)
	Address() _net.Addr
}

Codec is an transport can encode messages to bytes, transmit bytes, then decode bytes to messages

func MockPipe

func MockPipe() (c1, c2 Codec)

func NewTransport

func NewTransport(conn _net.Conn, minCompressLength int, readTimeout, writeTimeout time.Duration) Codec

type CodecFactory

type CodecFactory interface {
	CreateCodec(conn _net.Conn) Codec
}

type Connector

type Connector interface {
	ConnectNode(node *vnode.Node) error
}

type Consensus

type Consensus interface {
	SubscribeProducers(gid types.Gid, id string, fn func(event consensus.ProducersEvent))
	UnSubscribe(gid types.Gid, id string)
	API() consensus.APIReader
}

type DownloaderStatus

type DownloaderStatus struct {
	Tasks       []string               `json:"tasks"`
	Connections []SyncConnectionStatus `json:"connections"`
}

type Exception

type Exception byte
const (
	ExpMissing     Exception = iota // I don`t have the resource you requested
	ExpUnsolicited                  // the request must have pre-checked
	ExpUnauthorized
	ExpServerError
	ExpChunkNotMatch
	ExpOther
)

func (*Exception) Deserialize

func (exp *Exception) Deserialize(buf []byte) error

func (Exception) Error

func (exp Exception) Error() string

func (Exception) Serialize

func (exp Exception) Serialize() ([]byte, error)

func (Exception) String

func (exp Exception) String() string

type Fetcher

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

	FetchSnapshotBlocksWithHeight(hash types.Hash, height uint64, 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 FilePoolStatus

type FilePoolStatus struct {
	Connections []SyncConnectionStatus `json:"connections"`
}

type FileServerStatus

type FileServerStatus struct {
	Connections []SyncConnectionStatus `json:"connections"`
}

type GetAccountBlocks

type GetAccountBlocks struct {
	Address types.Address
	From    ledger.HashHeight
	Count   uint64
	Forward bool
}

func (*GetAccountBlocks) Deserialize

func (b *GetAccountBlocks) Deserialize(buf []byte) error

func (*GetAccountBlocks) Serialize

func (b *GetAccountBlocks) Serialize() ([]byte, error)

func (*GetAccountBlocks) String

func (b *GetAccountBlocks) String() string

type GetHashHeightList

type GetHashHeightList struct {
	From []*ledger.HashHeight // from high to low
	Step uint64
	To   uint64
}

func (*GetHashHeightList) Deserialize

func (c *GetHashHeightList) Deserialize(data []byte) (err error)

func (*GetHashHeightList) Serialize

func (c *GetHashHeightList) Serialize() ([]byte, error)

type GetSnapshotBlocks

type GetSnapshotBlocks struct {
	From    ledger.HashHeight
	Count   uint64
	Forward bool
}

func (*GetSnapshotBlocks) Deserialize

func (b *GetSnapshotBlocks) Deserialize(buf []byte) error

func (*GetSnapshotBlocks) Serialize

func (b *GetSnapshotBlocks) Serialize() ([]byte, error)

func (*GetSnapshotBlocks) String

func (b *GetSnapshotBlocks) String() string

type HandshakeMsg

type HandshakeMsg struct {
	Version int64

	NetID int64

	Name string

	ID vnode.NodeID

	Timestamp int64

	Height  uint64
	Head    types.Hash
	Genesis types.Hash

	Key   ed25519.PublicKey // is producer
	Token []byte

	FileAddress   []byte
	PublicAddress []byte
}

func (*HandshakeMsg) Deserialize

func (b *HandshakeMsg) Deserialize(data []byte) (err error)

func (*HandshakeMsg) Serialize

func (b *HandshakeMsg) Serialize() (data []byte, err error)

type HashHeightPoint

type HashHeightPoint struct {
	ledger.HashHeight
	Size uint64
}

func (*HashHeightPoint) DeProto

func (p *HashHeightPoint) DeProto(pb *vitepb.HashHeightPoint) (err error)

func (*HashHeightPoint) Proto

type HashHeightPointList

type HashHeightPointList struct {
	Points []*HashHeightPoint // from low to high
}

func (*HashHeightPointList) Deserialize

func (c *HashHeightPointList) Deserialize(data []byte) (err error)

func (*HashHeightPointList) Serialize

func (c *HashHeightPointList) Serialize() ([]byte, error)

type IrreversibleReader

type IrreversibleReader interface {
	GetIrreversibleBlock() *ledger.SnapshotBlock
}

type MockCodec

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

func (*MockCodec) Address

func (m *MockCodec) Address() _net.Addr

func (*MockCodec) Close

func (m *MockCodec) Close() error

func (*MockCodec) ReadMsg

func (m *MockCodec) ReadMsg() (msg Msg, err error)

func (*MockCodec) SetReadTimeout

func (m *MockCodec) SetReadTimeout(timeout time.Duration)

func (*MockCodec) SetTimeout

func (m *MockCodec) SetTimeout(timeout time.Duration)

func (*MockCodec) SetWriteTimeout

func (m *MockCodec) SetWriteTimeout(timeout time.Duration)

func (*MockCodec) WriteMsg

func (m *MockCodec) WriteMsg(msg Msg) (err error)

type Msg

type Msg struct {
	Code       Code
	Id         uint32
	Payload    []byte
	ReceivedAt int64
	Sender     *Peer
}

func (Msg) Recycle

func (m Msg) Recycle()

Recycle will put Msg.Payload back to pool

type MsgId

type MsgId = uint32

type MsgIder

type MsgIder interface {
	MsgID() MsgId
}

type MsgReadWriter

type MsgReadWriter interface {
	MsgReader
	MsgWriter
}

type MsgReader

type MsgReader interface {
	ReadMsg() (Msg, error)
}

type MsgWriteCloser

type MsgWriteCloser interface {
	MsgWriter
	io.Closer
}

type MsgWriter

type MsgWriter interface {
	WriteMsg(Msg) error
}

type Net

type Net interface {
	Syncer
	Fetcher
	Broadcaster
	BlockSubscriber
	Start() error
	Stop() error
	Info() NodeInfo
	Nodes() []*vnode.Node
	PeerCount() int
	PeerKey() ed25519.PrivateKey
}

func New

func New(cfg *config.Net, chain Chain, verifier Verifier, consensus Consensus, irreader IrreversibleReader) (Net, error)

type NewAccountBlock

type NewAccountBlock struct {
	Block *ledger.AccountBlock
	TTL   int32
}

NewAccountBlock is use to propagate block, stop propagate when TTL is decrease to zero

func (*NewAccountBlock) Deserialize

func (b *NewAccountBlock) Deserialize(buf []byte) error

func (*NewAccountBlock) Serialize

func (b *NewAccountBlock) Serialize() ([]byte, error)

type NewSnapshotBlock

type NewSnapshotBlock struct {
	Block *ledger.SnapshotBlock
	TTL   int32
}

NewSnapshotBlock is use to propagate block, stop propagate when TTL is decrease to zero

func (*NewSnapshotBlock) Deserialize

func (b *NewSnapshotBlock) Deserialize(buf []byte) error

func (*NewSnapshotBlock) Serialize

func (b *NewSnapshotBlock) Serialize() ([]byte, error)

type NodeInfo

type NodeInfo struct {
	ID                    vnode.NodeID     `json:"id"`
	Name                  string           `json:"name"`
	NetID                 int              `json:"netId"`
	Version               int              `json:"version"`
	Address               string           `json:"address"`
	PeerCount             int              `json:"peerCount"`
	Peers                 []PeerInfo       `json:"peers"`
	Height                uint64           `json:"height"`
	Nodes                 int              `json:"nodes"`
	Latency               []int64          `json:"latency"` // [0,1,12,24]
	BroadCheckFailedRatio float32          `json:"broadCheckFailedRatio"`
	Server                FileServerStatus `json:"server"`
}

type Peer

type Peer struct {
	Id      peerId
	Name    string
	Height  uint64
	Head    types.Hash
	Version int64

	CreateAt int64

	Flag     PeerFlag
	Superior bool
	// contains filtered or unexported fields
}

func (*Peer) Close

func (p *Peer) Close(err error) (err2 error)

func (*Peer) Disconnect

func (p *Peer) Disconnect(err error)

func (*Peer) Info

func (p *Peer) Info() PeerInfo

func (*Peer) SetState

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

func (*Peer) SetSuperior

func (p *Peer) SetSuperior(superior bool) error

func (*Peer) String

func (p *Peer) String() string

func (*Peer) WriteMsg

func (p *Peer) WriteMsg(msg Msg) (err error)

WriteMsg will put msg into queue, then write asynchronously

type PeerError

type PeerError byte
const (
	PeerNetworkError PeerError = iota // read/write timeout, read/write error
	PeerDifferentNetwork
	PeerTooManyPeers
	PeerTooManySameNetPeers
	PeerTooManyInboundPeers
	PeerAlreadyConnected
	PeerIncompatibleVersion
	PeerQuitting
	PeerNotHandshakeMsg
	PeerInvalidSignature
	PeerConnectSelf
	PeerUnknownMessage
	PeerUnmarshalError
	PeerNoPermission
	PeerBanned
	PeerDifferentGenesis
	PeerInvalidBlock
	PeerInvalidMessage
	PeerResponseTimeout
	PeerInvalidToken
	PeerUnknownReason PeerError = 255
)

func (PeerError) Error

func (e PeerError) Error() string

func (PeerError) Serialize

func (e PeerError) Serialize() ([]byte, error)

func (PeerError) String

func (e PeerError) String() string

type PeerFlag

type PeerFlag byte
const (
	PeerFlagInbound  PeerFlag = 0
	PeerFlagOutbound PeerFlag = 1
	PeerFlagStatic   PeerFlag = 1 << 1
)

type PeerInfo

type PeerInfo struct {
	Id         string   `json:"id"`
	Name       string   `json:"name"`
	Version    int64    `json:"version"`
	Height     uint64   `json:"height"`
	Address    string   `json:"address"`
	Flag       PeerFlag `json:"flag"`
	Superior   bool     `json:"superior"`
	Reliable   bool     `json:"reliable"`
	CreateAt   string   `json:"createAt"`
	ReadQueue  int      `json:"readQueue"`
	WriteQueue int      `json:"writeQueue"`
	Peers      []string `json:"peers"`
}

PeerInfo is for api

type PeerManager

type PeerManager interface {
	UpdatePeer(p *Peer, newSuperior bool)
}

type Serializable

type Serializable interface {
	Serialize() ([]byte, error)
}

type SnapshotBlockCallback

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

SnapshotBlockCallback will be invoked when receive a block, source mark where the block come from: broadcast, sync or fetch

type SnapshotBlocks

type SnapshotBlocks struct {
	Blocks []*ledger.SnapshotBlock
}

SnapshotBlocks is batch of snapshot blocks

func (*SnapshotBlocks) Deserialize

func (b *SnapshotBlocks) Deserialize(buf []byte) error

func (*SnapshotBlocks) Serialize

func (b *SnapshotBlocks) Serialize() ([]byte, error)

func (*SnapshotBlocks) String

func (b *SnapshotBlocks) String() string

type Subscriber

type Subscriber interface {
	BlockSubscriber
	SyncStateSubscriber
}

type SyncConnectionStatus

type SyncConnectionStatus struct {
	Address string `json:"address"`
	Speed   string `json:"speed"`
	Task    string `json:"task"`
}

type SyncDetail

type SyncDetail struct {
	SyncStatus
	DownloaderStatus
	Chunks [][2]*ledger.HashHeight `json:"chunks"`
	Caches interfaces.SegmentList  `json:"caches"`
}

type SyncState

type SyncState byte
const (
	SyncInit SyncState = iota
	Syncing
	SyncDone
	SyncError
	SyncCancel
)

func (SyncState) MarshalText

func (s SyncState) MarshalText() (text []byte, err error)
Example
var s SyncState

data, err := s.MarshalText()
if err != nil {
	panic(err)
}

fmt.Printf("%s\n", data)
Output:

Sync Not Start

func (SyncState) String

func (s SyncState) String() string

func (*SyncState) UnmarshalText

func (s *SyncState) UnmarshalText(text []byte) error

type SyncStateCallback

type SyncStateCallback = func(SyncState)

SyncStateCallback will be invoked when sync state change, the param is state after change

type SyncStateSubscriber

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_bak
	SyncState() SyncState
}

type SyncStatus

type SyncStatus struct {
	From    uint64    `json:"from"`
	To      uint64    `json:"to"`
	Current uint64    `json:"current"`
	State   SyncState `json:"state"`
	Status  string    `json:"status"`
}

type Syncer

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

A Syncer implementation can synchronise blocks to peers

type Verifier

type Verifier interface {
	VerifyNetSnapshotBlock(block *ledger.SnapshotBlock) error
	VerifyNetAccountBlock(block *ledger.AccountBlock) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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