dcrd/wire
Dave Collins 42175088b6
blockchain: Implement enforced relative seq locks.
This repurposes the sequence number of version 2 transaction inputs to
provide consensus-enforced relative lock-time semantics so that a
transaction can require inputs to have a specified relative age before
they are allowed to be included in a block.  Each relative time lock can
either specify a relative number of seconds (with a granularity of 512
seconds and a maximum value of 33,553,920) or a specific number of
blocks (max 65535).

The number of seconds is calculated relative to the past median time of
the block before the one that contains the referenced output.  This is
done because, due to other changes that will also be included in the
same agenda vote, said time will become the earliest possible time the
block that contains the referenced output could have been (technically
it will be one second after that, but that complexity is ignored since
there is already a granularity involved anyways).

It is also possible to disable the behavior by setting bit 31 of the
sequence number (which all transactions currently do by default since
they are set to the max).

In order for the transaction to be permitted to the mempool, relayed,
considered for inclusion into block templates, and allowed into a block,
the specified relative time locks for all of its inputs must be
satisfied.

This only implements the required logic and tests to enforce the new
behavior.  Code to enforce the new behavior when considering candidate
transactions for acceptance to the mempool, relaying, and inclusion into
block templates will be added in a separate commit.

A consensus vote is required in order to reject blocks which contain
transactions that violate the new rules at a consensus level.  Code to
selectively enable consensus enforcement based on the result of an
agenda vote will be added in a separate commit.

In order to accomplish this new behavior, the concept of a sequence lock
is introduced which allows the minimum possible time and height at which
a transaction can be included into a block to be calculated from all
inputs with non-disabled relative time locks, and functions to calculate
and evaluate the sequence lock are added.

The following is an overview of the changes:

- Introduce a new struct named SequenceLock to represent the previously
  described sequence lock
- Define new constants related to sequence numbers named
  SequenceLockTimeDisabled, SequenceLockTimeIsSeconds,
  SequenceLockTimeMask, and SequenceLockTimeGranularity
- Add a new function named calcSequenceLock to calculate the sequence
  lock for a given transaction
- Add a new function named SequenceLockActive to determine if a given
  sequence lock is satisfied for a given block height and past median
  time
- Add a convenience function named LockTimeToSequence which can be used
  to convert a relative lock time to a sequence number
