From 3a19287da1106de60cbfc2ede6a935fb0abf82ca Mon Sep 17 00:00:00 2001 From: Jason Dreyzehner Date: Tue, 20 Oct 2015 09:20:39 -0400 Subject: [PATCH] format --- docs/index.md | 10 +--------- docs/messages.md | 17 ----------------- docs/peer.md | 20 ++++---------------- docs/pool.md | 11 +---------- 4 files changed, 6 insertions(+), 52 deletions(-) diff --git a/docs/index.md b/docs/index.md index 4375124..5709605 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,18 +1,11 @@ ---- -title: Peer-to-Peer Networking -description: Peer-to-Peer Networking Capabilities for Bitcore ---- # Peer-to-Peer - -## Description - The `bitcore-p2p` module provides peer-to-peer networking capabilites for [Bitcore](https://github.com/bitpay/bitcore), and includes [Peer](peer.md) and [Pool](pool.md) classes. A [Message](messages.md) class is also exposed, in addition to [several types of messages](messages.md). Pool will maintain connection to several peers, Peers represents a node in the bitcoin network, and Message represents data sent to and from a Peer. For detailed technical information about the bitcoin protocol, please visit the [Protocol Specification](https://en.bitcoin.it/wiki/Protocol_specification) on the Bitcoin Wiki. ## Installation - Peer-to-peer is implemented as a seperate module. For node projects: + ```bash npm install bitcore-p2p --save ``` @@ -29,5 +22,4 @@ peer.on('inv', function(message) { }); peer.connect(); - ``` diff --git a/docs/messages.md b/docs/messages.md index c70cfd0..15a39de 100644 --- a/docs/messages.md +++ b/docs/messages.md @@ -1,9 +1,4 @@ ---- -title: Messages -description: A superclass for the messages of the bitcoin network ---- # Messages - The bitcoin protocol specifies a set of [messages](https://en.bitcoin.it/wiki/Protocol_specification) that can be sent from peer to peer. `bitcore-p2p` provides support for some of these messages. To create a message, you can use any of the message constructors, here is a simple example: @@ -43,21 +38,16 @@ var messages = new Messages({network: Networks.testnet}); ``` ## List of Messages - ### Version - The version message (`ver`) is used on connection creation, to advertise the type of node. The remote node will respond with its version, and no communication is possible until both peers have exchanged their versions. By default, bitcore advertises itself as named `bitcore` with the current version of the `bitcore-p2p` package. ### VerAck - Finishes the connection handshake started by the `ver` message. ### Inventory - From the bitcoin protocol spec: "Allows a node to advertise its knowledge of one or more objects. It can be received unsolicited, or in reply to getblocks.". ### GetData - From the bitcoin protocol spec: `getdata` is used in response to `inv`, to retrieve the content of a specific object, and is usually sent after receiving an `inv` packet, after filtering known elements. It can be used to retrieve transactions, but only if they are in the memory pool or relay set - arbitrary access to transactions in the chain is not allowed to avoid having clients start to depend on nodes having full transaction indexes (which modern nodes do not). GetData inherits from Inventory, as they both have the same structure. @@ -66,31 +56,24 @@ GetData inherits from Inventory, as they both have the same structure. notfound is a response to a getdata, sent if any requested data items could not be relayed, for example, because the requested transaction was not in the memory pool or relay set. Contains inventory information specifying which items were not found. ### Ping - Sent to another peer mainly to check the connection is still alive. ### Pong - Sent in response to a `ping` message. ### Address and GetAddresses - Provides information on known nodes of the network. `GetAddresses` is used to query another peer for known addresses. ### GetHeaders and Headers - `getheaders` allows a peer to query another about blockheaders. `headers` is sent in response to a `getheaders` message, containing information about block headers. ### GetBlocks and Block - Same as `getheaders` and `headers`, but the response comes one block at the time. ### Transaction - Message that contains a transaction. ## Custom Messages - It is possible to extend the default peer to peer messages and add custom ones. First you will need to create a message which resembles the default messages in `lib/messages/commands`. Then to add the custom message: diff --git a/docs/peer.md b/docs/peer.md index e089e37..dde6b0f 100644 --- a/docs/peer.md +++ b/docs/peer.md @@ -1,15 +1,7 @@ ---- -title: Peer -description: The Peer class provides a simple interface for connecting to a node in the bitcoin network. ---- # Peer - -## Description - Represents a node from the p2p bitcoin network. The Peer class supports connecting directly to other nodes or through a socks5 proxy like Tor. ## Creating a peer - The code to create a new peer looks like this: ```javascript @@ -28,13 +20,11 @@ var peer = new Peer({host: '5.9.85.34'}).setProxy('localhost', 9050); ``` ## States - A peer instance is always in one of the following states: - -* `disconnected`: No connection with the remote node. -* `connecting`: While establishing the connection. -* `connected`: Exchanging version packages. -* `ready`: Connection ready for sending and receiving messages. +- `disconnected`: No connection with the remote node. +- `connecting`: While establishing the connection. +- `connected`: Exchanging version packages. +- `ready`: Connection ready for sending and receiving messages. You can subscribe to the change of those states as follows: @@ -56,7 +46,6 @@ peer.connect(); ``` ## Handle messages - Once connected, a peer instance can send and receive messages. Every time a message arrives it's emitted as a new event. Let's see an example of this: ```javascript @@ -80,7 +69,6 @@ peer.connect(); ``` ## Sending messages - In order to send messages the Peer class offers the `sendMessage(message)` method, which receives an instance of a message. All supported messages can be found in the `Messages` module. For more information about messages refer to the [protocol specification](https://en.bitcoin.it/wiki/Protocol_specification). An example for requesting other connected nodes to a peers looks like this: diff --git a/docs/pool.md b/docs/pool.md index ea81d66..7d0adb7 100644 --- a/docs/pool.md +++ b/docs/pool.md @@ -1,9 +1,4 @@ ---- -title: Pool -description: A simple interface to create and maintain a set of connections to bitcoin nodes. ---- # Pool - A pool maintains a connection of [Peers](peer.md). A pool will discover peers via DNS seeds, as well as when peer addresses are announced through the network. The quickest way to get connected is to run the following: @@ -25,13 +20,11 @@ pool.on('peerinv', function(peer, message) { // will disconnect all peers pool.disconnect() - ``` For more information about Peer events please read the [Peer](peer.md) documentation. Peer events are relayed to the pool, a peer event `inv` in the pool would be `peerinv`. When a peer is disconnected the pool will try to connect to the list of known addresses to maintain connection. ## Trusted Peers - By default, peers will be added via DNS discovery and as peers are announced in the network. Configuration options can be included to connect only to specific trusted peers: ```javascript @@ -50,11 +43,9 @@ var pool = new Pool({ }); pool.connect(); - ``` ## Listening for Peers - It's also possible to listen to incoming socket connections to add peers to the pool. To enable this capability, you can do the following: ```javascript @@ -62,4 +53,4 @@ var pool = new Pool({network: Networks.livenet}); pool.listen(); ``` -When there are incoming connections the peer will be added to the pool. \ No newline at end of file +When there are incoming connections the peer will be added to the pool.