Skip to content

Commit

Permalink
feat: add DATA_HASH field type in Load Cell By Field
Browse files Browse the repository at this point in the history
  • Loading branch information
xxuejie committed Dec 18, 2018
1 parent cae937f commit 2d0a378
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
4 changes: 4 additions & 0 deletions script/src/syscalls/load_cell_by_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ impl<'a, R: Register, M: Memory> Syscalls<R, M> for LoadCellByField<'a> {
store_data(machine, &cell.data)?;
SUCCESS
}
CellField::DataHash => {
store_data(machine, &cell.data_hash().as_bytes())?;
SUCCESS
}
CellField::LockHash => {
store_data(machine, &cell.lock.as_bytes())?;
SUCCESS
Expand Down
59 changes: 57 additions & 2 deletions script/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub const DEBUG_PRINT_SYSCALL_NUMBER: u64 = 2177;
enum CellField {
Capacity,
Data,
DataHash,
LockHash,
Contract,
ContractHash,
Expand All @@ -41,6 +42,7 @@ impl CellField {
2 => Ok(CellField::LockHash),
3 => Ok(CellField::Contract),
4 => Ok(CellField::ContractHash),
5 => Ok(CellField::DataHash),
_ => Err(Error::ParseError),
}
}
Expand Down Expand Up @@ -812,12 +814,18 @@ mod tests {
let dep_cells = vec![&dep_cell];
let mut load_cell = LoadCellByField::new(&outputs, &input_cells, &input_cell, &dep_cells);

assert!(machine.memory_mut().store64(size_addr as usize, data.len() as u64 + 20).is_ok());
assert!(machine
.memory_mut()
.store64(size_addr as usize, data.len() as u64 + 20)
.is_ok());

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

assert_eq!(machine.memory_mut().load64(size_addr as usize), Ok(data.len() as u64));
assert_eq!(
machine.memory_mut().load64(size_addr as usize),
Ok(data.len() as u64)
);

for (i, addr) in (addr as usize..addr as usize + data.len() as usize).enumerate() {
assert_eq!(machine.memory_mut().load8(addr), Ok(data[i]));
Expand All @@ -830,4 +838,51 @@ mod tests {
_test_load_dep_cell_data(data);
}
}

fn _test_load_dep_cell_data_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()[A2] = 0; // offset
machine.registers_mut()[A3] = 0; //index
machine.registers_mut()[A4] = 3; //source: 3 dep
machine.registers_mut()[A5] = 5; //field: 5 data hash
machine.registers_mut()[A7] = LOAD_CELL_BY_FIELD_SYSCALL_NUMBER; // syscall number

let input_cell = CellOutput::new(1000, vec![], H256::zero(), None);
let dep_cell = CellOutput::new(1000, data.clone(), H256::zero(), None);
let outputs = vec![];
let input_cells = vec![&input_cell];
let dep_cells = vec![&dep_cell];
let mut load_cell = LoadCellByField::new(&outputs, &input_cells, &input_cell, &dep_cells);

let data_hash = sha3_256(&data);

assert!(machine
.memory_mut()
.store64(size_addr as usize, data_hash.len() as u64 + 20)
.is_ok());

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

assert_eq!(
machine.memory_mut().load64(size_addr as usize),
Ok(data_hash.len() as u64)
);

for (i, addr) in (addr as usize..addr as usize + data_hash.len() as usize).enumerate() {
assert_eq!(machine.memory_mut().load8(addr), Ok(data_hash[i]));
}
}

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

0 comments on commit 2d0a378

Please sign in to comment.