Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to StreamReader #318

Merged
merged 2 commits into from Sep 23, 2019

Conversation

dr-orlovsky
Copy link
Collaborator

@dr-orlovsky dr-orlovsky commented Aug 19, 2019

StreamReader is now generalized to allow:

  • reading not only data from network TcpStream, but any input stream, including files
  • supporting Read, which is more performance-efficient than a raw read
  • parses and returns not just RawMessage, but any serializable data type
  • because of this, next_message function is renamed into read_next
  • embedded stream is now boxed

Since the changes are compatibility-breaking, if the PR will be accepted, the version number has to be bumped accordingly.

Necessary testcase included. You can play as well with this minimalistic test parsing shortened version of the blk00000.dat file: https://github.com/pandoracore/bpdump

Code sample:

    let mut stream_reader = match filename {
        Some(name) => {
            let buf_read: Box<dyn io::Read> = Box::new(io::BufReader::new(File::open(name)?));
            StreamReader::new(buf_read, None)
        },
        None => {
            let stdin: Box<dyn io::Read> = Box::new(io::stdin());
            StreamReader::new(stdin, None)
        },
    };

    let mut blkno = 0;
    loop {
        // Reading magic number
        match stream_reader.read_next::<u32>() {
            Ok(0xD9B4BEF9) => (),
            _ => {
                eprintln!("No magic number found");
                break;
            }
        }
        // Skipping block length
        eprintln!("Magic number ok");
        let _ = stream_reader.read_next::<u32>()?;
        // Reading block
        match stream_reader.read_next::<Block>() {
            Err(err) => {
                eprintln!("{}", err);
                break;
            },
            Ok(block) => {
                eprintln!("* read block no {}, id {}", blkno, block.bitcoin_hash());
                println!("{:#?}", block.header);
                println!("{:#?}", block.txdata[0]);
                blkno += 1;
            },
        }
    }

@stevenroose
Copy link
Collaborator

Can't these two commits be squashed together?

  • Making stream to be a ref in StreamReader
  • Making stream to be a boxed in StreamReader

@dr-orlovsky
Copy link
Collaborator Author

Can't these two commits be squashed together?

done

@dr-orlovsky
Copy link
Collaborator Author

Its strange that rust nightly, stable and beta are building, but 1.22 fails to solve cargo dependency on rand package (which I didn't touched at all). Any ideas what can cause this?

@elichai
Copy link
Member

elichai commented Aug 19, 2019

Because you reintroduced tempfile

@elichai
Copy link
Member

elichai commented Aug 19, 2019

NACK #306

Everything you plan to do with a file can be done in memory via a buffer(vec/iter something else)

src/network/stream_reader.rs Outdated Show resolved Hide resolved
src/network/stream_reader.rs Outdated Show resolved Hide resolved
@dr-orlovsky
Copy link
Collaborator Author

Everything you plan to do with a file can be done in memory via a buffer(vec/iter something else)

@elichai got it. It was introduced to mimic real-world file reading in tests as much as possible, but it is clearly not needed with that price of security

@codecov-io
Copy link

codecov-io commented Aug 20, 2019

Codecov Report

Merging #318 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #318      +/-   ##
==========================================
- Coverage   81.93%   81.93%   -0.01%     
==========================================
  Files          38       38              
  Lines        6982     6981       -1     
==========================================
- Hits         5721     5720       -1     
  Misses       1261     1261
Impacted Files Coverage Δ
src/network/stream_reader.rs 95.62% <100%> (+1.22%) ⬆️
src/util/misc.rs 96.77% <0%> (-0.37%) ⬇️
src/blockdata/script.rs 78.31% <0%> (-0.29%) ⬇️
src/blockdata/transaction.rs 93.57% <0%> (+0.09%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 24361dd...749251e. Read the comment docs.

@dr-orlovsky
Copy link
Collaborator Author

@apoelstra @elichai all suggestions are accepted and added to the PR; commits were squashed; so now its ready for your re-review

Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much nicer :)

@elichai
Copy link
Member

elichai commented Aug 24, 2019

ACK 749251e looks good :)

@dr-orlovsky
Copy link
Collaborator Author

@elichai I assume you need to complete review with a Github button in order to get this merged?

@dr-orlovsky
Copy link
Collaborator Author

Seems we are ready to merge this PR, all comments were addressed and two reviewers had ACK. What is the next step?

@apoelstra
Copy link
Member

We need another reviewer with merge access to ACK.

@dr-orlovsky
Copy link
Collaborator Author

@elichai it looks like we require your ACK to merge the stuff...

@tamasblummer tamasblummer merged commit 4b1d4ed into rust-bitcoin:master Sep 23, 2019
@dr-orlovsky dr-orlovsky deleted the generic-reader branch September 23, 2019 10:29
Davidson-Souza pushed a commit to Davidson-Souza/rust-bitcoin that referenced this pull request Jul 12, 2023
Fix a C compiler warning because of redefinition of SECP256K1_BUILD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants