Commit Graph

231 Commits

Author SHA1 Message Date
Steven Roose 61918dfe81
Change the signature of consensus_encode to return io::Error's
This is instead of encode::Errors because the encoders should
not be allowed to return errors that don't originate in the writer
they are writing into.

This is a part of the method definition that has been relied upon for a
while already.
2021-01-12 17:39:41 +00:00
sanket1729 62fb1ec7e6 fix service flags warning 2020-12-23 09:59:42 -06:00
Steven Roose 767b14f696
Make Inventory and NetworkMessage enums exhaustive
Both by added an `Unknown` variant.
2020-12-21 12:04:26 +00:00
Steven Roose 944371d6a2
Clean up CommandString
- Add length invariant.
- Siimplify constructors.
2020-12-21 11:56:51 +00:00
Andrew Poelstra 99201676ae
Merge pull request #514 from stevenroose/tosocketaddrs
network: Implement net::ToSocketAddrs for address messages
2020-12-16 17:11:35 +00:00
Andrew Poelstra c62cd98615
Merge pull request #516 from stevenroose/max-inv-size
Add message::MAX_INV_SIZE constant
2020-12-15 23:45:42 +00:00
Alekos Filini 373f355b5a Flush unrecognized network messages from the read buffer
Currently whenever an unrecognized network message is received, it is never
flushed from the read buffer, meaning that unless the stream is closed and
recreated it will keep returning the same error every time `read_next()` is
called.

This commit adds the length of the message to `UnrecognizedNetworkCommand`,
so that the `StreamReader` can flush those bytes before returning the error
to the caller.
2020-12-15 19:54:21 +01:00
Steven Roose f1b0f263ce
Implement PartialOrd and Ord for Inventory 2020-11-10 19:54:07 +00:00
Steven Roose 1ec3ed3fec
Add message::MAX_INV_SIZE constant 2020-11-09 22:22:59 +00:00
Steven Roose ab1e9cbb9e
network: Implement net::ToSocketAddrs for address messages 2020-11-09 20:13:00 +00:00
Steven Roose 4c70397a85
network: Add socket_addr method to AddrV2Message 2020-11-08 13:48:42 +00:00
Steven Roose c7ec4f171f
network: Move AddrV2Message definition down
So that it is grouped together with the impls.
2020-11-08 13:43:55 +00:00
Andrew Poelstra 8295885e8e
Merge pull request #454 from jrawsthorne/improved-bip158-types
Implement new FilterHeader type to differentiate from FilterHash
2020-11-05 16:46:50 +00:00
Jake Rawsthorne 921f64699c `addrv2` and `sendaddrv2` network message 2020-10-26 21:49:06 +00:00
Jake Rawsthorne cf8e290c99 AddrV2 structures 2020-10-26 21:46:10 +00:00
Andrew Poelstra c16053a265
Merge pull request #446 from jrawsthorne/bip339
BIP339: Add wtxidrelay message and WTx inv type
2020-10-26 20:03:36 +00:00
Andrew Poelstra 93df7cbd3e
Merge pull request #291 from kallewoof/2019-07-signet
add signet support
2020-10-26 19:46:10 +00:00
Jake Rawsthorne eeb14c4b08 Implement new FilterHeader type to differentiate from FilterHash 2020-10-10 20:38:10 +01:00
Elichai Turkel 2d70623356
Remove deprecated Error::description impl 2020-10-08 17:11:18 +03:00
Elichai Turkel 023fae1f65
Add the dyn keyword where appropriate 2020-10-08 17:11:16 +03:00
Elichai Turkel fdd6f4f196
Derive macros instead of implementing via macro_rules 2020-10-08 16:17:14 +03:00
Jake Rawsthorne c7987d8fcd
Add wtxidrelay message and WTx inv type, document PROTOCOL_VERSION 2020-10-07 20:03:15 +01:00
Karl-Johan Alm a3d9899cb1
add signet support 2020-09-18 16:32:33 +09:00
Alexis Sellier 5017b33326
Add `Clone` instance to `RawNetworkMessage` 2020-08-31 19:21:29 +02:00
Jake Rawsthorne e47fcae435 feefilter message 2020-07-30 17:25:21 +01:00
Andrew Poelstra 7efde3ae47
Merge pull request #419 from elichai/2020-03-description
Deprecate Error::description
2020-05-23 17:26:38 +00:00
Jake Rawsthorne 81c061a68c Change WitnessTransaction inv type to use txid 2020-04-14 14:11:44 +01:00
Elichai Turkel 654232a3dc
Deprecate Error::description 2020-04-13 02:15:28 +03:00
Elichai Turkel a473d01b17
Made some idiomatic changes 2020-03-29 17:15:15 +03:00
Elichai Turkel 3f2d428706
Remove needless references 2020-03-29 17:15:14 +03:00
Elichai Turkel 16eb81e1f7
Replaced slow vec initialization, and dual calls to hashmap 2020-03-29 17:10:27 +03:00
Elichai Turkel c19b736566
Remove the hex dependency 2020-01-20 18:50:02 +02:00
kiminuo 9e223988fa Improve fmt::Debug for network/Address
Original output:

  "Address {services: ServiceFlags(9), address: [0, 0, 0, 0, 0, 65535, 2560, 1], port: 8333}"

