Decred's serialized format for transactions split the 32-bit version field into two 16-bit components such that the upper bits are used to encode a serialization type and the lower 16 bits are the actual transaction version. Unfortunately, when this was done, the in-memory transaction struct was not also updated to hide this complexity, which means that callers currently have to understand and take special care when dealing with the version field of the transaction. Since the main purpose of the wire package is precisely to hide these details, this remedies the situation by introducing a new field on the in-memory transaction struct named SerType which houses the serialization type and changes the Version field back to having the desired semantics of actually being the real transaction version. Also, since the maximum version can only be a 16-bit value, the Version field has been changed to a uint16 to properly reflect this. The serialization and deserialization functions now deal with properly converting to and from these fields to the actual serialized format as intended. Finally, these changes also include a fairly significant amount of related code cleanup and optimization along with some bug fixes in order to allow the transaction version to be bumped as intended. The following is an overview of all changes: - Introduce new SerType field to MsgTx to specify the serialization type - Change MsgTx.Version to a uint16 to properly reflect its maximum allowed value - Change the semantics of MsgTx.Version to be the actual transaction version as intended - Update all callers that had special code to deal with the previous Version field semantics to use the new semantics - Switch all of the code that deals with encoding and decoding the serialized version field to use more efficient masks and shifts instead of binary writes into buffers which cause allocations - Correct several issues that would prevent producing expected serializations for transactions with actual transaction versions that are not 1 - Simplify the various serialize functions to use a single func which accepts the serialization type to reduce code duplication - Make serialization type switch usage more consistent with the rest of the code base - Update the utxoview and related code to use uint16s for the transaction version as well since it should not care about the serialization type due to using its own - Make code more consistent in how it uses bytes.Buffer - Clean up several of the comments regarding hashes and add some new comments to better describe the serialization types |
||
|---|---|---|
| .. | ||
| bench_test.go | ||
| blockheader_test.go | ||
| blockheader.go | ||
| common_test.go | ||
| common.go | ||
| doc.go | ||
| error.go | ||
| fakeconn_test.go | ||
| fakemessage_test.go | ||
| fixedIO_test.go | ||
| invvect_test.go | ||
| invvect.go | ||
| message_test.go | ||
| message.go | ||
| msgaddr_test.go | ||
| msgaddr.go | ||
| msgalert_test.go | ||
| msgalert.go | ||
| msgblock_test.go | ||
| msgblock.go | ||
| msgfeefilter_test.go | ||
| msgfeefilter.go | ||
| msgfilteradd_test.go | ||
| msgfilteradd.go | ||
| msgfilterclear_test.go | ||
| msgfilterclear.go | ||
| msgfilterload_test.go | ||
| msgfilterload.go | ||
| msggetaddr_test.go | ||
| msggetaddr.go | ||
| msggetblocks_test.go | ||
| msggetblocks.go | ||
| msggetdata_test.go | ||
| msggetdata.go | ||
| msggetheaders_test.go | ||
| msggetheaders.go | ||
| msggetminingstate.go | ||
| msgheaders_test.go | ||
| msgheaders.go | ||
| msginv_test.go | ||
| msginv.go | ||
| msgmempool_test.go | ||
| msgmempool.go | ||
| msgmerkleblock_test.go | ||
| msgmerkleblock.go | ||
| msgminingstate_test.go | ||
| msgminingstate.go | ||
| msgnotfound_test.go | ||
| msgnotfound.go | ||
| msgping_test.go | ||
| msgping.go | ||
| msgpong_test.go | ||
| msgpong.go | ||
| msgreject_test.go | ||
| msgreject.go | ||
| msgsendheaders_test.go | ||
| msgsendheaders.go | ||
| msgtx_test.go | ||
| msgtx.go | ||
| msgverack_test.go | ||
| msgverack.go | ||
| msgversion_test.go | ||
| msgversion.go | ||
| netaddress_test.go | ||
| netaddress.go | ||
| protocol_test.go | ||
| protocol.go | ||
| README.md | ||
wire
Package wire implements the decred wire protocol. A comprehensive suite of tests with 100% test coverage is provided to ensure proper functionality.
This package has intentionally been designed so it can be used as a standalone package for any projects needing to interface with decred peers at the wire protocol level.
Installation and Updating
$ go get -u github.com/decred/dcrd/wire
Decred Message Overview
The decred protocol consists of exchanging messages between peers. Each message is preceded by a header which identifies information about it such as which decred 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 decred 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 decred messages are provided. For these
supported messages, all of the details of marshalling and unmarshalling to and
from the wire using decred encoding are handled so the caller doesn't have to
concern themselves with the specifics.
Reading Messages Example
In order to unmarshal decred 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 decred peer. Example syntax is:
// Use the most recent protocol version supported by the package and the
// main decred network.
pver := wire.ProtocolVersion
dcrnet := wire.MainNet
// Reads and validates the next decred message from conn using the
// protocol version pver and the decred network dcrnet. 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, dcrnet)
if err != nil {
// Log and handle the error
}
See the package documentation for details on determining the message type.
Writing Messages Example
In order to marshal decred 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 decred peer. Example syntax to request addresses
from a remote peer is:
// Use the most recent protocol version supported by the package and the
// main decred network.
pver := wire.ProtocolVersion
dcrnet := wire.MainNet
// Create a new getaddr decred message.
msg := wire.NewMsgGetAddr()
// Writes a decred message msg to conn using the protocol version
// pver, and the decred network dcrnet. The return is a possible
// error.
err := wire.WriteMessage(conn, msg, pver, dcrnet)
if err != nil {
// Log and handle the error
}
License
Package wire is licensed under the copyfree ISC License.