Documentation ¶
Index ¶
- Constants
- func Distance(a, b NodeID) uint
- type EndPoint
- func (e *EndPoint) Deserialize(buf []byte) (err error)
- func (e *EndPoint) Equal(e2 *EndPoint) bool
- func (e EndPoint) Hostname() string
- func (e EndPoint) Length() (n int)
- func (e EndPoint) MarshalJSON() ([]byte, error)
- func (e EndPoint) MarshalText() (text []byte, err error)
- func (e EndPoint) Serialize() (buf []byte, err error)
- func (e EndPoint) String() string
- func (e *EndPoint) UnmarshalJSON(data []byte) error
- func (e *EndPoint) UnmarshalText(text []byte) (err error)
- type HostType
- type Node
- type NodeID
- func (id NodeID) Brief() string
- func (id NodeID) Bytes() []byte
- func (id NodeID) IsZero() bool
- func (id NodeID) MarshalJSON() ([]byte, error)
- func (id NodeID) MarshalText() (text []byte, err error)
- func (id NodeID) String() string
- func (id *NodeID) UnmarshalJSON(data []byte) error
- func (id *NodeID) UnmarshalText(text []byte) (err error)
- type NodeMode
- type PEndPoint
- func (*PEndPoint) Descriptor() ([]byte, []int)
- func (m *PEndPoint) GetHost() []byte
- func (m *PEndPoint) GetHostType() int32
- func (m *PEndPoint) GetPort() int32
- func (*PEndPoint) ProtoMessage()
- func (m *PEndPoint) Reset()
- func (m *PEndPoint) String() string
- func (m *PEndPoint) XXX_DiscardUnknown()
- func (m *PEndPoint) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *PEndPoint) XXX_Merge(src proto.Message)
- func (m *PEndPoint) XXX_Size() int
- func (m *PEndPoint) XXX_Unmarshal(b []byte) error
- type PNode
- func (*PNode) Descriptor() ([]byte, []int)
- func (m *PNode) GetExt() []byte
- func (m *PNode) GetHostType() uint32
- func (m *PNode) GetHostname() []byte
- func (m *PNode) GetId() []byte
- func (m *PNode) GetNet() uint32
- func (m *PNode) GetPort() uint32
- func (*PNode) ProtoMessage()
- func (m *PNode) Reset()
- func (m *PNode) String() string
- func (m *PNode) XXX_DiscardUnknown()
- func (m *PNode) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
- func (m *PNode) XXX_Merge(src proto.Message)
- func (m *PNode) XXX_Size() int
- func (m *PNode) XXX_Unmarshal(b []byte) error
Examples ¶
Constants ¶
const DefaultPort = 8483
const IDBits = idBytes * 8
const MaxHostLength = 1<<6 - 1
const PortLength = 2
Variables ¶
This section is empty.
Functions ¶
Types ¶
type EndPoint ¶
EndPoint is the net address format `IP:Port` or `domain:Port`
func ParseEndPoint ¶
ParseEndPoint parse a string to EndPoint host MUST format one of the following styles: 1. [IP]:port 2. [IP] 3. hostname:port 4. hostname 5. IPv4:port 6. IPv4
func (*EndPoint) Deserialize ¶
Deserialize parse []byte to EndPoint, the memory EndPoint should be allocate before.
func (EndPoint) MarshalJSON ¶
Example ¶
var addr = "127.0.0.1:8080" ep, err := ParseEndPoint(addr) if err != nil { panic(err) } data, err := ep.MarshalJSON() if err != nil { panic(err) } fmt.Printf("%s\n", data)
Output: "127.0.0.1:8080"
func (EndPoint) MarshalText ¶
func (EndPoint) Serialize ¶
Serialize not use ProtoBuffers, because we should ensure the neighbors message is short than 1200 bytes but PB is variable-length-encode, the length of encoded []byte is unknown before encode.
EndPoint serialize structure.
+----------+----------------------+-------------+ | Meta | Host | Port(opt) | | 1 byte | 0 ~ 63 bytes | 2 bytes | +----------+----------------------+-------------+
Meta structure
+---------------------+--------+--------+ | Host Length | Host | Port | | 6 bits | 1 bit | 1 bit | +---------------------+--------+--------+ Host Length is the byte-count of Host Host: 0 IP. 1 Domain Port: 0 no IP, mean DefaultPort. 1 has 2 bytes Port
func (*EndPoint) UnmarshalJSON ¶
func (*EndPoint) UnmarshalText ¶
type HostType ¶
type HostType byte
func (HostType) Is ¶
Example ¶
fmt.Println(HostIPv4.Is(HostIPv4)) fmt.Println(HostIPv6.Is(HostIPv6)) fmt.Println(HostIP.Is(HostIP)) fmt.Println(HostDomain.Is(HostDomain)) fmt.Println(HostIPv4.Is(HostIP)) fmt.Println(HostIPv6.Is(HostIP)) fmt.Println(HostIPv4.Is(HostIPv6)) fmt.Println(HostIPv6.Is(HostIPv4)) fmt.Println(HostIPv4.Is(HostDomain)) fmt.Println(HostIPv6.Is(HostDomain)) fmt.Println(HostDomain.Is(HostIP)) fmt.Println(HostIP.Is(HostDomain))
Output: true true true true true true false false false false false false
type Node ¶
type Node struct { ID NodeID `json:"id"` // ID is the unique node identity EndPoint EndPoint `json:"address"` Net int `json:"net"` // Net is the network this node belongs Ext []byte `json:"ext"` // Ext can be arbitrary data, will be sent to other nodes }
Node mean a node in vite P2P network
func (*Node) Deserialize ¶
Deserialize bytes to Node through protobuf, Node should be constructed before Deserialize.
// for example var n = new(Node) err := n.Deserialize(someBuf)
func (Node) String ¶
String marshal node to string, domain or IP is mandatory, other fields are optional. looks like:
<hex_node_id>@domain:port/net <hex_node_id>@IPv4:port/net <hex_node_id>@[IPv6]:port/net
missing fields will parse to default value.
NodeID default is Zero port default is `DefaultPort` net default is 0
the field `Ext` will not be included in the encoded string
type NodeID ¶
NodeID use to mark node, and build a structural network
var ZERO NodeID
ZERO is the zero-value of NodeID type
func Bytes2NodeID ¶
Bytes2NodeID turn a slice to NodeID
func Hex2NodeID ¶
Hex2NodeID parse a hex coded string to NodeID
func RandFromDistance ¶
RandFromDistance will generate a random NodeID which satisfy `Distance(id, rid) == d`
func RandomNodeID ¶
func RandomNodeID() (id NodeID)
RandomNodeID return a random NodeID, easy to test
func (NodeID) Bytes ¶
Bytes return bytes slice copy of the origin NodeID. So modify the result cannot effect the origin NodeID.
Example ¶
var id = RandomNodeID() var id2 = id copy(id.Bytes(), ZERO.Bytes()) fmt.Println(id == ZERO) id.Bytes()[0] = 0 fmt.Println(id == id2)
Output: false true
func (NodeID) MarshalJSON ¶
Example ¶
var hex = "864c763b198f7234e90e25c935c77f84866def8590afec4af1545ca2e45ca926" id, err := Hex2NodeID(hex) if err != nil { panic(err) } data, err := id.MarshalJSON() if err != nil { panic(err) } fmt.Printf("%s\n", data)
Output: "864c763b198f7234e90e25c935c77f84866def8590afec4af1545ca2e45ca926"
func (NodeID) MarshalText ¶
func (*NodeID) UnmarshalJSON ¶
func (*NodeID) UnmarshalText ¶
type NodeMode ¶
type NodeMode byte
NodeMode mean the level of a node in the current hierarchy Core nodes works on the highest level, usually are producers Relay nodes usually are the standby producers, and partial full nodes (like static nodes) Regular nodes usually are the full nodes Edge nodes usually are the light nodes
type PEndPoint ¶
type PEndPoint struct { Host []byte `protobuf:"bytes,1,opt,name=host,proto3" json:"host,omitempty"` Port int32 `protobuf:"varint,2,opt,name=port,proto3" json:"port,omitempty"` HostType int32 `protobuf:"varint,3,opt,name=hostType,proto3" json:"hostType,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` }
func (*PEndPoint) Descriptor ¶
func (*PEndPoint) GetHostType ¶
func (*PEndPoint) ProtoMessage ¶
func (*PEndPoint) ProtoMessage()
func (*PEndPoint) XXX_DiscardUnknown ¶
func (m *PEndPoint) XXX_DiscardUnknown()
func (*PEndPoint) XXX_Marshal ¶
func (*PEndPoint) XXX_Unmarshal ¶
type PNode ¶
type PNode struct { Id []byte `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` Hostname []byte `protobuf:"bytes,2,opt,name=hostname,proto3" json:"hostname,omitempty"` HostType uint32 `protobuf:"varint,3,opt,name=hostType,proto3" json:"hostType,omitempty"` Port uint32 `protobuf:"varint,4,opt,name=port,proto3" json:"port,omitempty"` Net uint32 `protobuf:"varint,5,opt,name=net,proto3" json:"net,omitempty"` Ext []byte `protobuf:"bytes,6,opt,name=ext,proto3" json:"ext,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` }
func (*PNode) Descriptor ¶
func (*PNode) GetHostType ¶
func (*PNode) GetHostname ¶
func (*PNode) ProtoMessage ¶
func (*PNode) ProtoMessage()
func (*PNode) XXX_DiscardUnknown ¶
func (m *PNode) XXX_DiscardUnknown()