8000 Added a simple handshake example. by tnull · Pull Request #411 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Added a simple handshake example. #411

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

Merged
merged 8 commits into from
Apr 4, 2020

Conversation

tnull
Copy link
@tnull tnull commented Feb 17, 2020

Since it recently took me some time to figure out how to use rust-bitcoin, and the contribution of example code is explicitly mentioned in the README, I thought I'd start with a basic handshake example: the supplied code connects to a Bitcoin node, complete the initial version/verack handshake, before it disconnects again.

Let me know if I should change something. If it is desired, I'd consider adding more small examples, if I stumble upon something worth highlighting.

// This example establishes a connection to a Bitcoin node, sends the intial
// "version" message, waits for the reply, and finally closes the connection.

let address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(130, 149, 80, 221)), 8333);
Copy link
Member

Choose a reason for hiding this comment

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

What IP is that?
I'm not sure it's a good idea to put hardcoded actual IPs here, we might cause an accidental DOS on that address

Copy link
Author

Choose a reason for hiding this comment

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

That is the address of a public Bitcoin node we run at TU Berlin. I doubt it couldn't handle some connections from people trying this example.

I put it in there because I wanted to supply a solution that would directly work. However, I could rewrite this so the IP is supplied via an argument. Should I?

Copy link
Author

Choose a reason for hiding this comment

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

The example now takes the address of a Bitcoin peer as an argument.


let address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(130, 149, 80, 221)), 8333);

let version_message = build_version_message(address.clone());
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

Ah, thanks, you are right!

Copy link
Contributor

Choose a reason for hiding this comment

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

Tip: use cargo clippy, or even better - RLS with clippy support turned on.

Copy link
Author
@tnull tnull Feb 20, 2020

Choose a reason for hiding this comment

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

I usually do, however in this case cargo clippy yields a lot of warnings and even errors for the rust-bitcoin codebase. Not sure if fixing all of these is a goal. If so, I could have a look at it in another PR.

Apart from that, checking out the RLS is something I intended to do for some time, thanks for the reminder! 👍

Copy link
Member

Choose a reason for hiding this comment

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

@tnull See #308


if let Ok(mut stream) = TcpStream::connect(address) {
// Send the message
let _ = stream.write(encode::serialize(&first_message).as_slice());
Copy link
Member

Choose a reason for hiding this comment

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

maybe use write_all instead? I don't know enough about TCP streams though

Copy link
Author
@tnull tnull Feb 17, 2020

Choose a reason for hiding this comment

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

I think it doesn't make a difference here, since I omitted error handling anyhow, but I'll change it. Thanks!

@stevenroose
Copy link
Collaborator

Fwiw, I've been working on https://github.com/stevenroose/rust-bitcoin-p2p/ for a while a few months back. I've been quite busy with work since, but I certainly intend to pick that back up!

@tnull
Copy link
Author
tnull commented Feb 17, 2020

Fwiw, I've been working on https://github.com/stevenroose/rust-bitcoin-p2p/ for a while a few months back. I've been quite busy with work since, but I certainly intend to pick that back up!

That's an interesting project! How mature is it?

@tnull
Copy link
Author
tnull commented Feb 19, 2020

I cleaned up the example a bit further. It also builds now via cargo build --examples.

@tnull tnull requested a review from elichai March 3, 2020 09:11
@elichai
Copy link
Member
elichai commented Mar 3, 2020

Can you please rebase this on master to remove the unrelated commits?

@tnull tnull force-pushed the handshake_example branch from 5284893 to aa46618 Compare March 3, 2020 09:49
@tnull
Copy link
Author
tnull commented Mar 3, 2020

Can you please rebase this on master to remove the unrelated commits?

Done!

@elichai
Copy link
Member
elichai commented Mar 3, 2020

Tested. looks awesome :)

cargo run --example handshake 130.149.80.221:8333
Sent version message
Received version message: Version(VersionMessage { version: 70015, services: ServiceFlags(1037), timestamp: 1583229923, receiver: Address {services: ServiceFlags(NONE), address: 81.218.187.144, port: 36006}, sender: Address {services: ServiceFlags(NETWORK|BLOOM|WITNESS|NETWORK_LIMITED), address: 0.0.0.0, port: 0}, nonce: 4282277031945067093, user_agent: "/Satoshi:0.18.0/", start_height: 619975, relay: true })
Sent verack message
Received verack message: Verack

Last thing from me, can you please fix this for rust 1.22.0?
This diff seems to be enough:

diff --git a/examples/handshake.rs b/examples/handshake.rs
index e04470a..23022e1 100644
--- a/examples/handshake.rs
+++ b/examples/handshake.rs
@@ -2,11 +2,14 @@ extern crate bitcoin;
 
 use std::net::{IpAddr, Ipv4Addr, Shutdown, SocketAddr, TcpStream};
 use std::time::{SystemTime, UNIX_EPOCH};
-use std::{env, io::Write, process};
+use std::{env, process};
+use std::io::Write;
 
 use bitcoin::consensus::encode;
-use bitcoin::network::{address, constants, message, message_network, stream_reader::StreamReader};
+use bitcoin::network::{address, constants, message, message_network};
+use bitcoin::network::stream_reader::StreamReader;
 use bitcoin::secp256k1::rand::Rng;
+use bitcoin::secp256k1;
 
 fn main() {
     // This example establishes a connection to a Bitcoin node, sends the intial

@tnull
Copy link
Author
tnull commented Mar 3, 2020

Thanks! Updated, now builds under nightly and 1.22.0 environments.

@codecov-io
Copy link

Codecov Report

Merging #411 into master will increase coverage by 0.23%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #411      +/-   ##
==========================================
+ Coverage   85.81%   86.05%   +0.23%     
==========================================
  Files          40       40              
  Lines        7461     7715     +254     
==========================================
+ Hits         6403     6639     +236     
- Misses       1058     1076      +18
Impacted Files Coverage Δ
src/util/base58.rs 84.1% <0%> (+0.32%) ⬆️
src/consensus/encode.rs 90.46% <0%> (+0.46%) ⬆️
src/util/amount.rs 89.74% <0%> (+0.75%) ⬆️

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 9cff794...827d98d. Read the comment docs.

Copy link
Member
@elichai elichai left a comment

Choose a reason for hiding this comment

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

tACK 827d98d

@apoelstra apoelstra merged commit 78183a3 into rust-bitcoin:master Apr 4, 2020
@apoelstra
Copy link
Member

Nice :) thanks!

@arjunv
Copy link
arjunv commented Dec 4, 2023

Has someone been able to recreate this in the nightly build?

@tcharding
Copy link
Member

What do you mean by 'recreate'?

FTR I get an EOF error when I try and run this example against a regtest node

cargo  run --example handshake --features=rand-std 127.0.0.1:18444                                                                              ✘ 101 
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `/home/tobin/.cache/cargo/debug/examples/handshake '127.0.0.1:18444'`
Sent version message
thread 'main' panicked at bitcoin/examples/handshake.rs:44:90:
called `Result::unwrap()` on an `Err` value: Io(Error { kind: UnexpectedEof, error: None })
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

@Kixunil
Copy link
Collaborator
Kixunil commented Dec 6, 2023

I guess we should have some tests for examples too.

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.

9 participants
0