Merge rust-bitcoin/rust-bitcoin#4091: primitives: Hide error internals

43ae9d7516 primitives: Hide script error internals (Tobin C. Harding)
2d8227f091 Hide relative locktime error internals (Tobin C. Harding)

Pull request description:

  Make the struct fields private and add getters.

ACKs for top commit:
  apoelstra:
    ACK 43ae9d751622c7bef548a469466d74cf01284129; successfully ran local tests; nice! Way easier to understand these types with the new incompatible / expected names

Tree-SHA512: cfe67d60ea61a2a4c27b09071a6b11739ca281bf0b4a655121f90215ce38c3a637acf53a6e01aa2ef26fa80004cd919bf3b3334dbd9566ee2f594cab7750b563
This commit is contained in:
merge-script 2025-03-01 04:44:01 +00:00
commit ef9a24eadf
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
3 changed files with 34 additions and 12 deletions

View File

@ -1067,10 +1067,8 @@ mod tests {
#[test]
fn p2sh_parse_for_large_script() {
let script = ScriptBuf::from_hex("552103a765fc35b3f210b95223846b36ef62a4e53e34e2925270c2c7906b92c9f718eb2103c327511374246759ec8d0b89fa6c6b23b33e11f92c5bc155409d86de0c79180121038cae7406af1f12f4786d820a1466eec7bc5785a1b5e4a387eca6d797753ef6db2103252bfb9dcaab0cd00353f2ac328954d791270203d66c2be8b430f115f451b8a12103e79412d42372c55dd336f2eb6eb639ef9d74a22041ba79382c74da2338fe58ad21035049459a4ebc00e876a9eef02e72a3e70202d3d1f591fc0dd542f93f642021f82102016f682920d9723c61b27f562eb530c926c00106004798b6471e8c52c60ee02057ae12123122313123123ac1231231231231313123131231231231313212313213123123552103a765fc35b3f210b95223846b36ef62a4e53e34e2925270c2c7906b92c9f718eb2103c327511374246759ec8d0b89fa6c6b23b33e11f92c5bc155409d86de0c79180121038cae7406af1f12f4786d820a1466eec7bc5785a1b5e4a387eca6d797753ef6db2103252bfb9dcaab0cd00353f2ac328954d791270203d66c2be8b430f115f451b8a12103e79412d42372c55dd336f2eb6eb639ef9d74a22041ba79382c74da2338fe58ad21035049459a4ebc00e876a9eef02e72a3e70202d3d1f591fc0dd542f93f642021f82102016f682920d9723c61b27f562eb530c926c00106004798b6471e8c52c60ee02057ae12123122313123123ac1231231231231313123131231231231313212313213123123552103a765fc35b3f210b95223846b36ef62a4e53e34e2925270c2c7906b92c9f718eb2103c327511374246759ec8d0b89fa6c6b23b33e11f92c5bc155409d86de0c79180121038cae7406af1f12f4786d820a1466eec7bc5785a1b5e4a387eca6d797753ef6db2103252bfb9dcaab0cd00353f2ac328954d791270203d66c2be8b430f115f451b8a12103e79412d42372c55dd336f2eb6eb639ef9d74a22041ba79382c74da2338fe58ad21035049459a4ebc00e876a9eef02e72a3e70202d3d1f591fc0dd542f93f642021f82102016f682920d9723c61b27f562eb530c926c00106004798b6471e8c52c60ee02057ae12123122313123123ac1231231231231313123131231231231313212313213123123").unwrap();
assert_eq!(
Address::p2sh(&script, NetworkKind::Test),
Err(RedeemScriptSizeError { size: script.len() })
);
let res = Address::p2sh(&script, NetworkKind::Test);
assert_eq!(res.unwrap_err().invalid_size(), script.len())
}
#[test]

View File

@ -401,12 +401,19 @@ impl std::error::Error for DisabledLockTimeError {}
/// Tried to satisfy a lock-by-blocktime lock using a height value.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct IncompatibleHeightError {
/// Attempted to satisfy a lock-by-blocktime lock with this height.
pub height: Height,
height: Height,
/// The inner time value of the lock-by-blocktime lock.
pub time: Time,
time: Time,
}
impl IncompatibleHeightError {
/// Returns the height that was erroneously used to try and satisfy a lock-by-blocktime lock.
pub fn incompatible(&self) -> Height { self.height }
/// Returns the time value of the lock-by-blocktime lock.
pub fn expected(&self) -> Time { self.time }
}
impl fmt::Display for IncompatibleHeightError {
@ -424,12 +431,19 @@ impl std::error::Error for IncompatibleHeightError {}
/// Tried to satisfy a lock-by-blockheight lock using a time value.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct IncompatibleTimeError {
/// Attempted to satisfy a lock-by-blockheight lock with this time.
pub time: Time,
time: Time,
/// The inner height value of the lock-by-blockheight lock.
pub height: Height,
height: Height,
}
impl IncompatibleTimeError {
/// Returns the time that was erroneously used to try and satisfy a lock-by-blockheight lock.
pub fn incompatible(&self) -> Time { self.time }
/// Returns the height value of the lock-by-blockheight lock.
pub fn expected(&self) -> Height { self.height }
}
impl fmt::Display for IncompatibleTimeError {

View File

@ -174,7 +174,12 @@ impl TryFrom<&Script> for WScriptHash {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RedeemScriptSizeError {
/// Invalid redeem script size (cannot exceed 520 bytes).
pub size: usize,
size: usize,
}
impl RedeemScriptSizeError {
/// Returns the invalid redeem script size.
pub fn invalid_size(&self) -> usize { self.size }
}
impl From<Infallible> for RedeemScriptSizeError {
@ -196,7 +201,12 @@ impl std::error::Error for RedeemScriptSizeError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WitnessScriptSizeError {
/// Invalid witness script size (cannot exceed 10,000 bytes).
pub size: usize,
size: usize,
}
impl WitnessScriptSizeError {
/// Returns the invalid witness script size.
pub fn invalid_size(&self) -> usize { self.size }
}
impl From<Infallible> for WitnessScriptSizeError {