vntp2p

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2019 License: GPL-3.0 Imports: 39 Imported by: 0

README

Introduction

P.S.

  • local update: git pull vnt master:vnt

  • local push: git push vnt vnt:master

log.Info("[info] setBootstrapNodes()", "urls", urls, "and tests", tests, "and url length", len(urls))
  • libp2p database tips:

    • when libp2p store key-value, it will make key to hash of key and the key + value + timestamp will be value.

    • when libp2p store key-value, the key should start with \.

develop tips.

  • when get the url of bootnodes,we need change it to node,the code is at cmd/utils/flags.go#L610
url = "vnode://123b13dfdb555c69290acf510f2e1c00b9d31a917d8c9e7cf8216812da10caffd6e242879bbae43aa09c3bd2ef49c45999bb0c502d38d8c8f9d60287f2220c0c@127.0.0.1:30301"

node, err := discover.ParseNode(url)

VNT starts

bootnode start

$ cd bootnode-dir
$ bootnode -genkey=node.key
$ bootnode -nodekey=node.key

the database is :bootnode -datadir="./" -nodekey=node.key

membernode starts

ATTENTION:

  • datadir can't be too long。
  • vntbootnode is vnt's``bootnodeURL
  • port is vntnode's port
$ gvnt --datadir=./datadir1 --vntbootnode=/ip4/127.0.0.1/tcp/30301/ipfs/QmW1zhpCHrfoyXjWRkJMaTgtfy7BiqhZfHajgK3Xnysoxx --port 30306

producers node starts

$ gvnt account new --datadir ./datadir2

$ gvnt --datadir=./datadir2 --vntbootnode=/ip4/127.0.0.1/tcp/30301/ipfs/QmW1zhpCHrfoyXjWRkJMaTgtfy7BiqhZfHajgK3Xnysoxx --produce --producerthreads=1 --coinbase=0xf6f5038a406a7fe78229a80850ca8ed42fe03bfd --port 30307

now,vntdb is support --datadir tag

whisper of VNT

NO.1 gvnt start

$ gvnt --datadir dir1

NO.1 wnode start

$ wnode -topic=70a4beef -verbosity=4 -ip=:30304 
Please enter the peer's vnode: /ip4/127.0.0.1/tcp/30303/ipfs/1kHJWBz9NHQdMZt1ZmdFwknvNezNQYhcAHq6Fx3nDkNbDf6

NO.2 wnode start

$ wnode -topic=70a4beef -verbosity=4 -ip=:30305 
Please enter the peer's vnode: /ip4/127.0.0.1/tcp/30303/ipfs/1kHJWBz9NHQdMZt1ZmdFwknvNezNQYhcAHq6Fx3nDkNbDf6

bzz of VNT

create a vnt account in datadir2

$ gvnt --datadir dir2 account new

start a gvnt node in datadir1

$ gvnt --datadir dir1

start swarm node:

$ swarm --bzzaccount bea9faa39f67da4580c09a65af9521048a52b8f9 --datadir=dir2 --swap-api=dir1/gvnt.ipc --nodiscover

get 127.0.0.1:8500 in browser

upload file in swarm

$ swarm up genesis.json
6a5694e49f29ecb8c53f3392a1ada8c6a2838e5d9372e24816afd20bc51725fd

input 6a5694e49f29ecb8c53f3392a1ada8c6a2838e5d9372e24816afd20bc51725fd to find file

Documentation

Index

Constants

View Source
const BootnodeCon = 1
View Source
const MessageHeaderLength = 5

MessageHeaderLength define message header length

View Source
const NodeIDBits = 512

包内都用peerID,对外方法使用NodeID

View Source
const (
	// PID vnt protocol basic id
	PID = "/p2p/1.0.0"
)

Variables

This section is empty.

Functions

func ConstructDHT

func ConstructDHT(ctx context.Context, listenstring string, nodekey *ecdsa.PrivateKey, datadir string, restrictList []*net.IPNet, natm libp2p.Option) (*dht.IpfsDHT, p2phost.Host, error)

ConstructDHT create Kademlia DHT

func ExpectMsg

func ExpectMsg(r MsgReader, code MessageType, content interface{}) error

ExpectMsg COMMENT: this function is just for _test.go files ExpectMsg reads a message from r and verifies that its code and encoded RLP content match the provided values. If content is nil, the payload is discarded and not verified.

func GetAddr

func GetAddr(target string) (ma.Multiaddr, peer.ID, error)

func GetIPfromAddr

func GetIPfromAddr(a ma.Multiaddr) string

