Commit Graph

881 Commits

Author SHA1 Message Date
Deirdre Connolly 3fbfffeb44 Remove superfluous zcash_(de)serialize impls 2019-12-05 13:48:42 -05:00
Deirdre Connolly 9a0c2198aa Parse out exact/max possible lengths for filterload filters
Add some unit tests that make sure we obey max values.
2019-12-05 13:48:42 -05:00
Deirdre Connolly 78f0b8aab0 Remove NODE_BLOOM service bit
We do not support bloom filters.
2019-12-05 13:48:42 -05:00
Deirdre Connolly 3c26092b01 Read max of 520 bytes for data fields when parsing filteradd messages 2019-12-05 13:48:42 -05:00
Deirdre Connolly 1b8b4d0fac Encode and decode Filter* messages
Also remove stubs related to MerkleBlock and the catchall case for unknown types since we
are finally matching all variants of the Message enum.
2019-12-05 13:48:42 -05:00
Deirdre Connolly 72def27b78 Add FilterLoad, FilterAdd, FilterClear message variants
Also remove MerkleBlock stub, we are no longer supporting it (zcashd doesn't either).
2019-12-05 13:48:42 -05:00
Deirdre Connolly f5aa5f3794 Add Tweak and Filter types 2019-12-05 13:48:42 -05:00
Henry de Valence 36cd6d6e06 cargo fmt 2019-11-27 23:53:36 -05:00
Henry de Valence f58aaac1ae Privately re-export ErrorSlot, ClientRequest in peer.
This means that all sub-modules of `peer` can import everything they need from
the `peer` module itself, without having to be aware of the internal structure
of their sibling modules.
2019-11-27 23:53:36 -05:00
Henry de Valence ad6525574b Rename PeerConnector -> peer::Connector 2019-11-27 23:53:36 -05:00
Henry de Valence 778e49b127 Rename PeerHandshake -> peer::Handshake 2019-11-27 23:53:36 -05:00
Henry de Valence 9ff0fd90dc Rename ServerState -> State.
There's no need to write `Server` every time because it's only used inside of
the server code, and when the handshake service constructs a Server.
2019-11-27 23:53:36 -05:00
Henry de Valence d1b3e8fe6b Rename PeerServer -> peer::Server 2019-11-27 23:53:36 -05:00
Henry de Valence 77191e62f6 Remove outdated fixup note. 2019-11-27 23:53:36 -05:00
Henry de Valence da78603d3a Rename `PeerClient` to `peer::Client`. 2019-11-27 23:53:36 -05:00
Henry de Valence 4fbc8270a2 Move PeerSet initialization into a submodule. 2019-11-27 05:06:01 -05:00
Henry de Valence 6db852fab2 Refactor protocol into internal, external modules.
This commit just moves things around and patches import paths.
2019-11-27 05:06:01 -05:00
Deirdre Connolly 8a9a5ba29b Revert "Add some simple proptests using the Arbitray trait on Requests and Responses, gated to test only"
This reverts commit 5a123acf56.
2019-11-26 19:35:49 -05:00
Deirdre Connolly 6f52fc7773 Add 'Other' opcode matching when parsing Reject messages 2019-11-26 19:35:49 -05:00
Deirdre Connolly dd042cf4d8 Add some simple proptests using the Arbitray trait on Requests and Responses, gated to test only 2019-11-26 19:35:49 -05:00
Deirdre Connolly 6168cb51d7 Prefixed currently unused error variable with underscore 2019-11-26 19:35:49 -05:00
Deirdre Connolly d78ead4a1a Removed unused trait import 2019-11-26 19:35:49 -05:00
Deirdre Connolly b9c27e5683 Handle Response::Error and send Message::Reject generated from a PeerError::Rejected 2019-11-26 19:35:49 -05:00
Deirdre Connolly f5f1fe9bbc Handle incoming Reject messages when we expect a response 2019-11-26 19:35:49 -05:00
Deirdre Connolly efd37300b9 Impl From trait for Responses from generic Error impls
Also include new Response::Error variant.
2019-11-26 19:35:49 -05:00
Deirdre Connolly 9cdef4acf0 Implement From trait for generic Error impls
Also add 'Other' RejectCode variant.
2019-11-26 19:35:49 -05:00
Deirdre Connolly 49c5265d41 Add Rejected variant to PeerError enum, for now 2019-11-26 19:35:49 -05:00
Deirdre Connolly bae9347f6e Rustfmt 2019-11-26 19:35:49 -05:00
Deirdre Connolly ed77aaacd9 Read and write Reject messages
Also change  field type to  as it's unclear if it actually has
to match the set of enum variants we care about.
2019-11-26 19:35:49 -05:00
Deirdre Connolly 189d89a7fc Handle 'mempool' messages as 'GetMempool' requests
With a 'Transactions' response that gets turned into an 'Inv(Vec<InventoryHash::Tx>)' message.

We don't yet handle a response from our peer for a 'mempool', which will have to be
a more generic 'Inv' type because we might receive transaction hashes we don't know about yet.

Pertains to #26
2019-11-18 15:55:25 -05:00
Deirdre Connolly 98079c9d77 Support Mempool message
This does not yet push requests into services that actually respond with transaction
hashes in our node's mempool, which doesn't exist yet.

Pertains to #26
2019-11-18 15:55:25 -05:00
Henry de Valence e63f8b51b1 Fix default port setting. 2019-11-13 18:47:44 -05:00
Henry de Valence 2ac77ab704 fmt 2019-11-13 18:43:18 -05:00
Henry de Valence d170608c13 Remove outdated note.
The MetaAddr type is used not just in the Bitcoin code so it doesn't make sense
to keep it in the Bitcoin protocol implementation.
2019-11-13 18:43:18 -05:00
Henry de Valence 9a0bffecb8 Sanitize outbound address responses.
This aims to prevent a remote peer from inspecting timings of all messages
received by this node.
2019-11-13 18:43:18 -05:00
Deirdre Connolly 910268a45b Remove to-fix comment 2019-11-12 22:39:47 -05:00
Deirdre Connolly 4d3ab201e6 seed command seems to be functional
Moved SeedService out of the command closure Command currently spawns
a tokio task to DOS the seed service with `Request::GetPeers` every
second.

Pertains to #54
2019-11-12 22:39:47 -05:00
Deirdre Connolly 0ac1b663fe Keep sets of initial peers as Strings in config file 2019-11-12 22:39:47 -05:00
Deirdre Connolly b5bbef5c47 Default init seed nodes based on network choice
And more flushed out but incomplete
2019-11-12 22:39:47 -05:00
Henry de Valence 47ec2e2689 Remove stub discover module. 2019-10-22 19:06:08 -07:00
Henry de Valence c3ec235a5b Suppress unused import warnings. 2019-10-22 19:06:08 -07:00
Henry de Valence ed2ee9d42f Add a PeerConnector wrapper around PeerHandshake 2019-10-22 19:06:08 -07:00
Henry de Valence 9e2678d76c Rename PeerConnector to PeerHandshake.
It's only responsible for doing the handshakes, so it should be named that way,
and then we can have a Connector responsible for actually opening the TCP
connection.
2019-10-22 19:06:08 -07:00
Henry de Valence 121cea610b Unlink peer spans from their creation details. 2019-10-22 19:06:08 -07:00
Henry de Valence 6faebe844c Make zebra-network::Config toml-able.
The toml serializer function we are using -- maybe because of to_string_pretty
(?) barfs on structs that mix ordering of simple values and "tables", so just
keep all the Durations to the end.
2019-10-22 19:06:08 -07:00
Henry de Valence 0833d31ec7 Use a timer to add peers by interval. 2019-10-22 19:06:08 -07:00
Henry de Valence 9a779a639f Add a timeout for peer handshakes. 2019-10-22 19:06:08 -07:00
Henry de Valence 027bdc8465 Rework initial crawler logic.
This splits out the connection handling code into a try_connect closure, which
could be refactored into a Service of its own.

On creation, when we are likely to have very few peers, launch many concurrent
connections to the first few candidates in the initial candidate set, before
continuing to grow the peer set according to demand signals.
2019-10-22 19:06:08 -07:00
Henry de Valence e1a35490af Move the CandidateSet to its own file.
Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>
2019-10-22 19:06:08 -07:00
Henry de Valence 2f3292759f Add an explicit tracing span to each address book.
Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>
2019-10-22 19:06:08 -07:00
Henry de Valence e0e17a4719 Send an initial request for peers on launching crawler.
Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>
2019-10-22 19:06:08 -07:00
Henry de Valence b1832ce593 Initial work to add a crawl-and-dial task.
This responds to peerset demand by connecting to additional peers.

Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>
2019-10-22 19:06:08 -07:00
Henry de Valence 4055eb8889 bugfix: ensure the PeerServer always calls fail_with before exit
This caused a panic in the PeerSet when remote peers disconnected from us.
2019-10-22 17:55:09 -04:00
Deirdre Connolly 32aea0cbf9 Add useful docstrings 2019-10-21 15:55:18 -04:00
Deirdre Connolly 3de34290e6 Construct LIVE_PEER_DURATION from other timeout and interval constants
Use constants::HEARTBEAT_INTERVAL in our ping generator, add a test to check that LIVE_PEER_DURATION
is consistent with the other constants.
2019-10-21 15:55:18 -04:00
Deirdre Connolly 8588c44bcf Add a comment about not cloning the server channel any more than we are 2019-10-21 15:55:18 -04:00
Deirdre Connolly 61a07c67ef Inside tokio::spawn, loop over Iterator stream and send ClientRequest
msgs on the channel instead

Related to #49
2019-10-21 15:55:18 -04:00
Deirdre Connolly e65f5a05ea Broken: I can't seem to return either an impl Future or Result to satisfy for_each 2019-10-21 15:55:18 -04:00
Deirdre Connolly 3548998980 Set server state to Failed if a response to a heartbeat Ping never comes 2019-10-21 15:55:18 -04:00
Deirdre Connolly adffc4239d Partially complete heartbeats to peer 2019-10-21 15:55:18 -04:00
Henry de Valence 62e423bad8 Update zebra-network/src/address_book.rs
Co-Authored-By: Deirdre Connolly <deirdre@zfnd.org>
2019-10-21 14:40:03 -04:00
Henry de Valence a8ef02c826 Refactor AddressBook::update, add contains, get.
This also makes the quadratic `assert_consistency` check run only in
test configs.
2019-10-21 14:40:03 -04:00
Henry de Valence 501db9fcc8 Add AddressBook::is_potentially_connected()
This allows checking whether a SocketAddr could potentially be
connected, based on the contents of the address book.
2019-10-21 14:40:03 -04:00
Henry de Valence fe9cef261d Allow draining AddressBook entries oldest-first. 2019-10-21 14:40:03 -04:00
Henry de Valence 39d38a8647 Rewrite AddressBook to use a BTreeSet.
The previous implementation failed when timestamps were duplicated between
peers, because there was not a 1-1 relationship between timestamps and peers.
2019-10-21 14:40:03 -04:00
Henry de Valence ecd57f43ed Implement Extend and Drain for AddressBook. 2019-10-21 14:40:03 -04:00
Henry de Valence 0bfd57def2 Add iteration functions to `AddressBook`.
The disconnected_peers() function allows us to prevent duplicate
connections without maintaining shared state between the peerset and the
dial-additional-peers task.
2019-10-21 14:40:03 -04:00
Henry de Valence b03a83fa86 Simplify TimestampCollector.
Previously, the TimestampCollector was intended to own the address book
data, so it was intended to be cloneable and hold shared state among all
of its handles.  This is now modeled more directly by an
`Arc<Mutex<AddressBook>>`, so the only functionality left in the
`TimestampCollector` is setting up the inital worker, which is better
called `spawn` than `new`.

This also fixes a problem introduced in the previous commit where the
`TimestampCollector` was dropped, causing the worker task to shut down
early.
2019-10-21 14:40:03 -04:00
Henry de Valence 53be838d51 Extract `TimestampData` to `AddressBook`.
This allows us to hide the `TimestampCollector` and to expose only the
address book data required by the inbound request service.  It also lets
us have a common data structure (the `AddressBook`) for collecting peer
information that can be used to manage information that other peers
report to us.
2019-10-21 14:40:03 -04:00
Henry de Valence 15a698b23c Use MetaAddr in the timestamp collector.
We will need service bits information to report on peer addresses, so we
need to collect it in the timestamp collector.
2019-10-21 14:40:03 -04:00
Henry de Valence ad43a61fb4 Ensure that all types appearing in public types are exported. 2019-10-18 16:11:01 -07:00
Henry de Valence 1f0a7e5e73 Remove Codec::builder doctest.
Doctests can only test public API, so now that the Codec is private, we can't
have a doctest.  Since this test was only a code example (no behaviour test),
there's no value in replacing it with a unit test.
2019-10-18 16:11:01 -07:00
Henry de Valence c7e0d63bed Export the BoxedZebraService type alias.
This gives API consumers a convenient name, and makes the Rustdoc output
significantly cleaner (because `init` can return a `BoxedZebraService`, not a
`Box<dyn ...ManyTypeConstraints.......>`.
2019-10-18 16:11:01 -07:00
Henry de Valence 1c6c4140ed Add a nice comment to zebra-network. 2019-10-18 16:11:01 -07:00
Henry de Valence f0bb2bff77 Do not export zebra-network internals.
Until we finish outbound peer dialing, this still contains
one module that re-exports the `PeerConnector`.
2019-10-18 16:11:01 -07:00
Henry de Valence d4dc4f0d04 Add tracing output on unhandled peer requests 2019-10-18 16:11:01 -07:00
Henry de Valence 54948b07e2 Add a peer listener task. 2019-10-18 16:11:01 -07:00
Henry de Valence 5847b490da Move PeerSet setup logic into a peer_set::init() 2019-10-18 16:11:01 -07:00
Henry de Valence 63cf340ab4 Add fields to zebra-network Config. 2019-10-18 16:11:01 -07:00
Henry de Valence 0a56830eab Ascii-fy the command string in message header trace 2019-10-17 13:22:33 -04:00
Henry de Valence ff27334e81 Make PeerConnector tower::Buffer'able 2019-10-17 09:34:18 -07:00
Henry de Valence db7ac53f3b Add a Mutex<HashSet<Nonce>> to detect self-conns. 2019-10-17 09:34:18 -07:00
Henry de Valence ed335e68f4 Remove outdated comment
Now that the `PeerConnector` handles both incoming and outgoing
handshakes, determining the next peer address is definitely out of scope
-- it takes a pre-existing tcp connection.
2019-10-17 09:34:18 -07:00
Henry de Valence 31651cf87c Fix copy-paste bug in Codec
Co-authored-by: George Tankersley <george@zfnd.org>
2019-10-17 09:34:18 -07:00
Henry de Valence 8a1aa71736 Modify PeerConnector to also handle inbound conns.
Because the Bitcoin handshake is symmetric, we can reuse the same logic
for both incoming and outgoing connections.
2019-10-17 09:34:18 -07:00
Deirdre Connolly 980ce4593b Fix merge artifacts in Cargo.toml 2019-10-17 09:33:10 -07:00
Deirdre Connolly 0db69addec Add custom Debug impl for DecodeState
Relates to #63
2019-10-17 09:33:10 -07:00
Deirdre Connolly 051dc2f039 Implement custom Debug for Magic types
Related to #63
2019-10-17 09:33:10 -07:00
Henry de Valence f6e62b0f5e Remove failure from zebra-chain, zebra-network.
Failure uses a distinct Fail trait rather than the standard library's
Error trait, which causes a lot of interoperability problems with tower
and other Error-using crates.  Since failure was created, the standard
library's Error trait was improved, and its conveniences are now
available without the custom Fail trait using `thiserror` (for easy
error derives) and `anyhow` (for a better boxed Error).
2019-10-16 13:16:52 -04:00
Deirdre Connolly 199038e6b8 Rename bound vars in match arms for PeerServer state machine
Co-Authored-By: Henry de Valence <hdevalence@hdevalence.ca>
2019-10-15 14:49:11 -04:00
Henry de Valence fc872ea03f Remove async-await feature of futures. 2019-10-15 14:49:11 -04:00
Henry de Valence 373a8fbcfd Refactor PeerServer event loop to avoid select! 2019-10-15 14:49:11 -04:00
Henry de Valence 16f51e4d48 Add a timeout to the `PeerServer` event loop.
I think this code could be cleaned up significantly (e.g., removing the
other use of select!) but that's potentially a larger change than this
PR.
2019-10-15 14:49:11 -04:00
Henry de Valence b45efbdaf2
Add peer count data to TimestampData::update trace (#66)
* Add peer count data to TimestampData::update trace

* Update docstring typo
2019-10-11 12:41:37 -07:00
Henry de Valence ae1a164ff8
Beginning of peerset implementation. (#62)
* Don't expose submodules of zebra_network::peer.

* PeerSet, PeerDiscover stubs.

Co-authored-by: Deirdre Connolly <deirdre@zfnd.org>

* Initial work on PeerSet.

This is adapted from the MIT-licensed tower-balance implementation.

* Use PeerSet in the connect stub.
2019-10-10 18:15:24 -07:00
Deirdre Connolly dc18e8f24c Remove .expect()s for block and transaction, they might fail for writer reasons 2019-10-09 22:25:37 -04:00
Deirdre Connolly 4ec7590b42 Add placeholders for remaining messages in write_body 2019-10-09 22:25:37 -04:00
Deirdre Connolly d8986098c6 Add write_body implementations for GetData, NotFound, and a placeholder for Reject 2019-10-09 22:25:37 -04:00
Deirdre Connolly 483d6584f9 Add Block msg reader and writers, and Tx msg writer 2019-10-09 22:25:37 -04:00
Deirdre Connolly 8dc9181610 Fill out write_body for GetBlocks, GetHeaders, Headers messages 2019-10-09 22:25:37 -04:00
Deirdre Connolly a0302a5507 Implement read_getaddr 2019-10-09 22:25:37 -04:00
Deirdre Connolly cfbe8dfdbf Add comments about the 500 blocks /2000 headers max numbers are speculative based on Bitcoin docs 2019-10-09 22:25:37 -04:00
Deirdre Connolly 5acdaa6d2f Remove some defunct XXXs 2019-10-09 22:25:37 -04:00
Deirdre Connolly dd1b9166b0 Implement Zcash(De)Serialize for BlockHeaderHash, use general read_list for getheaders/headers 2019-10-09 22:25:37 -04:00
Deirdre Connolly eed69063f6 Add read_list to ReadZcashExt 2019-10-09 22:25:37 -04:00
Deirdre Connolly d470dd9709 Parse messages 2019-10-09 22:25:37 -04:00
Deirdre Connolly b991c413cd Parse GetBlock messages 2019-10-09 22:25:37 -04:00
Deirdre Connolly 7632d5b8cc Abstract the common case of a message with a Vec<InventoryHash> 2019-10-09 22:25:37 -04:00
Deirdre Connolly 9699ef2fa1 Codec::read_tx() 2019-10-09 22:25:37 -04:00
Deirdre Connolly 565dc92afe Support a response message 2019-10-09 22:25:37 -04:00
Henry de Valence 8a3b4f48fc Move all git dependencies to published crates. (#58)
The earlier issues with version resolution incompatibilities are now
resolved, and all the upstream changes we made to tracing are released.
2019-10-09 00:10:01 -04:00
Henry de Valence 6f79f2843d Use emojis in user-agent to detect Unicode support. 2019-10-08 23:34:16 -04:00
Henry de Valence f284f6d6cf Tweak debug output in PeerConnector handshake. 2019-10-08 23:34:16 -04:00
Henry de Valence fb2b502eb9 Add a `Config` struct to zebra-network.
This struct is pulled into the main abscissa config as a subsection.
2019-10-08 23:34:16 -04:00
Henry de Valence 1266653be2
Handle error conversions properly. (#56)
This adds a type alias, BoxedStdError, for a boxed std::error::Error
trait object, and uses it in the where bounds for the generic service
code.  In the future, we may want to standardize on using
std::error::Error exclusively, but we would then possibly lose out on
backtrace information.
2019-10-08 13:49:12 -07:00
Henry de Valence 92dc7a5ea1
Fix authorship, license information. (#55)
* Fix authorship, license information.

I *thought* I had done a sed pass over the Cargo defaults when doing
repository initialization, but I guess I missed it or something.
Anyways, fixed now.
2019-10-08 09:25:59 -07:00
Henry de Valence ed608f7231
Initial tower-based peer implementation. (#17)
Add a tower-based peer implementation.  

Tower provides middleware for request-response oriented protocols, while Bitcoin/Zcash just send messages which could be interpreted either as requests or responses, depending on context.  To bridge this mismatch we define our own internal request/response protocol, and implement a per-peer event loop that scans incoming messages and interprets them either as requests from the remote peer to our node, or as responses to requests we made previously.  This is performed by the `PeerService` task, and a corresponding `PeerClient: tower::Service` can send it requests.  These tasks are themselves created by a `PeerConnector: tower::Service` which dials a remote peer and performs a handshake.
2019-10-07 15:36:16 -07:00
Henry de Valence f5dca597dd Replace PeerServices(u64) with a bitflags struct.
This gives considerably better ergonomics.
2019-10-01 01:07:56 -04:00
Henry de Valence 9603a29399 Rename `Services` to `PeerServices`.
This field is called `services` in Bitcoin and Zcash, but because we use
that word internally for other purposes, calling it `PeerServices`
disambiguates the meaning to "the services advertised by the peer",
rather than, e.g., a `tower::Service`.
2019-10-01 01:07:56 -04:00
Deirdre Connolly 2739970113 Fill out TransactionHash and rename
Resolves #35
2019-09-27 19:22:34 -07:00
Deirdre Connolly b21b09bf8e Moved stub TxHash into zebra_chain::transaction 2019-09-27 19:22:34 -07:00
Deirdre Connolly 29591df47e Use the BlockHeaderHash from zebra-chain in the Inv message parsing 2019-09-27 19:22:34 -07:00
Henry de Valence 0a85be285d Add addr, getaddr serialization. 2019-09-27 20:41:45 -04:00
Henry de Valence b426630613 Clarify comment in decoder state. 2019-09-27 20:41:45 -04:00
Henry de Valence 422c783a47 Rename Message::Inventory -> Message::Inv
I don't feel super strongly about this change, so I'm happy to drop it,
but it makes the parsing match statements line up nicely and aligns
naming with the naming used in Bitcoin.
2019-09-27 20:41:45 -04:00
Henry de Valence 958fca8e68 Parse inv messages, refactor inventory vectors.
This removes the inventory vector structs from `zebra-chain` (as they
are really part of the network protocol) and refactors them into a
single `InventoryHash` type.  This corresponds to Bitcoin's "inventory
vector" but with a different, better name (it's not a vector, it's just
a typed hash of some other item).
2019-09-27 20:41:45 -04:00
Henry de Valence ab06750db3 zebra-network: move types -> protocol::types
These types are used for protocol messages, so it makes more sense to
keep them scoped with the protocol handling, rather than other
networking logic.
2019-09-27 20:41:45 -04:00
Deirdre Connolly 35f03dc55d Make Message.Block just point at a Block type, in codec call block.zcash_serialize() 2019-09-26 23:41:25 -04:00
Deirdre Connolly 3f2a1b4f2c Move around MerkleTree* structs 2019-09-26 23:41:25 -04:00
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 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
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 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 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 3032da8b1b Remove defunct and dead try_read 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
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
Henry de Valence 50f749a817 Try writing message headers. 2019-09-18 17:32:06 -04:00
Henry de Valence cf63f00171 Add a Magic type for network magics. 2019-09-18 17:32:06 -04:00
Henry de Valence b9af047a09 Introduce a `MetaAddr` type replacing `NetworkAddress`.
The `NetworkAddress` type was a `(Services, SocketAddr)` pair as used in the
`version` handshake message, described as the `net_addr` struct in the Bitcoin
wiki protocol documentation.  However, all of the other uses of the `net_addr`
struct are a `(Timestamp, Services, SocketAddr)` pair (where the timestamp is
the last-seen time of the peer), and the timestamp is omitted only during the
`version` messages, which are used only during the handshake, so it seems
better to include the timestamp field and omit it during serialization of
`version` packets.
2019-09-18 17:32:06 -04:00
Henry de Valence 1d0517fe56 Split parts of message.rs into constants.rs, types.rs 2019-09-18 17:32:06 -04:00
Henry de Valence eeb0ab7c43 fixup! Add InventoryType, InventoryVector, and Message::{Inventory, GetData, NotFound} 2019-09-18 17:32:06 -04:00
Deirdre Connolly a2e50833be Add InventoryType, InventoryVector, and Message::{Inventory, GetData, NotFound} 2019-09-18 17:32:06 -04:00
Henry de Valence 7fb71a7a9e Replace Timestamp with chrono::DateTime<Utc> 2019-09-18 17:32:06 -04:00
Deirdre Connolly fa8b5290b5 Add Reject message type and RejectReason enum 2019-09-18 17:32:06 -04:00
Deirdre Connolly a4a21138d0 Update user_agent docstring
Co-authored-by: Henry de Valence <hdevalence@hdevalence.ca>
2019-09-18 17:32:06 -04:00
Deirdre Connolly c39d2c7510 Doc comments for various structures and fields
Co-authored-by: Henry de Valence <hdevalence@hdevalence.ca>
2019-09-18 17:32:06 -04:00
Deirdre Connolly ac0d9732a0 WIP: Version message and various sub structures
Co-authored-by: Henry de Valence <hdevalence@hdevalence.ca>
2019-09-18 17:32:06 -04:00
Henry de Valence 8c2b066885 Add more message variants. 2019-09-18 17:32:06 -04:00
Deirdre Connolly 42412ec2a8 Some line breaks for readability 2019-09-18 17:32:06 -04:00
Henry de Valence 80308f85e1 Add a skeleton enum for network messages. 2019-09-18 17:32:06 -04:00
Henry de Valence ec363d2d41 Create workspace skeleton based on design.md 2019-08-29 14:46:54 -07:00