diff --git a/bitcoin/examples/sighash.rs b/bitcoin/examples/sighash.rs index bc29bbda7..e4bf819ef 100644 --- a/bitcoin/examples/sighash.rs +++ b/bitcoin/examples/sighash.rs @@ -29,8 +29,8 @@ fn compute_sighash_p2wpkh(raw_tx: &[u8], inp_idx: usize, amount: Amount) { // BIP-141: The witness must consist of exactly 2 items (≤ 520 bytes each). The first one a // signature, and the second one a public key. assert_eq!(witness.len(), 2); - let sig_bytes = witness.nth(0).unwrap(); - let pk_bytes = witness.nth(1).unwrap(); + let sig_bytes = witness.get(0).unwrap(); + let pk_bytes = witness.get(1).unwrap(); let sig = ecdsa::Signature::from_slice(sig_bytes).expect("failed to parse sig"); @@ -118,7 +118,7 @@ fn compute_sighash_p2wsh(raw_tx: &[u8], inp_idx: usize, amount: Amount) { //in an M of N multisig, the witness elements from 1 (0-based) to M-2 are signatures (with sighash flags as the last byte) for n in 1..=witness.len() - 2 { - let sig_bytes = witness.nth(n).expect("out of bounds"); + let sig_bytes = witness.get(n).expect("out of bounds"); let sig = ecdsa::Signature::from_slice(sig_bytes).expect("failed to parse sig"); let sig_len = sig_bytes.len() - 1; //last byte is EcdsaSighashType sighash flag //ECDSA signature in DER format lengths are between 70 and 72 bytes diff --git a/bitcoin/src/blockdata/witness.rs b/bitcoin/src/blockdata/witness.rs index 99d42f316..89b998081 100644 --- a/bitcoin/src/blockdata/witness.rs +++ b/bitcoin/src/blockdata/witness.rs @@ -257,7 +257,7 @@ impl<'a> P2TrSpend<'a> { }), 2 if witness.last().expect("len > 0").starts_with(&[TAPROOT_ANNEX_PREFIX]) => { let spend = P2TrSpend::Key { - // signature: witness.second_to_last().expect("len > 1"), + // signature: witness.get_back(1).expect("len > 1"), annex: witness.last(), }; Some(spend) @@ -267,15 +267,15 @@ impl<'a> P2TrSpend<'a> { // arm. 3.. if witness.last().expect("len > 0").starts_with(&[TAPROOT_ANNEX_PREFIX]) => { let spend = P2TrSpend::Script { - leaf_script: Script::from_bytes(witness.third_to_last().expect("len > 2")), - control_block: witness.second_to_last().expect("len > 1"), + leaf_script: Script::from_bytes(witness.get_back(2).expect("len > 2")), + control_block: witness.get_back(1).expect("len > 1"), annex: witness.last(), }; Some(spend) } _ => { let spend = P2TrSpend::Script { - leaf_script: Script::from_bytes(witness.second_to_last().expect("len > 1")), + leaf_script: Script::from_bytes(witness.get_back(1).expect("len > 1")), control_block: witness.last().expect("len > 0"), annex: None, }; @@ -514,13 +514,10 @@ mod test { assert_eq!(expected_wit[i], wit_el.to_lower_hex_string()); } assert_eq!(expected_wit[1], tx.input[0].witness.last().unwrap().to_lower_hex_string()); - assert_eq!( - expected_wit[0], - tx.input[0].witness.second_to_last().unwrap().to_lower_hex_string() - ); - assert_eq!(expected_wit[0], tx.input[0].witness.nth(0).unwrap().to_lower_hex_string()); - assert_eq!(expected_wit[1], tx.input[0].witness.nth(1).unwrap().to_lower_hex_string()); - assert_eq!(None, tx.input[0].witness.nth(2)); + assert_eq!(expected_wit[0], tx.input[0].witness.get_back(1).unwrap().to_lower_hex_string()); + assert_eq!(expected_wit[0], tx.input[0].witness.get(0).unwrap().to_lower_hex_string()); + assert_eq!(expected_wit[1], tx.input[0].witness.get(1).unwrap().to_lower_hex_string()); + assert_eq!(None, tx.input[0].witness.get(2)); assert_eq!(expected_wit[0], tx.input[0].witness[0].to_lower_hex_string()); assert_eq!(expected_wit[1], tx.input[0].witness[1].to_lower_hex_string()); diff --git a/primitives/src/block.rs b/primitives/src/block.rs index b30a2c6c9..b819734e5 100644 --- a/primitives/src/block.rs +++ b/primitives/src/block.rs @@ -70,6 +70,7 @@ where #[cfg(feature = "alloc")] impl Block { /// Constructs a new `Block` without doing any validation. + #[inline] pub fn new_unchecked(header: Header, transactions: Vec) -> Block { Block { header, transactions, witness_root: None, marker: PhantomData:: } } @@ -78,6 +79,7 @@ impl Block { /// /// You should only use this function if you trust the block i.e., it comes from a trusted node. #[must_use] + #[inline] pub fn assume_checked(self, witness_root: Option) -> Block { Block { header: self.header, @@ -88,37 +90,44 @@ impl Block { } /// Decomposes block into its constituent parts. + #[inline] pub fn into_parts(self) -> (Header, Vec) { (self.header, self.transactions) } } #[cfg(feature = "alloc")] impl Block { /// Gets a reference to the block header. + #[inline] pub fn header(&self) -> &Header { &self.header } /// Gets a reference to the block's list of transactions. + #[inline] pub fn transactions(&self) -> &[Transaction] { &self.transactions } /// Returns the cached witness root if one is present. /// /// It is assumed that a block will have the witness root calculated and cached as part of the /// validation process. + #[inline] pub fn cached_witness_root(&self) -> Option { self.witness_root } } #[cfg(feature = "alloc")] impl Block { /// Returns the block hash. + #[inline] pub fn block_hash(&self) -> BlockHash { self.header.block_hash() } } #[cfg(feature = "alloc")] impl From for BlockHash { + #[inline] fn from(block: Block) -> BlockHash { block.block_hash() } } #[cfg(feature = "alloc")] impl From<&Block> for BlockHash { + #[inline] fn from(block: &Block) -> BlockHash { block.block_hash() } } @@ -212,10 +221,12 @@ impl fmt::Debug for Header { } impl From
for BlockHash { + #[inline] fn from(header: Header) -> BlockHash { header.block_hash() } } impl From<&Header> for BlockHash { + #[inline] fn from(header: &Header) -> BlockHash { header.block_hash() } } @@ -263,6 +274,7 @@ impl Version { /// Returns the inner `i32` value. /// /// This is the data type used in consensus code in Bitcoin Core. + #[inline] pub fn to_consensus(self) -> i32 { self.0 } /// Checks whether the version number is signalling a soft fork at the given bit. @@ -286,6 +298,7 @@ impl Version { } impl Default for Version { + #[inline] fn default() -> Version { Self::NO_SOFT_FORK_SIGNALLING } } diff --git a/primitives/src/locktime/absolute.rs b/primitives/src/locktime/absolute.rs index b6321f772..639963aa7 100644 --- a/primitives/src/locktime/absolute.rs +++ b/primitives/src/locktime/absolute.rs @@ -102,6 +102,7 @@ impl LockTime { /// /// # Ok::<_, units::parse::PrefixedHexError>(()) /// ``` + #[inline] pub fn from_hex(s: &str) -> Result { let lock_time = parse::hex_u32_prefixed(s)?; Ok(Self::from_consensus(lock_time)) @@ -119,6 +120,7 @@ impl LockTime { /// /// # Ok::<_, units::parse::UnprefixedHexError>(()) /// ``` + #[inline] pub fn from_unprefixed_hex(s: &str) -> Result { let lock_time = parse::hex_u32_unprefixed(s)?; Ok(Self::from_consensus(lock_time)) @@ -320,6 +322,7 @@ impl From