github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/wire/doc.go (about)

     1  // Copyright (c) 2013-2016 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  /*
     7  Package wire implements the bitcoin wire protocol.
     8  
     9  For the complete details of the bitcoin protocol, see the official wiki entry
    10  at https://en.bitcoin.it/wiki/Protocol_specification.  The following only serves
    11  as a quick overview to provide information on how to use the package.
    12  
    13  At a high level, this package provides support for marshalling and unmarshalling
    14  supported bitcoin messages to and from the wire.  This package does not deal
    15  with the specifics of message handling such as what to do when a message is
    16  received.  This provides the caller with a high level of flexibility.
    17  
    18  Bitcoin Message Overview
    19  
    20  The bitcoin protocol consists of exchanging messages between peers.  Each
    21  message is preceded by a header which identifies information about it such as
    22  which bitcoin network it is a part of, its type, how big it is, and a checksum
    23  to verify validity.  All encoding and decoding of message headers is handled by
    24  this package.
    25  
    26  To accomplish this, there is a generic interface for bitcoin messages named
    27  Message which allows messages of any type to be read, written, or passed around
    28  through channels, functions, etc.  In addition, concrete implementations of most
    29  of the currently supported bitcoin messages are provided.  For these supported
    30  messages, all of the details of marshalling and unmarshalling to and from the
    31  wire using bitcoin encoding are handled so the caller doesn't have to concern
    32  themselves with the specifics.
    33  
    34  Message Interaction
    35  
    36  The following provides a quick summary of how the bitcoin messages are intended
    37  to interact with one another.  As stated above, these interactions are not
    38  directly handled by this package.  For more in-depth details about the
    39  appropriate interactions, see the official bitcoin protocol wiki entry at
    40  https://en.bitcoin.it/wiki/Protocol_specification.
    41  
    42  The initial handshake consists of two peers sending each other a version message
    43  (MsgVersion) followed by responding with a verack message (MsgVerAck).  Both
    44  peers use the information in the version message (MsgVersion) to negotiate
    45  things such as protocol version and supported services with each other.  Once
    46  the initial handshake is complete, the following chart indicates message
    47  interactions in no particular order.
    48  
    49  	Peer A Sends                          Peer B Responds
    50  	----------------------------------------------------------------------------
    51  	getaddr message (MsgGetAddr)          addr message (MsgAddr)
    52  	getblocks message (MsgGetBlocks)      inv message (MsgInv)
    53  	inv message (MsgInv)                  getdata message (MsgGetData)
    54  	getdata message (MsgGetData)          block message (MsgBlock) -or-
    55  	                                      tx message (MsgTx) -or-
    56  	                                      notfound message (MsgNotFound)
    57  	getheaders message (MsgGetHeaders)    headers message (MsgHeaders)
    58  	ping message (MsgPing)                pong message (MsgHeaders)* -or-
    59  	                                      (none -- Ability to send message is enough)
    60  
    61  	NOTES:
    62  	* The pong message was not added until later protocol versions as defined
    63  	  in BIP0031.  The BIP0031Version constant can be used to detect a recent
    64  	  enough protocol version for this purpose (version > BIP0031Version).
    65  
    66  Common Parameters
    67  
    68  There are several common parameters that arise when using this package to read
    69  and write bitcoin messages.  The following sections provide a quick overview of
    70  these parameters so the next sections can build on them.
    71  
    72  Protocol Version
    73  
    74  The protocol version should be negotiated with the remote peer at a higher
    75  level than this package via the version (MsgVersion) message exchange, however,
    76  this package provides the wire.ProtocolVersion constant which indicates the
    77  latest protocol version this package supports and is typically the value to use
    78  for all outbound connections before a potentially lower protocol version is
    79  negotiated.
    80  
    81  Bitcoin Network
    82  
    83  The bitcoin network is a magic number which is used to identify the start of a
    84  message and which bitcoin network the message applies to.  This package provides
    85  the following constants:
    86  
    87  	wire.MainNet
    88  	wire.TestNet  (Regression test network)
    89  	wire.TestNet3 (Test network version 3)
    90  	wire.SimNet   (Simulation test network)
    91  
    92  Determining Message Type
    93  
    94  As discussed in the bitcoin message overview section, this package reads
    95  and writes bitcoin messages using a generic interface named Message.  In
    96  order to determine the actual concrete type of the message, use a type
    97  switch or type assertion.  An example of a type switch follows:
    98  
    99  	// Assumes msg is already a valid concrete message such as one created
   100  	// via NewMsgVersion or read via ReadMessage.
   101  	switch msg := msg.(type) {
   102  	case *wire.MsgVersion:
   103  		// The message is a pointer to a MsgVersion struct.
   104  		fmt.Printf("Protocol version: %v", msg.ProtocolVersion)
   105  	case *wire.MsgBlock:
   106  		// The message is a pointer to a MsgBlock struct.
   107  		fmt.Printf("Number of tx in block: %v", msg.Header.TxnCount)
   108  	}
   109  
   110  Reading Messages
   111  
   112  In order to unmarshall bitcoin messages from the wire, use the ReadMessage
   113  function.  It accepts any io.Reader, but typically this will be a net.Conn to
   114  a remote node running a bitcoin peer.  Example syntax is:
   115  
   116  	// Reads and validates the next bitcoin message from conn using the
   117  	// protocol version pver and the bitcoin network btcnet.  The returns
   118  	// are a wire.Message, a []byte which contains the unmarshalled
   119  	// raw payload, and a possible error.
   120  	msg, rawPayload, err := wire.ReadMessage(conn, pver, btcnet)
   121  	if err != nil {
   122  		// Log and handle the error
   123  	}
   124  
   125  Writing Messages
   126  
   127  In order to marshall bitcoin messages to the wire, use the WriteMessage
   128  function.  It accepts any io.Writer, but typically this will be a net.Conn to
   129  a remote node running a bitcoin peer.  Example syntax to request addresses
   130  from a remote peer is:
   131  
   132  	// Create a new getaddr bitcoin message.
   133  	msg := wire.NewMsgGetAddr()
   134  
   135  	// Writes a bitcoin message msg to conn using the protocol version
   136  	// pver, and the bitcoin network btcnet.  The return is a possible
   137  	// error.
   138  	err := wire.WriteMessage(conn, msg, pver, btcnet)
   139  	if err != nil {
   140  		// Log and handle the error
   141  	}
   142  
   143  Errors
   144  
   145  Errors returned by this package are either the raw errors provided by underlying
   146  calls to read/write from streams such as io.EOF, io.ErrUnexpectedEOF, and
   147  io.ErrShortWrite, or of type wire.MessageError.  This allows the caller to
   148  differentiate between general IO errors and malformed messages through type
   149  assertions.
   150  
   151  Bitcoin Improvement Proposals
   152  
   153  This package includes spec changes outlined by the following BIPs:
   154  
   155  	BIP0014 (https://github.com/bitcoin/bips/blob/master/bip-0014.mediawiki)
   156  	BIP0031 (https://github.com/bitcoin/bips/blob/master/bip-0031.mediawiki)
   157  	BIP0035 (https://github.com/bitcoin/bips/blob/master/bip-0035.mediawiki)
   158  	BIP0037 (https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki)
   159  	BIP0111	(https://github.com/bitcoin/bips/blob/master/bip-0111.mediawiki)
   160  	BIP0130 (https://github.com/bitcoin/bips/blob/master/bip-0130.mediawiki)
   161  */
   162  package wire