Skip to content

Commit

Permalink
Remove f_ prefix from translator method names
Browse files Browse the repository at this point in the history
We can shorten the unusual `Translator` trait method names with no loss
of meaning.

It is not immediately obvious what the `f_` prefix means, it is a relic
of history from when we used function pointers.

Remove the `f_` prefix, shortening the translator method names to just
the thing they are translating.
  • Loading branch information
tcharding authored and sanket1729 committed Jun 7, 2022
1 parent 627d5c6 commit 870c0cd
Show file tree
Hide file tree
Showing 14 changed files with 68 additions and 69 deletions.
2 changes: 1 addition & 1 deletion src/descriptor/bare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,6 @@ where
where
T: Translator<P, Q, E>,
{
Ok(Pkh::new(t.f_pk(&self.pk)?))
Ok(Pkh::new(t.pk(&self.pk)?))
}
}
20 changes: 10 additions & 10 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,11 @@ impl Descriptor<DescriptorPublicKey> {
struct Derivator(u32);

impl PkTranslator<DescriptorPublicKey, DerivedDescriptorKey, ()> for Derivator {
fn f_pk(&mut self, pk: &DescriptorPublicKey) -> Result<DerivedDescriptorKey, ()> {
fn pk(&mut self, pk: &DescriptorPublicKey) -> Result<DerivedDescriptorKey, ()> {
Ok(pk.clone().derive(self.0))
}

fn f_pkh(&mut self, pkh: &DescriptorPublicKey) -> Result<DerivedDescriptorKey, ()> {
fn pkh(&mut self, pkh: &DescriptorPublicKey) -> Result<DerivedDescriptorKey, ()> {
Ok(pkh.clone().derive(self.0))
}
}
Expand Down Expand Up @@ -574,14 +574,14 @@ impl Descriptor<DescriptorPublicKey> {
PkTranslator<DerivedDescriptorKey, bitcoin::PublicKey, ConversionError>
for Derivator<'a, C>
{
fn f_pk(
fn pk(
&mut self,
pk: &DerivedDescriptorKey,
) -> Result<bitcoin::PublicKey, ConversionError> {
pk.derive_public_key(&self.0)
}

fn f_pkh(
fn pkh(
&mut self,
pkh: &DerivedDescriptorKey,
) -> Result<bitcoin::hashes::hash160::Hash, ConversionError> {
Expand Down Expand Up @@ -633,15 +633,15 @@ impl Descriptor<DescriptorPublicKey> {
impl<'a, C: secp256k1::Signing> Translator<String, DescriptorPublicKey, Error>
for KeyMapWrapper<'a, C>
{
fn f_pk(&mut self, pk: &String) -> Result<DescriptorPublicKey, Error> {
fn pk(&mut self, pk: &String) -> Result<DescriptorPublicKey, Error> {
parse_key(pk, &mut self.0, self.1)
}

fn f_pkh(&mut self, pkh: &String) -> Result<DescriptorPublicKey, Error> {
fn pkh(&mut self, pkh: &String) -> Result<DescriptorPublicKey, Error> {
parse_key(pkh, &mut self.0, self.1)
}

fn f_sha256(&mut self, sha256: &String) -> Result<sha256::Hash, Error> {
fn sha256(&mut self, sha256: &String) -> Result<sha256::Hash, Error> {
let hash =
sha256::Hash::from_str(sha256).map_err(|e| Error::Unexpected(e.to_string()))?;
Ok(hash)
Expand All @@ -661,15 +661,15 @@ impl Descriptor<DescriptorPublicKey> {
struct KeyMapLookUp<'a>(&'a KeyMap);

impl<'a> Translator<DescriptorPublicKey, String, ()> for KeyMapLookUp<'a> {
fn f_pk(&mut self, pk: &DescriptorPublicKey) -> Result<String, ()> {
fn pk(&mut self, pk: &DescriptorPublicKey) -> Result<String, ()> {
key_to_string(pk, self.0)
}

fn f_pkh(&mut self, pkh: &DescriptorPublicKey) -> Result<String, ()> {
fn pkh(&mut self, pkh: &DescriptorPublicKey) -> Result<String, ()> {
key_to_string(pkh, self.0)
}

fn f_sha256(&mut self, sha256: &sha256::Hash) -> Result<String, ()> {
fn sha256(&mut self, sha256: &sha256::Hash) -> Result<String, ()> {
Ok(sha256.to_string())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/descriptor/segwitv0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,6 @@ where
where
T: Translator<P, Q, E>,
{
Ok(Wpkh::new(t.f_pk(&self.pk)?).expect("Uncompressed keys in Wpkh"))
Ok(Wpkh::new(t.pk(&self.pk)?).expect("Uncompressed keys in Wpkh"))
}
}
2 changes: 1 addition & 1 deletion src/descriptor/sortedmulti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> SortedMultiVec<Pk, Ctx> {
T: Translator<Pk, Q, FuncError>,
Q: MiniscriptKey,
{
let pks: Result<Vec<Q>, _> = self.pks.iter().map(|pk| t.f_pk(pk)).collect();
let pks: Result<Vec<Q>, _> = self.pks.iter().map(|pk| t.pk(pk)).collect();
Ok(SortedMultiVec {
k: self.k,
pks: pks?,
Expand Down
2 changes: 1 addition & 1 deletion src/descriptor/tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ where
T: Translator<P, Q, E>,
{
let translate_desc = Tr {
internal_key: translate.f_pk(&self.internal_key)?,
internal_key: translate.pk(&self.internal_key)?,
tree: match &self.tree {
Some(tree) => Some(tree.translate_helper(translate)?),
None => None,
Expand Down
9 changes: 4 additions & 5 deletions src/interpreter/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,16 +378,15 @@ impl<Ctx: ScriptContext> ToNoChecks for Miniscript<bitcoin::PublicKey, Ctx> {
struct TranslateFullPk;

impl PkTranslator<bitcoin::PublicKey, BitcoinKey, ()> for TranslateFullPk {
fn f_pk(&mut self, pk: &bitcoin::PublicKey) -> Result<BitcoinKey, ()> {
fn pk(&mut self, pk: &bitcoin::PublicKey) -> Result<BitcoinKey, ()> {
Ok(BitcoinKey::Fullkey(*pk))
}

fn f_pkh(&mut self, pkh: &hash160::Hash) -> Result<TypedHash160, ()> {
fn pkh(&mut self, pkh: &hash160::Hash) -> Result<TypedHash160, ()> {
Ok(TypedHash160::FullKey(*pkh))
}
}

// specify the () error type as this cannot error
self.real_translate_pk(&mut TranslateFullPk)
.expect("Translation should succeed")
}
Expand All @@ -399,11 +398,11 @@ impl<Ctx: ScriptContext> ToNoChecks for Miniscript<bitcoin::XOnlyPublicKey, Ctx>
struct TranslateXOnlyPk;

impl PkTranslator<bitcoin::XOnlyPublicKey, BitcoinKey, ()> for TranslateXOnlyPk {
fn f_pk(&mut self, pk: &bitcoin::XOnlyPublicKey) -> Result<BitcoinKey, ()> {
fn pk(&mut self, pk: &bitcoin::XOnlyPublicKey) -> Result<BitcoinKey, ()> {
Ok(BitcoinKey::XOnlyPublicKey(*pk))
}

fn f_pkh(&mut self, pkh: &hash160::Hash) -> Result<TypedHash160, ()> {
fn pkh(&mut self, pkh: &hash160::Hash) -> Result<TypedHash160, ()> {
Ok(TypedHash160::XonlyKey(*pkh))
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,13 @@ where
Q: MiniscriptKey,
{
/// Translates public keys P -> Q.
fn f_pk(&mut self, pk: &P) -> Result<Q, E>;
fn pk(&mut self, pk: &P) -> Result<Q, E>;

/// Translates public key hashes P::Hash -> Q::Hash.
fn f_pkh(&mut self, pkh: &P::Hash) -> Result<Q::Hash, E>;
fn pkh(&mut self, pkh: &P::Hash) -> Result<Q::Hash, E>;

/// Translates sha256 hashes from P::Sha256 -> Q::Sha256
fn f_sha256(&mut self, sha256: &P::Sha256) -> Result<Q::Sha256, E>;
fn sha256(&mut self, sha256: &P::Sha256) -> Result<Q::Sha256, E>;
}

/// Provides the conversion information required in [`TranslatePk`].
Expand All @@ -415,10 +415,10 @@ where
Q: MiniscriptKey<Sha256 = P::Sha256>,
{
/// Provides the translation public keys P -> Q
fn f_pk(&mut self, pk: &P) -> Result<Q, E>;
fn pk(&mut self, pk: &P) -> Result<Q, E>;

/// Provides the translation public keys hashes P::Hash -> Q::Hash
fn f_pkh(&mut self, pkh: &P::Hash) -> Result<Q::Hash, E>;
fn pkh(&mut self, pkh: &P::Hash) -> Result<Q::Hash, E>;
}

impl<P, Q, E, T> Translator<P, Q, E> for T
Expand All @@ -427,15 +427,15 @@ where
P: MiniscriptKey,
Q: MiniscriptKey<Sha256 = P::Sha256>,
{
fn f_pk(&mut self, pk: &P) -> Result<Q, E> {
<Self as PkTranslator<P, Q, E>>::f_pk(self, pk)
fn pk(&mut self, pk: &P) -> Result<Q, E> {
<Self as PkTranslator<P, Q, E>>::pk(self, pk)
}

fn f_pkh(&mut self, pkh: &<P as MiniscriptKey>::Hash) -> Result<<Q as MiniscriptKey>::Hash, E> {
<Self as PkTranslator<P, Q, E>>::f_pkh(self, pkh)
fn pkh(&mut self, pkh: &<P as MiniscriptKey>::Hash) -> Result<<Q as MiniscriptKey>::Hash, E> {
<Self as PkTranslator<P, Q, E>>::pkh(self, pkh)
}

fn f_sha256(&mut self, sha256: &<P as MiniscriptKey>::Sha256) -> Result<<Q>::Sha256, E> {
fn sha256(&mut self, sha256: &<P as MiniscriptKey>::Sha256) -> Result<<Q>::Sha256, E> {
Ok(sha256.clone())
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/miniscript/astelem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
T: Translator<Pk, Q, E>,
{
let frag: Terminal<Q, CtxQ> = match *self {
Terminal::PkK(ref p) => Terminal::PkK(t.f_pk(p)?),
Terminal::PkH(ref p) => Terminal::PkH(t.f_pkh(p)?),
Terminal::PkK(ref p) => Terminal::PkK(t.pk(p)?),
Terminal::PkH(ref p) => Terminal::PkH(t.pkh(p)?),
Terminal::After(n) => Terminal::After(n),
Terminal::Older(n) => Terminal::Older(n),
Terminal::Sha256(ref x) => Terminal::Sha256(t.f_sha256(&x)?),
Terminal::Sha256(ref x) => Terminal::Sha256(t.sha256(&x)?),
Terminal::Hash256(x) => Terminal::Hash256(x),
Terminal::Ripemd160(x) => Terminal::Ripemd160(x),
Terminal::Hash160(x) => Terminal::Hash160(x),
Expand Down Expand Up @@ -185,11 +185,11 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
Terminal::Thresh(k, subs?)
}
Terminal::Multi(k, ref keys) => {
let keys: Result<Vec<Q>, _> = keys.iter().map(|k| t.f_pk(k)).collect();
let keys: Result<Vec<Q>, _> = keys.iter().map(|k| t.pk(k)).collect();
Terminal::Multi(k, keys?)
}
Terminal::MultiA(k, ref keys) => {
let keys: Result<Vec<Q>, _> = keys.iter().map(|k| t.f_pk(k)).collect();
let keys: Result<Vec<Q>, _> = keys.iter().map(|k| t.pk(k)).collect();
Terminal::MultiA(k, keys?)
}
};
Expand Down
12 changes: 6 additions & 6 deletions src/miniscript/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
/// NB: The function analyzes only single miniscript item and not any of its descendants in AST.
/// To obtain a list of all public keys within AST use [Miniscript::iter_pk()] function, for example
/// `miniscript.iter_pubkeys().collect()`.
pub fn get_leaf_pk(&self) -> Vec<Pk> {
pub fn get_leapk(&self) -> Vec<Pk> {
match self.node {
Terminal::PkK(ref key) => vec![key.clone()],
Terminal::Multi(_, ref keys) | Terminal::MultiA(_, ref keys) => keys.clone(),
Expand All @@ -138,7 +138,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
/// NB: The function analyzes only single miniscript item and not any of its descendants in AST.
/// To obtain a list of all public key hashes within AST use [Miniscript::iter_pkh()] function,
/// for example `miniscript.iter_pubkey_hashes().collect()`.
pub fn get_leaf_pkh(&self) -> Vec<Pk::Hash> {
pub fn get_leapkh(&self) -> Vec<Pk::Hash> {
match self.node {
Terminal::PkH(ref hash) => vec![hash.clone()],
Terminal::PkK(ref key) => vec![key.to_pubkeyhash()],
Expand All @@ -156,7 +156,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
/// NB: The function analyzes only single miniscript item and not any of its descendants in AST.
/// To obtain a list of all public keys or hashes within AST use [Miniscript::iter_pk_pkh()]
/// function, for example `miniscript.iter_pubkeys_and_hashes().collect()`.
pub fn get_leaf_pk_pkh(&self) -> Vec<PkPkh<Pk>> {
pub fn get_leapk_pkh(&self) -> Vec<PkPkh<Pk>> {
match self.node {
Terminal::PkH(ref hash) => vec![PkPkh::HashedPubkey(hash.clone())],
Terminal::PkK(ref key) => vec![PkPkh::PlainPubkey(key.clone())],
Expand Down Expand Up @@ -605,7 +605,7 @@ pub mod test {
return;
}
let ms = *ms.branches().first().unwrap_or(&&ms);
assert_eq!(ms.get_leaf_pk(), k);
assert_eq!(ms.get_leapk(), k);
})
}

Expand All @@ -624,7 +624,7 @@ pub mod test {
.collect();
// In our test cases we always have plain keys going first
all.extend(h);
assert_eq!(ms.get_leaf_pkh(), all);
assert_eq!(ms.get_leapkh(), all);
})
}

Expand All @@ -642,7 +642,7 @@ pub mod test {
} else {
k.into_iter().map(|k| PkPkh::PlainPubkey(k)).collect()
};
assert_eq!(ms.get_leaf_pk_pkh(), r);
assert_eq!(ms.get_leapk_pkh(), r);
})
}

Expand Down
10 changes: 5 additions & 5 deletions src/policy/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,17 +344,17 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
/// // we would use the general Translator Trait.
/// impl Translator<String, bitcoin::PublicKey, ()> for StrPkTranslator {
/// // Provides the translation public keys P -> Q
/// fn f_pk(&mut self, pk: &String) -> Result<bitcoin::PublicKey, ()> {
/// fn pk(&mut self, pk: &String) -> Result<bitcoin::PublicKey, ()> {
/// self.pk_map.get(pk).copied().ok_or(()) // Dummy Err
/// }
///
/// // If our policy also contained other fragments, we could provide the translation here.
/// fn f_pkh(&mut self, pkh: &String) -> Result<hash160::Hash, ()> {
/// fn pkh(&mut self, pkh: &String) -> Result<hash160::Hash, ()> {
/// unreachable!("Policy does not contain any pkh fragment");
/// }
///
/// // If our policy also contained other fragments, we could provide the translation here.
/// fn f_sha256(&mut self, sha256: &String) -> Result<sha256::Hash, ()> {
/// fn sha256(&mut self, sha256: &String) -> Result<sha256::Hash, ()> {
/// unreachable!("Policy does not contain any sha256 fragment");
/// }
/// }
Expand Down Expand Up @@ -385,8 +385,8 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
match *self {
Policy::Unsatisfiable => Ok(Policy::Unsatisfiable),
Policy::Trivial => Ok(Policy::Trivial),
Policy::Key(ref pk) => t.f_pk(pk).map(Policy::Key),
Policy::Sha256(ref h) => t.f_sha256(h).map(Policy::Sha256),
Policy::Key(ref pk) => t.pk(pk).map(Policy::Key),
Policy::Sha256(ref h) => t.sha256(h).map(Policy::Sha256),
Policy::Hash256(ref h) => Ok(Policy::Hash256(*h)),
Policy::Ripemd160(ref h) => Ok(Policy::Ripemd160(*h)),
Policy::Hash160(ref h) => Ok(Policy::Hash160(*h)),
Expand Down
10 changes: 5 additions & 5 deletions src/policy/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,18 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
/// // If we also wanted to provide mapping of other associated types(sha256, older etc),
/// // we would use the general Translator Trait.
/// impl Translator<String, bitcoin::PublicKey, ()> for StrPkTranslator {
/// fn f_pk(&mut self, pk: &String) -> Result<bitcoin::PublicKey, ()> {
/// fn pk(&mut self, pk: &String) -> Result<bitcoin::PublicKey, ()> {
/// unreachable!("Policy does not contain any pk fragment");
/// }
///
/// // Provides the translation public keys P::Hash -> Q::Hash
/// // If our policy also contained other fragments, we could provide the translation here.
/// fn f_pkh(&mut self, pkh: &String) -> Result<hash160::Hash, ()> {
/// fn pkh(&mut self, pkh: &String) -> Result<hash160::Hash, ()> {
/// self.pk_map.get(pkh).copied().ok_or(()) // Dummy Err
/// }
///
/// // If our policy also contained other fragments, we could provide the translation here.
/// fn f_sha256(&mut self, sha256: &String) -> Result<sha256::Hash, ()> {
/// fn sha256(&mut self, sha256: &String) -> Result<sha256::Hash, ()> {
/// unreachable!("Policy does not contain any sha256 fragment");
/// }
/// }
Expand Down Expand Up @@ -141,8 +141,8 @@ impl<Pk: MiniscriptKey> Policy<Pk> {
match *self {
Policy::Unsatisfiable => Ok(Policy::Unsatisfiable),
Policy::Trivial => Ok(Policy::Trivial),
Policy::KeyHash(ref pkh) => t.f_pkh(pkh).map(Policy::KeyHash),
Policy::Sha256(ref h) => t.f_sha256(h).map(Policy::Sha256),
Policy::KeyHash(ref pkh) => t.pkh(pkh).map(Policy::KeyHash),
Policy::Sha256(ref h) => t.sha256(h).map(Policy::Sha256),
Policy::Hash256(ref h) => Ok(Policy::Hash256(*h)),
Policy::Ripemd160(ref h) => Ok(Policy::Ripemd160(*h)),
Policy::Hash160(ref h) => Ok(Policy::Hash160(*h)),
Expand Down
10 changes: 5 additions & 5 deletions src/psbt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,14 +940,14 @@ struct XOnlyHashLookUp(
impl PkTranslator<DescriptorPublicKey, bitcoin::PublicKey, descriptor::ConversionError>
for XOnlyHashLookUp
{
fn f_pk(
fn pk(
&mut self,
xpk: &DescriptorPublicKey,
) -> Result<bitcoin::PublicKey, descriptor::ConversionError> {
xpk.derive_public_key(&self.1)
}

fn f_pkh(
fn pkh(
&mut self,
xpk: &DescriptorPublicKey,
) -> Result<hash160::Hash, descriptor::ConversionError> {
Expand All @@ -969,7 +969,7 @@ struct KeySourceLookUp(
impl PkTranslator<DescriptorPublicKey, bitcoin::PublicKey, descriptor::ConversionError>
for KeySourceLookUp
{
fn f_pk(
fn pk(
&mut self,
xpk: &DescriptorPublicKey,
) -> Result<bitcoin::PublicKey, descriptor::ConversionError> {
Expand All @@ -981,11 +981,11 @@ impl PkTranslator<DescriptorPublicKey, bitcoin::PublicKey, descriptor::Conversio
Ok(derived)
}

fn f_pkh(
fn pkh(
&mut self,
xpk: &DescriptorPublicKey,
) -> Result<hash160::Hash, descriptor::ConversionError> {
Ok(self.f_pk(xpk)?.to_pubkeyhash())
Ok(self.pk(xpk)?.to_pubkeyhash())
}
}

Expand Down
Loading

0 comments on commit 870c0cd

Please sign in to comment.