func MakePort

func MakePort(port string) string

func NATParse

func NATParse(spec string) (libp2p.Option, error)

func ParseNetlist

func ParseNetlist(s string) ([]*net.IPNet, error)

func PubkeyID

func PubkeyID(pub *ecdsa.PublicKey) libp2p.ID

func Send

func Send(w MsgWriter, protocolID string, msgType MessageType, data interface{}) error

Send is used to send message payload with specific messge type

func SendItems

func SendItems(w MsgWriter, protocolID string, msgType MessageType, elems ...interface{}) error

SendItems can send many payload in one function call

func WritePackage

func WritePackage(rw *bufio.ReadWriter, code uint64, data []byte) ([]byte, error)

临时测试使用

Types

type Config

type Config struct {
	PrivateKey      *ecdsa.PrivateKey `toml:"-"`
	MaxPeers        int
	MaxPendingPeers int `toml:",omitempty"`
	DialRatio       int `toml:",omitempty"`
	NoDiscovery     bool
	Name            string `toml:"-"`

	BootstrapNodes []*Node
	StaticNodes    []*Node
	TrustedNodes   []*Node

	NetRestrict  []*net.IPNet `toml:",omitempty"`
	NodeDatabase string       `toml:",omitempty"`
	Protocols    []Protocol   `toml:"-"`
	ListenAddr   string
	NAT          libp2p.Option `toml:"-"`

	EnableMsgEvents bool
	Logger          log.Logger `toml:",omitempty"`
}

type DhtTable

type DhtTable interface {
	Start(ctx context.Context) error
	Lookup(ctx context.Context, targetID NodeID) []*NodeID
	Update(ctx context.Context, id peer.ID) error
	RandomPeer() []peer.ID
	GetDhtTable() *dht.IpfsDHT
}

type DiscReason

type DiscReason uint
const (
	DiscRequested DiscReason = iota
	DiscNetworkError
	DiscProtocolError
	DiscUselessPeer
	DiscTooManyPeers
	DiscAlreadyConnected
	DiscIncompatibleVersion
	DiscInvalidIdentity
	DiscQuitting
	DiscUnexpectedIdentity
	DiscSelf
	DiscReadTimeout
	DiscSubprotocolError = 0x10
)

func (DiscReason) Error

func (d DiscReason) Error() string

func (DiscReason) String

func (d DiscReason) String() string

type GoodMorningMsg

type GoodMorningMsg struct {
	Greet     string
	Timestamp string
}

GoodMorningMsg message for goodmorning protocol

func (*GoodMorningMsg) HandleMessage

func (gmm *GoodMorningMsg) HandleMessage() error

HandleMessage implement VNTMessage interface

type LevelDB

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

LevelDB vntdb object

func GetDatastore

func GetDatastore(path string) (*LevelDB, error)

GetDatastore singleton design pattern

func (*LevelDB) Batch

func (d *LevelDB) Batch() (ds.Batch, error)

Batch implement Batch() of ds.Batching interface

func (*LevelDB) Close

func (d *LevelDB) Close() error

Close implement Close() of ds.Batching interface

func (*LevelDB) Delete

func (d *LevelDB) Delete(key ds.Key) (err error)

Delete implement Delete() of ds.Batching interface

func (*LevelDB) Get

func (d *LevelDB) Get(key ds.Key) (value interface{}, err error)

Get implement Get() of ds.Batching interface

func (*LevelDB) Has

func (d *LevelDB) Has(key ds.Key) (exists bool, err error)

Has implement Has() of ds.Batching interface

func (*LevelDB) Put

func (d *LevelDB) Put(key ds.Key, value interface{}) (err error)

Put implement Put() of ds.Batching interface

func (*LevelDB) Query

func (d *LevelDB) Query(q query.Query) (query.Results, error)

Query implement Query() of ds.Batching interface

type MessageType

type MessageType uint64

MessageType define vnt p2p protocol message type

const (
	// GoodMorning say good morning protocol
	GoodMorning MessageType = iota
	// GoodAfternoon say good afternoon protocol
	GoodAfternoon
	// GoodNight say good night protocol
	GoodNight
)

type Msg

type Msg struct {
	Header MsgHeader
	Body   MsgBody
}

Msg message struct

func (Msg) Decode

func (msg Msg) Decode(val interface{}) error

Decode using json unmarshal decode msg payload

func (*Msg) GetBodySize

func (msg *Msg) GetBodySize() uint32

GetBodySize get message body size in uint32

type MsgBody

