From 4b6e86658dd408f64da881c79f66efcd15d6e964 Mon Sep 17 00:00:00 2001 From: Tobin Harding Date: Tue, 25 Jan 2022 10:07:43 +1100 Subject: [PATCH] Refactor is_provably_unspendable Refactor with the aim of making the code easier to read. Code path is covered by current unit tests. Refactor only, no logic changes. --- src/blockdata/script.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index 80c5c580..f71e9a02 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -499,14 +499,25 @@ impl Script { /// Check if this is an OP_RETURN output pub fn is_op_return (&self) -> bool { - !self.0.is_empty() && (opcodes::All::from(self.0[0]) == opcodes::all::OP_RETURN) + match self.0.first() { + Some(b) => *b == opcodes::all::OP_RETURN.into_u8(), + None => false + } } /// Whether a script can be proven to have no satisfying input pub fn is_provably_unspendable(&self) -> bool { - !self.0.is_empty() && - (opcodes::All::from(self.0[0]).classify(opcodes::ClassifyContext::Legacy) == opcodes::Class::ReturnOp || - opcodes::All::from(self.0[0]).classify(opcodes::ClassifyContext::Legacy) == opcodes::Class::IllegalOp) + use blockdata::opcodes::Class::{ReturnOp, IllegalOp}; + + match self.0.first() { + Some(b) => { + let first = opcodes::All::from(*b); + let class = first.classify(opcodes::ClassifyContext::Legacy); + + class == ReturnOp || class == IllegalOp + }, + None => false, + } } /// Gets the minimum value an output with this script should have in order to be