Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve write limit error message #1889

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 25 additions & 30 deletions packages/vm/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,25 @@ pub fn do_db_write<A: BackendApi + 'static, S: Storage + 'static, Q: Querier + '
return Err(VmError::write_access_denied());
}

let key = read_region(&data.memory(&store), key_ptr, MAX_LENGTH_DB_KEY)?;
let value = read_region(&data.memory(&store), value_ptr, MAX_LENGTH_DB_VALUE)?;
/// Converts a region length error to a different variant for better understandability
fn convert_error(e: VmError, kind: &'static str) -> VmError {
if let VmError::CommunicationErr {
source: CommunicationError::RegionLengthTooBig { length, max_length },
..
} = e
{
VmError::generic_err(format!(
"{kind} too big. Tried to write {length} bytes to storage, limit is {max_length}."
))
} else {
e
}
}

let key = read_region(&data.memory(&store), key_ptr, MAX_LENGTH_DB_KEY)
.map_err(|e| convert_error(e, "Key"))?;
let value = read_region(&data.memory(&store), value_ptr, MAX_LENGTH_DB_VALUE)
.map_err(|e| convert_error(e, "Value"))?;
chipshort marked this conversation as resolved.
Show resolved Hide resolved

let (result, gas_info) =
data.with_storage_from_context::<_, _>(|store| Ok(store.set(&key, &value)))?;
Expand Down Expand Up @@ -877,25 +894,14 @@ mod tests {
let (fe, mut store, _instance) = make_instance(api);
let mut fe_mut = fe.into_mut(&mut store);

let key_ptr = write_data(&mut fe_mut, &vec![4u8; 300 * 1024]);
const KEY_SIZE: usize = 300 * 1024;
let key_ptr = write_data(&mut fe_mut, &vec![4u8; KEY_SIZE]);
let value_ptr = write_data(&mut fe_mut, b"new value");

leave_default_data(&mut fe_mut);

let result = do_db_write(fe_mut, key_ptr, value_ptr);
match result.unwrap_err() {
VmError::CommunicationErr {
source:
CommunicationError::RegionLengthTooBig {
length, max_length, ..
},
..
} => {
assert_eq!(length, 300 * 1024);
assert_eq!(max_length, MAX_LENGTH_DB_KEY);
}
err => panic!("unexpected error: {err:?}"),
};
assert_eq!(result.unwrap_err().to_string(), format!("Generic error: Key too big. Tried to write {KEY_SIZE} bytes to storage, limit is {MAX_LENGTH_DB_KEY}."));
}

#[test]
Expand All @@ -904,25 +910,14 @@ mod tests {
let (fe, mut store, _instance) = make_instance(api);
let mut fe_mut = fe.into_mut(&mut store);

const VAL_SIZE: usize = 300 * 1024;
let key_ptr = write_data(&mut fe_mut, b"new storage key");
let value_ptr = write_data(&mut fe_mut, &vec![5u8; 300 * 1024]);
let value_ptr = write_data(&mut fe_mut, &vec![5u8; VAL_SIZE]);

leave_default_data(&mut fe_mut);

let result = do_db_write(fe_mut, key_ptr, value_ptr);
match result.unwrap_err() {
VmError::CommunicationErr {
source:
CommunicationError::RegionLengthTooBig {
length, max_length, ..
},
..
} => {
assert_eq!(length, 300 * 1024);
assert_eq!(max_length, MAX_LENGTH_DB_VALUE);
}
err => panic!("unexpected error: {err:?}"),
};
assert_eq!(result.unwrap_err().to_string(), format!("Generic error: Value too big. Tried to write {VAL_SIZE} bytes to storage, limit is {MAX_LENGTH_DB_VALUE}."));
}

#[test]
Expand Down
Loading