Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new syscall to fetch current script hash #42

Merged
merged 1 commit into from
Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions script/src/syscalls/fetch_script_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@ use bigint::H256;
use ckb_core::transaction::{CellInput, CellOutput};
use ckb_vm::{CoreMachine, Error as VMError, Memory, Register, Syscalls, A0, A1, A2, A3, A4, A7};
use syscalls::{
Category, Source, FETCH_SCRIPT_HASH_SYSCALL_NUMBER, ITEM_MISSING, OVERRIDE_LEN, SUCCESS,
Category, Source, FETCH_CURRENT_SCRIPT_HASH_SYSCALL_NUMBER, FETCH_SCRIPT_HASH_SYSCALL_NUMBER,
ITEM_MISSING, OVERRIDE_LEN, SUCCESS,
};

#[derive(Debug)]
pub struct FetchScriptHash<'a> {
outputs: &'a [&'a CellOutput],
inputs: &'a [&'a CellInput],
input_cells: &'a [&'a CellOutput],
current_script_hash: H256,
}

impl<'a> FetchScriptHash<'a> {
pub fn new(
outputs: &'a [&'a CellOutput],
inputs: &'a [&'a CellInput],
input_cells: &'a [&'a CellOutput],
current_script_hash: H256,
) -> FetchScriptHash<'a> {
FetchScriptHash {
outputs,
inputs,
input_cells,
current_script_hash,
}
}

Expand Down Expand Up @@ -55,19 +59,27 @@ impl<'a, R: Register, M: Memory> Syscalls<R, M> for FetchScriptHash<'a> {
}

