2022-06-29 04:05:31 +00:00
|
|
|
// Written in 2014 by Andrew Poelstra <apoelstra@wpsoftware.net>
|
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2021-11-05 21:58:18 +00:00
|
|
|
//! Bitcoin blockdata network messages.
|
2014-07-18 13:56:17 +00:00
|
|
|
//!
|
|
|
|
//! This module describes network messages which are used for passing
|
|
|
|
//! Bitcoin data (blocks and transactions) around.
|
|
|
|
//!
|
|
|
|
|
2022-05-02 22:13:57 +00:00
|
|
|
use crate::consensus::encode::{self, Decodable, Encodable};
|
|
|
|
use crate::hash_types::{BlockHash, Txid, Wtxid};
|
2022-11-15 23:23:01 +00:00
|
|
|
use crate::hashes::{sha256d, Hash as _};
|
2022-06-07 03:25:27 +00:00
|
|
|
use crate::internal_macros::impl_consensus_encoding;
|
2022-11-15 23:23:01 +00:00
|
|
|
use crate::io;
|
|
|
|
use crate::network::constants;
|
|
|
|
use crate::prelude::*;
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2019-12-18 11:40:46 +00:00
|
|
|
/// An inventory item.
|
2020-11-10 19:54:07 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug, Copy, Hash, PartialOrd, Ord)]
|
2019-12-18 11:40:46 +00:00
|
|
|
pub enum Inventory {
|
2015-04-07 22:51:57 +00:00
|
|
|
/// Error --- these inventories can be ignored
|
|
|
|
Error,
|
|
|
|
/// Transaction
|
2019-12-18 11:40:46 +00:00
|
|
|
Transaction(Txid),
|
2015-04-07 22:51:57 +00:00
|
|
|
/// Block
|
2019-12-18 11:40:46 +00:00
|
|
|
Block(BlockHash),
|
2019-03-24 15:21:44 +00:00
|
|
|
/// Compact Block
|
|
|
|
CompactBlock(BlockHash),
|
2020-07-22 22:00:04 +00:00
|
|
|
/// Witness Transaction by Wtxid
|
|
|
|
WTx(Wtxid),
|
2018-05-28 13:24:35 +00:00
|
|
|
/// Witness Transaction
|
2020-04-14 13:11:44 +00:00
|
|
|
WitnessTransaction(Txid),
|
2019-12-18 11:40:46 +00:00
|
|
|
/// Witness Block
|
|
|
|
WitnessBlock(BlockHash),
|
2020-12-21 11:59:25 +00:00
|
|
|
/// Unknown inventory type
|
|
|
|
Unknown {
|
|
|
|
/// The inventory item type.
|
|
|
|
inv_type: u32,
|
|
|
|
/// The hash of the inventory item
|
|
|
|
hash: [u8; 32],
|
2022-11-15 23:23:01 +00:00
|
|
|
},
|
2019-12-18 11:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Encodable for Inventory {
|
|
|
|
#[inline]
|
2022-06-29 01:22:12 +00:00
|
|
|
fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
|
2019-12-18 11:40:46 +00:00
|
|
|
macro_rules! encode_inv {
|
|
|
|
($code:expr, $item:expr) => {
|
Take Writer/Reader by `&mut` in consensus en/decoding
Fix #1020 (see more relevant discussion there)
This definitely makes the amount of generics compiler
has to generate by avoding generating the same functions
for `R`, &mut R`, `&mut &mut R` and so on.
old:
```
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 9947832 Jun 2 22:42 target/release/deps/bitcoin-07a9dabf1f3e0266
> strip target/release/deps/bitcoin-07a9dabf1f3e0266
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 4463024 Jun 2 22:46 target/release/deps/bitcoin-07a9dabf1f3e0266
```
new:
```
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 9866800 Jun 2 22:44 target/release/deps/bitcoin-07a9dabf1f3e0266
> strip target/release/deps/bitcoin-07a9dabf1f3e0266
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 4393392 Jun 2 22:45 target/release/deps/bitcoin-07a9dabf1f3e0266
```
In the unit-test binary itself, it saves ~100KB of data.
I did not expect much performance gains, but turn out I was wrong(*):
old:
```
test blockdata::block::benches::bench_block_deserialize ... bench: 1,072,710 ns/iter (+/- 21,871)
test blockdata::block::benches::bench_block_serialize ... bench: 191,223 ns/iter (+/- 5,833)
test blockdata::block::benches::bench_block_serialize_logic ... bench: 37,543 ns/iter (+/- 732)
test blockdata::block::benches::bench_stream_reader ... bench: 1,872,455 ns/iter (+/- 149,519)
test blockdata::transaction::benches::bench_transaction_deserialize ... bench: 136 ns/iter (+/- 3)
test blockdata::transaction::benches::bench_transaction_serialize ... bench: 51 ns/iter (+/- 8)
test blockdata::transaction::benches::bench_transaction_serialize_logic ... bench: 5 ns/iter (+/- 0)
test blockdata::transaction::benches::bench_transaction_size ... bench: 3 ns/iter (+/- 0)
```
new:
```
test blockdata::block::benches::bench_block_deserialize ... bench: 1,028,574 ns/iter (+/- 10,910)
test blockdata::block::benches::bench_block_serialize ... bench: 162,143 ns/iter (+/- 3,363)
test blockdata::block::benches::bench_block_serialize_logic ... bench: 30,725 ns/iter (+/- 695)
test blockdata::block::benches::bench_stream_reader ... bench: 1,437,071 ns/iter (+/- 53,694)
test blockdata::transaction::benches::bench_transaction_deserialize ... bench: 92 ns/iter (+/- 2)
test blockdata::transaction::benches::bench_transaction_serialize ... bench: 17 ns/iter (+/- 0)
test blockdata::transaction::benches::bench_transaction_serialize_logic ... bench: 5 ns/iter (+/- 0)
test blockdata::transaction::benches::bench_transaction_size ... bench: 4 ns/iter (+/- 0)
```
(*) - I'm benchmarking on a noisy laptop. Take this with a grain of salt. But I think
at least it doesn't make anything slower.
While doing all this manual labor that will probably generate conflicts,
I took a liberty of changing generic type names and variable names to
`r` and `R` (reader) and `w` and `W` for writer.
2022-06-03 04:50:42 +00:00
|
|
|
u32::consensus_encode(&$code, w)? + $item.consensus_encode(w)?
|
2022-11-15 23:23:01 +00:00
|
|
|
};
|
2019-12-18 11:40:46 +00:00
|
|
|
}
|
|
|
|
Ok(match *self {
|
2022-06-07 02:39:25 +00:00
|
|
|
Inventory::Error => encode_inv!(0, sha256d::Hash::all_zeros()),
|
2019-12-18 11:40:46 +00:00
|
|
|
Inventory::Transaction(ref t) => encode_inv!(1, t),
|
|
|
|
Inventory::Block(ref b) => encode_inv!(2, b),
|
2019-03-24 15:21:44 +00:00
|
|
|
Inventory::CompactBlock(ref b) => encode_inv!(4, b),
|
2020-07-22 22:00:04 +00:00
|
|
|
Inventory::WTx(w) => encode_inv!(5, w),
|
2019-12-18 11:40:46 +00:00
|
|
|
Inventory::WitnessTransaction(ref t) => encode_inv!(0x40000001, t),
|
|
|
|
Inventory::WitnessBlock(ref b) => encode_inv!(0x40000002, b),
|
2020-12-21 11:59:25 +00:00
|
|
|
Inventory::Unknown { inv_type: t, hash: ref d } => encode_inv!(t, d),
|
2019-12-18 11:40:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decodable for Inventory {
|
|
|
|
#[inline]
|
2022-06-29 01:22:12 +00:00
|
|
|
fn consensus_decode<R: io::Read + ?Sized>(r: &mut R) -> Result<Self, encode::Error> {
|
Take Writer/Reader by `&mut` in consensus en/decoding
Fix #1020 (see more relevant discussion there)
This definitely makes the amount of generics compiler
has to generate by avoding generating the same functions
for `R`, &mut R`, `&mut &mut R` and so on.
old:
```
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 9947832 Jun 2 22:42 target/release/deps/bitcoin-07a9dabf1f3e0266
> strip target/release/deps/bitcoin-07a9dabf1f3e0266
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 4463024 Jun 2 22:46 target/release/deps/bitcoin-07a9dabf1f3e0266
```
new:
```
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 9866800 Jun 2 22:44 target/release/deps/bitcoin-07a9dabf1f3e0266
> strip target/release/deps/bitcoin-07a9dabf1f3e0266
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 4393392 Jun 2 22:45 target/release/deps/bitcoin-07a9dabf1f3e0266
```
In the unit-test binary itself, it saves ~100KB of data.
I did not expect much performance gains, but turn out I was wrong(*):
old:
```
test blockdata::block::benches::bench_block_deserialize ... bench: 1,072,710 ns/iter (+/- 21,871)
test blockdata::block::benches::bench_block_serialize ... bench: 191,223 ns/iter (+/- 5,833)
test blockdata::block::benches::bench_block_serialize_logic ... bench: 37,543 ns/iter (+/- 732)
test blockdata::block::benches::bench_stream_reader ... bench: 1,872,455 ns/iter (+/- 149,519)
test blockdata::transaction::benches::bench_transaction_deserialize ... bench: 136 ns/iter (+/- 3)
test blockdata::transaction::benches::bench_transaction_serialize ... bench: 51 ns/iter (+/- 8)
test blockdata::transaction::benches::bench_transaction_serialize_logic ... bench: 5 ns/iter (+/- 0)
test blockdata::transaction::benches::bench_transaction_size ... bench: 3 ns/iter (+/- 0)
```
new:
```
test blockdata::block::benches::bench_block_deserialize ... bench: 1,028,574 ns/iter (+/- 10,910)
test blockdata::block::benches::bench_block_serialize ... bench: 162,143 ns/iter (+/- 3,363)
test blockdata::block::benches::bench_block_serialize_logic ... bench: 30,725 ns/iter (+/- 695)
test blockdata::block::benches::bench_stream_reader ... bench: 1,437,071 ns/iter (+/- 53,694)
test blockdata::transaction::benches::bench_transaction_deserialize ... bench: 92 ns/iter (+/- 2)
test blockdata::transaction::benches::bench_transaction_serialize ... bench: 17 ns/iter (+/- 0)
test blockdata::transaction::benches::bench_transaction_serialize_logic ... bench: 5 ns/iter (+/- 0)
test blockdata::transaction::benches::bench_transaction_size ... bench: 4 ns/iter (+/- 0)
```
(*) - I'm benchmarking on a noisy laptop. Take this with a grain of salt. But I think
at least it doesn't make anything slower.
While doing all this manual labor that will probably generate conflicts,
I took a liberty of changing generic type names and variable names to
`r` and `R` (reader) and `w` and `W` for writer.
2022-06-03 04:50:42 +00:00
|
|
|
let inv_type: u32 = Decodable::consensus_decode(r)?;
|
2019-12-18 11:40:46 +00:00
|
|
|
Ok(match inv_type {
|
|
|
|
0 => Inventory::Error,
|
Take Writer/Reader by `&mut` in consensus en/decoding
Fix #1020 (see more relevant discussion there)
This definitely makes the amount of generics compiler
has to generate by avoding generating the same functions
for `R`, &mut R`, `&mut &mut R` and so on.
old:
```
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 9947832 Jun 2 22:42 target/release/deps/bitcoin-07a9dabf1f3e0266
> strip target/release/deps/bitcoin-07a9dabf1f3e0266
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 4463024 Jun 2 22:46 target/release/deps/bitcoin-07a9dabf1f3e0266
```
new:
```
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 9866800 Jun 2 22:44 target/release/deps/bitcoin-07a9dabf1f3e0266
> strip target/release/deps/bitcoin-07a9dabf1f3e0266
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 4393392 Jun 2 22:45 target/release/deps/bitcoin-07a9dabf1f3e0266
```
In the unit-test binary itself, it saves ~100KB of data.
I did not expect much performance gains, but turn out I was wrong(*):
old:
```
test blockdata::block::benches::bench_block_deserialize ... bench: 1,072,710 ns/iter (+/- 21,871)
test blockdata::block::benches::bench_block_serialize ... bench: 191,223 ns/iter (+/- 5,833)
test blockdata::block::benches::bench_block_serialize_logic ... bench: 37,543 ns/iter (+/- 732)
test blockdata::block::benches::bench_stream_reader ... bench: 1,872,455 ns/iter (+/- 149,519)
test blockdata::transaction::benches::bench_transaction_deserialize ... bench: 136 ns/iter (+/- 3)
test blockdata::transaction::benches::bench_transaction_serialize ... bench: 51 ns/iter (+/- 8)
test blockdata::transaction::benches::bench_transaction_serialize_logic ... bench: 5 ns/iter (+/- 0)
test blockdata::transaction::benches::bench_transaction_size ... bench: 3 ns/iter (+/- 0)
```
new:
```
test blockdata::block::benches::bench_block_deserialize ... bench: 1,028,574 ns/iter (+/- 10,910)
test blockdata::block::benches::bench_block_serialize ... bench: 162,143 ns/iter (+/- 3,363)
test blockdata::block::benches::bench_block_serialize_logic ... bench: 30,725 ns/iter (+/- 695)
test blockdata::block::benches::bench_stream_reader ... bench: 1,437,071 ns/iter (+/- 53,694)
test blockdata::transaction::benches::bench_transaction_deserialize ... bench: 92 ns/iter (+/- 2)
test blockdata::transaction::benches::bench_transaction_serialize ... bench: 17 ns/iter (+/- 0)
test blockdata::transaction::benches::bench_transaction_serialize_logic ... bench: 5 ns/iter (+/- 0)
test blockdata::transaction::benches::bench_transaction_size ... bench: 4 ns/iter (+/- 0)
```
(*) - I'm benchmarking on a noisy laptop. Take this with a grain of salt. But I think
at least it doesn't make anything slower.
While doing all this manual labor that will probably generate conflicts,
I took a liberty of changing generic type names and variable names to
`r` and `R` (reader) and `w` and `W` for writer.
2022-06-03 04:50:42 +00:00
|
|
|
1 => Inventory::Transaction(Decodable::consensus_decode(r)?),
|
|
|
|
2 => Inventory::Block(Decodable::consensus_decode(r)?),
|
2019-03-24 15:21:44 +00:00
|
|
|
4 => Inventory::CompactBlock(Decodable::consensus_decode(r)?),
|
Take Writer/Reader by `&mut` in consensus en/decoding
Fix #1020 (see more relevant discussion there)
This definitely makes the amount of generics compiler
has to generate by avoding generating the same functions
for `R`, &mut R`, `&mut &mut R` and so on.
old:
```
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 9947832 Jun 2 22:42 target/release/deps/bitcoin-07a9dabf1f3e0266
> strip target/release/deps/bitcoin-07a9dabf1f3e0266
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 4463024 Jun 2 22:46 target/release/deps/bitcoin-07a9dabf1f3e0266
```
new:
```
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 9866800 Jun 2 22:44 target/release/deps/bitcoin-07a9dabf1f3e0266
> strip target/release/deps/bitcoin-07a9dabf1f3e0266
> ls -al target/release/deps/bitcoin-07a9dabf1f3e0266
-rwxrwxr-x 1 dpc dpc 4393392 Jun 2 22:45 target/release/deps/bitcoin-07a9dabf1f3e0266
```
In the unit-test binary itself, it saves ~100KB of data.
I did not expect much performance gains, but turn out I was wrong(*):
old:
```
test blockdata::block::benches::bench_block_deserialize ... bench: 1,072,710 ns/iter (+/- 21,871)
test blockdata::block::benches::bench_block_serialize ... bench: 191,223 ns/iter (+/- 5,833)
test blockdata::block::benches::bench_block_serialize_logic ... bench: 37,543 ns/iter (+/- 732)
test blockdata::block::benches::bench_stream_reader ... bench: 1,872,455 ns/iter (+/- 149,519)
test blockdata::transaction::benches::bench_transaction_deserialize ... bench: 136 ns/iter (+/- 3)
test blockdata::transaction::benches::bench_transaction_serialize ... bench: 51 ns/iter (+/- 8)
test blockdata::transaction::benches::bench_transaction_serialize_logic ... bench: 5 ns/iter (+/- 0)
test blockdata::transaction::benches::bench_transaction_size ... bench: 3 ns/iter (+/- 0)
```
new:
```
test blockdata::block::benches::bench_block_deserialize ... bench: 1,028,574 ns/iter (+/- 10,910)
test blockdata::block::benches::bench_block_serialize ... bench: 162,143 ns/iter (+/- 3,363)
test blockdata::block::benches::bench_block_serialize_logic ... bench: 30,725 ns/iter (+/- 695)
test blockdata::block::benches::bench_stream_reader ... bench: 1,437,071 ns/iter (+/- 53,694)
test blockdata::transaction::benches::bench_transaction_deserialize ... bench: 92 ns/iter (+/- 2)
test blockdata::transaction::benches::bench_transaction_serialize ... bench: 17 ns/iter (+/- 0)
test blockdata::transaction::benches::bench_transaction_serialize_logic ... bench: 5 ns/iter (+/- 0)
test blockdata::transaction::benches::bench_transaction_size ... bench: 4 ns/iter (+/- 0)
```
(*) - I'm benchmarking on a noisy laptop. Take this with a grain of salt. But I think
at least it doesn't make anything slower.
While doing all this manual labor that will probably generate conflicts,
I took a liberty of changing generic type names and variable names to
`r` and `R` (reader) and `w` and `W` for writer.
2022-06-03 04:50:42 +00:00
|
|
|
5 => Inventory::WTx(Decodable::consensus_decode(r)?),
|
|
|
|
0x40000001 => Inventory::WitnessTransaction(Decodable::consensus_decode(r)?),
|
|
|
|
0x40000002 => Inventory::WitnessBlock(Decodable::consensus_decode(r)?),
|
2022-11-15 23:23:01 +00:00
|
|
|
tp => Inventory::Unknown { inv_type: tp, hash: Decodable::consensus_decode(r)? },
|
2019-12-18 11:40:46 +00:00
|
|
|
})
|
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Some simple messages
|
|
|
|
|
|
|
|
/// The `getblocks` message
|
2015-03-26 15:35:31 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
2014-07-18 13:56:17 +00:00
|
|
|
pub struct GetBlocksMessage {
|
2015-04-07 22:51:57 +00:00
|
|
|
/// The protocol version
|
|
|
|
pub version: u32,
|
|
|
|
/// Locator hashes --- ordered newest to oldest. The remote peer will
|
|
|
|
/// reply with its longest known chain, starting from a locator hash
|
|
|
|
/// if possible and block 1 otherwise.
|
2019-12-18 11:40:46 +00:00
|
|
|
pub locator_hashes: Vec<BlockHash>,
|
2015-04-07 22:51:57 +00:00
|
|
|
/// References the block to stop at, or zero to just fetch the maximum 500 blocks
|
2019-12-18 11:40:46 +00:00
|
|
|
pub stop_hash: BlockHash,
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The `getheaders` message
|
2015-03-26 15:35:31 +00:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug)]
|
2014-07-18 13:56:17 +00:00
|
|
|
pub struct GetHeadersMessage {
|
2015-04-07 22:51:57 +00:00
|
|
|
/// The protocol version
|
|
|
|
pub version: u32,
|
|
|
|
/// Locator hashes --- ordered newest to oldest. The remote peer will
|
|
|
|
/// reply with its longest known chain, starting from a locator hash
|
|
|
|
/// if possible and block 1 otherwise.
|
2019-12-18 11:40:46 +00:00
|
|
|
pub locator_hashes: Vec<BlockHash>,
|
2015-04-07 22:51:57 +00:00
|
|
|
/// References the header to stop at, or zero to just fetch the maximum 2000 headers
|
2022-11-15 23:23:01 +00:00
|
|
|
pub stop_hash: BlockHash,
|
2019-12-08 17:44:51 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 13:56:17 +00:00
|
|
|
impl GetBlocksMessage {
|
2015-04-07 22:51:57 +00:00
|
|
|
/// Construct a new `getblocks` message
|
2019-12-18 11:40:46 +00:00
|
|
|
pub fn new(locator_hashes: Vec<BlockHash>, stop_hash: BlockHash) -> GetBlocksMessage {
|
2022-11-15 23:23:01 +00:00
|
|
|
GetBlocksMessage { version: constants::PROTOCOL_VERSION, locator_hashes, stop_hash }
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-18 18:16:01 +00:00
|
|
|
impl_consensus_encoding!(GetBlocksMessage, version, locator_hashes, stop_hash);
|
2014-07-18 13:56:17 +00:00
|
|
|
|
|
|
|
impl GetHeadersMessage {
|
2015-04-07 22:51:57 +00:00
|
|
|
/// Construct a new `getheaders` message
|
2019-12-18 11:40:46 +00:00
|
|
|
pub fn new(locator_hashes: Vec<BlockHash>, stop_hash: BlockHash) -> GetHeadersMessage {
|
2022-11-15 23:23:01 +00:00
|
|
|
GetHeadersMessage { version: constants::PROTOCOL_VERSION, locator_hashes, stop_hash }
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-18 18:16:01 +00:00
|
|
|
impl_consensus_encoding!(GetHeadersMessage, version, locator_hashes, stop_hash);
|
2014-07-18 13:56:17 +00:00
|
|
|
|
2014-08-01 16:01:39 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-11-15 23:23:01 +00:00
|
|
|
use super::{GetBlocksMessage, GetHeadersMessage, Vec};
|
|
|
|
use crate::consensus::encode::{deserialize, serialize};
|
2022-06-07 02:39:25 +00:00
|
|
|
use crate::hashes::Hash;
|
2022-12-03 19:57:18 +00:00
|
|
|
use crate::internal_macros::hex;
|
2015-04-07 22:51:57 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn getblocks_message_test() {
|
2022-12-03 19:57:18 +00:00
|
|
|
let from_sat = hex!("72110100014a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b0000000000000000000000000000000000000000000000000000000000000000");
|
2022-11-15 23:23:01 +00:00
|
|
|
let genhash =
|
2022-12-03 19:57:18 +00:00
|
|
|
hex!("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b");
|
2015-04-07 22:51:57 +00:00
|
|
|
|
2015-04-08 22:23:45 +00:00
|
|
|
let decode: Result<GetBlocksMessage, _> = deserialize(&from_sat);
|
2015-04-07 22:51:57 +00:00
|
|
|
assert!(decode.is_ok());
|
|
|
|
let real_decode = decode.unwrap();
|
|
|
|
assert_eq!(real_decode.version, 70002);
|
|
|
|
assert_eq!(real_decode.locator_hashes.len(), 1);
|
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-20 10:15:45 +00:00
|
|
|
assert_eq!(serialize(&real_decode.locator_hashes[0]), genhash);
|
2022-06-07 02:39:25 +00:00
|
|
|
assert_eq!(real_decode.stop_hash, Hash::all_zeros());
|
2015-04-07 22:51:57 +00:00
|
|
|
|
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-20 10:15:45 +00:00
|
|
|
assert_eq!(serialize(&real_decode), from_sat);
|
2015-04-07 22:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn getheaders_message_test() {
|
2022-12-03 19:57:18 +00:00
|
|
|
let from_sat = hex!("72110100014a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b0000000000000000000000000000000000000000000000000000000000000000");
|
2022-11-15 23:23:01 +00:00
|
|
|
let genhash =
|
2022-12-03 19:57:18 +00:00
|
|
|
hex!("4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b");
|
2015-04-07 22:51:57 +00:00
|
|
|
|
2015-04-08 22:23:45 +00:00
|
|
|
let decode: Result<GetHeadersMessage, _> = deserialize(&from_sat);
|
2015-04-07 22:51:57 +00:00
|
|
|
assert!(decode.is_ok());
|
|
|
|
let real_decode = decode.unwrap();
|
|
|
|
assert_eq!(real_decode.version, 70002);
|
|
|
|
assert_eq!(real_decode.locator_hashes.len(), 1);
|
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-20 10:15:45 +00:00
|
|
|
assert_eq!(serialize(&real_decode.locator_hashes[0]), genhash);
|
2022-06-07 02:39:25 +00:00
|
|
|
assert_eq!(real_decode.stop_hash, Hash::all_zeros());
|
2015-04-07 22:51:57 +00:00
|
|
|
|
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-20 10:15:45 +00:00
|
|
|
assert_eq!(serialize(&real_decode), from_sat);
|
2015-04-07 22:51:57 +00:00
|
|
|
}
|
2014-07-18 13:56:17 +00:00
|
|
|
}
|