From 2d8227f09127383ac22464a45250ef1adad79242 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 21 Feb 2025 09:32:35 +1100 Subject: [PATCH 1/2] Hide relative locktime error internals As part of the 1.0 effort and forward maintainability hide the internals of the two error types in the `relative` locktime module. Doing so allows us to remove the `non_exhaustive` attribute. Add getters to get at the error innards. --- primitives/src/locktime/relative.rs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/primitives/src/locktime/relative.rs b/primitives/src/locktime/relative.rs index 663223efe..5acd78169 100644 --- a/primitives/src/locktime/relative.rs +++ b/primitives/src/locktime/relative.rs @@ -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 { From 43ae9d751622c7bef548a469466d74cf01284129 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 21 Feb 2025 09:38:50 +1100 Subject: [PATCH 2/2] primitives: Hide script error internals As part of the 1.0 effort and forward maintainability hide the internals of the two error types in the `script` module. Add getters to get at the invalid size. --- bitcoin/src/address/mod.rs | 6 ++---- primitives/src/script/mod.rs | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs index 45b547c74..e41a88320 100644 --- a/bitcoin/src/address/mod.rs +++ b/bitcoin/src/address/mod.rs @@ -1061,10 +1061,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] diff --git a/primitives/src/script/mod.rs b/primitives/src/script/mod.rs index dda224022..dc2246c19 100644 --- a/primitives/src/script/mod.rs +++ b/primitives/src/script/mod.rs @@ -156,7 +156,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 for RedeemScriptSizeError { @@ -176,7 +181,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 for WitnessScriptSizeError {