From 0ec6d96a7beece696701d0c146adfa1ad03ddd9a Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Wed, 9 Feb 2022 18:37:34 +0100 Subject: [PATCH] Cleanup after `Instructions` refactoring * Changes `Option` to `Result` to avoid repeated `.ok_or(...)` * Renames `max` to `min_push_len` * Removes useless variable --- src/blockdata/script.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index d45faa1e..d17df327 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -743,18 +743,18 @@ impl<'a> Instructions<'a> { /// takes `len` bytes long slice from iterator and returns it advancing iterator /// if the iterator is not long enough `None` is returned and the iterator is killed /// to avoid returning an infinite stream of errors. - fn take_slice_or_kill(&mut self, len: usize) -> Option<&'a [u8]> { + fn take_slice_or_kill(&mut self, len: usize) -> Result<&'a [u8], Error> { if self.data.len() >= len { let slice = &self.data.as_slice()[..len]; self.data.nth(len.max(1) - 1); - Some(slice) + Ok(slice) } else { self.kill(); - None + Err(Error::EarlyEndOfScript) } } - fn next_push_data_len(&mut self, len: usize, max: usize) -> Option, Error>> { + fn next_push_data_len(&mut self, len: usize, min_push_len: usize) -> Option, Error>> { let n = match read_uint_iter(&mut self.data, len) { Ok(n) => n, // We do exhaustive matching to not forget to handle new variants if we extend @@ -766,11 +766,11 @@ impl<'a> Instructions<'a> { return Some(Err(Error::EarlyEndOfScript)); }, }; - if self.enforce_minimal && n < max { + if self.enforce_minimal && n < min_push_len { self.kill(); return Some(Err(Error::NonMinimalPush)); } - Some(self.take_slice_or_kill(n).map(Instruction::PushBytes).ok_or(Error::EarlyEndOfScript)) + Some(self.take_slice_or_kill(n).map(Instruction::PushBytes)) } } @@ -801,7 +801,7 @@ impl<'a> Iterator for Instructions<'a> { Some(Ok(Instruction::PushBytes(&[]))) }, _ => { - Some(self.take_slice_or_kill(n).map(Instruction::PushBytes).ok_or(Error::EarlyEndOfScript)) + Some(self.take_slice_or_kill(n).map(Instruction::PushBytes)) } } } @@ -816,8 +816,7 @@ impl<'a> Iterator for Instructions<'a> { } // Everything else we can push right through _ => { - let ret = Some(Ok(Instruction::Op(opcodes::All::from(byte)))); - ret + Some(Ok(Instruction::Op(opcodes::All::from(byte)))) } } }