New output:

  * for IPv4: "Address {services: ServiceFlags(NETWORK|WITNESS), address: 10.0.0.1, port: 8333}"
  * for IPv6: "Address {services: ServiceFlags(NETWORK_LIMITED), address: fd87:d87e:eb43::ffff:a00:1, port: 8333}"
2020-01-07 12:30:47 +01:00
Dr Maxim Orlovsky 5fc24dea33 Multiple fixes for hash types and their computing
Unit test for wtxid and SegWit transactions
2020-01-01 13:54:23 +01:00
Dr Maxim Orlovsky f5a8087105 New hash types: MerkleRoot/Branch, WitnessCommit, SigHash, FilterHash 2020-01-01 13:50:17 +01:00
Dr Maxim Orlovsky 4746ccb88e Final work on Txid and other hashes
Fixing issue with external dependency and hash_newtype macro implementation

Reverting back to the bitcoin_hashes crate after new version release
2020-01-01 13:48:28 +01:00
Steven Roose e2caebc42e
Merge pull request #370 from elichai/2019-12-rawmessage
Add a full round test for RawNetworkMessage
2019-12-23 12:30:11 +01:00
Andrew Poelstra efd2168364
Merge pull request #368 from mauzmorose/bip157-service-flag
bip157: Add NODE_COMPACT_FILTERS Service Flag
2019-12-22 16:48:16 +00:00
Elichai Turkel bc2ba5bb12
Add a full round test for RawNetworkMessage 2019-12-22 14:01:12 +02:00
Sofiane Baltaci 8617de51c8 Adapt service_flags_test to reflect changes 2019-12-21 03:36:47 +01:00
Sofiane Baltaci 43789b1d5a Add COMPACT_FILTERS service flag to ServiceFlags fmt::Display impl 2019-12-21 03:06:03 +01:00
Sofiane Baltaci b40d94641d Add bip157 NODE_COMPACT_FILTERS Service Flag 2019-12-21 02:39:30 +01:00
Sofiane Baltaci 2605141ada Fix typo on getcfcheckpt command string 2019-12-20 23:47:40 +01:00
kiminuo 74285738ce Convert numeric representation of ServiceFlags to bitwise OR of the flag names
The changes affect only tests
2019-12-12 00:11:13 +01:00
Steven Roose 0f4f060e8b
Merge pull request #363 from kiminuo/feature/remove-old-todos
Remove two old TODO comments [nit]
2019-12-11 16:53:20 +00:00
Steven Roose 024557fe47
Merge pull request #357 from stevenroose/command-str
Various optimizations of the network code
2019-12-11 15:38:52 +00:00
kiminuo 269ddb6f07 Remove some old TODO comments 2019-12-11 08:26:04 +01:00
Steven Roose a8f14af24d
Prevent panic on oversized CommandString's 2019-12-10 20:20:04 +00:00
Steven Roose fe3397399e
Add Copy to InvType enum 2019-12-08 20:58:52 +00:00
Steven Roose 671b3173c8
Make internals for CommandString private
The From traits and AsRef and Display implementations
let you do all you want.
2019-12-08 20:58:50 +00:00
Steven Roose 83f55b7f1d
Follow Rust std practice for RejectReason enum 2019-12-08 20:58:49 +00:00
Steven Roose 5c84e9671f
Optimize Reject message 2019-12-08 20:58:46 +00:00
Steven Roose e37fdb7319
Also have getter for CommandString 2019-12-08 20:57:07 +00:00
Steven Roose e2eed78964
nit: Reject is implemented 2019-12-08 20:57:05 +00:00
Steven Roose 50a37f415e
Implement From<String> and From<&'static str> for CommandString 2019-12-08 20:57:04 +00:00
Steven Roose 36838b7918
Make network::CommandString a Cow on 'static 2019-12-08 20:57:03 +00:00
Steven Roose bac3e0308b
Add command method to NetworkMessage
Also make the return type an &'static str
2019-12-08 20:57:00 +00:00
Steven Roose b4c4a9658d
Implement std:#️⃣:Hash for Inventory 2019-12-08 17:49:11 +00:00
Andrew Poelstra 854718219e
Merge pull request #345 from stevenroose/serviceflags
Add ServiceFlags type
2019-12-06 21:31:46 +00:00
Steven Roose 3e1e4f92b7
Merge pull request #337 from TheBlueMatt/2019-10-less-deps
Drop hex and byteorder (non-test) deps (and disable useless lints)
2019-12-06 15:38:58 +00:00
Steven Roose 1edc436f8f
Add an extensive Display implementation for ServiceFlags 2019-12-05 20:13:45 +00:00
Matt Corallo acb43af981 Drop byteorder dependency
Taking an external dependency just to convert ints to byte arrays
is somewhat of a waste, especially when Rust isn't very aggressive
about doing cross-crate LTO.

