Skip to content

Commit cf97c9c

Browse files
committed
Add SUCCESS, FAIL opcode
1 parent 970d660 commit cf97c9c

File tree

7 files changed

+20
-2
lines changed

7 files changed

+20
-2
lines changed

spec/CodeChain-Virtual-Machine.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ Leading zeros must be truncated. Note that it is allowed to decode value with le
3939
## Special instructions
4040
* NOP(0x00): Do nothing
4141
* BURN(0x01): Stop script execution, and return `BURN` as result.
42+
* SUCCESS(0x02): Stop script execution, and return `SUCCESS` as result.
43+
* FAIL(0x03): Stop script execution, and return `FAIL` as result.
4244

4345
## Boolean computation
4446
* NOT(0x10): Pop one value from stack as boolean, and push negated value.
@@ -84,4 +86,4 @@ Leading zeros must be truncated. Note that it is allowed to decode value with le
8486
* KECCAK256(0x93): Pop one value from stack, and push keccak-256 hash of it.
8587

8688
## Environment
87-
* BLKNUM(0xa0): Push block number specified in parcel to stack as integer. If there's no specified block number, machine must fail immediately.
89+
* BLKNUM(0xa0): Push block number specified in parcel to stack as integer. If there's no specified block number, machine must fail immediately.

vm/src/decoder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub fn decode(bytes: &[u8]) -> Result<Vec<Instruction>, DecoderError> {
3030
match *b {
3131
opcode::NOP => result.push(Instruction::Nop),
3232
opcode::BURN => result.push(Instruction::Burn),
33+
opcode::SUCCESS => result.push(Instruction::Success),
34+
opcode::FAIL => result.push(Instruction::Fail),
3335
opcode::NOT => result.push(Instruction::Not),
3436
opcode::EQ => result.push(Instruction::Eq),
3537
opcode::JMP => {

vm/src/executor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ pub fn execute(
161161
match &script[pc] {
162162
Instruction::Nop => {}
163163
Instruction::Burn => return Ok(ScriptResult::Burnt),
164+
Instruction::Success => return Ok(ScriptResult::Unlocked),
165+
Instruction::Fail => return Ok(ScriptResult::Fail),
164166
Instruction::Not => {
165167
let value: bool = stack.pop()?.into();
166168
stack.push(Item::from(!value))?;

vm/src/instruction.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
pub enum Instruction {
1919
Nop,
2020
Burn,
21+
Success,
22+
Fail,
2123
Not,
2224
Eq,
2325
Jmp(u8),

vm/src/opcode.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
pub const NOP: u8 = 0x00;
1818
pub const BURN: u8 = 0x01;
19+
pub const SUCCESS: u8 = 0x02;
20+
pub const FAIL: u8 = 0x03;
1921
pub const NOT: u8 = 0x10;
2022
pub const EQ: u8 = 0x11;
2123
pub const JMP: u8 = 0x20;

vm/src/tests/decoder.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ macro_rules! test_one_argument_opcode {
5050

5151
test_no_argument_opcode!(NOP, Nop);
5252
test_no_argument_opcode!(BURN, Burn);
53+
test_no_argument_opcode!(SUCCESS, Success);
54+
test_no_argument_opcode!(FAIL, Fail);
5355
test_no_argument_opcode!(NOT, Not);
5456
test_no_argument_opcode!(EQ, Eq);
5557
test_one_argument_opcode!(JMP, Jmp);

vm/src/tests/executor.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,20 @@ use instruction::Instruction;
2626
#[test]
2727
fn simple_success() {
2828
assert_eq!(
29-
execute(&[Instruction::Push(1)], &[], &[], H256::default(), Config::default()),
29+
execute(&[], &[], &[Instruction::Push(1)], H256::default(), Config::default()),
30+
Ok(ScriptResult::Unlocked)
31+
);
32+
33+
assert_eq!(
34+
execute(&[], &[], &[Instruction::Success], H256::default(), Config::default()),
3035
Ok(ScriptResult::Unlocked)
3136
);
3237
}
3338

3439
#[test]
3540
fn simple_failure() {
3641
assert_eq!(execute(&[Instruction::Push(0)], &[], &[], H256::default(), Config::default()), Ok(ScriptResult::Fail));
42+
assert_eq!(execute(&[], &[], &[Instruction::Fail], H256::default(), Config::default()), Ok(ScriptResult::Fail));
3743
}
3844

3945
#[test]

0 commit comments

Comments
 (0)