Skip to content

Commit c5de4f4

Browse files
junbeomleesgkim126
authored andcommitted
Add logic to limit expensive operations in script
1 parent b50459f commit c5de4f4

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

vm/src/executor.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use ccrypto::{blake256, keccak256, ripemd160, sha256};
1818
use ckey::{verify, Public, Signature, SIGNATURE_LENGTH};
1919
use primitives::H256;
2020

21-
use instruction::{is_valid_unlock_script, Instruction};
21+
use instruction::{has_expensive_opcodes, is_valid_unlock_script, Instruction};
2222

2323
const DEFAULT_MAX_MEMORY: usize = 1024;
2424

@@ -152,6 +152,10 @@ pub fn execute(
152152
return Ok(ScriptResult::Fail)
153153
}
154154

155+
if has_expensive_opcodes(unlock) {
156+
return Ok(ScriptResult::Fail)
157+
}
158+
155159
let param_scripts: Vec<_> = params.iter().map(|p| Instruction::PushB(p.clone())).rev().collect();
156160
let script = [unlock, &param_scripts, lock].concat();
157161

vm/src/instruction.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,28 @@ pub fn is_valid_unlock_script(instrs: &[Instruction]) -> bool {
4646
_ => false,
4747
})
4848
}
49+
50+
pub fn has_expensive_opcodes(instrs: &[Instruction]) -> bool {
51+
let count = instrs.iter().filter(|instr| instr == &&Instruction::ChkSig).count();
52+
count >= 6
53+
}
54+
55+
#[test]
56+
fn should_true_when_script_has_more_than_six_chksig_opcodes() {
57+
let expensive_script = vec![
58+
Instruction::ChkSig,
59+
Instruction::ChkSig,
60+
Instruction::ChkSig,
61+
Instruction::ChkSig,
62+
Instruction::ChkSig,
63+
Instruction::ChkSig,
64+
];
65+
assert_eq!(has_expensive_opcodes(&expensive_script), true);
66+
}
67+
68+
#[test]
69+
fn should_false_when_script_has_lower_than_five_chksig_opcodes() {
70+
let unexpensive_script =
71+
vec![Instruction::ChkSig, Instruction::ChkSig, Instruction::ChkSig, Instruction::ChkSig, Instruction::ChkSig];
72+
assert_eq!(has_expensive_opcodes(&unexpensive_script), false);
73+
}

0 commit comments

Comments
 (0)