2022-06-29 04:05:31 +00:00
|
|
|
// SPDX-License-Identifier: CC0-1.0
|
2018-09-09 10:34:29 +00:00
|
|
|
|
2021-11-05 21:58:18 +00:00
|
|
|
//! Raw PSBT key-value pairs.
|
2018-08-10 15:28:48 +00:00
|
|
|
//!
|
|
|
|
//! Raw PSBT key-value pairs as defined at
|
2021-05-03 11:46:10 +00:00
|
|
|
//! <https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki>.
|
2021-11-05 21:58:18 +00:00
|
|
|
//!
|
2018-08-10 15:28:48 +00:00
|
|
|
|
2022-05-25 06:58:07 +00:00
|
|
|
use core::convert::TryFrom;
|
2022-12-05 23:03:19 +00:00
|
|
|
use core::fmt;
|
2018-08-10 15:28:48 +00:00
|
|
|
|
2022-12-05 23:03:19 +00:00
|
|
|
use super::serialize::{Deserialize, Serialize};
|
|
|
|
use crate::consensus::encode::{
|
|
|
|
self, deserialize, serialize, Decodable, Encodable, ReadExt, VarInt, WriteExt, MAX_VEC_SIZE,
|
|
|
|
};
|
2023-01-10 04:21:20 +00:00
|
|
|
use crate::io;
|
2022-12-05 23:03:19 +00:00
|
|
|
use crate::prelude::*;
|
2022-08-05 03:23:03 +00:00
|
|
|
use crate::psbt::Error;
|
2018-08-10 15:28:48 +00:00
|
|
|
|
|
|
|
/// A PSBT key in its raw byte form.
|
2019-08-09 15:03:12 +00:00
|
|
|
#[derive(Debug, PartialEq, Hash, Eq, Clone, Ord, PartialOrd)]
|
2020-10-25 18:03:20 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
2022-05-25 06:41:59 +00:00
|
|
|
#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))]
|
2018-08-10 15:28:48 +00:00
|
|
|
pub struct Key {
|
|
|
|
/// The type of this PSBT key.
|
|
|
|
pub type_value: u8,
|
|
|
|
/// The key itself in raw byte form.
|
2022-07-25 11:44:28 +00:00
|
|
|
/// `<key> := <keylen> <keytype> <keydata>`
|
2022-05-02 23:02:58 +00:00
|
|
|
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))]
|
2018-08-10 15:28:48 +00:00
|
|
|
pub key: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A PSBT key-value pair in its raw byte form.
|
2022-07-25 11:44:28 +00:00
|
|
|
/// `<keypair> := <key> <value>`
|
2022-08-11 19:46:43 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2020-10-25 18:03:20 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
2022-05-25 06:41:59 +00:00
|
|
|
#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))]
|
2018-08-10 15:28:48 +00:00
|
|
|
pub struct Pair {
|
|
|
|
/// The key of this key-value pair.
|
|
|
|
pub key: Key,
|
2022-07-25 11:44:28 +00:00
|
|
|
/// The value data of this key-value pair in raw byte form.
|
|
|
|
/// `<value> := <valuelen> <valuedata>`
|
2022-05-02 23:02:58 +00:00
|
|
|
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))]
|
2018-08-10 15:28:48 +00:00
|
|
|
pub value: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
2020-12-05 14:10:44 +00:00
|
|
|
/// Default implementation for proprietary key subtyping
|
|
|
|
pub type ProprietaryType = u8;
|
|
|
|
|
|
|
|
/// Proprietary keys (i.e. keys starting with 0xFC byte) with their internal
|
|
|
|
/// structure according to BIP 174.
|
|
|
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
2020-10-25 18:03:20 +00:00
|
|
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
2022-05-25 06:41:59 +00:00
|
|
|
#[cfg_attr(feature = "serde", serde(crate = "actual_serde"))]
|
2022-12-05 23:03:19 +00:00
|
|
|
pub struct ProprietaryKey<Subtype = ProprietaryType>
|
|
|
|
where
|
|
|
|
Subtype: Copy + From<u8> + Into<u8>,
|
|
|
|
{
|
2020-12-05 14:10:44 +00:00
|
|
|
/// Proprietary type prefix used for grouping together keys under some
|
|
|
|
/// application and avoid namespace collision
|
2022-05-02 23:02:58 +00:00
|
|
|
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))]
|
2020-12-05 14:10:44 +00:00
|
|
|
pub prefix: Vec<u8>,
|
|
|
|
/// Custom proprietary subtype
|
|
|
|
pub subtype: Subtype,
|
|
|
|
/// Additional key bytes (like serialized public key data etc)
|
2022-05-02 23:02:58 +00:00
|
|
|
#[cfg_attr(feature = "serde", serde(with = "crate::serde_utils::hex_bytes"))]
|
2020-12-05 14:10:44 +00:00
|
|
|
pub key: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
2018-08-10 15:28:48 +00:00
|
|
|
impl fmt::Display for Key {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2023-01-07 15:39:11 +00:00
|
|
|
write!(f, "type: {:#x}, key: {:x}", self.type_value, self.key.as_hex())
|
2018-08-10 15:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-08 23:16:56 +00:00
|
|
|
impl Key {
|
2022-12-09 16:55:11 +00:00
|
|
|
pub(crate) fn decode<R: io::Read + ?Sized>(r: &mut R) -> Result<Self, 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 VarInt(byte_size): VarInt = Decodable::consensus_decode(r)?;
|
2018-08-10 15:28:48 +00:00
|
|
|
|
|
|
|
if byte_size == 0 {
|
2022-12-09 16:55:11 +00:00
|
|
|
return Err(Error::NoMorePairs);
|
2018-08-10 15:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let key_byte_size: u64 = byte_size - 1;
|
|
|
|
|
|
|
|
if key_byte_size > MAX_VEC_SIZE as u64 {
|
2019-07-11 17:06:42 +00:00
|
|
|
return Err(encode::Error::OversizedVectorAllocation {
|
|
|
|
requested: key_byte_size as usize,
|
|
|
|
max: MAX_VEC_SIZE,
|
clippy: more worning fixes
error[E0308]: mismatched types
--> bitcoin/src/psbt/raw.rs:87:24
|
87 | return Err(encode::Error::OversizedVectorAllocation {
| ________________________^
88 | | requested: key_byte_size as usize,
89 | | max: MAX_VEC_SIZE,
90 | | });
| |_____________^ expected enum `psbt::error::Error`, found enum `consensus::encode::Error`
|
help: try wrapping the expression in `psbt::error::Error::ConsensusEncoding`
|
87 ~ return Err(psbt::error::Error::ConsensusEncoding(encode::Error::OversizedVectorAllocation {
88 | requested: key_byte_size as usize,
89 | max: MAX_VEC_SIZE,
90 ~ }));
|
----
Compiling bitcoin v0.30.0 (/home/vincent/github/work/rust-btc/rust-bitcoin/bitcoin)
Checking bitcoin-fuzz v0.0.1 (/home/vincent/github/work/rust-btc/rust-bitcoin/fuzz)
error: redundant clone
--> bitcoin/examples/taproot-psbt.rs:453:77
|
453 | witness_utxo: { Some(TxOut { value, script_pubkey: script_pubkey.clone() }) },
| ^^^^^^^^ help: remove this
|
= note: `-D clippy::redundant-clone` implied by `-D warnings`
note: this value is dropped without further use
--> bitcoin/examples/taproot-psbt.rs:453:64
|
453 | witness_utxo: { Some(TxOut { value, script_pubkey: script_pubkey.clone() }) },
| ^^^^^^^^^^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
error: could not compile `bitcoin` due to previous error
warning: build failed, waiting for other jobs to finish...
error: redundant clone
--> bitcoin/src/psbt/mod.rs:1095:13
|
1095 | .clone()
| ^^^^^^^^ help: remove this
|
= note: `-D clippy::redundant-clone` implied by `-D warnings`
note: this value is dropped without further use
--> bitcoin/src/psbt/mod.rs:1094:17
|
1094 | assert!(psbt
| _________________^
1095 | | .clone()
| |____________^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2023-10-07 09:36:31 +00:00
|
|
|
}
|
|
|
|
.into());
|
2018-08-10 15:28:48 +00:00
|
|
|
}
|
|
|
|
|
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 type_value: u8 = Decodable::consensus_decode(r)?;
|
2018-08-10 15:28:48 +00:00
|
|
|
|
|
|
|
let mut key = Vec::with_capacity(key_byte_size as usize);
|
|
|
|
for _ in 0..key_byte_size {
|
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
|
|
|
key.push(Decodable::consensus_decode(r)?);
|
2018-08-10 15:28:48 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 00:31:39 +00:00
|
|
|
Ok(Key { type_value, key })
|
2018-08-10 15:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 11:44:28 +00:00
|
|
|
impl Serialize for Key {
|
|
|
|
fn serialize(&self) -> Vec<u8> {
|
|
|
|
let mut buf = Vec::new();
|
2023-08-24 00:37:53 +00:00
|
|
|
VarInt::from(self.key.len() + 1)
|
2022-12-05 23:03:19 +00:00
|
|
|
.consensus_encode(&mut buf)
|
|
|
|
.expect("in-memory writers don't error");
|
2018-08-10 15:28:48 +00:00
|
|
|
|
2022-07-25 11:44:28 +00:00
|
|
|
self.type_value.consensus_encode(&mut buf).expect("in-memory writers don't error");
|
2018-08-10 15:28:48 +00:00
|
|
|
|
|
|
|
for key in &self.key {
|
2022-07-25 11:44:28 +00:00
|
|
|
key.consensus_encode(&mut buf).expect("in-memory writers don't error");
|
2018-08-10 15:28:48 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 11:44:28 +00:00
|
|
|
buf
|
2018-08-10 15:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-25 11:44:28 +00:00
|
|
|
impl Serialize for Pair {
|
|
|
|
fn serialize(&self) -> Vec<u8> {
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
buf.extend(self.key.serialize());
|
|
|
|
// <value> := <valuelen> <valuedata>
|
|
|
|
self.value.consensus_encode(&mut buf).unwrap();
|
|
|
|
buf
|
2018-08-10 15:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-08 23:16:56 +00:00
|
|
|
impl Deserialize for Pair {
|
2022-12-09 16:55:11 +00:00
|
|
|
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
|
2023-01-10 04:21:20 +00:00
|
|
|
let mut decoder = bytes;
|
2022-12-08 23:16:56 +00:00
|
|
|
Pair::decode(&mut decoder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Pair {
|
2022-12-09 16:55:11 +00:00
|
|
|
pub(crate) fn decode<R: io::Read + ?Sized>(r: &mut R) -> Result<Self, Error> {
|
2022-12-05 23:03:19 +00:00
|
|
|
Ok(Pair { key: Key::decode(r)?, value: Decodable::consensus_decode(r)? })
|
2018-08-10 15:28:48 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-05 14:10:44 +00:00
|
|
|
|
2022-12-05 23:03:19 +00:00
|
|
|
impl<Subtype> Encodable for ProprietaryKey<Subtype>
|
|
|
|
where
|
|
|
|
Subtype: Copy + From<u8> + Into<u8>,
|
|
|
|
{
|
2022-06-29 01:22:12 +00:00
|
|
|
fn consensus_encode<W: io::Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::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 mut len = self.prefix.consensus_encode(w)? + 1;
|
|
|
|
w.emit_u8(self.subtype.into())?;
|
|
|
|
w.write_all(&self.key)?;
|
2022-01-25 13:50:06 +00:00
|
|
|
len += self.key.len();
|
2020-12-05 14:10:44 +00:00
|
|
|
Ok(len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 23:03:19 +00:00
|
|
|
impl<Subtype> Decodable for ProprietaryKey<Subtype>
|
|
|
|
where
|
|
|
|
Subtype: Copy + From<u8> + Into<u8>,
|
|
|
|
{
|
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 prefix = Vec::<u8>::consensus_decode(r)?;
|
|
|
|
let subtype = Subtype::from(r.read_u8()?);
|
|
|
|
let key = read_to_end(r)?;
|
2020-12-05 14:10:44 +00:00
|
|
|
|
2022-01-24 00:31:39 +00:00
|
|
|
Ok(ProprietaryKey { prefix, subtype, key })
|
2020-12-05 14:10:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 23:03:19 +00:00
|
|
|
impl<Subtype> ProprietaryKey<Subtype>
|
|
|
|
where
|
|
|
|
Subtype: Copy + From<u8> + Into<u8>,
|
|
|
|
{
|
2020-12-05 14:10:44 +00:00
|
|
|
/// Constructs full [Key] corresponding to this proprietary key type
|
2022-12-05 23:03:19 +00:00
|
|
|
pub fn to_key(&self) -> Key { Key { type_value: 0xFC, key: serialize(self) } }
|
2020-12-05 14:10:44 +00:00
|
|
|
}
|
2022-05-25 06:58:07 +00:00
|
|
|
|
|
|
|
impl<Subtype> TryFrom<Key> for ProprietaryKey<Subtype>
|
|
|
|
where
|
2022-12-05 23:03:19 +00:00
|
|
|
Subtype: Copy + From<u8> + Into<u8>,
|
|
|
|
{
|
2022-05-25 06:58:07 +00:00
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
/// Constructs a [`ProprietaryKey`] from a [`Key`].
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// Returns [`Error::InvalidProprietaryKey`] if `key` does not start with `0xFC` byte.
|
|
|
|
fn try_from(key: Key) -> Result<Self, Self::Error> {
|
|
|
|
if key.type_value != 0xFC {
|
2022-12-05 23:03:19 +00:00
|
|
|
return Err(Error::InvalidProprietaryKey);
|
2022-05-25 06:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(deserialize(&key.key)?)
|
|
|
|
}
|
|
|
|
}
|
2022-08-02 06:10:30 +00:00
|
|
|
|
|
|
|
// core2 doesn't have read_to_end
|
|
|
|
pub(crate) fn read_to_end<D: io::Read>(mut d: D) -> Result<Vec<u8>, io::Error> {
|
|
|
|
let mut result = vec![];
|
|
|
|
let mut buf = [0u8; 64];
|
|
|
|
loop {
|
|
|
|
match d.read(&mut buf) {
|
|
|
|
Ok(0) => break,
|
|
|
|
Ok(n) => result.extend_from_slice(&buf[0..n]),
|
2022-12-05 23:03:19 +00:00
|
|
|
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
|
2022-08-02 06:10:30 +00:00
|
|
|
Err(e) => return Err(e),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Ok(result)
|
|
|
|
}
|