-
Notifications
You must be signed in to change notification settings - Fork 831
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
Conversation
examples/handshake.rs
Outdated
// 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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
examples/handshake.rs
Outdated
|
||
let address = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(130, 149, 80, 221)), 8333); | ||
|
||
let version_message = build_version_message(address.clone()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ScoketAddr
is Copy, no need to clone https://doc.rust-lang.org/std/net/enum.SocketAddr.html#impl-Copy
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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! 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
examples/handshake.rs
Outdated
|
||
if let Ok(mut stream) = TcpStream::connect(address) { | ||
// Send the message | ||
let _ = stream.write(encode::serialize(&first_message).as_slice()); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
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? |
I cleaned up the example a bit further. It also builds now via |
Can you please rebase this on master to remove the unrelated commits? |
Done! |
Tested. looks awesome :)
Last thing from me, can you please fix this for rust 1.22.0?
|
Thanks! Updated, now builds under nightly and 1.22.0 environments. |
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tACK 827d98d
Nice :) thanks! |
Has someone been able to recreate this in the nightly build? |
What do you mean by 'recreate'? FTR I get an EOF error when I try and run this example against a regtest node
|
I guess we should have some tests for examples too. |
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 initialversion
/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.