Skip to content

Commit

Permalink
feat: Add cycle costs to CKB syscalls
Browse files Browse the repository at this point in the history
  • Loading branch information
xxuejie committed Jan 4, 2019
1 parent 22adb37 commit 6e10311
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions script/src/syscalls/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl<'a, R: Register, M: Memory> Syscalls<R, M> for Debugger<'a> {
addr += 1;
}

machine.add_cycles((buffer.len() as u64 + 1) * 10);
let s = String::from_utf8(buffer).map_err(|_| VMError::ParseError)?;
debug!(target: "script", "{} DEBUG OUTPUT: {}", self.prefix, s);
Ok(true)
Expand Down
2 changes: 2 additions & 0 deletions script/src/syscalls/load_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl<'a, R: Register, M: Memory> Syscalls<R, M> for LoadCell<'a> {
if machine.registers()[A7].to_u64() != LOAD_CELL_SYSCALL_NUMBER {
return Ok(false);
}
machine.add_cycles(100);

let addr = machine.registers()[A0].to_usize();
let size_addr = machine.registers()[A1].to_usize();
Expand Down Expand Up @@ -84,6 +85,7 @@ impl<'a, R: Register, M: Memory> Syscalls<R, M> for LoadCell<'a> {
.memory_mut()
.store_bytes(addr, &data[offset..offset + real_size])?;
machine.registers_mut()[A0] = R::from_u8(SUCCESS);
machine.add_cycles(data.len() as u64 * 100);
Ok(true)
}
}
31 changes: 19 additions & 12 deletions script/src/syscalls/load_cell_by_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl<'a, R: Register, M: Memory> Syscalls<R, M> for LoadCellByField<'a> {
if machine.registers()[A7].to_u64() != LOAD_CELL_BY_FIELD_SYSCALL_NUMBER {
return Ok(false);
}
machine.add_cycles(10);

let index = machine.registers()[A3].to_usize();
let source = Source::parse_from_u64(machine.registers()[A4].to_u64())?;
Expand All @@ -61,24 +62,27 @@ impl<'a, R: Register, M: Memory> Syscalls<R, M> for LoadCellByField<'a> {
}
let cell = cell.unwrap();

let return_code = match field {
let (return_code, data_length) = match field {
CellField::Capacity => {
let mut buffer = vec![];
buffer.write_u64::<LittleEndian>(cell.capacity)?;
store_data(machine, &buffer)?;
SUCCESS
(SUCCESS, buffer.len())
}
CellField::Data => {
store_data(machine, &cell.data)?;
SUCCESS
(SUCCESS, cell.data.len())
}
CellField::DataHash => {
store_data(machine, &cell.data_hash().as_bytes())?;
SUCCESS
let hash = cell.data_hash();
let bytes = hash.as_bytes();
store_data(machine, &bytes)?;
(SUCCESS, bytes.len())
}
CellField::LockHash => {
store_data(machine, &cell.lock.as_bytes())?;
SUCCESS
let bytes = cell.lock.as_bytes();
store_data(machine, &bytes)?;
(SUCCESS, bytes.len())
}
CellField::Type => match cell.type_ {
Some(ref type_) => {
Expand All @@ -87,19 +91,22 @@ impl<'a, R: Register, M: Memory> Syscalls<R, M> for LoadCellByField<'a> {
builder.finish(offset, None);
let data = builder.finished_data();
store_data(machine, data)?;
SUCCESS
(SUCCESS, data.len())
}
None => ITEM_MISSING,
None => (ITEM_MISSING, 0),
},
CellField::TypeHash => match cell.type_ {
Some(ref type_) => {
store_data(machine, &type_.type_hash().as_bytes())?;
SUCCESS
let hash = type_.type_hash();
let bytes = hash.as_bytes();
store_data(machine, &bytes)?;
(SUCCESS, bytes.len())
}
None => ITEM_MISSING,
None => (ITEM_MISSING, 0),
},
};
machine.registers_mut()[A0] = R::from_u8(return_code);
machine.add_cycles(data_length as u64 * 10);
Ok(true)
}
}
6 changes: 5 additions & 1 deletion script/src/syscalls/load_input_by_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl<'a, R: Register, M: Memory> Syscalls<R, M> for LoadInputByField<'a> {
if machine.registers()[A7].to_u64() != LOAD_INPUT_BY_FIELD_SYSCALL_NUMBER {
return Ok(false);
}
machine.add_cycles(10);

let index = machine.registers()[A3].to_usize();
let source = Source::parse_from_u64(machine.registers()[A4].to_u64())?;
Expand All @@ -52,23 +53,26 @@ impl<'a, R: Register, M: Memory> Syscalls<R, M> for LoadInputByField<'a> {
}
let input = input.unwrap();

match field {
let data_length = match field {
InputField::Unlock => {
let mut builder = FlatBufferBuilder::new();
let offset = FbsScript::build(&mut builder, &input.unlock);
builder.finish(offset, None);
let data = builder.finished_data();
store_data(machine, data)?;
data.len()
}
InputField::OutPoint => {
let mut builder = FlatBufferBuilder::new();
let offset = FbsOutPoint::build(&mut builder, &input.previous_output);
builder.finish(offset, None);
let data = builder.finished_data();
store_data(machine, data)?;
data.len()
}
};
machine.registers_mut()[A0] = R::from_u8(SUCCESS);
machine.add_cycles(data_length as u64 * 10);
Ok(true)
}
}
1 change: 1 addition & 0 deletions script/src/syscalls/load_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl<'a, R: Register, M: Memory> Syscalls<R, M> for LoadTx<'a> {
.memory_mut()
.store_bytes(addr, &data[offset..offset + real_size])?;
machine.registers_mut()[A0] = R::from_u8(SUCCESS);
machine.add_cycles((data.len() as u64 + 1) * 10);
Ok(true)
}
}

0 comments on commit 6e10311

Please sign in to comment.