Commit graph

196 commits

Author SHA1 Message Date
Dave Collins 5032b07c66 Rename blockHashLen to blockHeaderLen.
The old name no longer makes sense because the hash length is the same as
the header length.
2014-06-29 17:45:46 -05:00
Dave Collins 843e71515a Expose new SerializeSize API for blocks.
This commit adds a new function named SerializeSize to the public API for
MsgBlock which can be used to determine how many bytes the serialized data would
take without having to actually serialize it.  In addition, it makes the
exported BlockVersion an untyped constant as well as changes the block and
tx versions to a signed integer to more closely match the protocol.

Finally, this commit also adds tests for the new function.

The following benchmark shows the difference between using the new
function to get the serialize size for a typical block and serializing
into a temporary buffer and taking the length of it:

Bufffer: BenchmarkBlockSerializeSizeBuffer     200000          27050 ns/op
New:     BenchmarkBlockSerializeSizeNew     100000000             34 ns/op

Closes #19.
2014-06-29 17:43:54 -05:00
Josh Rickmar 7dcb68275f Make MaxMessagePayload an exported constant.
ok @davecgh
2014-06-06 22:33:39 -05:00
Dave Collins ee46a0b108 Use bytes.NewReader for deserialize when possible.
Rather than using bytes.NewBuffer, which is a read/write entity
(io.ReadWriter), use bytes.NewReader which is only a read entitiy
(io.Reader) in all cases where it is possible.  Benchmarking shows it's
slightly faster and it's also technically more accurate since it ensures
the data is read-only.

There are a few cases where bytes.NewBuffer must still be used since a
buffer with a known length is required for those instances.
2014-06-04 23:39:03 -05:00
Dave Collins 2054fa7581 Update doc.go to reflect reality.
This commit updates doc.go to include the new simulation test network
magic constant as well as remove the information about BIP0037 not being
supported since that is no longer true.
2014-06-02 12:35:00 -05:00
Dave Collins 31ee545aa9 Add bitcoin network type for simulation testing. 2014-05-29 12:50:17 -05:00
Dave Collins c4135db728 Move default network ports to btcnet package.
The default network ports are not really part of the wire protocol rather
they are part of a network parameters.  Thus, this commit moves them to
the btcnet package.
2014-05-29 12:45:11 -05:00
Dave Collins dfdd223223 Move genesis blocks to btcnet package.
The genesis blocks are not really part of the wire protocol rather they
are part of a network parameters.  Thus, this commit moves the all of the
gensis blocks and tests to the btcnet package.

Also, create variables in the test package for the mainnet genesis hash,
merkle root, and coinbase transaction for use throughout the tests since
they the exported values are no longer available.
2014-05-28 09:14:38 -05:00
Dave Collins a4978ba4c8 Add tests for the new MsgReject message.
This commit adds a full suite tests for the new reject message added in
protocol version 70002 to bring the overall test coverage of btcwire back
up to 100%.

Closes #9.
2014-05-08 00:17:11 -05:00
Dave Collins f38e5854c1 Bump protocol version to 70002. 2014-05-08 00:17:11 -05:00
Dave Collins c2a1444a88 Add new reject command.
This commit adds the new reject protocol message added to recent versions
of the reference implementation.  This message is intended to be used in
response to messages from a remote peer when it is rejected for some
reason such as blocks being rejected due to not conforming to the chain
rules, transactions double spending inputs, and version messages sent
after they're already sent.

This is work toward issue #9.
2014-05-08 00:17:05 -05:00
Dave Collins f8ec476691 Complete BIP0037 support started by dhill.
- Correct MsgFilterLoad max payload
- Enforce max flag bytes per merkle block
- Improve and finish tests to include testing all error paths
- Add fast paths for BloomUpdateType
- Convert all byte fields to use read/writeVarBytes
- Style and consistency updates
- README.md and doc.go updates

Closes #12.
2014-05-07 23:57:55 -05:00
David Hill cf754d09bf Initial implementation of BIP0037.
Implement filteradd, filterclear, filterload, and merkleblock.
2014-05-07 23:41:39 -05:00
Dave Collins a98f5ca38e Cleanup a few nitpicks with recent alert additions.
- Group the new read/writeVarBytes functions together to be consistent
  with the existing code
- Modify the comments on the new read/writeVarBytes to be a little more
  descriptive and consistent with existing code
- Use "test payload" for field name in the tests for the
  read/writeVarBytes functions which is more accurate
- Remove reserved param from NewAlert since there is no point is having
  the caller deal with a reserved param
- Various comment tweaks for clarity and consistency
- Use camel case for fuction params for consistency
- Move the NewAlert and NewAlertFromPayload functions after the receiver
  definitions for code layout consistency

