Skip to content

Commit 52dcbb3

Browse files
author
Joon-Mo Yang
committed
Update opcodes and instructions
1 parent 93369a8 commit 52dcbb3

File tree

5 files changed

+81
-11
lines changed

5 files changed

+81
-11
lines changed

vm/src/decoder.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,18 @@ pub fn decode(bytes: &[u8]) -> Result<Vec<Instruction>, DecoderError> {
3131
opcode::NOP => result.push(Instruction::Nop),
3232
opcode::NOT => result.push(Instruction::Not),
3333
opcode::EQ => result.push(Instruction::Eq),
34-
opcode::JMP => result.push(Instruction::Jmp),
34+
opcode::JMP => {
35+
let val = *iter.next().ok_or(DecoderError::ScriptTooShort)?;
36+
result.push(Instruction::Jmp(val));
37+
},
38+
opcode::JNZ => {
39+
let val = *iter.next().ok_or(DecoderError::ScriptTooShort)?;
40+
result.push(Instruction::Jnz(val));
41+
},
42+
opcode::JZ => {
43+
let val = *iter.next().ok_or(DecoderError::ScriptTooShort)?;
44+
result.push(Instruction::Jz(val));
45+
},
3546
opcode::PUSH => {
3647
let val = *iter.next().ok_or(DecoderError::ScriptTooShort)?;
3748
result.push(Instruction::Push(val));
@@ -48,8 +59,18 @@ pub fn decode(bytes: &[u8]) -> Result<Vec<Instruction>, DecoderError> {
4859
}
4960
opcode::DUP => result.push(Instruction::Dup),
5061
opcode::SWAP => result.push(Instruction::Swap),
51-
opcode::BLAKE256 => result.push(Instruction::Blake256),
62+
opcode::COPY => {
63+
let val = *iter.next().ok_or(DecoderError::ScriptTooShort)?;
64+
result.push(Instruction::Copy(val));
65+
},
66+
opcode::DROP => {
67+
let val = *iter.next().ok_or(DecoderError::ScriptTooShort)?;
68+
result.push(Instruction::Drop(val));
69+
},
5270
opcode::CHKSIG => result.push(Instruction::ChkSig),
71+
opcode::BLAKE256 => result.push(Instruction::Blake256),
72+
opcode::SHA256 => result.push(Instruction::Sha256),
73+
opcode::RIPEMD160 => result.push(Instruction::Ripemd160),
5374
invalid_opcode => return Err(DecoderError::InvalidOpCode(invalid_opcode)),
5475
}
5576
}

vm/src/executor.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,18 @@ pub fn execute(
125125
Instruction::Nop => {}
126126
Instruction::Not => unimplemented!(),
127127
Instruction::Eq => unimplemented!(),
128-
Instruction::Jmp => unimplemented!(),
128+
Instruction::Jmp(..) => unimplemented!(),
129+
Instruction::Jnz(..) => unimplemented!(),
130+
Instruction::Jz(..) => unimplemented!(),
129131
Instruction::Push(val) => stack.push(Item(vec![*val]))?,
130132
Instruction::Pop => {
131133
stack.pop()?;
132134
}
133135
Instruction::PushB(blob) => stack.push(Item(blob.clone()))?,
134136
Instruction::Dup => unimplemented!(),
135137
Instruction::Swap => unimplemented!(),
136-
Instruction::Blake256 => unimplemented!(),
138+
Instruction::Copy(..) => unimplemented!(),
139+
Instruction::Drop(..) => unimplemented!(),
137140
Instruction::ChkSig => {
138141
let pubkey = Public::from_slice(stack.pop()?.assert_len(64)?.as_ref());
139142
let signature = ECDSASignature::from(H520::from(stack.pop()?.assert_len(65)?.as_ref()));
@@ -142,7 +145,10 @@ pub fn execute(
142145
_ => 0,
143146
};
144147
stack.push(Item(vec![result]))?;
145-
}
148+
},
149+
Instruction::Blake256 => unimplemented!(),
150+
Instruction::Sha256 => unimplemented!(),
151+
Instruction::Ripemd160 => unimplemented!(),
146152
}
147153
pc += 1;
148154
}

vm/src/instruction.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,18 @@ pub enum Instruction {
1919
Nop,
2020
Not,
2121
Eq,
22-
Jmp,
22+
Jmp(u8),
23+
Jnz(u8),
24+
Jz(u8),
2325
Push(u8),
2426
Pop,
2527
PushB(Vec<u8>),
2628
Dup,
2729
Swap,
28-
Blake256,
30+
Copy(u8),
31+
Drop(u8),
2932
ChkSig,
33+
Blake256,
34+
Sha256,
35+
Ripemd160,
3036
}

