Skip to content

Commit 95ea71f

Browse files
committed
Use new OP_x variants (for x in 1-16)
Remove all the new lint warnings by using the new opcodes instead of the old ones.
1 parent 85f9b10 commit 95ea71f

File tree

6 files changed

+69
-69
lines changed

6 files changed

+69
-69
lines changed

bitcoin/src/blockdata/opcodes.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ macro_rules! all_opcodes {
5353
/// Empty stack is also FALSE.
5454
pub const OP_FALSE: Opcode = OP_PUSHBYTES_0;
5555
/// Number 1 is also TRUE.
56-
pub const OP_TRUE: Opcode = OP_PUSHNUM_1;
56+
pub const OP_TRUE: Opcode = OP_1;
5757
/// Previously called OP_NOP2.
5858
pub const OP_NOP2: Opcode = OP_CLTV;
5959
/// Previously called OP_NOP3.
@@ -452,8 +452,8 @@ impl Opcode {
452452
(OP_1NEGATE, _) => Class::PushNum(-1),
453453

454454
// 16 opcodes of PushNum class
455-
(op, _) if op.code >= OP_PUSHNUM_1.code && op.code <= OP_PUSHNUM_16.code =>
456-
Class::PushNum(1 + self.code as i32 - OP_PUSHNUM_1.code as i32),
455+
(op, _) if op.code >= OP_1.code && op.code <= OP_16.code =>
456+
Class::PushNum(1 + self.code as i32 - OP_1.code as i32),
457457

458458
// 76 opcodes of PushBytes class
459459
(op, _) if op.code <= OP_PUSHBYTES_75.code => Class::PushBytes(self.code as u32),
@@ -477,8 +477,8 @@ impl Opcode {
477477
#[inline]
478478
#[must_use]
479479
pub const fn decode_pushnum(self) -> Option<u8> {
480-
const START: u8 = OP_PUSHNUM_1.code;
481-
const END: u8 = OP_PUSHNUM_16.code;
480+
const START: u8 = OP_1.code;
481+
const END: u8 = OP_16.code;
482482
match self.code {
483483
START..=END => Some(self.code - START + 1),
484484
_ => None,
@@ -627,11 +627,11 @@ mod tests {
627627
fn decode_pushnum() {
628628
// Test all possible opcodes
629629
// - Sanity check
630-
assert_eq!(OP_PUSHNUM_1.code, 0x51_u8);
631-
assert_eq!(OP_PUSHNUM_16.code, 0x60_u8);
630+
assert_eq!(OP_1.code, 0x51_u8);
631+
assert_eq!(OP_16.code, 0x60_u8);
632632
for i in 0x00..=0xff_u8 {
633633
let expected = match i {
634-
// OP_PUSHNUM_1 ..= OP_PUSHNUM_16
634+
// OP_1 ..= OP_16
635635
0x51..=0x60 => Some(i - 0x50),
636636
_ => None,
637637
};
@@ -641,22 +641,22 @@ mod tests {
641641
// Test the named opcode constants
642642
// - This is the OP right before PUSHNUMs start
643643
assert!(OP_RESERVED.decode_pushnum().is_none());
644-
assert_eq!(OP_PUSHNUM_1.decode_pushnum().expect("pushnum"), 1);
645-
assert_eq!(OP_PUSHNUM_2.decode_pushnum().expect("pushnum"), 2);
646-
assert_eq!(OP_PUSHNUM_3.decode_pushnum().expect("pushnum"), 3);
647-
assert_eq!(OP_PUSHNUM_4.decode_pushnum().expect("pushnum"), 4);
648-
assert_eq!(OP_PUSHNUM_5.decode_pushnum().expect("pushnum"), 5);
649-
assert_eq!(OP_PUSHNUM_6.decode_pushnum().expect("pushnum"), 6);
650-
assert_eq!(OP_PUSHNUM_7.decode_pushnum().expect("pushnum"), 7);
651-
assert_eq!(OP_PUSHNUM_8.decode_pushnum().expect("pushnum"), 8);
652-
assert_eq!(OP_PUSHNUM_9.decode_pushnum().expect("pushnum"), 9);
653-
assert_eq!(OP_PUSHNUM_10.decode_pushnum().expect("pushnum"), 10);
654-
assert_eq!(OP_PUSHNUM_11.decode_pushnum().expect("pushnum"), 11);
655-
assert_eq!(OP_PUSHNUM_12.decode_pushnum().expect("pushnum"), 12);
656-
assert_eq!(OP_PUSHNUM_13.decode_pushnum().expect("pushnum"), 13);
657-
assert_eq!(OP_PUSHNUM_14.decode_pushnum().expect("pushnum"), 14);
658-
assert_eq!(OP_PUSHNUM_15.decode_pushnum().expect("pushnum"), 15);
659-
assert_eq!(OP_PUSHNUM_16.decode_pushnum().expect("pushnum"), 16);
644+
assert_eq!(OP_1.decode_pushnum().expect("pushnum"), 1);
645+
assert_eq!(OP_2.decode_pushnum().expect("pushnum"), 2);
646+
assert_eq!(OP_3.decode_pushnum().expect("pushnum"), 3);
647+
assert_eq!(OP_4.decode_pushnum().expect("pushnum"), 4);
648+
assert_eq!(OP_5.decode_pushnum().expect("pushnum"), 5);
649+
assert_eq!(OP_6.decode_pushnum().expect("pushnum"), 6);
650+
assert_eq!(OP_7.decode_pushnum().expect("pushnum"), 7);
651+
assert_eq!(OP_8.decode_pushnum().expect("pushnum"), 8);
652+
assert_eq!(OP_9.decode_pushnum().expect("pushnum"), 9);
653+
assert_eq!(OP_10.decode_pushnum().expect("pushnum"), 10);
654+
assert_eq!(OP_11.decode_pushnum().expect("pushnum"), 11);
655+
assert_eq!(OP_12.decode_pushnum().expect("pushnum"), 12);
656+
assert_eq!(OP_13.decode_pushnum().expect("pushnum"), 13);
657+
assert_eq!(OP_14.decode_pushnum().expect("pushnum"), 14);
658+
assert_eq!(OP_15.decode_pushnum().expect("pushnum"), 15);
659+
assert_eq!(OP_16.decode_pushnum().expect("pushnum"), 16);
660660
// - This is the OP right after PUSHNUMs end
661661
assert!(OP_NOP.decode_pushnum().is_none());
662662
}

bitcoin/src/blockdata/script/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ impl<T> Builder<T> {
7878
let bytes = data.as_ref().as_bytes();
7979
if bytes.len() == 1 && (bytes[0] == 0x81 || bytes[0] <= 16) {
8080
match bytes[0] {
81-
0x81 => self.push_opcode(OP_PUSHNUM_NEG1),
81+
0x81 => self.push_opcode(OP_1NEGATE),
8282
0 => self.push_opcode(OP_PUSHBYTES_0),
83-
1..=16 => self.push_opcode(Opcode::from(bytes[0] + (OP_PUSHNUM_1.to_u8() - 1))),
83+
1..=16 => self.push_opcode(Opcode::from(bytes[0] + (OP_1.to_u8() - 1))),
8484
_ => self, // unreachable arm
8585
}
8686
} else {

bitcoin/src/blockdata/script/owned.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ internal_macros::define_extension_trait! {
6464
/// Does not check whether `n` is in the range of [-2^31 +1...2^31 -1].
6565
fn push_int_unchecked(&mut self, n: i64) {
6666
match n {
67-
-1 => self.push_opcode(OP_PUSHNUM_NEG1),
67+
-1 => self.push_opcode(OP_1NEGATE),
6868
0 => self.push_opcode(OP_PUSHBYTES_0),
69-
1..=16 => self.push_opcode(Opcode::from(n as u8 + (OP_PUSHNUM_1.to_u8() - 1))),
69+
1..=16 => self.push_opcode(Opcode::from(n as u8 + (OP_1.to_u8() - 1))),
7070
_ => self.push_int_non_minimal(n),
7171
}
7272
}
@@ -79,9 +79,9 @@ internal_macros::define_extension_trait! {
7979
let bytes = data.as_ref().as_bytes();
8080
if bytes.len() == 1 && (bytes[0] == 0x81 || bytes[0] <= 16) {
8181
match bytes[0] {
82-
0x81 => { self.push_opcode(OP_PUSHNUM_NEG1); },
82+
0x81 => { self.push_opcode(OP_1NEGATE); },
8383
0 => { self.push_opcode(OP_PUSHBYTES_0); },
84-
1..=16 => { self.push_opcode(Opcode::from(bytes[0] + (OP_PUSHNUM_1.to_u8() - 1))); },
84+
1..=16 => { self.push_opcode(Opcode::from(bytes[0] + (OP_1.to_u8() - 1))); },
8585
_ => {}, // unreachable arm
8686
}
8787
} else {

bitcoin/src/blockdata/script/tests.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -858,34 +858,34 @@ fn script_get_sigop_count() {
858858
.push_slice([42; 20])
859859
.push_opcode(OP_EQUALVERIFY)
860860
.push_opcode(OP_CHECKSIGVERIFY)
861-
.push_opcode(OP_PUSHNUM_1)
861+
.push_opcode(OP_1)
862862
.into_script()
863863
.count_sigops(),
864864
1
865865
);
866866
let multi = Script::builder()
867-
.push_opcode(OP_PUSHNUM_1)
867+
.push_opcode(OP_1)
868868
.push_slice([3; 33])
869869
.push_slice([3; 33])
870870
.push_slice([3; 33])
871-
.push_opcode(OP_PUSHNUM_3)
871+
.push_opcode(OP_3)
872872
.push_opcode(OP_CHECKMULTISIG)
873873
.into_script();
874874
assert_eq!(multi.count_sigops(), 3);
875875
assert_eq!(multi.count_sigops_legacy(), 20);
876876
let multi_verify = Script::builder()
877-
.push_opcode(OP_PUSHNUM_1)
877+
.push_opcode(OP_1)
878878
.push_slice([3; 33])
879879
.push_slice([3; 33])
880880
.push_slice([3; 33])
881-
.push_opcode(OP_PUSHNUM_3)
881+
.push_opcode(OP_3)
882882
.push_opcode(OP_CHECKMULTISIGVERIFY)
883-
.push_opcode(OP_PUSHNUM_1)
883+
.push_opcode(OP_1)
884884
.into_script();
885885
assert_eq!(multi_verify.count_sigops(), 3);
886886
assert_eq!(multi_verify.count_sigops_legacy(), 20);
887887
let multi_nopushnum_pushdata = Script::builder()
888-
.push_opcode(OP_PUSHNUM_1)
888+
.push_opcode(OP_1)
889889
.push_slice([3; 33])
890890
.push_slice([3; 33])
891891
.push_slice([3; 33])
@@ -894,7 +894,7 @@ fn script_get_sigop_count() {
894894
assert_eq!(multi_nopushnum_pushdata.count_sigops(), 20);
895895
assert_eq!(multi_nopushnum_pushdata.count_sigops_legacy(), 20);
896896
let multi_nopushnum_op = Script::builder()
897-
.push_opcode(OP_PUSHNUM_1)
897+
.push_opcode(OP_1)
898898
.push_slice([3; 33])
899899
.push_slice([3; 33])
900900
.push_opcode(OP_DROP)
@@ -1017,10 +1017,10 @@ fn instruction_script_num_parse() {
10171017
];
10181018
let ops = [
10191019
(Instruction::Op(opcodes::all::OP_PUSHDATA4), None),
1020-
(Instruction::Op(opcodes::all::OP_PUSHNUM_NEG1), Some(-1)),
1020+
(Instruction::Op(opcodes::all::OP_1NEGATE), Some(-1)),
10211021
(Instruction::Op(opcodes::all::OP_RESERVED), None),
1022-
(Instruction::Op(opcodes::all::OP_PUSHNUM_1), Some(1)),
1023-
(Instruction::Op(opcodes::all::OP_PUSHNUM_16), Some(16)),
1022+
(Instruction::Op(opcodes::all::OP_1), Some(1)),
1023+
(Instruction::Op(opcodes::all::OP_16), Some(16)),
10241024
(Instruction::Op(opcodes::all::OP_NOP), None),
10251025
];
10261026
for (input, expected) in &push_bytes {

bitcoin/src/blockdata/script/witness_version.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ impl TryFrom<Opcode> for WitnessVersion {
122122
fn try_from(opcode: Opcode) -> Result<Self, Self::Error> {
123123
match opcode.to_u8() {
124124
0 => Ok(WitnessVersion::V0),
125-
version if version >= OP_PUSHNUM_1.to_u8() && version <= OP_PUSHNUM_16.to_u8() =>
126-
WitnessVersion::try_from(version - OP_PUSHNUM_1.to_u8() + 1),
125+
version if version >= OP_1.to_u8() && version <= OP_16.to_u8() =>
126+
WitnessVersion::try_from(version - OP_1.to_u8() + 1),
127127
invalid => Err(TryFromError { invalid }),
128128
}
129129
}
@@ -145,7 +145,7 @@ impl From<WitnessVersion> for Opcode {
145145
fn from(version: WitnessVersion) -> Opcode {
146146
match version {
147147
WitnessVersion::V0 => OP_PUSHBYTES_0,
148-
no => Opcode::from(OP_PUSHNUM_1.to_u8() + no.to_num() - 1),
148+
no => Opcode::from(OP_1.to_u8() + no.to_num() - 1),
149149
}
150150
}
151151
}

primitives/src/opcodes.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ macro_rules! all_opcodes {
5555
/// Empty stack is also `FALSE`.
5656
pub const OP_FALSE: Opcode = OP_PUSHBYTES_0;
5757
/// Number 1 is also TRUE.
58-
pub const OP_TRUE: Opcode = OP_PUSHNUM_1;
58+
pub const OP_TRUE: Opcode = OP_1;
5959
/// Previously called `OP_NOP2`.
6060
pub const OP_NOP2: Opcode = OP_CLTV;
6161
/// Previously called `OP_NOP3`.
@@ -454,8 +454,8 @@ impl Opcode {
454454
(OP_1NEGATE, _) => Class::PushNum(-1),
455455

456456
// 16 opcodes of PushNum class
457-
(op, _) if op.code >= OP_PUSHNUM_1.code && op.code <= OP_PUSHNUM_16.code =>
458-
Class::PushNum(1 + i32::from(self.code) - i32::from(OP_PUSHNUM_1.code)),
457+
(op, _) if op.code >= OP_1.code && op.code <= OP_16.code =>
458+
Class::PushNum(1 + i32::from(self.code) - i32::from(OP_1.code)),
459459

460460
// 76 opcodes of PushBytes class
461461
(op, _) if op.code <= OP_PUSHBYTES_75.code => Class::PushBytes(u32::from(self.code)),
@@ -471,16 +471,16 @@ impl Opcode {
471471

472472
/// Decodes PUSHNUM [`Opcode`] as a `u8` representing its number (1-16).
473473
///
474-
/// Does not convert `OP_FALSE` to 0. Only `1` to `OP_PUSHNUM_16` are covered.
474+
/// Does not convert `OP_FALSE` to 0. Only `1` to `OP_16` are covered.
475475
///
476476
/// # Returns
477477
///
478478
/// Returns `None` if `self` is not a PUSHNUM.
479479
#[inline]
480480
#[must_use]
481481
pub const fn decode_pushnum(self) -> Option<u8> {
482-
const START: u8 = OP_PUSHNUM_1.code;
483-
const END: u8 = OP_PUSHNUM_16.code;
482+
const START: u8 = OP_1.code;
483+
const END: u8 = OP_16.code;
484484
match self.code {
485485
START..=END => Some(self.code - START + 1),
486486
_ => None,
@@ -633,11 +633,11 @@ mod tests {
633633
fn decode_pushnum() {
634634
// Test all possible opcodes
635635
// - Sanity check
636-
assert_eq!(OP_PUSHNUM_1.code, 0x51_u8);
637-
assert_eq!(OP_PUSHNUM_16.code, 0x60_u8);
636+
assert_eq!(OP_1.code, 0x51_u8);
637+
assert_eq!(OP_16.code, 0x60_u8);
638638
for i in 0x00..=0xff_u8 {
639639
let expected = match i {
640-
// OP_PUSHNUM_1 ..= OP_PUSHNUM_16
640+
// OP_1 ..= OP_16
641641
0x51..=0x60 => Some(i - 0x50),
642642
_ => None,
643643
};
@@ -647,22 +647,22 @@ mod tests {
647647
// Test the named opcode constants
648648
// - This is the OP right before PUSHNUMs start
649649
assert!(OP_RESERVED.decode_pushnum().is_none());
650-
assert_eq!(OP_PUSHNUM_1.decode_pushnum().expect("pushnum"), 1);
651-
assert_eq!(OP_PUSHNUM_2.decode_pushnum().expect("pushnum"), 2);
652-
assert_eq!(OP_PUSHNUM_3.decode_pushnum().expect("pushnum"), 3);
653-
assert_eq!(OP_PUSHNUM_4.decode_pushnum().expect("pushnum"), 4);
654-
assert_eq!(OP_PUSHNUM_5.decode_pushnum().expect("pushnum"), 5);
655-
assert_eq!(OP_PUSHNUM_6.decode_pushnum().expect("pushnum"), 6);
656-
assert_eq!(OP_PUSHNUM_7.decode_pushnum().expect("pushnum"), 7);
657-
assert_eq!(OP_PUSHNUM_8.decode_pushnum().expect("pushnum"), 8);
658-
assert_eq!(OP_PUSHNUM_9.decode_pushnum().expect("pushnum"), 9);
659-
assert_eq!(OP_PUSHNUM_10.decode_pushnum().expect("pushnum"), 10);
660-
assert_eq!(OP_PUSHNUM_11.decode_pushnum().expect("pushnum"), 11);
661-
assert_eq!(OP_PUSHNUM_12.decode_pushnum().expect("pushnum"), 12);
662-
assert_eq!(OP_PUSHNUM_13.decode_pushnum().expect("pushnum"), 13);
663-
assert_eq!(OP_PUSHNUM_14.decode_pushnum().expect("pushnum"), 14);
664-
assert_eq!(OP_PUSHNUM_15.decode_pushnum().expect("pushnum"), 15);
665-
assert_eq!(OP_PUSHNUM_16.decode_pushnum().expect("pushnum"), 16);
650+
assert_eq!(OP_1.decode_pushnum().expect("pushnum"), 1);
651+
assert_eq!(OP_2.decode_pushnum().expect("pushnum"), 2);
652+
assert_eq!(OP_3.decode_pushnum().expect("pushnum"), 3);
653+
assert_eq!(OP_4.decode_pushnum().expect("pushnum"), 4);
654+
assert_eq!(OP_5.decode_pushnum().expect("pushnum"), 5);
655+
assert_eq!(OP_6.decode_pushnum().expect("pushnum"), 6);
656+
assert_eq!(OP_7.decode_pushnum().expect("pushnum"), 7);
657+
assert_eq!(OP_8.decode_pushnum().expect("pushnum"), 8);
658+
assert_eq!(OP_9.decode_pushnum().expect("pushnum"), 9);
659+
assert_eq!(OP_10.decode_pushnum().expect("pushnum"), 10);
660+
assert_eq!(OP_11.decode_pushnum().expect("pushnum"), 11);
661+
assert_eq!(OP_12.decode_pushnum().expect("pushnum"), 12);
662+
assert_eq!(OP_13.decode_pushnum().expect("pushnum"), 13);
663+
assert_eq!(OP_14.decode_pushnum().expect("pushnum"), 14);
664+
assert_eq!(OP_15.decode_pushnum().expect("pushnum"), 15);
665+
assert_eq!(OP_16.decode_pushnum().expect("pushnum"), 16);
666666
// - This is the OP right after PUSHNUMs end
667667
assert!(OP_NOP.decode_pushnum().is_none());
668668
}

0 commit comments

Comments
 (0)