Closes #11.
2014-05-07 19:41:04 -05:00
Javed Khan bdec7f8abb Implemented alert message serialize/deserialize
* Introduced common methods readVarBytes, writeVarBytes.
* Added type Alert which knows how to deserialize
the serialized payload and also serialize itself back.
* Updated MsgAlert BtcEncode/BtcDecode methods to handle the
new Alert.
* Sane limits are placed on variable length fields like SetCancel
and SetSubVer
2014-05-05 23:12:28 +05:30
Dave Collins 2cc89bbf5b Remove function name from ShaHash error message.
This makes golint happy.
2014-04-21 10:53:22 -05:00
Dave Collins 8857485fa8 Remove note that is no longer true.
The code was updated to automatically handle the transaction count in the
block header without having the additional field some time ago.  This
comment was outdated.
2014-04-20 23:24:56 -05:00
Dave Collins d848bc453b Improve comments on new AddUserAgent function. 2014-04-20 17:44:46 -05:00
Javed Khan 620cbdeb8e Implemented BIP 0014 format for user agent
the new function AddUserAgent adds the user agent to the stack
and formats it as per BIP 0014
e.g: "/btcwire:0.1.4/myclient:1.2.3(optional; comments)/"

the validation on UserAgent has been moved to a new function
validateUserAgent
2014-04-21 02:36:13 +05:30
Dave Collins a9293bd32e Add godoc reference badge to README.md. 2014-04-19 15:46:03 -05:00
Dave Collins 44e34926a7 Remove test coverage report refs from README.md.
This is no longer needed since the repository is now configured to work
with coveralls.io for test coverage reporting.

Closes #17
2014-04-19 14:49:42 -05:00
Dave Collins 755944738a Instruct TravisCI to install coverage report tool. 2014-04-19 14:41:52 -05:00
Dave Collins 6b82da13b4 Setup TravisCI to report cov stats to coveralls.io.
Also add a coveralls.io test coverage badge to the README.
2014-04-19 14:36:16 -05:00
Javed Khan 9130de2de2 add tmp wire file to .gitignore 2014-04-19 15:53:03 +05:30
David Hill 805ce37d31 Use the stack for most known sizes.
By using the stack for known sizes, there is less pressure on the
garbage collector.
2014-04-08 15:55:21 -04:00
Dave Collins f0581b565c Correct two more comments in block bytes breakdown. 2014-04-06 14:14:48 -05:00
Dave Collins 0e1f8fda6d Correct some comments in block bytes breakdown. 2014-04-05 21:00:28 -05:00
Dave Collins 937374c95a Cleanup and finish relay transaction work.
- Coalesce the new bytes into the max message size constant to stay
  consistent
- Correct optional relay tx field handling
- Rename the relay transactions field to DisableRelayTx so the zero value
  of false has the correct default behavior
- Add tests for new bool fast paths in read/writeElement
- Stay consistent with version order in tests
- Add a single entry to TestVersionWire to test the new functionality
  instead of adding a whole new TextVersionRelayTx function.
- Use BIP0037 in tests instead of hard coding 70001
- Nuke XXX that 70001 is different since this is handled now
- Fix and cleanup some comments
- Update test coverage report
2014-03-31 11:35:36 -05:00
David Hill e9a18fb14c Support RelayTx param in ver message for 70001+.
This commit adds support for the version message RelayTx parameter for
protocol version 70001+ as added by BIP0037.
2014-03-30 23:43:18 -05:00
Dave Collins 8721348051 Add tests for optional message fields.
Since fields of a version message after the AddrMe field are optional,
this commit adds a series of tests which ensure parsing version messages
which omit each of the optional fields works properly.
2014-03-30 23:39:28 -05:00
Dave Collins c917899303 Allow optional fields in MsgVersion decode.
This commit modifies the MsgVersion.BtcDecode function to match the
behavior where fields after the first address field (AddrYou) are optional
and only read if the buffer contains remaining bytes.

Unfortunately this means the reader for MsgVersion.BtcDecode must be a
*bytes.Buffer or an error is returned.  This is not an issue for the vast
majority of cases since all of the message reading code which is the main
way messages are read is already using a *bytes.Buffer, however, this
change might affect external callers if they are doing something special
with custom readers.

Fixes #14.
2014-03-30 15:56:52 -05:00
Dave Collins dfb2c149f6 Finish uncompleted comments in fixedIO_test.go. 2014-03-30 12:19:17 -05:00
David Hill b6e6fc25db Preallocate memory for the message header in WriteMessageN and the
serialization buffer in MsgTx's TxSha().

Benchmarking shows this is slightly faster due to avoiding the extra
garbage collection in addition to less peak memory usage.

