Commit Graph

4349 Commits

Author SHA1 Message Date
Andreas Schildbach
5bb1e123b6 VersionMessage: serialize negative time values
Surprisingly, version messages serialize time as a `int64_t` (signed 64-bit integer).
2023-03-24 15:26:10 +01:00
Andreas Schildbach
7a0939f7e4 Ping: require a nonce value
Pings without nonce are not protocol compliant since version 60001.
Our minimum version is at 70000.
2023-03-24 13:16:34 +01:00
Sean Gilligan
42eb791d39 SeedPeers: use ByteBuffer directly for address conversion
IPv4 addresses in `int` form don't care about signedness, and we
won't do math on them.
2023-03-24 11:31:26 +01:00
Andreas Schildbach
94bdbc29a0 UTXO, TransactionOutputChanges: remove unused serialization code
This was left over when we removed `UTXOsMessage` in d07e75e2a8.
2023-03-24 10:49:32 +01:00
Andreas Schildbach
dadeb71cb3 VersionMessage: use skipBytes() to skip over a value we don't ware about 2023-03-23 18:47:39 +01:00
Andreas Schildbach
ed57fa08ed Message: make serializer field immutable
This requires two new pass-through constructors, but in return we
get rid of the setter.
2023-03-23 13:34:31 +01:00
Andreas Schildbach
b8f77abe62 SeedPeers: fix conversion of int representation of IPv4 addresses to InetAddress
Usage of `writeUint32LE()` is wrong on two counts:

* `InetAddress.getByAddress()` expects big-endian values, and the method
  above encodes as little-endian.
* Java int is signed, even if the Java hex literal appears unsigned. The
  above method expects unsigned values.

So we add the previously missing `writeInt32BE()` to ByteUtils and use it.
2023-03-23 00:28:37 +01:00
Andreas Schildbach
72370d0ff6 Sha256Hash: add reading from and writing to ByteBuffer
Includes a test.
2023-03-22 22:19:54 +01:00
Andreas Schildbach
30dec4ff7c Message: replace readBytes(1)[0] with readByte() 2023-03-22 21:39:05 +01:00
Andreas Schildbach
89198853d1 TransactionInput: check sequence bounds
It turns out a test is supplying an invalid fake value.
This is now fixed.
2023-03-22 20:32:31 +01:00
Andreas Schildbach
014939410e TransactionOutPoint: check index bounds
It turns out some tests are supplying an invalid fake value for
coinbase transaction. This is now fixed.
2023-03-22 20:23:56 +01:00
Andreas Schildbach
c577303aed TransactionTest: don't use a custom protocol version in a test
The current default will do fine.
2023-03-22 20:06:41 +01:00
Andreas Schildbach
a7bd460865 Message: make params field immutable 2023-03-22 18:46:11 +01:00
Andreas Schildbach
be12c389f3 Message: parse the original ByteBuffer, rather than a copy of it
This has the advantage of using the read position contained in
`ByteBuffer`, so we remove our `offset` and `cursor` fields.
2023-03-22 15:04:05 +01:00
Andreas Schildbach
32837798c1 Message: remove length and optimalEncodingMessageSize fields
This does away with a lot of updating the fields while parsing and
mutating the data.

`getMessageSize()` now returns the length of the data as
serialized by `bitcoinSerializeToStream()`, which can be different
from the length of an original payload.

The default implementation of `getMessageSize()` is not performant
enough for all usecases, to it is overridden in strategic subclasses
to calculate the size in a much more efficient way.
2023-03-22 14:31:26 +01:00
Andreas Schildbach
7fba2d8c6b Message: fold unsafeBitcoinSerialize() into bitcoinSerialize() 2023-03-22 02:21:21 +01:00
Andreas Schildbach
e882322505 Message: fold bitcoinSerialize() into bitcoinSerializeToStream() 2023-03-22 02:06:35 +01:00
Andreas Schildbach
01bf08159d FilteredBlock: simplify parse() a bit by using readBytes() 2023-03-21 23:35:57 +01:00
Sean Gilligan
52b2f63d70 VarIntTest: use JUnitParamsRunner
* Parameterize the existing tests
* Test more functionality
* Test more data variations
2023-03-21 23:15:34 +01:00
Andreas Schildbach
58500cfb46 VarInt: add reading from and writing into ByteBuffer 2023-03-21 23:04:35 +01:00
Andreas Schildbach
0da35c6a0c VarInt: migrate native constructors to static constructors
The old native constructors have been deprecated.
2023-03-21 23:02:41 +01:00
Andreas Schildbach
abdba75019 Block: simplify parse()-parseTransactions() interaction 2023-03-21 22:54:44 +01:00
Andreas Schildbach
9d9cadd59c Message: in constructors wrap payload bytes and offset into a ByteBuffer
This reduces arguments of constructors and does away with
a couple of constructor variants.
2023-03-21 17:55:32 +01:00
Andreas Schildbach
7303043f37 ByteUtils: rename read helpers that read from streams 2023-03-21 17:46:26 +01:00
Andreas Schildbach
d89718aaf6 ByteUtils: add reading from and writing to ByteBuffer
This gets rid of a lot of manual bit shuffling.
2023-03-21 17:32:05 +01:00
Andreas Schildbach
c372a87ea4 ByteUtils: properly document all read and write helpers 2023-03-21 17:18:51 +01:00
Andreas Schildbach
371025f604 ByteUtils: rename helpers that write to byte arrays or streams to write*()
Note the read helpers already follow the `read*()` pattern.
2023-03-21 17:10:17 +01:00
Andreas Schildbach
34d8948be9 Message: do away with retaining of original message payload
This removes all caching of original message payload and "retain mode" in message serializers.