vm/src/opcode.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ pub const NOP: u8 = 0x00;
1818
pub const NOT: u8 = 0x10;
1919
pub const EQ: u8 = 0x11;
2020
pub const JMP: u8 = 0x20;
21+
pub const JNZ: u8 = 0x21;
22+
pub const JZ: u8 = 0x22;
2123
pub const PUSH: u8 = 0x30;
2224
pub const POP: u8 = 0x31;
2325
pub const PUSHB: u8 = 0x32;
2426
pub const DUP: u8 = 0x33;
2527
pub const SWAP: u8 = 0x34;
26-
pub const BLAKE256: u8 = 0x80;
27-
pub const CHKSIG: u8 = 0x81;
28+
pub const COPY: u8 = 0x35;
29+
pub const DROP: u8 = 0x36;
30+
pub const CHKSIG: u8 = 0x80;
31+
pub const BLAKE256: u8 = 0x90;
32+
pub const SHA256: u8 = 0x91;
33+
pub const RIPEMD160: u8 = 0x92;

vm/src/tests/decoder.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ fn test_single_byte_opcodes() {
2424
(opcode::NOP, Instruction::Nop),
2525
(opcode::NOT, Instruction::Not),
2626
(opcode::EQ, Instruction::Eq),
27-
(opcode::JMP, Instruction::Jmp),
2827
(opcode::POP, Instruction::Pop),
2928
(opcode::DUP, Instruction::Dup),
3029
(opcode::SWAP, Instruction::Swap),
31-
(opcode::BLAKE256, Instruction::Blake256),
3230
(opcode::CHKSIG, Instruction::ChkSig),
31+
(opcode::BLAKE256, Instruction::Blake256),
32+
(opcode::SHA256, Instruction::Sha256),
33+
(opcode::RIPEMD160, Instruction::Ripemd160),
3334
];
3435
for &(ref byte, ref code) in target.into_iter() {
3536
let script = decode(&[byte.clone(), byte.clone(), byte.clone()]);
@@ -43,6 +44,36 @@ fn push() {
4344
assert_eq!(decode(&[opcode::PUSH, 0, opcode::PUSH]), Err(DecoderError::ScriptTooShort));
4445
}
4546

47+
#[test]
48+
fn copy() {
49+
assert_eq!(decode(&[opcode::COPY, 0, opcode::COPY, 10]), Ok(vec![Instruction::Copy(0), Instruction::Copy(10)]));
50+
assert_eq!(decode(&[opcode::COPY, 0, opcode::COPY]), Err(DecoderError::ScriptTooShort));
51+
}
52+
53+
#[test]
54+
fn drop() {
55+
assert_eq!(decode(&[opcode::DROP, 0, opcode::DROP, 10]), Ok(vec![Instruction::Drop(0), Instruction::Drop(10)]));
56+
assert_eq!(decode(&[opcode::DROP, 0, opcode::DROP]), Err(DecoderError::ScriptTooShort));
57+
}
58+
59+
#[test]
60+
fn jmp() {
61+
assert_eq!(decode(&[opcode::JMP, 0, opcode::JMP, 10]), Ok(vec![Instruction::Jmp(0), Instruction::Jmp(10)]));
62+
assert_eq!(decode(&[opcode::JMP, 0, opcode::JMP]), Err(DecoderError::ScriptTooShort));
63+
}
64+
65+
#[test]
66+
fn jnz() {
67+
assert_eq!(decode(&[opcode::JNZ, 0, opcode::JNZ, 10]), Ok(vec![Instruction::Jnz(0), Instruction::Jnz(10)]));
68+
assert_eq!(decode(&[opcode::JNZ, 0, opcode::JNZ]), Err(DecoderError::ScriptTooShort));
69+
}
70+
71+
#[test]
72+
fn jz() {
73+
assert_eq!(decode(&[opcode::JZ, 0, opcode::JZ, 10]), Ok(vec![Instruction::Jz(0), Instruction::Jz(10)]));
74+
assert_eq!(decode(&[opcode::JZ, 0, opcode::JZ]), Err(DecoderError::ScriptTooShort));
75+
}
76+
4677
#[test]
4778
fn pushb() {
4879
let blobs: Vec<&[u8]> = vec![&[0xed, 0x11, 0xe7], &[0x8b, 0x0c, 0x92, 0x24, 0x3f]];

0 commit comments

Comments
 (0)