Skip to content

Commit

Permalink
fix: in case of missing cell, return ITEM_MISSING error instead of ha…
Browse files Browse the repository at this point in the history
…lting
  • Loading branch information
xxuejie committed Dec 11, 2018
1 parent 7727360 commit 707d661
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
11 changes: 7 additions & 4 deletions script/src/syscalls/load_cell.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::syscalls::{Source, LOAD_CELL_SYSCALL_NUMBER, SUCCESS};
use crate::syscalls::{Source, ITEM_MISSING, LOAD_CELL_SYSCALL_NUMBER, SUCCESS};
use ckb_core::transaction::CellOutput;
use ckb_protocol::CellOutput as FbsCellOutput;
use ckb_vm::{CoreMachine, Error as VMError, Memory, Register, Syscalls, A0, A1, A2, A3, A4, A7};
Expand Down Expand Up @@ -51,9 +51,12 @@ impl<'a, R: Register, M: Memory> Syscalls<R, M> for LoadCell<'a> {
let index = machine.registers()[A3].to_usize();
let source = Source::parse_from_u64(machine.registers()[A4].to_u64())?;

let cell = self
.fetch_cell(source, index)
.ok_or_else(|| VMError::OutOfBound)?;
let cell = self.fetch_cell(source, index);
if cell.is_none() {
machine.registers_mut()[A0] = R::from_u8(ITEM_MISSING);
return Ok(true);
}
let cell = cell.unwrap();

// NOTE: this is a very expensive operation here since we need to copy
// everything in a cell to a flatbuffer object, serialize the object
Expand Down
9 changes: 6 additions & 3 deletions script/src/syscalls/load_cell_by_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ impl<'a, R: Register, M: Memory> Syscalls<R, M> for LoadCellByField<'a> {
let source = Source::parse_from_u64(machine.registers()[A4].to_u64())?;
let field = CellField::parse_from_u64(machine.registers()[A5].to_u64())?;

let cell = self
.fetch_cell(source, index)
.ok_or_else(|| VMError::OutOfBound)?;
let cell = self.fetch_cell(source, index);
if cell.is_none() {
machine.registers_mut()[A0] = R::from_u8(ITEM_MISSING);
return Ok(true);
}
let cell = cell.unwrap();

let return_code = match field {
CellField::Capacity => {
Expand Down
13 changes: 6 additions & 7 deletions script/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ mod tests {
use ckb_core::transaction::{CellInput, CellOutput, OutPoint};
use ckb_protocol::{CellOutput as FbsCellOutput, OutPoint as FbsOutPoint, Script as FbsScript};
use ckb_vm::machine::DefaultCoreMachine;
use ckb_vm::{
CoreMachine, Error as VMError, Memory, SparseMemory, Syscalls, A0, A1, A2, A3, A4, A5, A7,
};
use ckb_vm::{CoreMachine, Memory, SparseMemory, Syscalls, A0, A1, A2, A3, A4, A5, A7};
use flatbuffers::FlatBufferBuilder;
use hash::sha3_256;
use numext_fixed_hash::H256;
Expand Down Expand Up @@ -163,7 +161,7 @@ mod tests {
}
}

fn _test_load_cell_out_of_bound(data: Vec<u8>) {
fn _test_load_cell_item_missing(data: Vec<u8>) {
let mut machine = DefaultCoreMachine::<u64, SparseMemory>::default();
let size_addr = 0;
let addr = 100;
Expand Down Expand Up @@ -191,13 +189,14 @@ mod tests {
let input_cells = vec![&input_cell];
let mut load_cell = LoadCell::new(&outputs, &input_cells, &input_cell);

assert_eq!(load_cell.ecall(&mut machine), Err(VMError::OutOfBound)); // index out of bounds
assert!(load_cell.ecall(&mut machine).is_ok());
assert_eq!(machine.registers()[A0], ITEM_MISSING as u64);
}

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

Expand Down

0 comments on commit 707d661

Please sign in to comment.