From 94b678e73f4e18a276a00ffc3ff81a75dc3b06ff Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Mon, 2 Jan 2023 23:14:04 +0100 Subject: [PATCH] Rename `push_scriptint` and make it private `push_scriptint` is a significant footgun with an unclear name. This renames it and unpublishes to avoid mistakes by downstream crates. Closes #1517 --- bitcoin/src/blockdata/constants.rs | 4 ++-- bitcoin/src/blockdata/script.rs | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/bitcoin/src/blockdata/constants.rs b/bitcoin/src/blockdata/constants.rs index b5e18bbf..3eeb9e03 100644 --- a/bitcoin/src/blockdata/constants.rs +++ b/bitcoin/src/blockdata/constants.rs @@ -73,8 +73,8 @@ fn bitcoin_genesis_tx() -> Transaction { }; // Inputs - let in_script = script::Builder::new().push_scriptint(486604799) - .push_scriptint(4) + let in_script = script::Builder::new().push_int(486604799) + .push_int_non_minimal(4) .push_slice(b"The Times 03/Jan/2009 Chancellor on brink of second bailout for banks") .into_script(); ret.input.push(TxIn { diff --git a/bitcoin/src/blockdata/script.rs b/bitcoin/src/blockdata/script.rs index 979b4b3d..454d0c35 100644 --- a/bitcoin/src/blockdata/script.rs +++ b/bitcoin/src/blockdata/script.rs @@ -1757,12 +1757,13 @@ impl Builder { self.push_opcode(opcodes::OP_FALSE) } // Otherwise encode it as data - else { self.push_scriptint(data) } + else { self.push_int_non_minimal(data) } } - /// Adds instructions to push an integer onto the stack, using the explicit - /// encoding regardless of the availability of dedicated opcodes. - pub fn push_scriptint(self, data: i64) -> Builder { + /// Adds instructions to push an integer onto the stack without optimization. + /// + /// This uses the explicit encoding regardless of the availability of dedicated opcodes. + pub(super) fn push_int_non_minimal(self, data: i64) -> Builder { let mut buf = [0u8; 8]; let len = write_scriptint(&mut buf, data); self.push_slice(&buf[..len]) @@ -2029,7 +2030,7 @@ mod test { script = script.push_int(4); comp.push(84u8); assert_eq!(script.as_bytes(), &comp[..]); script = script.push_int(-1); comp.push(79u8); assert_eq!(script.as_bytes(), &comp[..]); // forced scriptint - script = script.push_scriptint(4); comp.extend([1u8, 4].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]); + script = script.push_int_non_minimal(4); comp.extend([1u8, 4].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]); // big ints script = script.push_int(17); comp.extend([1u8, 17].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]); script = script.push_int(10000); comp.extend([2u8, 16, 39].iter().cloned()); assert_eq!(script.as_bytes(), &comp[..]);