Note that the latest LLVM pattern-matches this, and while I haven't
tested it, that should mean this means no loss of optimization.
2019-12-05 10:41:00 -05:00
Steven Roose de18e926c1
Use ServiceFlags type in existing API 2019-12-04 23:28:25 +00:00
Steven Roose fdf4b2f74e
Add ServiceFlags type 2019-12-04 23:28:23 +00:00
Steven Roose 33ba7eaa36
Nit in VersionMessage documentation 2019-11-19 22:58:49 +00:00
Carl Dong 242ce14cee
Add reject message (#323)
Add reject message
2019-09-30 14:52:19 +00:00
Dr. Maxim Orlovsky 4b1d4edc14 Improvements to `StreamReader` (#318)
* Generalizing StreamReader to support arbitrary data structures

* Using Read trait and adding test cases
2019-09-23 08:31:52 +02:00
Tamas Blummer 1b0e31c233 fixed typo 2019-09-21 13:31:03 +02:00
Tamas Blummer 960e3da75b remove #repr on RejectReason
and typo fix
2019-09-10 20:11:25 +02:00
Tamas Blummer c93b1ed8ce add reverse mapping 2019-09-03 19:59:48 +02:00
Tamas Blummer 3266c192b6 add reject message 2019-08-27 13:42:46 +02:00
Steven Roose 48f4c1989f
Rename bitcoin_hashes dependency to hashes 2019-08-16 15:52:27 +01:00
Elichai Turkel 4a1830c423 Replaced Read trait with a generic over Read (#307)
Removed tempfile usage from stream_reader
2019-08-07 17:35:22 +02:00
Andrew Poelstra 3b9a94a178 eliminate type parameter from the `Decodable` trait 2019-07-11 17:23:01 +00:00
Andrew Poelstra 42960b959f eliminate type parameter from `Encodable` trait 2019-07-11 17:21:19 +00:00
Andrew Poelstra b734d6488a make consensus_encode return the encoded length 2019-07-11 17:15:32 +00:00
Andrew Poelstra 7e6ad7c893 rename Encoder to WriteExt and Decoder to ReadExt 2019-07-11 15:01:38 +00:00
Matt Corallo 84835f244c Support sendheaders network message decode 2019-05-30 11:25:37 -04:00
Matt Corallo 4f96a87475 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.
2019-05-17 17:55:02 -04:00
Dr. Maxim Orlovsky 3c21e301aa Better RawNewtorkMessage deserealization from IO stream (#231)
Follow-up to https://github.com/rust-bitcoin/rust-bitcoin/pull/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
2019-02-27 16:41:28 -05:00
Tamás Blummer 1cd2782122
add BIP157 (Client Side Block Filtering) Messages (#225)
* add BIP57 (Client Side Block Filtering) Messages

* rabased after https://github.com/rust-bitcoin/rust-bitcoin/pull/215
2019-02-08 13:00:51 +01:00
Carl Dong 99f63a8ca4 Convert codebase from util::hash to bitcoin_hashes
Also replace unsafe transmute with call to read_u64_into
2019-01-24 16:27:52 -05:00
Dimitris Apostolou 132ca5ea95 Fix typos 2019-01-23 14:17:29 -05:00
Carl Dong 0f42ca69b0 Move relevant names into consensus::encode
- Move network::encodable::* to consensus::encode::*
- Rename Consensus{En,De}codable to {En,De}codable (now under
  consensus::encode)
- Move network::serialize::Error to consensus::encode::Error
- Remove Raw{En,De}coder, implement {En,De}coder for T: {Write,Read}
  instead
- Move network::serialize::Simple{En,De}coder to
  consensus::encode::{En,De}coder
- Rename util::Error::Serialize to util::Error::Encode
- Modify comments to refer to new names
- Modify files to refer to new names
- Expose {En,De}cod{able,er}, {de,}serialize, Params
- Do not return Result for serialize{,_hex} as serializing to a Vec
  should never fail
2018-09-25 21:19:35 +08:00
Carl Dong 8e0e4eb55a Move serialize::BitcoinHash to util:#️⃣:BitcoinHash
- Use Sha256dEncoder for calculating merkle root
- Remove BitcoinHash implementation for Vec<u8>
2018-09-25 21:19:10 +08:00
Carl Dong 97937b1b5f Move network::consensus_params to consensus::params 2018-09-25 21:13:34 +08:00
Carl Dong 7e9d393d03 Remove low-level networking support
- Modify VersionMessage constructor to take in parameters directly that
  would have otherwise been extracted from a Socket (now removed)
2018-09-25 21:13:34 +08:00
Andrew Poelstra ef642295c5 encodable: reject non-compact VarInts on Vec and Box<[T]> lengths 2018-08-24 20:31:46 +00:00
Carl Dong bccdd06794 Replace catch-all match arms with specific ones 2018-08-21 01:58:40 -07:00
Carl Dong 0c172941af Replace serialize::Error::Detail with variants
- Add serialize::Error::ParseFailed(&'static str) variant for
  serialization errors without context
- Add appropriate variants to replace network::Error::Detail for
  serialization error with context
- Remove error method from SimpleDecoders
2018-08-21 01:58:40 -07:00
Carl Dong d12a861f85 Remove unnecessary network::Error::Detail variant 2018-08-21 01:58:40 -07:00
Carl Dong 95303a1d28 Use full path in macros to to eliminate uses 2018-08-21 01:58:40 -07:00
Carl Dong e5b5cbfadb Fix Error type for SimpleDecoder and SimpleEncoder
- Separate serialize::Error and network::Error from util::Error
- Remove unneeded propagate_err and consume_err
- Change fuzzing code to ignore Err type
2018-08-21 01:58:40 -07:00
Aleksei 356d13e465 Add some useful standard trait implementations 2018-08-20 12:11:17 +03:00
Roman Zeyde fdbccf055d
Remove variable shadowing in listener.rs 2018-08-15 09:43:35 +03:00
Carl Dong 70203831d6 Make deserialize error if input bytes not consumed
- Adjust tests in encodable.rs to account for this change
2018-08-14 16:50:33 -07:00
Jean Pierre Dudey b2594087db Use the `?` (try) instead of the `try!` macro.
Signed-off-by: Jean Pierre Dudey <jeandudey@hotmail.com>
2018-08-12 12:47:31 -04:00
Andrew Poelstra ebe5133d1a
Merge pull request #110 from D4nte/regtest
Regtest bech32 address support
2018-08-11 17:21:12 +00:00
Andrew Poelstra 259c5902f1
Merge pull request #116 from jeandudey/2018-08-08-module-docs
Fix modules documentation title.
2018-08-11 17:12:50 +00:00
Jean Pierre Dudey 7ecb6b9dea Refactor and add more documentation for the `Network` type.
Signed-off-by: Jean Pierre Dudey <jeandudey@hotmail.com>
2018-08-10 14:56:19 -04:00