Before: BenchmarkTxShaOld  500000         5626 ns/op
After:  BenchmarkTxShaNew  500000         5457 ns/op
2014-03-20 14:33:44 -04:00
Dave Collins 2de1b73d12 Add a benchmark for TxSha.
This commit adds a benchmark for the TxSha function of a MsgTx.
2014-03-20 10:46:36 -05:00
Dave Collins 0e1f6a6628 Export MaxBlockHeaderPayload. 2014-03-11 20:22:32 -05:00
Dave Collins e5d15b0fa8 Export MaxVarIntPayload. 2014-03-11 20:09:55 -05:00
Dave Collins b9c21bd518 Export VarIntSerializeSize function.
This commit exports the VarIntSerializeSize function to provide callers
with an easy method to determine how many bytes it would take to serialize
the passed value as a variable length integer.
2014-03-03 18:43:09 -06:00
Dave Collins 1623818c12 Export a new constant for MaxPrevOutIndex.
This commit exports a new constant for the maximum index a previous
outpoint can contain.
2014-02-25 16:00:03 -06:00
Dave Collins 13e0b0e7b9 Limit generated timestamps to one second precision.
This commit changes all cases which generate default timestamps to
time.Now to limit the timestamp to one second precision.  The code which
serializes and deserializes timestamps already does this, but it is useful
to make sure defaults don't exceed the precision of the protocol either.

With this change there is less chance that developers using defaults will
end up with structures that have a higher time precision than what will
ultimately be sent across the wire.
2014-02-24 09:55:02 -06:00
David Hill f6b03bf8a8 gofmt 2014-02-04 16:14:08 -05:00
David Hill d0dfff8292 fix typo in test: use %s for strings 2014-02-04 14:35:38 -05:00
Dave Collins ff783faf53 Add Read/WriteMessageN to return number of bytes.
This commit adds two new funtions named ReadMessageN and WriteMessageN
which return an additional paramter for the number of bytes read or written,
respectively.

It also adds tests to ensure the number of bytes read and written are the
expected values both for successful reads/writes and unsuccessful ones.

Closes #6.
2014-02-03 23:12:43 -06:00
Dave Collins 6672f71d68 Add tests for new Serialize/Deserialize functions. 2014-01-18 21:43:11 -06:00
Dave Collins 06d1236d49 Add Serialize/Deserialize for BlockHeader.
This commit introduces two new functions for Blockheader named Serialize
and Deserialize.  The functions provide a stable mechanism for serializing
and deserializing block headers to and from disk.  The main benefit here
is deserialization of the header since typically only full blocks are
serialized to disk.  Then when a header is needed, only the header portion
of the block is read and deserialized.
2014-01-18 21:42:43 -06:00
Dave Collins d6aea22adb Remove deprecated InvType_* constants.
This closes #13.
2014-01-18 20:53:50 -06:00
Dave Collins 144822d4bf Remove BlockHeader.TxnCount field.
This commit removes the TxnCount field from the BlockHeader type and
updates the tests accordingly.  Note that this change does not affect the
actual wire protocol encoding in any way.

The reason the field has been removed is it really doesn't belong there
even though the wire protocol wiki entry on the official bitcoin wiki
implies it does.  The implication is an artifact from the way the
reference implementation serializes headers (MsgHeaders) messages.  It
includes the transaction count, which is naturally always 0 for headers,
along with every header.  However, in reality, a block header does not
include the transaction count.  This can be evidenced by looking at how a
block hash is calculated.  It is only up to and including the Nonce field
(a total of 80 bytes).

From an API standpoint, having the field as part of the BlockHeader type
results in several odd cases.

For example, the transaction count for MsgBlocks (the only place that
actually has a real transaction count since MsgHeaders does not) is
available by taking the len of the Transactions slice.  As such, having
the extra field in the BlockHeader is really a useless field that could
potentially get out of sync and cause the encode to fail.

Another example is related to deserializing a block header from the
database in order to serve it in response to a getheaders (MsgGetheaders)
request.  If a block header is assumed to have the transaction count as a
part of it, then derserializing a block header not only consumes more than
the 80 bytes that actually comprise the header as stated above, but you
then need to change the transaction count to 0 before sending the headers
(MsgHeaders) message.  So, not only are you reading and deserializing more
bytes than needed, but worse, you generally have to make a copy of it so
you can change the transaction count without busting cached headers.

This is part 1 of #13.
2014-01-18 20:53:20 -06:00
Dave Collins 6c7f45fdb7 Add 2014 to copyright dates. 2014-01-08 23:44:08 -06:00
Dave Collins 2458841855 Add tests for new size hint functions.
This brings the test coverage back up to 100%.
2014-01-08 17:43:18 -06:00
Dave Collins 497aac6d4a Add new funcs to allow size hints on getdata/inv.
This commit adds two new functions named NewMsgGetDataSizeHint and
NewMsgInvSizeHint.  These are intended to allow callers which know in
advance how large the inventory lists will grow the ability to provides
that information when creating the message.  This in turn provides a
mechanism to avoid the need to perform several grow operations of the
backing array when adding large number of inventory vectors.
2014-01-08 17:21:51 -06:00
Dave Collins e64e6f0757 Add tests for new BitcoinNet Stringer. 2014-01-03 08:36:11 -06:00