Skip to content

Commit 2494af9

Browse files
author
ChallengeDev210
committed
Merge rust-bitcoin/rust-bitcoin#752: Make Map trait private
dfd8924 Remove insert_pair from Map trait (Tobin Harding) ad75d51 Make Map trait private to psbt module (Tobin Harding) 53225c0 Improve docs in map module (Tobin Harding) 92059c2 Add full stops to rustdocs (Tobin Harding) 11c046b Refactor match arms (Tobin Harding) e6af569 Move imports to top of file (Tobin Harding) Pull request description: The `Map` method `insert_pair` is never called for `PartiallySignedTransaction`. Separate the method into its own trait (`Insert`) and delete dead code. The dead code contains the alleged bug in #576. - Patch 1: Preparatory cleanup - Patch 2: Preparatory refactor - Patch 3 and 4: Improve docs in the module that this PR touches - Patch 5: Make `Map` trait private to the `psbt` module - ~Patch 6: Make `concensus_decode_global` method into a function~ - Patch ~7~ 6: Pull `insert_pair` method out of `Map` trait into newly create `Insert` trait Resolves: rust-bitcoin/rust-bitcoin#576 (Title of PR is `Make Map trait private` because that is the API break.) ACKs for top commit: dr-orlovsky: ACK dfd8924 apoelstra: ACK dfd8924 Tree-SHA512: 1a78294bc8a455552d93caf64db697f886345ba979f574abad55820415958fee1c2dd16945f4eafdbe542fa202cb7e08618aa137ec7ee22b3c9dac5df0328157
2 parents 5e80b85 + 356a7c8 commit 2494af9

File tree

6 files changed

+65
-75
lines changed

6 files changed

+65
-75
lines changed

