Implements Ethereum's Wire Protocol and provides networking functions.
API
Network
new Network([host], [post], [options])
Network
optionsNetwork
methodsnetwork.listen([port], [host])
network.connect(port, host, [callback])
network.stop([callback])
network.getPeers()
network.getPeerList()
network.broadcastPing([callback])
network.broadcastGetPeers([callback])
network.broadcastTransactions(transactions, [callback])
network.broadcastBlocks(blocks, [callback])
network.broadcastDisconnect(reason, [callback])
Network
events
Peer
Peer
methodspeer.sendHello([callback])
peer.sendDisconnect(reason, [callback])
peer.sendPing([callback])
peer.sendPong([callback])
peer.sendGetPeers([callback])
peer.sendPeers(peers, [callback])
peer.sendTransactions(transactions, [callback])
peer.sendBlocks(blocks, [callback])
peer.sendGetChain(parents, count,[callback])
peer.sendNotInChain([callback])
peer.sendGetTransactions([callback])
Peer
events
- Schemas
Network
new Network([options])
Creates new Network object with the following arguments
options
- An object with the Network configuration. SeeNetwork
options
Network
options
When creating a Network the following options can be used to configure its behavoir.
ehtVersion
- The version of the Ethereum protocol this peer implements. Defaults to 33 at present.timeout
- The lenght of time in milliseconds to wait for a peer to response after connecting to itmaxPeers
- The max number of peer the network will try to connect toclientId
- specifies the client software identity, as a human-readable stringpublicIp
- The public ip of this node
Network
methods
network.listen([port], [host])
start the tcp server
host
- The hostname or IP address the server is bound to. Defaults to0.0.0.0
which means any available networkport
- The TPC port the server is listening to. Defaults to port30303
network.connect(port, host, [callback])
connect to a peer
host
- the hostname or IP of the peerport
- the port of the peercallback
- a callback function
network.stop([callback])
stops the tcp server and disconnects any peers
network.getPeers()
returns an array of connected peers a instances of the peer object
network.getPeerList()
returns an array of peers the server knows about but is not connected to. The server uses this list to replace peers that disconnect.
network.broadcastPing([callback])
Broadcast a ping to all of the peers.
network.broadcastGetPeers([callback])
Broadcast a get peers packet to all of the peers.
network.broadcastTransactions(transactions, [callback])
broadcasts an array of transactions to the connected peers
transactions
- an array of valid transactions
network.broadcastBlocks(blocks, [callback])
broadcast an array of blocks to the connected peers
blocks
- an array of blocks to broadcast
network.broadcastDisconnect(reason, [callback])
broadcast a disconnect packet to all of the peers
reason
- the reason the client is disconnecting. Seepeer.sendDisconnect(reason, [callback])
Network
events
The Network object inherits from Events.EventEmitter
and emits the following events.
'message.hello'
- emitted on receiving a hello packet. Provides ahello
object as an argument.'message.disconnect'
- emitted on receiving a disconnect packet.Provides adisconnect
object as an argument.'message.ping'
- emitted on receiving a ping'message.pong'
- emitted on receiving a pong'message.sendPeers'
- emitted on receiving a send a peers packet.'message.peers'
- emitted on receiving a peers packet. Provides apeers
object as an argument.'message.transaction'
- emitted on receiving a transaction packet. Provides atransaction
object as an argument.'message.blocks'
- emitted on receiving a blocks packet. Provides ablocks
object as an argument.'message.getChain'
- emitted on receiving a get chain packet. Provides agetChain
object as an argument.'message.getNotInChain'
- emitted on receiving a not in chain packet'message.getTransactions'
- emitted on receiving a get transactions packet
Each of the events are provided with the following arguments in this order
message
- The decoded message parsed to an Object. See event Message Objectspeer
- The peer that emitted the event
Peer
The peer represents a peer on the ethereum network. Peer objects cannot be created directly.
- file - lib/network/peer.js
Peer
methods
peer.sendHello([callback])
Sends the hello message
peer.sendDisconnect(reason, [callback])
Sends the disconnect message, where reason is one of the following integers
0x00
- Disconnect requested0x01
- TCP sub-system error0x02
- Bad protocol0x03
- Useless peer0x04
- Too many peers0x05
- Already connected0x06
- Wrong genesis block0x07
- Incompatible network protocols0x08
- Client quitting
peer.sendPing([callback])
Send Ping
peer.sendPong([callback])
Send Pong
peer.sendGetPeers([callback])
Send a get peers reqeust
peer.sendPeers(peers, [callback])
Send peer list TODO
peers
- an array of peers
peer.sendTransactions(transactions, [callback])
Sends a transaction list TODO
transactions
- an array of transactions to send
peer.sendBlocks(blocks, [callback])
Sends blocks
blocks
- an array of blocks to send
peer.sendGetChain(parents, count, [callback])
Sends a request for part of a block chain TODO
parents
- an array of parent block hashescount
- the number of requested blocks
peer.sendNotInChain([callback])
Sends not in chain message
peer.sendGetTransactions([callback])
Sends a request for transactions
Peer
events
peer events are the same as Network
events
Schemas
After the payload is parsed it passed along to the events in form of these objects
hello
protocolVersion
- the protocol version of the peernetworkId
- should be 0clientId
- Specifies the client software identity, as a human-readable string (e.g. "Ethereum(++)/1.0.0").capabilities
- pecifies the capabilities of the client as a set of boolean flagsblockchainQuerying
peerDiscovery
transactionRelaying
port
- specifies the port that the client is listening onip
- the ip of the connecting peerid
- a 512-bit hash that identifies this node
peers
The peers message is an array of object with the following fields
ip
- The IP of the peerport
- The port of the peerid
- The Id of the peer
getChain
parents
- An array of parent block hashescount
- The number of request blocks
disconnect
reason
- the reason for the disconnect
LICENSE
GPL