type MsgBody struct {
	ProtocolID  string //Protocol name
	Type        MessageType
	ReceivedAt  time.Time
	PayloadSize uint32
	Payload     io.Reader
}

MsgBody message body

type MsgHeader

type MsgHeader [MessageHeaderLength]byte

MsgHeader store the size of MsgBody

type MsgReadWriter

type MsgReadWriter interface {
	MsgReader
	MsgWriter
}

type MsgReader

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

type MsgWriter

type MsgWriter interface {
	WriteMsg(Msg) error
}

type Node

type Node struct {
	Addr ma.Multiaddr
	Id   peer.ID
}

func MustParseNode

func MustParseNode(rawurl string) *Node

func NewNode

func NewNode(id peer.ID, ip net.IP, udpPort, tcpPort uint16) *Node

func ParseNode

func ParseNode(url string) (*Node, error)

func (*Node) MarshalText

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

for toml marshal

func (*Node) String

func (n *Node) String() string

func (*Node) UnmarshalText

func (n *Node) UnmarshalText(data []byte) error

for toml unmarshal

type NodeID

type NodeID [NodeIDBits / 8]byte

func HexID

func HexID(in string) (NodeID, error)

func PeerIDtoNodeID

func PeerIDtoNodeID(n peer.ID) NodeID

func (NodeID) Bytes

func (n NodeID) Bytes() []byte

func (NodeID) GoString

func (n NodeID) GoString() string

The Go syntax representation of a NodeID is a call to HexID.

func (NodeID) MarshalText

func (n NodeID) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (NodeID) PeerID

func (n NodeID) PeerID() peer.ID

func (NodeID) Pubkey

func (n NodeID) Pubkey() (*ecdsa.PublicKey, error)

func (NodeID) String

func (n NodeID) String() string

NodeID prints as a long hexadecimal number.

func (NodeID) TerminalString

func (n NodeID) TerminalString() string

TerminalString returns a shortened hex string for terminal logging.

func (*NodeID) UnmarshalText

func (n *NodeID) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

type NodeInfo

type NodeInfo struct {
	ID      string `json:"id"`    // Unique node identifier (also the encryption key)
	Name    string `json:"name"`  // Name of the node, including client type, version, OS, custom data
	VNTNode string `json:"vnode"` // Vnode URL for adding this peer from remote peers
	IP      string `json:"ip"`    // IP address of the node
	Ports   struct {
		Discovery int `json:"discovery"` // UDP listening port for discovery protocol
		Listener  int `json:"listener"`  // TCP listening port for RLPx
	} `json:"ports"`
	ListenAddr string                 `json:"listenAddr"`
	Protocols  map[string]interface{} `json:"protocols"`
}

type Peer

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

func (*Peer) Disconnect

func (p *Peer) Disconnect(reason DiscReason)

func (*Peer) Drop added in v0.6.2

func (p *Peer) Drop()

Drop this peer forever because of protocol mismatch

func (*Peer) Info

func (p *Peer) Info() *PeerInfo

func (*Peer) LocalAddr

func (p *Peer) LocalAddr() net.Addr

func (*Peer) LocalID

func (p *Peer) LocalID() libp2p.ID

LocalID return local PeerID for upper application

func (*Peer) Log

func (p *Peer) Log() log.Logger

func (*Peer) RemoteAddr

func (p *Peer) RemoteAddr() net.Addr

func (*Peer) RemoteID

func (p *Peer) RemoteID() libp2p.ID

RemoteID return remote PeerID for upper application

func (*Peer) Reset added in v0.6.2

func (p *Peer) Reset()

Reset Close both direction. Use this to tell the remote side to hang up and go away. But only reset once.

type PeerEvent

type PeerEvent struct {
	Type     PeerEventType `json:"type"`
	Peer     libp2p.ID     `json:"peer"`
	Error    string        `json:"error,omitempty"`
	Protocol string        `json:"protocol,omitempty"`
	MsgCode  *uint64       `json:"msg_code,omitempty"`
	MsgSize  *uint32       `json:"msg_size,omitempty"`
}

PeerEvent is an event emitted when peers are either added or dropped from a p2p.Server or when a message is sent or received on a peer connection

type PeerEventType

