Fix minor comments on count_sigops PR
This commit is contained in:
parent
1a1fe0e313
commit
d961b9c4ee
|
@ -153,7 +153,7 @@ all_opcodes! {
|
||||||
OP_PUSHNUM_NEG1 => 0x4f, "Push the array `0x81` onto the stack.";
|
OP_PUSHNUM_NEG1 => 0x4f, "Push the array `0x81` onto the stack.";
|
||||||
OP_RESERVED => 0x50, "Synonym for OP_RETURN.";
|
OP_RESERVED => 0x50, "Synonym for OP_RETURN.";
|
||||||
OP_PUSHNUM_1 => 0x51, "Push the array `0x01` onto the stack.";
|
OP_PUSHNUM_1 => 0x51, "Push the array `0x01` onto the stack.";
|
||||||
OP_PUSHNUM_2 => 0x52, "the array `0x02` onto the stack.";
|
OP_PUSHNUM_2 => 0x52, "Push the array `0x02` onto the stack.";
|
||||||
OP_PUSHNUM_3 => 0x53, "Push the array `0x03` onto the stack.";
|
OP_PUSHNUM_3 => 0x53, "Push the array `0x03` onto the stack.";
|
||||||
OP_PUSHNUM_4 => 0x54, "Push the array `0x04` onto the stack.";
|
OP_PUSHNUM_4 => 0x54, "Push the array `0x04` onto the stack.";
|
||||||
OP_PUSHNUM_5 => 0x55, "Push the array `0x05` onto the stack.";
|
OP_PUSHNUM_5 => 0x55, "Push the array `0x05` onto the stack.";
|
||||||
|
@ -419,15 +419,8 @@ impl All {
|
||||||
/// # Returns
|
/// # Returns
|
||||||
///
|
///
|
||||||
/// Returns `None` if `self` is not a PUSHNUM.
|
/// Returns `None` if `self` is not a PUSHNUM.
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// use bitcoin::opcodes::all::*;
|
|
||||||
/// assert_eq!(OP_PUSHNUM_5.decode_pushnum().expect("pushnum"), 5)
|
|
||||||
/// ```
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn decode_pushnum(self) -> Option<u8> {
|
pub(crate) const fn decode_pushnum(self) -> Option<u8> {
|
||||||
const START: u8 = OP_PUSHNUM_1.code;
|
const START: u8 = OP_PUSHNUM_1.code;
|
||||||
const END: u8 = OP_PUSHNUM_16.code;
|
const END: u8 = OP_PUSHNUM_16.code;
|
||||||
match self.code {
|
match self.code {
|
||||||
|
@ -577,6 +570,44 @@ mod tests {
|
||||||
assert_eq!(s, " OP_NOP");
|
assert_eq!(s, " OP_NOP");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn decode_pushnum() {
|
||||||
|
// Test all possible opcodes
|
||||||
|
// - Sanity check
|
||||||
|
assert_eq!(OP_PUSHNUM_1.code, 0x51_u8);
|
||||||
|
assert_eq!(OP_PUSHNUM_16.code, 0x60_u8);
|
||||||
|
for i in 0x00..=0xff_u8 {
|
||||||
|
let expected = match i {
|
||||||
|
// OP_PUSHNUM_1 ..= OP_PUSHNUM_16
|
||||||
|
0x51..=0x60 => Some(i - 0x50),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
assert_eq!(All::from(i).decode_pushnum(), expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test the named opcode constants
|
||||||
|
// - This is the OP right before PUSHNUMs start
|
||||||
|
assert!(OP_RESERVED.decode_pushnum().is_none());
|
||||||
|
assert_eq!(OP_PUSHNUM_1.decode_pushnum().expect("pushnum"), 1);
|
||||||
|
assert_eq!(OP_PUSHNUM_2.decode_pushnum().expect("pushnum"), 2);
|
||||||
|
assert_eq!(OP_PUSHNUM_3.decode_pushnum().expect("pushnum"), 3);
|
||||||
|
assert_eq!(OP_PUSHNUM_4.decode_pushnum().expect("pushnum"), 4);
|
||||||
|
assert_eq!(OP_PUSHNUM_5.decode_pushnum().expect("pushnum"), 5);
|
||||||
|
assert_eq!(OP_PUSHNUM_6.decode_pushnum().expect("pushnum"), 6);
|
||||||
|
assert_eq!(OP_PUSHNUM_7.decode_pushnum().expect("pushnum"), 7);
|
||||||
|
assert_eq!(OP_PUSHNUM_8.decode_pushnum().expect("pushnum"), 8);
|
||||||
|
assert_eq!(OP_PUSHNUM_9.decode_pushnum().expect("pushnum"), 9);
|
||||||
|
assert_eq!(OP_PUSHNUM_10.decode_pushnum().expect("pushnum"), 10);
|
||||||
|
assert_eq!(OP_PUSHNUM_11.decode_pushnum().expect("pushnum"), 11);
|
||||||
|
assert_eq!(OP_PUSHNUM_12.decode_pushnum().expect("pushnum"), 12);
|
||||||
|
assert_eq!(OP_PUSHNUM_13.decode_pushnum().expect("pushnum"), 13);
|
||||||
|
assert_eq!(OP_PUSHNUM_14.decode_pushnum().expect("pushnum"), 14);
|
||||||
|
assert_eq!(OP_PUSHNUM_15.decode_pushnum().expect("pushnum"), 15);
|
||||||
|
assert_eq!(OP_PUSHNUM_16.decode_pushnum().expect("pushnum"), 16);
|
||||||
|
// - This is the OP right after PUSHNUMs end
|
||||||
|
assert!(OP_NOP.decode_pushnum().is_none());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn classify_test() {
|
fn classify_test() {
|
||||||
let op174 = OP_CHECKMULTISIG;
|
let op174 = OP_CHECKMULTISIG;
|
||||||
|
|
|
@ -318,7 +318,7 @@ impl Script {
|
||||||
crate::Amount::from_sat(sats)
|
crate::Amount::from_sat(sats)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Count the sigops for this Script using accurate counting.
|
/// Counts the sigops for this Script using accurate counting.
|
||||||
///
|
///
|
||||||
/// In Bitcoin Core, there are two ways to count sigops, "accurate" and "legacy".
|
/// In Bitcoin Core, there are two ways to count sigops, "accurate" and "legacy".
|
||||||
/// This method uses "accurate" counting. This means that OP_CHECKMULTISIG and its
|
/// This method uses "accurate" counting. This means that OP_CHECKMULTISIG and its
|
||||||
|
@ -332,9 +332,13 @@ impl Script {
|
||||||
/// (Note: taproot scripts don't count toward the sigop count of the block,
|
/// (Note: taproot scripts don't count toward the sigop count of the block,
|
||||||
/// nor do they have CHECKMULTISIG operations. This function does not count OP_CHECKSIGADD,
|
/// nor do they have CHECKMULTISIG operations. This function does not count OP_CHECKSIGADD,
|
||||||
/// so do not use this to try and estimate if a taproot script goes over the sigop budget.)
|
/// so do not use this to try and estimate if a taproot script goes over the sigop budget.)
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// If the Script is not able to be parsed to completion.
|
||||||
pub fn count_sigops(&self) -> Result<usize, super::Error> { self.count_sigops_internal(true) }
|
pub fn count_sigops(&self) -> Result<usize, super::Error> { self.count_sigops_internal(true) }
|
||||||
|
|
||||||
/// Count the sigops for this Script using legacy counting.
|
/// Counts the sigops for this Script using legacy counting.
|
||||||
///
|
///
|
||||||
/// In Bitcoin Core, there are two ways to count sigops, "accurate" and "legacy".
|
/// In Bitcoin Core, there are two ways to count sigops, "accurate" and "legacy".
|
||||||
/// This method uses "legacy" counting. This means that OP_CHECKMULTISIG and its
|
/// This method uses "legacy" counting. This means that OP_CHECKMULTISIG and its
|
||||||
|
@ -346,6 +350,10 @@ impl Script {
|
||||||
/// (Note: taproot scripts don't count toward the sigop count of the block,
|
/// (Note: taproot scripts don't count toward the sigop count of the block,
|
||||||
/// nor do they have CHECKMULTISIG operations. This function does not count OP_CHECKSIGADD,
|
/// nor do they have CHECKMULTISIG operations. This function does not count OP_CHECKSIGADD,
|
||||||
/// so do not use this to try and estimate if a taproot script goes over the sigop budget.)
|
/// so do not use this to try and estimate if a taproot script goes over the sigop budget.)
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// If the Script is not able to be parsed to completion.
|
||||||
pub fn count_sigops_legacy(&self) -> Result<usize, super::Error> {
|
pub fn count_sigops_legacy(&self) -> Result<usize, super::Error> {
|
||||||
self.count_sigops_internal(false)
|
self.count_sigops_internal(false)
|
||||||
}
|
}
|
||||||
|
@ -364,7 +372,7 @@ impl Script {
|
||||||
match (accurate, pushnum_cache) {
|
match (accurate, pushnum_cache) {
|
||||||
(true, Some(pushnum)) => {
|
(true, Some(pushnum)) => {
|
||||||
// Add the number of pubkeys in the multisig as sigop count
|
// Add the number of pubkeys in the multisig as sigop count
|
||||||
n += pushnum as usize;
|
n += usize::from(pushnum);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
// MAX_PUBKEYS_PER_MULTISIG from Bitcoin Core
|
// MAX_PUBKEYS_PER_MULTISIG from Bitcoin Core
|
||||||
|
|
Loading…
Reference in New Issue