src/util/psbt/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ macro_rules! impl_psbtmap_consensus_decoding {
7575

7676
loop {
7777
match $crate::consensus::Decodable::consensus_decode(&mut d) {
78-
Ok(pair) => $crate::util::psbt::Map::insert_pair(&mut rv, pair)?,
78+
Ok(pair) => rv.insert_pair(pair)?,
7979
Err($crate::consensus::encode::Error::Psbt($crate::util::psbt::Error::NoMorePairs)) => return Ok(rv),
8080
Err(e) => return Err(e),
8181
}

src/util/psbt/map/global.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,6 @@ const PSBT_GLOBAL_VERSION: u8 = 0xFB;
3737
const PSBT_GLOBAL_PROPRIETARY: u8 = 0xFC;
3838

3939
impl Map for PartiallySignedTransaction {
40-
fn insert_pair(&mut self, pair: raw::Pair) -> Result<(), encode::Error> {
41-
let raw::Pair {
42-
key: raw_key,
43-
value: raw_value,
44-
} = pair;
45-
46-
match raw_key.type_value {
47-
PSBT_GLOBAL_UNSIGNED_TX => return Err(Error::DuplicateKey(raw_key).into()),
48-
PSBT_GLOBAL_PROPRIETARY => match self.proprietary.entry(raw::ProprietaryKey::from_key(raw_key.clone())?) {
49-
btree_map::Entry::Vacant(empty_key) => {empty_key.insert(raw_value);},
50-
btree_map::Entry::Occupied(_) => return Err(Error::DuplicateKey(raw_key).into()),
51-
}
52-
_ => match self.unknown.entry(raw_key) {
53-
btree_map::Entry::Vacant(empty_key) => {empty_key.insert(raw_value);},
54-
btree_map::Entry::Occupied(k) => return Err(Error::DuplicateKey(k.key().clone()).into()),
55-
}
56-
}
57-
58-
Ok(())
59-
}
60-
6140
fn get_pairs(&self) -> Result<Vec<raw::Pair>, io::Error> {
6241
let mut rv: Vec<raw::Pair> = Default::default();
6342

@@ -278,11 +257,15 @@ impl PartiallySignedTransaction {
278257
}
279258
}
280259
PSBT_GLOBAL_PROPRIETARY => match proprietary.entry(raw::ProprietaryKey::from_key(pair.key.clone())?) {
281-
btree_map::Entry::Vacant(empty_key) => {empty_key.insert(pair.value);},
260+
btree_map::Entry::Vacant(empty_key) => {
261+
empty_key.insert(pair.value);
262+
},
282263
btree_map::Entry::Occupied(_) => return Err(Error::DuplicateKey(pair.key).into()),
283264
}
284265
_ => match unknowns.entry(pair.key) {
285-
btree_map::Entry::Vacant(empty_key) => {empty_key.insert(pair.value);},
266+
btree_map::Entry::Vacant(empty_key) => {
267+
empty_key.insert(pair.value);
268+
},
286269
btree_map::Entry::Occupied(k) => return Err(Error::DuplicateKey(k.key().clone()).into()),
287270
}
288271
}

src/util/psbt/map/input.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -108,32 +108,32 @@ pub struct Input {
108108
/// other scripts necessary for this input to pass validation.
109109
pub final_script_witness: Option<Witness>,
110110
/// TODO: Proof of reserves commitment
111-
/// RIPEMD160 hash to preimage map
111+
/// RIPEMD160 hash to preimage map.
112112
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_byte_values"))]
113113
pub ripemd160_preimages: BTreeMap<ripemd160::Hash, Vec<u8>>,
114-
/// SHA256 hash to preimage map
114+
/// SHA256 hash to preimage map.
115115
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_byte_values"))]
116116
pub sha256_preimages: BTreeMap<sha256::Hash, Vec<u8>>,
117-
/// HSAH160 hash to preimage map
117+
/// HSAH160 hash to preimage map.
118118
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_byte_values"))]
119119
pub hash160_preimages: BTreeMap<hash160::Hash, Vec<u8>>,
120-
/// HAS256 hash to preimage map
120+
/// HAS256 hash to preimage map.
121121
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_byte_values"))]
122122
pub hash256_preimages: BTreeMap<sha256d::Hash, Vec<u8>>,
123-
/// Serialized schnorr signature with sighash type for key spend
123+
/// Serialized schnorr signature with sighash type for key spend.
124124
pub tap_key_sig: Option<SchnorrSig>,
125-
/// Map of <xonlypubkey>|<leafhash> with signature
125+
/// Map of <xonlypubkey>|<leafhash> with signature.
126126
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_as_seq"))]
127127
pub tap_script_sigs: BTreeMap<(XOnlyPublicKey, TapLeafHash), SchnorrSig>,
128-
/// Map of Control blocks to Script version pair
128+
/// Map of Control blocks to Script version pair.
129129
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_as_seq"))]
130130
pub tap_scripts: BTreeMap<ControlBlock, (Script, LeafVersion)>,
131-
/// Map of tap root x only keys to origin info and leaf hashes contained in it
131+
/// Map of tap root x only keys to origin info and leaf hashes contained in it.
132132
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_as_seq"))]
133133
pub tap_key_origins: BTreeMap<XOnlyPublicKey, (Vec<TapLeafHash>, KeySource)>,
134-
/// Taproot Internal key
134+
/// Taproot Internal key.
135135
pub tap_internal_key : Option<XOnlyPublicKey>,
136-
/// Taproot Merkle root
136+
/// Taproot Merkle root.
137137
pub tap_merkle_root : Option<TapBranchHash>,
138138
/// Proprietary key-value pairs for this input.
139139
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_as_seq_byte_values"))]
@@ -189,8 +189,8 @@ impl PsbtSigHashType {
189189
}
190190
}
191191

192-
impl Map for Input {
193-
fn insert_pair(&mut self, pair: raw::Pair) -> Result<(), encode::Error> {
192+
impl Input {
193+
pub(super) fn insert_pair(&mut self, pair: raw::Pair) -> Result<(), encode::Error> {
194194
let raw::Pair {
195195
key: raw_key,
196196
value: raw_value,
@@ -284,23 +284,28 @@ impl Map for Input {
284284
self.tap_merkle_root <= <raw_key: _>|< raw_value: TapBranchHash>
285285
}
286286
}
287-
PSBT_IN_PROPRIETARY => match self.proprietary.entry(raw::ProprietaryKey::from_key(raw_key.clone())?) {
288-
btree_map::Entry::Vacant(empty_key) => {empty_key.insert(raw_value);},
289-
btree_map::Entry::Occupied(_) => return Err(Error::DuplicateKey(raw_key).into()),
287+
PSBT_IN_PROPRIETARY => {
288+
let key = raw::ProprietaryKey::from_key(raw_key.clone())?;
289+
match self.proprietary.entry(key) {
290+
btree_map::Entry::Vacant(empty_key) => {
291+
empty_key.insert(raw_value);
292+
},
293+
btree_map::Entry::Occupied(_) => return Err(Error::DuplicateKey(raw_key).into()),
294+
}
290295
}
291296
_ => match self.unknown.entry(raw_key) {
292297
btree_map::Entry::Vacant(empty_key) => {
293298
empty_key.insert(raw_value);
294299
}
295-
btree_map::Entry::Occupied(k) => {
296-
return Err(Error::DuplicateKey(k.key().clone()).into())
297-
}
300+
btree_map::Entry::Occupied(k) => return Err(Error::DuplicateKey(k.key().clone()).into()),
298301
},
299302
}
300303

301304
Ok(())
302305
}
306+
}
303307

308+
impl Map for Input {
304309
fn get_pairs(&self) -> Result<Vec<raw::Pair>, io::Error> {
305310
let mut rv: Vec<raw::Pair> = Default::default();
306311

src/util/psbt/map/mod.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,22 @@ use consensus::encode;
2020
use util::psbt;
2121
use util::psbt::raw;
2222

23-
/// A trait that describes a PSBT key-value map.
24-
pub trait Map {
25-
/// Attempt to insert a key-value pair.
26-
fn insert_pair(&mut self, pair: raw::Pair) -> Result<(), encode::Error>;
23+
mod global;
24+
mod input;
25+
mod output;
2726

27+
pub use self::input::{Input, PsbtSigHashType};
28+
pub use self::output::{Output, TapTree};
29+
30+
/// A trait that describes a PSBT key-value map.
31+
pub(super) trait Map {
2832
/// Attempt to get all key-value pairs.
2933
fn get_pairs(&self) -> Result<Vec<raw::Pair>, io::Error>;
3034

3135
/// Attempt to merge with another key-value map of the same type.
3236
fn merge(&mut self, other: Self) -> Result<(), psbt::Error>;
3337

34-
/// Encodes map data with bitcoin consensus encoding
38+
/// Encodes map data with bitcoin consensus encoding.
3539
fn consensus_encode_map<S: io::Write>(
3640
&self,
3741
mut s: S,
@@ -47,12 +51,3 @@ pub trait Map {
4751
Ok(len + encode::Encodable::consensus_encode(&0x00_u8, s)?)
4852
}
4953
}
50-
51-
// place at end to pick up macros
52-
mod global;
53-
mod input;
54-
mod output;
55-
56-
pub use self::input::{Input, PsbtSigHashType};
57-
pub use self::output::Output;
58-
pub use self::output::TapTree;

src/util/psbt/map/output.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ pub struct Output {
5858
/// corresponding master key fingerprints and derivation paths.
5959
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_as_seq"))]
6060
pub bip32_derivation: BTreeMap<secp256k1::PublicKey, KeySource>,
61-
/// The internal pubkey
61+
/// The internal pubkey.
6262
pub tap_internal_key: Option<XOnlyPublicKey>,
63-
/// Taproot Output tree
63+
/// Taproot Output tree.
6464
pub tap_tree: Option<TapTree>,
65-
/// Map of tap root x only keys to origin info and leaf hashes contained in it
65+
/// Map of tap root x only keys to origin info and leaf hashes contained in it.
6666
#[cfg_attr(feature = "serde", serde(with = "::serde_utils::btreemap_as_seq"))]
6767
pub tap_key_origins: BTreeMap<XOnlyPublicKey, (Vec<TapLeafHash>, KeySource)>,
6868
/// Proprietary key-value pairs for this output.
@@ -79,7 +79,7 @@ pub struct Output {
7979
pub unknown: BTreeMap<raw::Key, Vec<u8>>,
8080
}
8181

82-
/// Taproot Tree representing a finalized [`TaprootBuilder`] (a complete binary tree)
82+
/// Taproot Tree representing a finalized [`TaprootBuilder`] (a complete binary tree).
8383
#[derive(Clone, Debug)]
8484
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8585
pub struct TapTree(pub(crate) TaprootBuilder);
@@ -93,7 +93,7 @@ impl PartialEq for TapTree {
9393
impl Eq for TapTree {}
9494

9595
impl TapTree {
96-
// get the inner node info as the builder is finalized
96+
/// Gets the inner node info as the builder is finalized.
9797
fn node_info(&self) -> &NodeInfo {
9898
// The builder algorithm invariant guarantees that is_complete builder
9999
// have only 1 element in branch and that is not None.
@@ -102,8 +102,10 @@ impl TapTree {
102102
self.0.branch()[0].as_ref().expect("from_inner only parses is_complete builders")
103103
}
104104

105-
/// Convert a [`TaprootBuilder`] into a tree if it is complete binary tree.
106-
/// Returns the inner as Err if it is not a complete tree
105+
/// Converts a [`TaprootBuilder`] into a tree if it is complete binary tree.
106+
///
107+
/// # Return
108+
/// A `TapTree` iff the `inner` builder is complete, otherwise return the inner as `Err`.
107109
pub fn from_inner(inner: TaprootBuilder) -> Result<Self, TaprootBuilder> {
108110
if inner.is_complete() {
109111
Ok(TapTree(inner))
@@ -112,15 +114,14 @@ impl TapTree {
112114
}
113115
}
114116

115-
/// Convert self into builder [`TaprootBuilder`]. The builder is guaranteed to
116-
/// be finalized.
117+
/// Converts self into builder [`TaprootBuilder`]. The builder is guaranteed to be finalized.
117118
pub fn into_inner(self) -> TaprootBuilder {
118119
self.0
119120
}
120121
}
121122

122-
impl Map for Output {
123-
fn insert_pair(&mut self, pair: raw::Pair) -> Result<(), encode::Error> {
123+
impl Output {
124+
pub(super) fn insert_pair(&mut self, pair: raw::Pair) -> Result<(), encode::Error> {
124125
let raw::Pair {
125126
key: raw_key,
126127
value: raw_value,
@@ -142,9 +143,14 @@ impl Map for Output {
142143
self.bip32_derivation <= <raw_key: secp256k1::PublicKey>|<raw_value: KeySource>
143144
}
144145
}
145-
PSBT_OUT_PROPRIETARY => match self.proprietary.entry(raw::ProprietaryKey::from_key(raw_key.clone())?) {
146-
btree_map::Entry::Vacant(empty_key) => {empty_key.insert(raw_value);},
147-
btree_map::Entry::Occupied(_) => return Err(Error::DuplicateKey(raw_key).into()),
146+
PSBT_OUT_PROPRIETARY => {
147+
let key = raw::ProprietaryKey::from_key(raw_key.clone())?;
148+
match self.proprietary.entry(key) {
149+
btree_map::Entry::Vacant(empty_key) => {
150+
empty_key.insert(raw_value);
151+
},
152+
btree_map::Entry::Occupied(_) => return Err(Error::DuplicateKey(raw_key).into()),
153+
}
148154
}
149155
PSBT_OUT_TAP_INTERNAL_KEY => {
150156
impl_psbt_insert_pair! {
@@ -165,15 +171,15 @@ impl Map for Output {
165171
btree_map::Entry::Vacant(empty_key) => {
166172
empty_key.insert(raw_value);
167173
}
168-
btree_map::Entry::Occupied(k) => {
169-
return Err(Error::DuplicateKey(k.key().clone()).into())
170-
}
171-
},
174+
btree_map::Entry::Occupied(k) => return Err(Error::DuplicateKey(k.key().clone()).into()),
175+
}
172176
}
173177

174178
Ok(())
175179
}
180+
}
176181

182+
impl Map for Output {
177183
fn get_pairs(&self) -> Result<Vec<raw::Pair>, io::Error> {
178184
let mut rv: Vec<raw::Pair> = Default::default();
179185

src/util/psbt/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ mod macros;
3939
pub mod serialize;
4040

4141
mod map;
42-
pub use self::map::{Map, Input, Output, TapTree};
42+
pub use self::map::{Input, Output, TapTree};
43+
use self::map::Map;
4344

4445
use util::bip32::{ExtendedPubKey, KeySource};
4546

0 commit comments

Comments
 (0)