Commit graph

25 commits

Author SHA1 Message Date
Dave Collins
d3e2947c05 Update btcwire import paths to new location. 2015-01-16 13:54:11 -06:00
Jonathan Gillham
ccee51a0be Changed TxIn.PreviousOutpoint to TxIn.PreviousOutPoint for consistency. 2014-10-01 13:43:37 +01:00
Dave Collins
3a1009529f goimports -w . 2014-07-02 19:43:33 -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
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
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
David Hill
f6b03bf8a8 gofmt 2014-02-04 16:14:08 -05: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
d1edbf1f0b Add tests to exercise intential malicious overflow.
This commit adds several tests which intentionally attempt to feed
malicious data to the various deserialize functions.
2013-10-25 08:57:39 -05:00
Dave Collins
b4b7204a97 Remove BtcDecodeTxLoc since it has been replaced.
BtcDecodeTxLoc is no longer needed since it has been replaced by
DeserializeTxLoc.
2013-08-05 19:18:45 -05:00
Dave Collins
d90740728e Remove protocol version param from BlockSha/Txsha.
Both of these depend on the serialized bytes which are dependent on the
version field in the block/transaction.  They must be independent of the
protocol version so there is no need to require it.
2013-08-05 18:08:57 -05:00
Dave Collins
dea1bac359 Add negative tests for Serialize/Deserialize funcs.
This commit adds tests for the error paths when serializing and
deserializing MsgBlock and MsgTx.
2013-08-05 18:08:40 -05:00
Dave Collins
29eb4d4250 Add tests for new Serialize/Deserialize functions. 2013-08-05 18:08:26 -05:00
Dave Collins
bfbc08beed Allow var defs to infer type from right-hand side.
Found by golint.
2013-07-27 16:10:38 -05:00
Dave Collins
562bde6902 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.
2013-07-27 15:55:06 -05:00
Dave Collins
90bf9e7449 Update tests for corrected max block payload size. 2013-07-27 15:32:36 -05:00
Dave Collins
14a1da417f Enforce max block payload size of 1MB.
This commit changes MsgBlock to enforce a 1MB max payload per the spec.
Previously it was only limited to the max overall message size.  While
here, also enforce max payloads per message type (instead of only the max
overall message payload) when writing messages.
2013-05-16 09:07:04 -05:00
Dave Collins
1ca8015ea0 Add negative tests for MsgBlock tx location decode.
This commit adds tests for the error paths when decoding MsgBlock via the
transaction location decode variant.
2013-05-12 12:48:52 -05:00
Dave Collins
0e033023bf Improve MsgBlock negative tests.
Rather than decoding from a zero-filled buffer, use valid block encoded
test data.
2013-05-12 12:43:01 -05:00
Dave Collins
4e16030fe8 Add tests for MsgBlock transaction location decode. 2013-05-12 11:52:35 -05:00
Dave Collins
953a236e2a Add negative tests for MsgBlock.
This commit adds tests for the error paths when encoded and decoding
MsgBlock.
2013-05-10 09:12:46 -05:00
Dave Collins
135552cd72 Improve MsgBlock tests.
This commit corrects the tests so that the main API functions are tested
against the latest protocol version, but the TxSha and BlockSha functions
are run against the specific protocol version used to encode the test
data.  This will help future proof the tests against protocol changes.
2013-05-09 23:47:12 -05:00
Dave Collins
69b27dd5d3 Initial implementation. 2013-05-08 18:58:29 -05:00