There is some effect on tests. Asserts on the cache status have been removed, rendering some
tests almost pointless. Rather than removing them, comments have been added to explain the
history of these tests. An assert on a large binary blob of a block was dependent on retain mode, likely
due to non-optimal encoding. It had to be disabled.

Note: Some classes still cache individual values, like hashes.
2023-03-20 16:55:29 +01:00
Andreas Schildbach
f516c25524 HexFormat: small performance enhancement for formatHex() 2023-03-20 16:01:29 +01:00
Sean Gilligan
466444ebf7 Peer: calculate lastPing and averagePing when Pong is received
This eliminates the need for locking in the getters.

The fields are marked as volatile so that when written, they will be
flushed to "main memory".
2023-03-20 00:34:00 +01:00
Andreas Schildbach
aab3e32624 Message: remove length arguments from constructors of the hierarchy
There was never a case where `length` wasn't equal to `payload.length`,
which is supplied by the same constructors. This also does away with
a couple of constructor variants.
2023-03-19 23:58:06 +01:00
Andreas Schildbach
8faf61ae0c Peer: fix JavaDoc about number of buckets 2023-03-19 23:17:00 +01:00
Sean Gilligan
78cc85dc1e Peer: replace pingIntervals array with Deque 2023-03-19 22:57:01 +01:00
Sean Gilligan
98c3a14a54 Peer: reimplement getPingTime() and getLastPingTime() in a functional way 2023-03-19 22:51:15 +01:00
Andreas Schildbach
04c72ce003 Peer: migrate pingIntervals field to java.time API 2023-03-19 20:15:00 +01:00
Andreas Schildbach
dce930f487 Message: get rid of the readVarInt(offset) variant
None of the other helpers have this.
2023-03-19 19:09:17 +01:00
Andreas Schildbach
d222efd6de ParseByteCacheTest: rewrite arrayContains() helper
The previous implementation was based on hex strings and was prone
to raising a false positive when matching at an uneven hex position.

This also adds a test.
2023-03-19 18:23:58 +01:00
Andreas Schildbach
979490ad97 UnsafeByteArrayOutputStream: remove this performance optimisation
Migrate all usages of `UnsafeByteArrayOutputStream` to `ByteArrayOutputStream`.
2023-03-19 17:55:05 +01:00
Andreas Schildbach
e37b2ad78c Preconditions: replace Guava checkArgument() and checkState() for the entire codebase
This also fixes many precondition messages, and rewrites two cases
of `checkPositionIndex()` and `checkElementIndex()`.
2023-03-19 17:48:27 +01:00
Andreas Schildbach
12b78a6d9b ParseByteCacheTest: parse test data using parseHex()
Gets rid of Guava BaseEncoding.
2023-03-19 16:59:59 +01:00
Andreas Schildbach
91c706ca33 TransactionTest: make testOptimalEncodingMessageSize() a more meaningful 2023-03-19 16:44:01 +01:00
Andreas Schildbach
d3ba6c03ec Message: check that payload is retained in retain mode 2023-03-19 14:36:40 +01:00
Andreas Schildbach
43123396c0 ParseByteCacheTest: rename variable serializer from bs
The previous name was just too confusing in company with "bos"
and the like.
2023-03-19 11:19:32 +01:00
Sean Gilligan
0f216d328f Block, BitcoinNetwork, BitcoinNetworkParams, KeyChainGroupTest: use _000 in constants to improve readability 2023-03-18 21:00:18 +01:00
Andreas Schildbach
2061555afa WatchMempool: migrate class to java.time API 2023-03-18 20:57:44 +01:00
Andreas Schildbach
06e031a51e LockTime: make native constructors private 2023-03-18 20:33:38 +01:00
Andreas Schildbach
e11fa0686f Transaction: rename estimateUnlockTime() method from estimateLockTimeInstant() 2023-03-18 11:36:47 +01:00
Andreas Schildbach
178fc0ab8f Script: rename creationTime() method from getCreationTime() 2023-03-18 11:29:07 +01:00
Andreas Schildbach
b309308076 EncryptableItem: rename creationTime() method from getCreationTime() 2023-03-18 11:23:05 +01:00
Andreas Schildbach
45f87d8339 PeerGroup: rename fastCatchupTime() method from getFastCatchupTime() 2023-03-18 11:16:31 +01:00