Commit graph

206 commits

Author SHA1 Message Date
Dave Collins
4b6cd17561 Make RandomUint64 block until entropy is available.
This commit modifies the RandomUint64 function so that rather than
returning an io.ErrShortBuffer when the system does not have enough
entropy available, it now blocks until it does have enough.  This means
that RandomUint64 will now always eventually succeed unless the entropy
source is closed (which only really ever happens when the operating system
is shutting down).

The tests have also been updated for the change in semantics to maintain
100% coverage.

Closes #23.
2014-11-14 12:17:18 -06:00
Jonathan Gillham
a0fe3822fc Fixed minTxInPayload documentation. 2014-10-01 18:14:42 +01:00
Jonathan Gillham
ccee51a0be Changed TxIn.PreviousOutpoint to TxIn.PreviousOutPoint for consistency. 2014-10-01 13:43:37 +01:00
Dave Collins
8733b9c8df Bump default user agent to 0.2.0. 2014-09-20 12:45:18 -05:00
Josh Rickmar
0127b3aafe Make ErrHashStrSize slightly more descriptive.
Add the word 'string' to the error string so if this is printed, it
will be clearer that it was a hash string decode that failed (as
opposed to other errors creating a ShaHash from a byte slice) and
change 'chars' to 'bytes', since the string length is measured in
bytes, not UTF-8 code points.

ok @davecgh
2014-09-12 08:44:15 -05:00
Dave Collins
225248d283 Use byte literals in tests to make go vet happy.
The go vet command complains about untagged struct initializers when
defining a ShaHash directly.  This seems to be a limitation where go vet
does not exclude the warning for types which are a constant size byte array
like it does for normal constant size byte array definition.

This commit simply modifies the tests to use a constant definition cast to a
ShaHash to overcome the limitation of go vet.
2014-09-05 13:58:40 -05:00
Dave Collins
4deb922c9d Export command constants.
Although it is possible to get the command name for each msg type by
creating an instances of the type and calling the Command method against
it, it's slightly more efficient to simply allows callers to have direct
access to the exported constants.

This is currently really useful for the reject message since callers need
to be able to examine the command type to determine whether or not the
hash field needs to be included.
2014-07-14 10:38:29 -05:00
Dave Collins
7ee3a2220b Use a more specific license adjective. 2014-07-13 12:15:50 -05:00
Dave Collins
dd70618cc1 Export CommandSize constant.
This commit exports the CommandSize constant to provide callers with the
ability to know the size of the command field in a bitcoin message header.
2014-07-12 17:35:14 -05:00
Dave Collins
3a1009529f goimports -w . 2014-07-02 19:43:33 -05:00
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