mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00

* [DEV-134] Implement Continuous Integration Squashed commit: [5e41d830] Dev 223 fix txindex (#100) * [DEV-201] In handleGetBlockDAGInfo calculate difficulty by the tip with the lowest bits * [DEV-202] Move VirtualBlock.GetUTXOEntry to BlockDAG * [DEV-203] Move VirtualBlock.SelectedTip() to BlockDAG * [DEV-203] Move VirtualBlock.SelectedTip() to BlockDAG * [DEV-204] Unexport VirtualBlock() and add CalcMedianTime method for DAG * [DEV-204] add explanation about difficulty in CurrentBits() comment * [DEV-204] unexport VirtualBlock type * [DEV-223] make applyUTXOChanges return pastUTXOResults * [DEV-223] add bluestxdata for current block as well * [DEV-223] re-design tx index * [DEV-223] edit txindex comments * [DEV-223] rename BluesTxData -> AcceptedTxData, and return from applyUTXOChanges only transactions that got accepted * [DEV-223] add unit test for txindex * [DEV-223] fix comments and unite blueTransaction and AcceptedTxData to one type * [DEV-223] use bucket cursor for dbFetchFirstTxRegion * [DEV-223] use the same cursor instance for dbFetchFirstTxRegion * [DEV-223] write in dbFetchFirstTxRegion's comment that it returns the first block region * [DEV-223] rename type BlueBlockTransaction to TxWithBlockHash * [DEV-223] add named returned value for applyUTXOChanges [4c95e293] [DEV-134] Made golint ignore the vendor directory. [21736dbc] [DEV-134] Renamed ExampleBlockChain_ProcessBlock to ExampleBlockDAG_ProcessBlock to satisfy go vet. [beea6486] [DEV-134] Removed pushing the built docker to a remove repository. That's unnecessary at this stage. [bee911ed] [DEV-134] Made all precompilation checks run on everything instead of only the root dir. [585f92ae] [DEV-134] Added "github.com/pkg/errors" to dep. [5f02f570] [DEV-134] -vendor-only is written with only one hyphen. [3eee7f95] [DEV-134] go vet instead of go tool vet. [0c2d4343] [DEV-134] Split all the pre-compile checks to separate lines to be able to tell which of them is failing. [780519c8] [DEV-134] Ran gofmt on everything. [8247146b] Dev 223 fix txindex (#100) * [DEV-201] In handleGetBlockDAGInfo calculate difficulty by the tip with the lowest bits * [DEV-202] Move VirtualBlock.GetUTXOEntry to BlockDAG * [DEV-203] Move VirtualBlock.SelectedTip() to BlockDAG * [DEV-203] Move VirtualBlock.SelectedTip() to BlockDAG * [DEV-204] Unexport VirtualBlock() and add CalcMedianTime method for DAG * [DEV-204] add explanation about difficulty in CurrentBits() comment * [DEV-204] unexport VirtualBlock type * [DEV-223] make applyUTXOChanges return pastUTXOResults * [DEV-223] add bluestxdata for current block as well * [DEV-223] re-design tx index * [DEV-223] edit txindex comments * [DEV-223] rename BluesTxData -> AcceptedTxData, and return from applyUTXOChanges only transactions that got accepted * [DEV-223] add unit test for txindex * [DEV-223] fix comments and unite blueTransaction and AcceptedTxData to one type * [DEV-223] use bucket cursor for dbFetchFirstTxRegion * [DEV-223] use the same cursor instance for dbFetchFirstTxRegion * [DEV-223] write in dbFetchFirstTxRegion's comment that it returns the first block region * [DEV-223] rename type BlueBlockTransaction to TxWithBlockHash * [DEV-223] add named returned value for applyUTXOChanges [bff68aa3] [DEV-134] Gave executable permission to deploy.sh [638a99d9] [DEV-134] Added jenkinsfile and deploy script. * [DEV-134] Added a robust testing script. * [DEV-134] Fixed a bash-ism. * [DEV-134] Disabled testing with coverage for now. * [DEV-134] Disabled golint and removed removing debug symbols. * [DEV-134] Disabled aligncheck. * [DEV-134] Disabled structcheck and varcheck. * [DEV-134] Added "don't inline functions" to compiler flags for testing. * [DEV-134] Made build fail if gofmt prints out anything. * [DEV-134] Fixed misleading comment. * [DEV-134] Added comments to test.sh. * [DEV-134] Renamed tm to measure_runtime and removed do_ prefixes from functions. * [DEV-134] Fixed gofmt line in build script. * [DEV-134] Fixed gofmt some more. * [DEV-134] Fixed gofmt not actually failing due to logical or.
163 lines
7.1 KiB
Go
163 lines
7.1 KiB
Go
// Copyright (c) 2013-2016 The btcsuite developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
/*
|
|
Package wire implements the bitcoin wire protocol.
|
|
|
|
For the complete details of the bitcoin protocol, see the official wiki entry
|
|
at https://en.bitcoin.it/wiki/Protocol_specification. The following only serves
|
|
as a quick overview to provide information on how to use the package.
|
|
|
|
At a high level, this package provides support for marshalling and unmarshalling
|
|
supported bitcoin messages to and from the wire. This package does not deal
|
|
with the specifics of message handling such as what to do when a message is
|
|
received. This provides the caller with a high level of flexibility.
|
|
|
|
Bitcoin Message Overview
|
|
|
|
The bitcoin protocol consists of exchanging messages between peers. Each
|
|
message is preceded by a header which identifies information about it such as
|
|
which bitcoin network it is a part of, its type, how big it is, and a checksum
|
|
to verify validity. All encoding and decoding of message headers is handled by
|
|
this package.
|
|
|
|
To accomplish this, there is a generic interface for bitcoin messages named
|
|
Message which allows messages of any type to be read, written, or passed around
|
|
through channels, functions, etc. In addition, concrete implementations of most
|
|
of the currently supported bitcoin messages are provided. For these supported
|
|
messages, all of the details of marshalling and unmarshalling to and from the
|
|
wire using bitcoin encoding are handled so the caller doesn't have to concern
|
|
themselves with the specifics.
|
|
|
|
Message Interaction
|
|
|
|
The following provides a quick summary of how the bitcoin messages are intended
|
|
to interact with one another. As stated above, these interactions are not
|
|
directly handled by this package. For more in-depth details about the
|
|
appropriate interactions, see the official bitcoin protocol wiki entry at
|
|
https://en.bitcoin.it/wiki/Protocol_specification.
|
|
|
|
The initial handshake consists of two peers sending each other a version message
|
|
(MsgVersion) followed by responding with a verack message (MsgVerAck). Both
|
|
peers use the information in the version message (MsgVersion) to negotiate
|
|
things such as protocol version and supported services with each other. Once
|
|
the initial handshake is complete, the following chart indicates message
|
|
interactions in no particular order.
|
|
|
|
Peer A Sends Peer B Responds
|
|
----------------------------------------------------------------------------
|
|
getaddr message (MsgGetAddr) addr message (MsgAddr)
|
|
getblocks message (MsgGetBlocks) inv message (MsgInv)
|
|
inv message (MsgInv) getdata message (MsgGetData)
|
|
getdata message (MsgGetData) block message (MsgBlock) -or-
|
|
tx message (MsgTx) -or-
|
|
notfound message (MsgNotFound)
|
|
getheaders message (MsgGetHeaders) headers message (MsgHeaders)
|
|
ping message (MsgPing) pong message (MsgHeaders)* -or-
|
|
(none -- Ability to send message is enough)
|
|
|
|
NOTES:
|
|
* The pong message was not added until later protocol versions as defined
|
|
in BIP0031. The BIP0031Version constant can be used to detect a recent
|
|
enough protocol version for this purpose (version > BIP0031Version).
|
|
|
|
Common Parameters
|
|
|
|
There are several common parameters that arise when using this package to read
|
|
and write bitcoin messages. The following sections provide a quick overview of
|
|
these parameters so the next sections can build on them.
|
|
|
|
Protocol Version
|
|
|
|
The protocol version should be negotiated with the remote peer at a higher
|
|
level than this package via the version (MsgVersion) message exchange, however,
|
|
this package provides the wire.ProtocolVersion constant which indicates the
|
|
latest protocol version this package supports and is typically the value to use
|
|
for all outbound connections before a potentially lower protocol version is
|
|
negotiated.
|
|
|
|
Bitcoin Network
|
|
|
|
The bitcoin network is a magic number which is used to identify the start of a
|
|
message and which bitcoin network the message applies to. This package provides
|
|
the following constants:
|
|
|
|
wire.MainNet
|
|
wire.TestNet (Regression test network)
|
|
wire.TestNet3 (Test network version 3)
|
|
wire.SimNet (Simulation test network)
|
|
|
|
Determining Message Type
|
|
|
|
As discussed in the bitcoin message overview section, this package reads
|
|
and writes bitcoin messages using a generic interface named Message. In
|
|
order to determine the actual concrete type of the message, use a type
|
|
switch or type assertion. An example of a type switch follows:
|
|
|
|
// Assumes msg is already a valid concrete message such as one created
|
|
// via NewMsgVersion or read via ReadMessage.
|
|
switch msg := msg.(type) {
|
|
case *wire.MsgVersion:
|
|
// The message is a pointer to a MsgVersion struct.
|
|
fmt.Printf("Protocol version: %v", msg.ProtocolVersion)
|
|
case *wire.MsgBlock:
|
|
// The message is a pointer to a MsgBlock struct.
|
|
fmt.Printf("Number of tx in block: %v", msg.Header.TxnCount)
|
|
}
|
|
|
|
Reading Messages
|
|
|
|
In order to unmarshall bitcoin messages from the wire, use the ReadMessage
|
|
function. It accepts any io.Reader, but typically this will be a net.Conn to
|
|
a remote node running a bitcoin peer. Example syntax is:
|
|
|
|
// Reads and validates the next bitcoin message from conn using the
|
|
// protocol version pver and the bitcoin network btcnet. The returns
|
|
// are a wire.Message, a []byte which contains the unmarshalled
|
|
// raw payload, and a possible error.
|
|
msg, rawPayload, err := wire.ReadMessage(conn, pver, btcnet)
|
|
if err != nil {
|
|
// Log and handle the error
|
|
}
|
|
|
|
Writing Messages
|
|
|
|
In order to marshall bitcoin messages to the wire, use the WriteMessage
|
|
function. It accepts any io.Writer, but typically this will be a net.Conn to
|
|
a remote node running a bitcoin peer. Example syntax to request addresses
|
|
from a remote peer is:
|
|
|
|
// Create a new getaddr bitcoin message.
|
|
msg := wire.NewMsgGetAddr()
|
|
|
|
// Writes a bitcoin message msg to conn using the protocol version
|
|
// pver, and the bitcoin network btcnet. The return is a possible
|
|
// error.
|
|
err := wire.WriteMessage(conn, msg, pver, btcnet)
|
|
if err != nil {
|
|
// Log and handle the error
|
|
}
|
|
|
|
Errors
|
|
|
|
Errors returned by this package are either the raw errors provided by underlying
|
|
calls to read/write from streams such as io.EOF, io.ErrUnexpectedEOF, and
|
|
io.ErrShortWrite, or of type wire.MessageError. This allows the caller to
|
|
differentiate between general IO errors and malformed messages through type
|
|
assertions.
|
|
|
|
Bitcoin Improvement Proposals
|
|
|
|
This package includes spec changes outlined by the following BIPs:
|
|
|
|
BIP0014 (https://github.com/bitcoin/bips/blob/master/bip-0014.mediawiki)
|
|
BIP0031 (https://github.com/bitcoin/bips/blob/master/bip-0031.mediawiki)
|
|
BIP0035 (https://github.com/bitcoin/bips/blob/master/bip-0035.mediawiki)
|
|
BIP0037 (https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki)
|
|
BIP0111 (https://github.com/bitcoin/bips/blob/master/bip-0111.mediawiki)
|
|
BIP0130 (https://github.com/bitcoin/bips/blob/master/bip-0130.mediawiki)
|
|
BIP0133 (https://github.com/bitcoin/bips/blob/master/bip-0133.mediawiki)
|
|
*/
|
|
package wire
|