vnode

package
v2.6.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2019 License: GPL-3.0 Imports: 11 Imported by: 14

Documentation

Index

Examples

Constants

View Source
const DefaultPort = 8483
View Source
const IDBits = idBytes * 8
View Source
const MaxHostLength = 1<<6 - 1
View Source
const PortLength = 2

Variables

This section is empty.

Functions

func Distance

func Distance(a, b NodeID) uint

Distance is bit-count minus the common bits, from left to right continuously, between a and b. eg:

a: 0000 1111
b: 0100 0011
Distance(a, b) == 8 - 1 // true

Types

type EndPoint

type EndPoint struct {
	Host []byte
	Port int
	Typ  HostType
}

EndPoint is the net address format `IP:Port` or `domain:Port`

func ParseEndPoint

func ParseEndPoint(host string) (e EndPoint, err error)

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

func (e *EndPoint) Deserialize(buf []byte) (err error)

Deserialize parse []byte to EndPoint, the memory EndPoint should be allocate before.

func (*EndPoint) Equal

func (e *EndPoint) Equal(e2 *EndPoint) bool

func (EndPoint) Hostname

func (e EndPoint) Hostname() string

Hostname return `domain` or `IPv4` or `[IPv6]`

func (EndPoint) Length

func (e EndPoint) Length() (n int)

Length return the serialized []byte length

func (EndPoint) MarshalJSON

func (e EndPoint) MarshalJSON() ([]byte, error)
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 (e EndPoint) MarshalText() (text []byte, err error)

func (EndPoint) Serialize

func (e EndPoint) Serialize() (buf []byte, err error)

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) String

func (e EndPoint) String() string

String return domain:port or IPv4:port or [IPv6]:port

func (*EndPoint) UnmarshalJSON

func (e *EndPoint) UnmarshalJSON(data []byte) error

func (*EndPoint) UnmarshalText

func (e *EndPoint) UnmarshalText(text []byte) (err error)

type HostType

type HostType byte
const (
	HostIPv4   HostType = 1
	HostIPv6   HostType = 2
	HostIP     HostType = 3
	HostDomain HostType = 4
)

func (HostType) Is

func (ht HostType) Is(ht2 HostType) bool
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 MockNode

func MockNode(domain bool, ext bool) *Node

func ParseNode

func ParseNode(u string) (n *Node, err error)

ParseNode parse a string to Node, return error if missing Hostname/IP.

func (Node) Address

func (n Node) Address() string

Address is formatted `domain:Port` or `IP:Port`

func (*Node) Deserialize

func (n *Node) Deserialize(data []byte) (err error)

Deserialize bytes to Node through protobuf, Node should be constructed before Deserialize.

// for example
var n = new(Node)
err := n.Deserialize(someBuf)

func (*Node) Equal

func (n *Node) Equal(n2 *Node) bool

func (*Node) Serialize

func (n *Node) Serialize() ([]byte, error)

Serialize a Node to bytes through protobuf

func (Node) String

func (n Node) String() (str 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

type NodeID [IDBits / 8]byte

NodeID use to mark node, and build a structural network

var ZERO NodeID

ZERO is the zero-value of NodeID type

func Bytes2NodeID

func Bytes2NodeID(buf []byte) (id NodeID, err error)

Bytes2NodeID turn a slice to NodeID

func Hex2NodeID

func Hex2NodeID(str string) (id NodeID, err error)

Hex2NodeID parse a hex coded string to NodeID

func RandFromDistance

func RandFromDistance(id NodeID, d uint) (rid NodeID)

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) Brief

func (id NodeID) Brief() string

Brief return the first 8 chars of id.String()

func (NodeID) Bytes

func (id NodeID) Bytes() []byte

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) IsZero

func (id NodeID) IsZero() bool

IsZero validate whether a NodeID is zero-value

func (NodeID) MarshalJSON

func (id NodeID) MarshalJSON() ([]byte, error)
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 (id NodeID) MarshalText() (text []byte, err error)

func (NodeID) String

func (id NodeID) String() string

String return a hex coded string of NodeID

func (*NodeID) UnmarshalJSON

func (id *NodeID) UnmarshalJSON(data []byte) error

func (*NodeID) UnmarshalText

func (id *NodeID) UnmarshalText(text []byte) (err error)

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

const (
	Edge NodeMode = 1 << iota
	Regular
	Relay
	Core
)

func (NodeMode) String

func (n NodeMode) String() string

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) Descriptor() ([]byte, []int)

func (*PEndPoint) GetHost

func (m *PEndPoint) GetHost() []byte

func (*PEndPoint) GetHostType

func (m *PEndPoint) GetHostType() int32

func (*PEndPoint) GetPort

func (m *PEndPoint) GetPort() int32

func (*PEndPoint) ProtoMessage

func (*PEndPoint) ProtoMessage()

func (*PEndPoint) Reset

func (m *PEndPoint) Reset()

func (*PEndPoint) String

func (m *PEndPoint) String() string

func (*PEndPoint) XXX_DiscardUnknown

func (m *PEndPoint) XXX_DiscardUnknown()

func (*PEndPoint) XXX_Marshal

func (m *PEndPoint) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*PEndPoint) XXX_Merge

func (m *PEndPoint) XXX_Merge(src proto.Message)

func (*PEndPoint) XXX_Size

func (m *PEndPoint) XXX_Size() int

func (*PEndPoint) XXX_Unmarshal

func (m *PEndPoint) XXX_Unmarshal(b []byte) error

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) Descriptor() ([]byte, []int)

func (*PNode) GetExt

func (m *PNode) GetExt() []byte

func (*PNode) GetHostType

func (m *PNode) GetHostType() uint32

func (*PNode) GetHostname

func (m *PNode) GetHostname() []byte

func (*PNode) GetId

func (m *PNode) GetId() []byte

func (*PNode) GetNet

func (m *PNode) GetNet() uint32

func (*PNode) GetPort

func (m *PNode) GetPort() uint32

func (*PNode) ProtoMessage

func (*PNode) ProtoMessage()

func (*PNode) Reset

func (m *PNode) Reset()

func (*PNode) String

func (m *PNode) String() string

func (*PNode) XXX_DiscardUnknown

func (m *PNode) XXX_DiscardUnknown()

func (*PNode) XXX_Marshal

func (m *PNode) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*PNode) XXX_Merge

func (m *PNode) XXX_Merge(src proto.Message)

func (*PNode) XXX_Size

func (m *PNode) XXX_Size() int

func (*PNode) XXX_Unmarshal

func (m *PNode) XXX_Unmarshal(b []byte) error

Jump to

Keyboard shortcuts

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