Commit Graph

1618 Commits

Author SHA1 Message Date
Deirdre Connolly 677d53897f Use Vec<u8> for the equihash solution instead of [u8; 1344] for now 2019-09-26 23:41:25 -04:00
Deirdre Connolly c4547ea806 Derive Clone, Debug, Default, Eq, and PartialEq for new structs 2019-09-26 23:41:25 -04:00
Deirdre Connolly 38015c11a6 Add stubbed-out *NoteCommitmentTree* structs 2019-09-26 23:41:25 -04:00
Deirdre Connolly 90e5ae2f4b Add improved doc comments 2019-09-26 23:41:25 -04:00
Deirdre Connolly cecbb1cc0a Fill out the Block Message type
Should we serialize out `Block` or leave explicit like so? ¯\_(ツ)_/¯
2019-09-26 23:41:25 -04:00
Deirdre Connolly d77dfb2344 Remove unused imports 2019-09-26 23:41:25 -04:00
Deirdre Connolly 0f84a6d38e Implement Zcash(De)serialize traits specifically for MerkleTree<Transaction>
This is a general placeholder for now.
2019-09-26 23:41:25 -04:00
Deirdre Connolly 7340c7d9ce Adding BlockHeader, BlockHash, MerkleRootHash, Sha256dWriter 2019-09-26 23:41:25 -04:00
Deirdre Connolly 00494d4963 Block and BlockHeader types 2019-09-26 23:41:25 -04:00
Henry de Valence fe95ad3824 Suppress unused import warning in ReadZcashExt. 2019-09-25 14:59:47 -07:00
Henry de Valence 48a5054c87 Delete unused variable.
This is no longer required because the body reader methods have access
to the version via the codec state.
2019-09-25 14:59:47 -07:00
Henry de Valence 0196c2c4cd Place header encoding prior to body encoding. 2019-09-25 14:59:47 -07:00
Henry de Valence 94a07b05cc Move HEADER_LEN constant to top of file. 2019-09-25 14:59:47 -07:00
Henry de Valence 28904e01c7 Trace the decoded message in the decoder. 2019-09-25 14:59:47 -07:00
Henry de Valence ea1b60d8e3 Make message body reader fns part of Codec. 2019-09-25 14:59:47 -07:00
Henry de Valence 4e1285b568 Refactor message serialization as a tokio codec.
This provides a significantly cleaner API to consumers, because it
allows using adaptors that convert a TCP stream to a stream of messages,
and potentially allows more efficient message handling.
2019-09-25 14:59:47 -07:00
Henry de Valence 0b1acc50c3 Make a new protocol module with message submodule.
This allows us to organize all of the Bitcoin-Zcash specific parts of
the protocol into a subtree.
2019-09-25 14:59:47 -07:00
Henry de Valence c8a3d47b56 Use tracing::instrument and monitor for messages. 2019-09-23 22:17:12 -04:00
Henry de Valence 15ca12a2f5 Add a `connect` command for testing.
With `./src/zcashd -debug=net -logips=1`:
```
2019-09-19 15:24:38 Added connection to 127.0.0.1:35932 peer=1
2019-09-19 15:24:38 connection from 127.0.0.1:35932 accepted
2019-09-19 15:24:38 socket closed
2019-09-19 15:24:38 disconnecting peer=1
```

With `RUST_LOG="trace"`, `cargo run connect`:
```
Sep 19 08:24:24.530  INFO zebrad::commands::connect: version=Version { version: Version(170007), services: Services(1), timestamp: 2019-09-19T15:24:24.530059300Z, address_recv: (Services(1), V4(127.0.0.1:8233)), address_from: (Services(1), V4(127.0.0.1:9000)), nonce: Nonce(1), user_agent: "Zebra Connect", start_height: BlockHeight(0), relay: false }
Sep 19 08:24:24.530 TRACE Task::run: tokio_executor::threadpool::task: state=Running
Sep 19 08:24:24.530 DEBUG Task::run: tokio_net::driver::reactor: adding I/O source token=0
Sep 19 08:24:24.530  INFO zebrad::commands::connect: version_bytes="24e9276476657273696f6e000000000063000000cb30ab03179802000100000000000000a89d835d00000000010000000000000000000000000000000000ffff7f0000012029010000000000000000000000000000000000ffff7f000001232801000000000000000d5a6562726120436f6e6e6563740000000000"
Sep 19 08:24:24.530 TRACE Task::run: log: registering with poller
```
2019-09-22 17:27:08 -04:00
Henry de Valence df7801d623 Temporarily change hyper to git version.
This avoids some crate selection conflicts, but makes some futures
extension traits fall out of order?  This seems to be an issue with
`pin-project` resolved in the git branch of `hyper` (but not yet
released).
2019-09-22 17:27:08 -04:00
Henry de Valence 976a81e7b9 Use failure::Error in zebra_network::message.
This gives backtraces and more ergonomic errors, at the cost of possible
allocations (which we do here anyways).
2019-09-22 17:06:07 -04:00
Henry de Valence 9fe8f22a84 Add verack, ping, pong serialization. 2019-09-22 17:06:07 -04:00
Henry de Valence 3b51056857 Change Message serialization to async send/recv.
Because we want to be able to read messages from async sources (like a
tcp socket), we need to have at least async header parsing logic, so
that we can correctly determine how many bytes to await to parse each
message, so it makes sense for the entire message parsing functions
to be async.