type PeerEventType string
const (
	// PeerEventTypeAdd is the type of event emitted when a peer is added
	// to a p2p.Server
	PeerEventTypeAdd PeerEventType = "add"

	// PeerEventTypeDrop is the type of event emitted when a peer is
	// dropped from a p2p.Server
	PeerEventTypeDrop PeerEventType = "drop"

	// PeerEventTypeMsgSend is the type of event emitted when a
	// message is successfully sent to a peer
	PeerEventTypeMsgSend PeerEventType = "msgsend"

	// PeerEventTypeMsgRecv is the type of event emitted when a
	// message is received from a peer
	PeerEventTypeMsgRecv PeerEventType = "msgrecv"
)

type PeerInfo

type PeerInfo struct {
	ID      string   `json:"id"`   // Unique node identifier (also the encryption key)
	Name    string   `json:"name"` // Name of the node, including client type, version, OS, custom data
	Caps    []string `json:"caps"` // Sum-protocols advertised by this particular peer
	Network struct {
		LocalAddress  string `json:"localAddress"`  // Local endpoint of the TCP data connection
		RemoteAddress string `json:"remoteAddress"` // Remote endpoint of the TCP data connection
		Inbound       bool   `json:"inbound"`
		Trusted       bool   `json:"trusted"`
		Static        bool   `json:"static"`
	} `json:"network"`
	Protocols map[string]interface{} `json:"protocols"` // Sub-protocol specific metadata fields
}

type Protocol

type Protocol struct {
	Name     string
	Version  uint
	Length   uint64
	Run      func(peer *Peer, rw MsgReadWriter) error
	NodeInfo func() interface{}
	PeerInfo func(id libp2p.ID) interface{}
}

目前依然沿用原有的子协议结构,减少上层的改动

type Server

type Server struct {
	Config
	// contains filtered or unexported fields
}

func (*Server) AddPeer

func (server *Server) AddPeer(ctx context.Context, node *Node)

func (*Server) Close added in v0.6.2

func (server *Server) Close()

close server.quit to broadcast the server shutdown

func (*Server) GetPeerByRemoteID

func (server *Server) GetPeerByRemoteID(s inet.Stream) *Peer

GetPeerByRemoteID get specific peer by remoteID if it doesn't exist, new it this function guarantee get the wanted peer

func (*Server) HandleStream

func (server *Server) HandleStream(s inet.Stream)

HandleStream handle all message which is from anywhere 主、被动连接都走的流程

func (*Server) LoadConfig

func (server *Server) LoadConfig(ctx context.Context) []peer.ID

func (*Server) NodeInfo

func (server *Server) NodeInfo() *NodeInfo

func (*Server) PeerCount

func (server *Server) PeerCount() int

func (*Server) Peers

func (server *Server) Peers() []*Peer

func (*Server) PeersInfo

func (server *Server) PeersInfo() []*PeerInfo

func (*Server) RemovePeer

func (server *Server) RemovePeer(node *Node)

func (*Server) Self

func (server *Server) Self() *Node

func (*Server) SetupStream

func (server *Server) SetupStream(ctx context.Context, target peer.ID, pid string) error

SetupStream 主动发起连接

func (*Server) Start

func (server *Server) Start() error

func (*Server) Stop

func (server *Server) Stop()

func (*Server) SubscribeEvents

func (server *Server) SubscribeEvents(ch chan *PeerEvent) event.Subscription

type Stream

type Stream struct {
	Protocols []Protocol
	// contains filtered or unexported fields
}

type VNTDht

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

func NewDHTTable

func NewDHTTable(dht *dht.IpfsDHT, id peer.ID) *VNTDht

func (*VNTDht) GetDhtTable added in v0.6.2

func (vdht *VNTDht) GetDhtTable() *dht.IpfsDHT

func (*VNTDht) Lookup

func (vdht *VNTDht) Lookup(ctx context.Context, targetID NodeID) []*NodeID

func (*VNTDht) RandomPeer

func (vdht *VNTDht) RandomPeer() []peer.ID

func (*VNTDht) Start

func (vdht *VNTDht) Start(ctx context.Context) error

func (*VNTDht) Update

func (vdht *VNTDht) Update(ctx context.Context, id peer.ID) error

type VNTMsger added in v0.6.2

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

VNTMsger vnt chain message readwriter

func (*VNTMsger) ReadMsg added in v0.6.2

func (rw *VNTMsger) ReadMsg() (Msg, error)

ReadMsg implement MsgReadWriter interface

func (*VNTMsger) WriteMsg added in v0.6.2

func (rw *VNTMsger) WriteMsg(msg Msg) (err error)

WriteMsg implement MsgReadWriter interface

Directories

Path Synopsis
Package netutil contains extensions to the net package.
Package netutil contains extensions to the net package.

Jump to

Keyboard shortcuts

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