fn ecall(&mut self, machine: &mut CoreMachine<R, M>) -> Result<bool, VMError> {
if machine.registers()[A7].to_u64() != FETCH_SCRIPT_HASH_SYSCALL_NUMBER {
let code = machine.registers()[A7].to_u64();
if code != FETCH_SCRIPT_HASH_SYSCALL_NUMBER
&& code != FETCH_CURRENT_SCRIPT_HASH_SYSCALL_NUMBER
{
return Ok(false);
}

let addr = machine.registers()[A0].to_usize();
let size_addr = machine.registers()[A1].to_usize();
let size = machine.memory_mut().load64(size_addr)? as usize;

let index = machine.registers()[A2].to_usize();
let source = Source::parse_from_u64(machine.registers()[A3].to_u64())?;
let category = Category::parse_from_u64(machine.registers()[A4].to_u64())?;
let hash = if code == FETCH_SCRIPT_HASH_SYSCALL_NUMBER {
let index = machine.registers()[A2].to_usize();
let source = Source::parse_from_u64(machine.registers()[A3].to_u64())?;
let category = Category::parse_from_u64(machine.registers()[A4].to_u64())?;
self.fetch_hash(source, category, index)
} else {
Some(self.current_script_hash)
};

match self.fetch_hash(source, category, index) {
match hash {
Some(hash) => {
let hash: &[u8] = &hash;
machine.memory_mut().store64(size_addr, hash.len() as u64)?;
Expand Down
86 changes: 81 additions & 5 deletions script/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub const ITEM_MISSING: u8 = 2;
pub const MMAP_TX_SYSCALL_NUMBER: u64 = 2049;
pub const MMAP_CELL_SYSCALL_NUMBER: u64 = 2050;
pub const FETCH_SCRIPT_HASH_SYSCALL_NUMBER: u64 = 2051;
pub const FETCH_CURRENT_SCRIPT_HASH_SYSCALL_NUMBER: u64 = 2052;
pub const DEBUG_PRINT_SYSCALL_NUMBER: u64 = 2177;

#[derive(Debug, PartialEq, Clone, Copy, Eq)]
Expand Down Expand Up @@ -368,7 +369,8 @@ mod tests {
let input_cells = Vec::new();
let outputs = Vec::new();

let mut fetch_script_hash = FetchScriptHash::new(&outputs, &inputs, &input_cells);
let mut fetch_script_hash =
FetchScriptHash::new(&outputs, &inputs, &input_cells, H256::from(0));

assert!(fetch_script_hash.ecall(&mut machine).is_ok());
assert_eq!(machine.registers()[A0], SUCCESS as u64);
Expand Down Expand Up @@ -406,7 +408,8 @@ mod tests {
let input_cells = vec![&output];
let outputs = Vec::new();

let mut fetch_script_hash = FetchScriptHash::new(&outputs, &inputs, &input_cells);
let mut fetch_script_hash =
FetchScriptHash::new(&outputs, &inputs, &input_cells, H256::from(0));

assert!(fetch_script_hash.ecall(&mut machine).is_ok());
assert_eq!(machine.registers()[A0], SUCCESS as u64);
Expand Down Expand Up @@ -444,7 +447,8 @@ mod tests {
let input_cells = Vec::new();
let outputs = vec![&output];

let mut fetch_script_hash = FetchScriptHash::new(&outputs, &inputs, &input_cells);
let mut fetch_script_hash =
FetchScriptHash::new(&outputs, &inputs, &input_cells, H256::from(0));

assert!(fetch_script_hash.ecall(&mut machine).is_ok());
assert_eq!(machine.registers()[A0], SUCCESS as u64);
Expand Down Expand Up @@ -482,7 +486,8 @@ mod tests {
let input_cells = Vec::new();
let outputs = vec![&output];

let mut fetch_script_hash = FetchScriptHash::new(&outputs, &inputs, &input_cells);
let mut fetch_script_hash =
FetchScriptHash::new(&outputs, &inputs, &input_cells, H256::from(0));

assert!(fetch_script_hash.ecall(&mut machine).is_ok());
assert_eq!(machine.registers()[A0], OVERRIDE_LEN as u64);
Expand Down Expand Up @@ -516,9 +521,80 @@ mod tests {
let input_cells = Vec::new();
let outputs = Vec::new();

let mut fetch_script_hash = FetchScriptHash::new(&outputs, &inputs, &input_cells);
let mut fetch_script_hash =
FetchScriptHash::new(&outputs, &inputs, &input_cells, H256::from(0));

assert!(fetch_script_hash.ecall(&mut machine).is_ok());
assert_eq!(machine.registers()[A0], ITEM_MISSING as u64);
}

fn _test_fetch_current_script_hash(data: Vec<u8>) {
let mut machine = DefaultCoreMachine::<u64, SparseMemory>::default();
let size_addr = 0;
let addr = 100;

machine.registers_mut()[A0] = addr; // addr
machine.registers_mut()[A1] = size_addr; // size_addr
machine.registers_mut()[A7] = FETCH_CURRENT_SCRIPT_HASH_SYSCALL_NUMBER; // syscall number

assert!(machine.memory_mut().store64(size_addr as usize, 32).is_ok());

let script = Script::new(0, Vec::new(), None, Some(data), Vec::new());
let inputs = Vec::new();
let input_cells = Vec::new();
let outputs = Vec::new();

let mut fetch_script_hash =
FetchScriptHash::new(&outputs, &inputs, &input_cells, script.type_hash());

assert!(fetch_script_hash.ecall(&mut machine).is_ok());
assert_eq!(machine.registers()[A0], SUCCESS as u64);

let hash = &script.type_hash();
for (i, addr) in (addr as usize..addr as usize + hash.len()).enumerate() {
assert_eq!(machine.memory_mut().load8(addr), Ok(hash[i]))
}
}

proptest! {
#[test]
fn test_fetch_current_script_hash(data in any_with::<Vec<u8>>(size_range(1000).lift())) {
_test_fetch_current_script_hash(data);
}
}

fn _test_fetch_current_script_hash_not_enough_space(data: Vec<u8>) {
let mut machine = DefaultCoreMachine::<u64, SparseMemory>::default();
let size_addr = 0;
let addr = 100;

machine.registers_mut()[A0] = addr; // addr
machine.registers_mut()[A1] = size_addr; // size_addr
machine.registers_mut()[A2] = 0; // index
machine.registers_mut()[A3] = 1; // source: 1 output
machine.registers_mut()[A4] = 1; // category: 1 contract
machine.registers_mut()[A7] = FETCH_CURRENT_SCRIPT_HASH_SYSCALL_NUMBER; // syscall number

assert!(machine.memory_mut().store64(size_addr as usize, 16).is_ok());

let script = Script::new(0, Vec::new(), None, Some(data), Vec::new());
let inputs = Vec::new();
let input_cells = Vec::new();
let outputs = Vec::new();

let mut fetch_script_hash =
FetchScriptHash::new(&outputs, &inputs, &input_cells, script.type_hash());

assert!(fetch_script_hash.ecall(&mut machine).is_ok());
assert_eq!(machine.registers()[A0], OVERRIDE_LEN as u64);

assert_eq!(machine.memory_mut().load64(size_addr as usize), Ok(32));
}

proptest! {
#[test]
fn test_fetch_current_script_hash_not_enough_space(data in any_with::<Vec<u8>>(size_range(1000).lift())) {
_test_fetch_current_script_hash_not_enough_space(data);
}
}
}
11 changes: 8 additions & 3 deletions script/src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ impl<'a> TransactionScriptsVerifier<'a> {
MmapCell::new(&self.outputs, &self.input_cells)
}

fn build_fetch_script_hash(&self) -> FetchScriptHash {
FetchScriptHash::new(&self.outputs, &self.inputs, &self.input_cells)
fn build_fetch_script_hash(&self, current_script_hash: H256) -> FetchScriptHash {
FetchScriptHash::new(
&self.outputs,
&self.inputs,
&self.input_cells,
current_script_hash,
)
}

// Script struct might contain references to external cells, this
Expand All @@ -94,7 +99,7 @@ impl<'a> TransactionScriptsVerifier<'a> {
let mut machine = DefaultMachine::<u64, SparseMemory>::default();
machine.add_syscall_module(Box::new(self.build_mmap_tx()));
machine.add_syscall_module(Box::new(self.build_mmap_cell()));
machine.add_syscall_module(Box::new(self.build_fetch_script_hash()));
machine.add_syscall_module(Box::new(self.build_fetch_script_hash(script.type_hash())));
machine.add_syscall_module(Box::new(Debugger::new(prefix)));
machine
.run(script_binary, &args)
Expand Down