Better RawNewtorkMessage deserealization from IO stream#231
Better RawNewtorkMessage deserealization from IO stream#231dongcarl merged 27 commits intorust-bitcoin:masterfrom
Conversation
…ity out of RawNetworkMessage scope as suggested at rust-bitcoin#231 (comment)
|
@tamasblummer I have commented on the reason why it's impossible to implement the suggestion - see the comments in the code above pls |
|
How about this: b8bd1d9 |
|
@tamasblummer this looks very nice, I do agree with the solution and that |
|
Then please pick the commit b8bd1d9 into your branch and squash all commits. |
|
There is no such functionality on GitHub - I just can't pick this commit since it is not a part of any existing branch - neither via GitHub web interface or from a command line |
|
you do not do it in github but on your own git. |
|
may work pull -ing my branch into yours. like if not: git checkout master |
|
@tamasblummer done, thank you. The problem was that I can't reference your changes, but with the given remote branch name (dr-orlovsky-network-streaming) it all went smoothly. |
|
now please squash all commits into one and force push to have a review by someone else too. |
c727897 to
c960679
Compare
|
You also squashed in changes of master since your branch started, your branch should have been rebased before squashing, sorry for not warning. Its part of this business to get the workflow of contributing to OS right. |
|
actually the problem is you merged in master just before squashing. |
|
If you still have the history then revert that merge of master into your branch and squash again. |
3562b49 to
07eabf0
Compare
commit 3562b494f3e25e6a88e5c696d150e4a74948dbd5
Author: Tamas Blummer <tamas.blummer@gmail.com>
Date: Thu Feb 7 03:37:49 2019 +0100
return a single message
commit f509fc1
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Tue Feb 5 03:27:10 2019 +0200
Verbose comments explaining the workflow for stream parsing in `StreamReader`
commit ff85da8
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Mon Feb 4 03:37:29 2019 +0200
Fixed tests failure in StreamReader
caused by parallel-runned tests opening two simultaneuos TCP sockets on the same port
commit 50bb68d
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Mon Feb 4 02:31:58 2019 +0200
Additional complex unit test for StreamReader from real-world case of Bitcoin Core communications
grabbed with Wireshark
commit d590101
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Mon Feb 4 02:03:56 2019 +0200
Improved & more detailed unit tests for StreamReader
using both file & TcpStream IO, covering private methods
commit e68c186
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Sun Feb 3 23:04:14 2019 +0200
Making tests passing
commit 4ed9fd8
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Sun Feb 3 22:31:06 2019 +0200
Initial unit tests for new StreamReader
commit 150f00a
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Sun Feb 3 22:29:57 2019 +0200
Fixing compuler warning on the order of trait methods implementation
commit 27f8ed7
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Sun Feb 3 22:29:30 2019 +0200
Moving StreamReader to a separate file and abstracting its functionality out of RawNetworkMessage scope
as suggested at rust-bitcoin#231 (comment)
commit cd1a170
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Sun Feb 3 18:38:13 2019 +0200
Fixing rust-bitcoin#231 (comment)
commit 48021db
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Sat Feb 2 21:17:53 2019 +0200
Configuring network::message::RawNetworkMessage.from_stream buffer size and iterations with special struct
commit 4426a14
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Sat Feb 2 20:42:33 2019 +0200
Improved consensus.encode.deserealize_partial function signature
Now function communicateds back to the caller the amount of consumed data by adding Cursor part to the Result value tuple – instead of using &mut argument.
This have led to simplified and more straightforward logic of the function and its callers.
commit f5587cd
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Mon Jan 28 03:23:22 2019 +0200
Documented `deserialize_partial` function
commit 4d464fa
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Mon Jan 28 02:59:02 2019 +0200
Fixed error reporting on incomplete message deserealization from a stream
commit 365facc
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Mon Jan 28 02:35:57 2019 +0200
Stream reading unit test updated with more complex case
commit 34a00e8
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Mon Jan 28 02:35:34 2019 +0200
Improved stream reading algorithm
commit 4410af8
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Mon Jan 28 01:54:25 2019 +0200
Stream deserealization unit test case
commit d3afdc4
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Mon Jan 28 01:54:02 2019 +0200
Generalizing stream deserialization (from TcpStream to any IO stream)
commit 28a1ffa
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Mon Jan 28 01:29:38 2019 +0200
Improving buffering while deserializing from TcpStream
commit e382886
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Mon Jan 28 01:23:52 2019 +0200
Trying reading TcpStream with BufRead
commit 6abbd15
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Mon Jan 28 00:31:12 2019 +0200
Hiding network::Error conversion trait from docs
commit 940d63d
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Sun Jan 27 23:52:16 2019 +0200
Support for automatic IO errors in ? syntax
commit afcc7c8
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Sun Jan 27 23:10:05 2019 +0200
Function that parses TcpStream and creates RawNetworkMessage out of it: `RawNetworkMessage::from_tcpstream`
commit e6ee7e5
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Sun Jan 27 19:44:12 2019 +0200
Improved unit tests for message peyload deserialization
commit 83acf9a
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Sun Jan 27 19:39:03 2019 +0200
Desearilizing partial message content, which will be required lately for reading TcpStream partial content
commit 4a16bcc
Author: Dr. Maxim Orlovsky <dr.orlovsky@gmail.com>
Date: Sun Jan 27 19:37:49 2019 +0200
Support for debug mode formattin in RawNetworkMessage
|
I didn't have the history, but I was able to reconstruct it and get rid of all merges from origin/master - and then squashed in again. Pls check if everything is correct for now |
dongcarl
left a comment
There was a problem hiding this comment.
I'm inclined to approve this change, especially because of the illustrative tests. Thank you for your contribution! Just need to resolve the conflict and I'd be more than happy to reACK.
Now function communicateds back to the caller the amount of consumed data by adding Cursor part to the Result value tuple – instead of using &mut argument. This have led to simplified and more straightforward logic of the function and its callers.
…ze and iterations with special struct
…ity out of RawNetworkMessage scope as suggested at rust-bitcoin#231 (comment)
using both file & TcpStream IO, covering private methods
… Bitcoin Core communications grabbed with Wireshark
caused by parallel-runned tests opening two simultaneuos TCP sockets on the same port
… TCP stream reader
22c7d1e to
3dd0b8f
Compare
|
@dongcarl rebased. Not sure if this is the thing you have asked me for, so pls let me know if I've done something wrong |
|
This looks good. It does no longer contain changes from master. It would be perfect if squashed. |
|
Since this was already reviewed I approve it. Next time please do it like here (rebase + squash). |
|
@dongcarl please "Squash and merge" if ok. |
newtype implementation of opcodes::All Removes unsafety when converting u8 -> All safe implementation of All -> Ordinary squashme: work around lack of associated constants make opcode PR work with 1.14.0 add some opcode tests Fix indentation in opcodes.rs Fix comment on transaction version Internalize unnecessarily exported macros Bump secp to 0.12 Replace slow hex decoding function with optimized version Fixes rust-bitcoin#207. Remove unused Pair iterator and util::iter module Add feature gated hex decode benchmark bump version to 0.16 Bump rustc version to 1.22.0 it is annoying to have a difference between debug and print for hash Fix typos Run cargo bench on rustc nightly in travis, remote useless move Add bitcoin_hashes dependency, rename some features Because features and dependencies share the same namespace, and we want to pass down the optional dependence on serde to bitcoin_hashes, we need to rename the feature to something other than serde. Right now only features can be passed down to dependencies. Note that we could have also renamed the dependency to something like serde-dep and kept the same feature name, however, dependency renaming has only been available since cargo 0.27.0 Features that represent optional dependencies have been prefixed with 'use-'. The travis file has also been modified to conform to this change. Implement En/Decodable for sha256d::Hash Convert codebase from util::hash to bitcoin_hashes Also replace unsafe transmute with call to read_u64_into Remove code deprecated by bitcoin_hashes from util::hash Remove unused internal macro Remove fuzz_util module Not needed anymore as the bitcoin_hashes crate handles this. Remove rust-crypto dependency We no longer need rust-crypto after integrating bitcoin_hashes. Fix typos and clarify some comment in blockdata, block, address (rust-bitcoin#230) Remove Address::p2pk There is no address format for p2pk. add BIP157 (Client Side Block Filtering) Messages (rust-bitcoin#225) * add BIP57 (Client Side Block Filtering) Messages * rabased after rust-bitcoin#215 Cleanup util::privkey in preparation for PublicKey - Rename privkey::PrivKey to privkey::PrivateKey - Remove unnecessary methods for privkey::PrivateKey - Modify tests to work with above changes Add PublicKey struct encapsulating compressedness - Move util::privkey to util::key - Add PublicKey struct to util::key - Implement de/serialization methods for util::key::PublicKey Integrate newly-added PublicKey with Address - Switch util::address::Payload::Pubkey variant to wrap util::key::PublicKey - Switch util::address::Address::p*k* constructors to use util::key::PublicKey - Fix tests for aforementioned switch - Add convenience methods for util::key::PublicKey to util::key::PrivateKey conversion - Switch BIP143 tests to use util::key::PublicKey key: Reword and clarify comments key: Use correct error for decoding This change also moves the secp256k1::Error wrapper from util::Error to consensus::encode::Error, since we do not use it anywhere else. We can add it back to util::Error once we have instances of secp256k1::Error that are not related to consensus::encode. Extract travis testing into locally-runnable script Extract the Script assembly creator from fmt::Debug Added contributing part to README point to IRC bip32: ChildNumber constructors return Result They can produce an error if the index is out of range. bip32: Introduce DerivationPath type Implements Display and FromStr for easy usage with serialized types. bip32: Change test vectors to use DerivationPath bip32: Add additional methods and traits to DerivationPath - From<&[ChildNumber]> (cloning) - AsRef<[ChildNumber]> - std::iter::FromIterator<ChildNumber> - std::iter::IntoIterator<ChildNumber> - std::ops::Index (returning &[ChildNumber]) Also add two methods: - child(&self, ChildNumber) -> DerivationPath - into_child(self, ChildNumber) -> DerivationPath Implement Witness commitment check for Block. Remove MerkleRoot implementations for types implementing BitcoinHash as it is misleading. MerkleRoot is defined instead for a Block. Forbid unsafe code Remove extraneous clones in consensus::params Remove unused Option en/decoding Better RawNewtorkMessage deserealization from IO stream (rust-bitcoin#231) Follow-up to rust-bitcoin#229 While working with remote peers over the network it is required to deserealize RawNetworkMessage from `TCPStream` to read the incoming messages. These messages can be partial – or one TCP packet can contain few of them. To make the library usable for such use cases, I have implemented the required functionality and covered it with unit tests. Sample usage: ```rust fn run() -> Result<(), Error> { // Opening stream to the remote bitcoind peer let mut stream = TcpStream::connect(SocketAddr::from(([37, 187, 0, 47], 8333)); let start = SystemTime::now(); // Constructing and sending `version` message to get some messages back from the remote peer let since_the_epoch = start.duration_since(UNIX_EPOCH) .expect("Time went backwards"); let version_msg = message::RawNetworkMessage { magic: constants::Network::Bitcoin.magic(), payload: message::NetworkMessage::Version(message_network::VersionMessage::new( 0, since_the_epoch.as_secs() as i64, address::Address::new(receiver, 0), address::Address::new(receiver, 0), 0, String::from("macx0r"), 0 )) }; stream.write(encode::serialize(&version_msg).as_slice())?; // Receiving incoming messages let mut buffer = vec![]; loop { let result = StreamReader::new(&mut stream, None).read_messages(); if let Err(err) = result { stream.shutdown(Shutdown::Both)?; return Err(Error::DataError(err)) } for msg in result.unwrap() { println!("Received message: {:?}", msg.payload); } } } ``` Sample output is the following: ``` Received message: Version(VersionMessage { version: 70015, services: 1037, timestamp: 1548637162, receiver: Address {services: 0, address: [0, 0, 0, 0, 0, 65535, 23536, 35968], port: 33716}, sender: Address {services: 1037, address: [0, 0, 0, 0, 0, 0, 0, 0], port: 0}, nonce: 1370726880972892633, user_agent: "/Satoshi:0.17.99/", start_height: 560412, relay: true }) Received message: Verack Received message: Alert([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 127, 0, 0, 0, 0, 255, 255, 255, 127, 254, 255, 255, 127, 1, 255, 255, 255, 127, 0, 0, 0, 0, 255, 255, 255, 127, 0, 255, 255, 255, 127, 0, 47, 85, 82, 71, 69, 78, 84, 58, 32, 65, 108, 101, 114, 116, 32, 107, 101, 121, 32, 99, 111, 109, 112, 114, 111, 109, 105, 115, 101, 100, 44, 32, 117, 112, 103, 114, 97, 100, 101, 32, 114, 101, 113, 117, 105, 114, 101, 100, 0]) ``` Working sample code can be found here: https://github.com/dr-orlovsky/bitcoinbigdata-netlistener key: add some missing functionality bip32: replace rust-secp key types with rust-bitcoin key types We continue to support only compressed keys when doing key derivation, but de/serialization of uncompressed keys will now work, and it will be easier/more consistent to implement PSBT on top of this. Add PSBT-specific Error data type - Implement psbt::Error data type - Implement conversion from psbt::Error to util::Error - Create util::psbt module - Create non-public util::psbt::error module Add data types for raw PSBT key-value pairs - Add (en)decoding logic for said data types Add trait for PSBT key-value maps Add PSBT global data key-value map type - Implement psbt::Map trait for psbt::Global - Add converting constructor logic from Transaction for psbt::Global - Add (en)decoding logic for psbt::Global - Always deserialize unsigned_tx as non-witness - Add trait for PSBT (de)serialization - Implement PSBT (de)serialization trait for relevant psbt::Global types - Add macros for consensus::encode-backed PSBT (de)serialization implementations - Add macro for implementing encoding logic for PSBT key-value maps Add PSBT output data key-value map type - Implement psbt::Map trait for psbt::Output - Add (en)decoding logic for psbt::Output - Implement PSBT (de)serialization trait for relevant psbt::Output types - Add macro for merging fields for PSBT key-value maps - Add macro for implementing decoding logic for PSBT key-value maps - Add convenience macro for implementing both encoding and decoding logic for PSBT key-value maps - Add macro for inserting raw PSBT key-value pairs into PSBT key-value maps - Add macro for getting raw PSBT key-value pairs from PSBT key-value maps Add PSBT input data key-value map type - Implement psbt::Map trait for psbt::Input - Add (en)decoding logic for psbt::Input - Implement PSBT (de)serialization trait for relevant psbt::Input types Add Partially Signed Transaction type - Add merging logic for PartiallySignedTransactions - Add (en)decoding logic for PartiallySignedTransaction - Add converting constructor logic from Transaction for PartiallySignedTransaction - Add extracting constructor logic from PartiallySignedTransaction for Transaction Squashed in fixes from stevenroose <stevenroose@gmail.com> - Prevent PSBT::extract_tx from panicking - Make PartiallySignedTransaction fields public Add test vectors from BIP174 specification - Add macro for decoding and unwrapping PartiallySignedTransaction from hex string Add fuzz testing for PartiallySignedTransaction Add copyright notice to PSBT-related files bump version to 0.17.0 add rust-bitcoin#231 to CHANGELOG for 0.17.0 key: implement ToString and FromStr for PublicKey script: add `push_key` function to Builder to allow serializing public keys more easily bump version to 0.17.1 Bump bitcoin-bech32 dependency This makes the Address::Payload::WitnessProgram inner type compatible with rust-lightning-invoice's Fallback::SegWitProgram's inner type. This allows specifying fallbacks from addresses. util::key: Provide to_bytes() methods for key types These are mainly utility methods around the existing way to serialize the key types. util::key add serde de/serialization contracthash: add fixed test vector contracthash: use `PublicKey` and `PrivateKey` types; minor cleanups contracthash: more cleanups bump version to 0.18 Fix nit in CHANGELOG.md bip32: Add DerivationPathIterator and related methods Adds methods - ChildNumber::increment - DerivationPath::children_from - DerivationPath::normal_children - DerivationPath::hardened_children Drop LoneHeaders and just use BlockHeader The protocol has a bug where a 0u8 is pushed at the end of each block header on the wire in headers messages. WHy this bug came about is unrealted and shouldn't impact API design. Implement util::misc::signed_msg_hash() Add slice consensus encode/decode functions and use for short arrays Swap a few more [d]encoders to slice emit/read functions Support sendheaders network message decode Drop some unused/not-needed Encodable impls Speed up Vec<u8> [d]e[n]code operations by dropping the generic Decrease travis-fuzz iterations to fix hangs Two serde quirks from switching dependencies Add Amount and SignedAmount types Add fuzz target for Amount parsing Fix trivial DoS when deserializing messages from the network fuzz: Add fuzzer for RawNetworkMessage. Rename deserialize_raw_network_message to make my afl scripts happy Switch Travis fuzzing to 30 seconds per target from an iter count. Rename BlockHeader::spv_validate to validate_pow Remove confusing mentions of SPV Rename OP_NOP2 and OP_NOP3 to OP_CLTV and OP_CSV Add OutPoint::new() for one-liner construction (rust-bitcoin#285) Remove Decimal and replace strason with serde_json Slightly update README
[descriptors] Expose inner
Follow-up to #229
While working with remote peers over the network it is required to deserealize RawNetworkMessage from
TCPStreamto read the incoming messages. These messages can be partial – or one TCP packet can contain few of them. To make the library usable for such use cases, I have implemented the required functionality and covered it with unit tests.Sample usage:
Sample output is the following:
Working sample code can be found here: https://github.com/dr-orlovsky/bitcoinbigdata-netlistener