diff --git a/script/src/syscalls/debugger.rs b/script/src/syscalls/debugger.rs index 5c5b6d5195..4bc2514f76 100644 --- a/script/src/syscalls/debugger.rs +++ b/script/src/syscalls/debugger.rs @@ -35,6 +35,7 @@ impl<'a, R: Register, M: Memory> Syscalls 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) diff --git a/script/src/syscalls/load_cell.rs b/script/src/syscalls/load_cell.rs index 5f5bd7fdc8..455d1147e9 100644 --- a/script/src/syscalls/load_cell.rs +++ b/script/src/syscalls/load_cell.rs @@ -47,6 +47,7 @@ impl<'a, R: Register, M: Memory> Syscalls 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(); @@ -84,6 +85,7 @@ impl<'a, R: Register, M: Memory> Syscalls 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) } } diff --git a/script/src/syscalls/load_cell_by_field.rs b/script/src/syscalls/load_cell_by_field.rs index 609462b133..246d4064fb 100644 --- a/script/src/syscalls/load_cell_by_field.rs +++ b/script/src/syscalls/load_cell_by_field.rs @@ -49,6 +49,7 @@ impl<'a, R: Register, M: Memory> Syscalls 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())?; @@ -61,24 +62,27 @@ impl<'a, R: Register, M: Memory> Syscalls 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::(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_) => { @@ -87,19 +91,22 @@ impl<'a, R: Register, M: Memory> Syscalls 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) } } diff --git a/script/src/syscalls/load_input_by_field.rs b/script/src/syscalls/load_input_by_field.rs index 061a4f991e..815f34f811 100644 --- a/script/src/syscalls/load_input_by_field.rs +++ b/script/src/syscalls/load_input_by_field.rs @@ -40,6 +40,7 @@ impl<'a, R: Register, M: Memory> Syscalls 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())?; @@ -52,13 +53,14 @@ impl<'a, R: Register, M: Memory> Syscalls 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(); @@ -66,9 +68,11 @@ impl<'a, R: Register, M: Memory> Syscalls for LoadInputByField<'a> { 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) } } diff --git a/script/src/syscalls/load_tx.rs b/script/src/syscalls/load_tx.rs index 066195b8dd..6bf47f9b04 100644 --- a/script/src/syscalls/load_tx.rs +++ b/script/src/syscalls/load_tx.rs @@ -36,6 +36,7 @@ impl<'a, R: Register, M: Memory> Syscalls 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) } }