- Add a comprehensive set of tests for all of the new funcs
2017-09-21 15:58:36 -05:00
..
bench_test.go multi: Separate tx serialization type from version. 2017-08-07 14:10:27 -05:00
blockheader_test.go multi: Separate tx serialization type from version. 2017-08-07 14:10:27 -05:00
blockheader.go multi: Separate tx serialization type from version. 2017-08-07 14:10:27 -05:00
common_test.go multi: Upstream chainhash abstraction sync 2016-11-16 12:48:40 -06:00
common.go wire: Decrease num allocs when decoding headers. 2017-09-21 00:43:32 -05:00
doc.go wire/peer: Implement feefilter p2p message. 2017-08-01 21:59:11 -05:00
error.go blockchain: Rework to use new db interface. 2016-08-18 15:42:18 -04:00
fakeconn_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
fakemessage_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
fixedIO_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
invvect_test.go multi: Upstream chainhash abstraction sync 2016-11-16 12:48:40 -06:00
invvect.go travis: enable gometalinter (#603) 2017-03-08 15:44:15 -05:00
message_test.go Remove variables for testnet v1. 2017-03-16 20:19:07 -04:00
message.go wire/peer: Implement feefilter p2p message. 2017-08-01 21:59:11 -05:00
msgaddr_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
msgaddr.go Merge in btcd commit '2adfb3b56acd280e84451e94dd0c06203eef9832' 2016-09-23 18:02:41 -04:00
msgalert_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
msgalert.go travis: enable gometalinter (#603) 2017-03-08 15:44:15 -05:00
msgblock_test.go multi: Separate tx serialization type from version. 2017-08-07 14:10:27 -05:00
msgblock.go multi: Separate tx serialization type from version. 2017-08-07 14:10:27 -05:00
msgfeefilter_test.go wire/peer: Implement feefilter p2p message. 2017-08-01 21:59:11 -05:00
msgfeefilter.go wire/peer: Implement feefilter p2p message. 2017-08-01 21:59:11 -05:00
msgfilteradd_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
msgfilteradd.go travis: enable gometalinter (#603) 2017-03-08 15:44:15 -05:00
msgfilterclear_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
msgfilterclear.go blockchain: Rework to use new db interface. 2016-08-18 15:42:18 -04:00
msgfilterload_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
msgfilterload.go travis: enable gometalinter (#603) 2017-03-08 15:44:15 -05:00
msggetaddr_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
msggetaddr.go blockchain: Rework to use new db interface. 2016-08-18 15:42:18 -04:00
msggetblocks_test.go multi: Upstream chainhash abstraction sync 2016-11-16 12:48:40 -06:00
msggetblocks.go travis: enable gometalinter (#603) 2017-03-08 15:44:15 -05:00
msggetdata_test.go multi: Upstream chainhash abstraction sync 2016-11-16 12:48:40 -06:00
msggetdata.go Merge in btcd commit '2adfb3b56acd280e84451e94dd0c06203eef9832' 2016-09-23 18:02:41 -04:00
msggetheaders_test.go multi: Upstream chainhash abstraction sync 2016-11-16 12:48:40 -06:00
msggetheaders.go travis: enable gometalinter (#603) 2017-03-08 15:44:15 -05:00
msggetminingstate.go blockchain: Rework to use new db interface. 2016-08-18 15:42:18 -04:00
msgheaders_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
msgheaders.go Merge in btcd commit '2adfb3b56acd280e84451e94dd0c06203eef9832' 2016-09-23 18:02:41 -04:00
msginv_test.go multi: Upstream chainhash abstraction sync 2016-11-16 12:48:40 -06:00
msginv.go Merge in btcd commit '2adfb3b56acd280e84451e94dd0c06203eef9832' 2016-09-23 18:02:41 -04:00
msgmempool_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
msgmempool.go blockchain: Rework to use new db interface. 2016-08-18 15:42:18 -04:00
msgmerkleblock_test.go multi: Implement block size hard fork demo voting. 2017-02-13 13:28:21 -06:00
msgmerkleblock.go travis: enable gometalinter (#603) 2017-03-08 15:44:15 -05:00
msgminingstate_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
msgminingstate.go server: send some vote hashes 2017-03-17 11:08:56 -05:00
msgnotfound_test.go multi: Upstream chainhash abstraction sync 2016-11-16 12:48:40 -06:00
msgnotfound.go Merge in btcd commit '2adfb3b56acd280e84451e94dd0c06203eef9832' 2016-09-23 18:02:41 -04:00
msgping_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
msgping.go travis: enable gometalinter (#603) 2017-03-08 15:44:15 -05:00
msgpong_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
msgpong.go travis: enable gometalinter (#603) 2017-03-08 15:44:15 -05:00
msgreject_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
msgreject.go multi: Upstream chainhash abstraction sync 2016-11-16 12:48:40 -06:00
msgsendheaders_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
msgsendheaders.go wire: Implement sendheaders command 2016-05-27 23:07:05 -05:00
msgtx_test.go multi: Separate tx serialization type from version. 2017-08-07 14:10:27 -05:00
msgtx.go blockchain: Implement enforced relative seq locks. 2017-09-21 15:58:36 -05:00
msgverack_test.go wire: Consolidate tests into the wire pkg. 2016-11-14 15:29:40 -06:00
msgverack.go blockchain: Rework to use new db interface. 2016-08-18 15:42:18 -04:00
msgversion_test.go wire: Lower MaxUserAgentLen to 256. 2017-03-18 01:06:28 -05:00
msgversion.go multi: Error descriptions should be lowercase 2017-08-15 14:36:51 -05:00
netaddress_test.go Preallocate the exact number of bytes if known. 2017-03-20 21:46:33 -05:00
netaddress.go wire: Treat NetAddress more like immutable 2017-03-24 17:07:28 -04:00
protocol_test.go Remove variables for testnet v1. 2017-03-16 20:19:07 -04:00
protocol.go wire/peer: Implement feefilter p2p message. 2017-08-01 21:59:11 -05:00
README.md multi: Update markdown in README files to match change in github parser. 2017-05-26 12:06:19 -04:00

wire

Build Status ISC License GoDoc

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.