Because we perform message serialization into async readers and writers
in the context of sending messages over the network, code using these
functions is more clear with these names.
2019-09-22 17:06:07 -04:00
Henry de Valence fa4ba442eb Add a MIN_VERSION constant to zebra_network.
When we perform a handshake with a remote peer, we need to encode the
version messages with a particular network version before we find out
what the remote peer's version preference is.  So in addition to having
a CURRENT_VERSION constant (which represents our preference), we need to
have a MIN_VERSION during the handshake (and later to determine whether
we'll talk to the peer at all).
2019-09-22 17:06:07 -04:00
Henry de Valence 0cb439301a Add a USER_AGENT constant to zebra_network. 2019-09-22 17:06:07 -04:00
Henry de Valence 252dce1bad Use rand::thread_rng to impl Default for Nonce. 2019-09-22 17:06:07 -04:00
Henry de Valence a64a051276 Clean tracing_subscriber deprecation warnings. 2019-09-20 16:02:55 -04:00
Henry de Valence b3e094bc40 Clean parsing via ReadZcashExt read-array helpers.
This adds convenience methods to `ReadZcashExt` that read 4 and 12 byte
fixed size arrays from the `Reader`, making the actual parsing code more
legible.

Closes #10.
2019-09-19 12:53:16 -04:00
Henry de Valence f45bbeba98
Replace `Version` `MetaAddr` with `(Services, SocketAddr)`. (#12)
* Replace Version MetaAddr by (Services, SocketAddr).

The version handshake message doesn't include last-seen timestamps for
the address fields, unlike other messages, so instead of modeling the
message data with a `MetaAddr` (which includes a timestamp), we should
just use a tuple.

* Simplify try_read_version implementation.

Because we no longer need to construct fake timestamps for the
`MetaAddr` fields, we don't need to use any of the parsed fields while
parsing later fields, and we can neatly wrap up the entire parsing logic
into a single expression.

* fmt

I didn't have the toolchain-specified `rustfmt` because I was mostly
offline and couldn't download it.
2019-09-19 09:38:02 -07:00
Deirdre Connolly 73740841e1 Move `Transaction` and related types to their own module (#9)
* Move `Transaction` and related types to their own module

Resolves #6

* Fix references to `Transaction` after move
2019-09-19 07:45:37 -07:00
Deirdre Connolly 60fb4f6bb0 Remove InventoryType::MsgCmpctBlock
From BIP-152, which is not implemented by Zcash.
2019-09-18 17:32:06 -04:00
Deirdre Connolly 3032da8b1b Remove defunct and dead try_read 2019-09-18 17:32:06 -04:00
Deirdre Connolly 65632e9f42 Correct comment about pzec bytes::Bytes 2019-09-18 17:32:06 -04:00
Deirdre Connolly 8edbc7b744 Resolve 'warning: unused that must be used' error 2019-09-18 17:32:06 -04:00
Deirdre Connolly 46984cbb27 Add `tx` message, along with `Transaction`, `Transaction(In|Out)put`, and `OutPoint` types 2019-09-18 17:32:06 -04:00
Henry de Valence e0cd099487 Fix type with updated tracing-subscriber
An updated tracing-subscriber version changed one of the public types;
because we hardcode the type instead of being generic over S:
Subscriber, this was actually a breaking change.  As noted in the
comment adjacent to this line, we would rather be generic over S, but
this requires fixing a bug in abscissa's proc-macros, so in the meantime
we hardcode the type.
2019-09-18 17:32:06 -04:00
Deirdre Connolly cc9da18554 Use an Option around optional Reject message data
And replace Timestamp with chrono::DateTime
2019-09-18 17:32:06 -04:00
Henry de Valence adc421f7fe Implement ZcashDeserialize for Message::Version.
This has a test that the serialization implementation round trips
correctly, but is very much a work in progress.  Issues with this code
include:

The deserialization logic for message headers is somewhat ugly because
of a lack of a convenient idiom for reading a few bytes at a time into a
fixed-size array.  Perhaps this could be fixed with an extension trait,
or avoided altogether by using `u32`s instead of `[u8; 4]`, even when
the latter is the "morally correct type".

Deserialization does an extra allocation, copying the body into a
temporary buffer.  This avoids two problems: 1) capping the number of
bytes that can be read by the `Read`er passed into the body parser and
2) allows making two passes over the body data, one to parse it and one
to compute the checksum.

We could avoid making two passes over the body by computing the checksum
simultaneously with the parsing. A convenient way to do this would be to
define a

```
struct ChecksumReader<R: Read> {
    inner: R,
    digest: Sha256,
}

impl<R: Read> Read for ChecksumReader<R> { /* ... */ }
```

and implement `Read` for `ChecksumReader` to forward reads from the
inner reader, copying data into the digest object as it does so.  (It
could also have a maximum length to enforce that we do not read past the
nominal end of the body).

A similar `ChecksumWriter` could be used during serialization, although
because the checksum is at the beginning rather than the end of the
message it does not help avoid an allocation there.  It could also be
generic over a `Digest` implementation, although because we need a
truncated double-SHA256 we would have to write a custom `Digest`
implementation, so this is probably not worthwhile unless we have other
checksum types.

Finally, this does very minimal testing -- just round-trip serialization
on a single message.  It would be good to build in support for
property-based testing, probably using `proptest`; if we could define
generation and shrinking strategies for every component type of every
message, we could do strong randomized testing of the serialization.
2019-09-18 17:32:06 -04:00
Henry de Valence 733d090b9b Add missing derives to newtypes. 2019-09-18 17:32:06 -04:00
Henry de Valence 32cf74db39 Move serialization to zebra-chain, rework traits.
The core serialization logic is now in zebra-chain and consists of two
pairs of traits:

These are analogues of the Serde `Serialize` and `Deserialize` traits,
but explicitly intended for consensus-critical serialization formats.
Thus some struct `Foo` may have derived `Serialize` and `Deserialize`
implementations for (internal) use with Serde, and explicitly-written
`ZcashSerialize` and `ZcashDeserialize` implementations for use in
consensus-critical contexts.  The consensus-critical implementations
provide `zcash`-prefixed `zcash_serialize` and `zcash_deserialize`
methods to make it clear in client contexts that the serialization is
consensus-critical.

These are utility traits, analogous to the `ReadBytesExt` and
`WriteBytesExt` traits provided by `byteorder`.  A generic
implementation is provided for any `io::Read` or `io::Write`, so that
bringing the traits into scope adds additional Zcash-specific traits to
generic readers and writers -- for instance, writing a `u64` in the
Bitcoin "CompactSize" format.
2019-09-18 17:32:06 -04:00
Henry de Valence 78b1aabed0 Deserialize Bitcoin-encoded strings. 2019-09-18 17:32:06 -04:00
Henry de Valence 715ed52617 Deserialize Bitcoin-encoded IP addresses. 2019-09-18 17:32:06 -04:00
Henry de Valence 3c32beb8f0 Add a &'static str error message to ParseError. 2019-09-18 17:32:06 -04:00
Henry de Valence d847dc1356 Start implementing serialization for Version. 2019-09-18 17:32:06 -04:00
Henry de Valence b98e1c7853 Add ZcashSerialization impls for some std types. 2019-09-18 17:32:06 -04:00
Henry de Valence 1f280b7bb8 Make MetaAddr fields public. 2019-09-18 17:32:06 -04:00
Henry de Valence 8a3cabc686 Define Magic newtype as `[u8; 4]`, not `u32`.
The magic value isn't a u32, it's a byte string, so specifying it as
such avoids ambiguity about endianness.
2019-09-18 17:32:06 -04:00
Henry de Valence 00cc1284ae Add a stub ZcashSerialization trait.
There are a few unresolved questions marked in the doc comment for the trait;
we can resolve them later and rework the trait as we map everything out.
2019-09-18 17:32:06 -04:00
Henry de Valence 73cd06b4dc Add derived impls to newtypes 2019-09-18 17:32:06 -04:00
Henry de Valence c3b7dcdfbe Add Read/WriteZcashExt extension traits.
Currently these just have write_compactsize and read_compactsize methods which
allow reading and writing u64s to any `Read` or `Write` implementation using
the Bitcoin "CompactSize" variable integer encoding.

These methods read and write u64s rather than defining a new `CompactSize`
type, because the `CompactSize` is just an encoding detail, not a different
type with any distinct meaning.
2019-09-18 17:32:06 -04:00