Skip to content

Commit

Permalink
fix: fix missing output lock hash (#46)
Browse files Browse the repository at this point in the history
For some reason I forgot to add logic for fetching output cell lock
hash in VM syscalls, which is in fact possible to fetch.
  • Loading branch information
xxuejie authored and doitian committed Nov 30, 2018
1 parent 2f8cd66 commit 51b1675
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion script/src/syscalls/fetch_script_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<'a> FetchScriptHash<'a> {
.map(|contract| contract.type_hash())
})
}
(Source::OUTPUT, Category::LOCK) => None,
(Source::OUTPUT, Category::LOCK) => self.outputs.get(index).map(|output| output.lock),
(Source::OUTPUT, Category::CONTRACT) => self.outputs.get(index).and_then(|output| {
output
.contract
Expand Down
39 changes: 39 additions & 0 deletions script/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,45 @@ mod tests {
}
}

fn _test_fetch_script_hash_output_lock(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] = 0; // category: 0 lock
machine.registers_mut()[A7] = FETCH_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 output = CellOutput::new(0, Vec::new(), script.type_hash(), None);
let inputs = Vec::new();
let input_cells = Vec::new();
let outputs = vec![&output];

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);

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_script_hash_output_lock(data in any_with::<Vec<u8>>(size_range(1000).lift())) {
_test_fetch_script_hash_output_lock(data);
}
}

fn _test_fetch_script_hash_output_contract(data: Vec<u8>) {
let mut machine = DefaultCoreMachine::<u64, SparseMemory>::default();
let size_addr = 0;
Expand Down

0 comments on commit 51b1675

Please sign in to comment.