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

Change read behavior on the InMemoryTransaction to use offset and allow not equal buf size #2673

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [2667](https://github.com/FuelLabs/fuel-core/pull/2667): Populate `Blobs` table in global merkle root storage.
- [2652](https://github.com/FuelLabs/fuel-core/pull/2652): Global Merkle Root storage crate: Add Raw contract bytecode to global merkle root storage when processing Create transactions.

### Fixed
- [2673](https://github.com/FuelLabs/fuel-core/pull/2673): Change read behavior on the InMemoryTransaction to use offset and allow not equal buf size (fix CCP and LDC broken from https://github.com/FuelLabs/fuel-vm/pull/847)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically the bug was introduced in #2438, the change in fuel-vm seems to be working as expected from what I can tell

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me it's FuelLabs/fuel-vm#847 that broke CCP and LDC opcode if this PR hasn't land on master, the read function would have been wrong since #2438 but the opcode would have been fine.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, but the error is triggered in the if branch of an if else statement that does not invoke any of the code in fuel-vm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I talked about the error just before in the sentence I just added this parenthesis with the opcode in case we want to find later to have beteer search results


## [Version 0.41.5]

### Changed
Expand Down
8 changes: 4 additions & 4 deletions crates/fuel-core/src/state/rocks_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,19 +915,19 @@ where
.map(|value| {
let bytes_len = value.len();
let start = offset;
let end = offset.saturating_add(buf.len());
let buf_len = buf.len();
let end = offset.saturating_add(buf_len);

if end > bytes_len {
return Err(StorageError::Other(anyhow::anyhow!(
"Offset `{offset}` is out of bounds `{bytes_len}` \
for key `{:?}` and column `{column:?}`",
"Offset `{offset}` + buf_len `{buf_len}` read until {end} which is out of bounds `{bytes_len}` for key `{:?}` and column `{column:?}`",
key
)));
}

let starting_from_offset = &value[start..end];
buf[..].copy_from_slice(starting_from_offset);
Ok(buf.len())
Ok(buf_len)
})
.transpose()?;

Expand Down
7 changes: 4 additions & 3 deletions crates/storage/src/kv_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,20 @@ pub trait KeyValueInspect {
.map(|value| {
let bytes_len = value.as_ref().len();
let start = offset;
let end = offset.saturating_add(buf.len());
let buf_len = buf.len();
let end = offset.saturating_add(buf_len);

if end > bytes_len {
return Err(anyhow::anyhow!(
"Offset `{offset}` is out of bounds `{bytes_len}` for key `{:?}`",
"Offset `{offset}` + buf_len `{buf_len}` read until {end} which is out of bounds `{bytes_len}` for key `{:?}`",
key
)
.into());
}

let starting_from_offset = &value.as_ref()[start..end];
buf[..].copy_from_slice(starting_from_offset);
Ok(buf.len())
Ok(buf_len)
})
.transpose()
}
Expand Down
100 changes: 93 additions & 7 deletions crates/storage/src/transactional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,22 @@ where
if let Some(operation) = self.get_from_changes(key, column) {
match operation {
WriteOperation::Insert(value) => {
let read = value.len();
if read != buf.len() {
return Err(crate::Error::Other(anyhow::anyhow!(
"Buffer size is not equal to the value size"
)));
let bytes_len = value.as_ref().len();
let start = offset;
let buf_len = buf.len();
let end = offset.saturating_add(buf.len());

if end > bytes_len {
return Err(anyhow::anyhow!(
"Offset `{offset}` + buf_len `{buf_len}` read until {end} which is out of bounds `{bytes_len}` for key `{:?}`",
key
)
.into());
}
buf.copy_from_slice(value.as_ref());
Ok(Some(read))

let starting_from_offset = &value.as_ref()[start..end];
buf[..].copy_from_slice(starting_from_offset);
Ok(Some(buf_len))
}
WriteOperation::Remove => Ok(None),
}
Expand Down Expand Up @@ -656,6 +664,84 @@ mod test {
use super::*;
use crate::column::Column;

#[test]
fn read_returns_from_view_exact_size() {
// setup
let storage = InMemoryStorage::<Column>::default();
let mut view = storage.read_transaction();
let key = vec![0xA, 0xB, 0xC];
let value = Value::from([1, 2, 3]);
view.put(&key, Column::Metadata, value).unwrap();
// test
let mut buf = [0; 3];
let ret = view.read(&key, Column::Metadata, 0, &mut buf).unwrap();
// verify
assert_eq!(ret, Some(3));
assert_eq!(buf, [1, 2, 3]);
}

#[test]
fn read_returns_from_view_buf_smaller() {
// setup
let storage = InMemoryStorage::<Column>::default();
let mut view = storage.read_transaction();
let key = vec![0xA, 0xB, 0xC];
let value = Value::from([1, 2, 3]);
view.put(&key, Column::Metadata, value).unwrap();
// test
let mut buf = [0; 2];
let ret = view.read(&key, Column::Metadata, 0, &mut buf).unwrap();
// verify
assert_eq!(ret, Some(2));
assert_eq!(buf, [1, 2]);
}

#[test]
fn read_returns_from_view_with_offset() {
// setup
let storage = InMemoryStorage::<Column>::default();
let mut view = storage.read_transaction();
let key = vec![0xA, 0xB, 0xC];
let value = Value::from([1, 2, 3]);
view.put(&key, Column::Metadata, value).unwrap();
// test
let mut buf = [0; 2];
let ret = view.read(&key, Column::Metadata, 1, &mut buf).unwrap();
// verify
assert_eq!(ret, Some(2));
assert_eq!(buf, [2, 3]);
}

#[test]
fn read_returns_from_view_buf_bigger() {
// setup
let storage = InMemoryStorage::<Column>::default();
let mut view = storage.read_transaction();
let key = vec![0xA, 0xB, 0xC];
let value = Value::from([1, 2, 3]);
view.put(&key, Column::Metadata, value).unwrap();
// test
let mut buf = [0; 4];
let ret = view.read(&key, Column::Metadata, 0, &mut buf).unwrap_err();
// verify
assert_eq!(ret, crate::Error::Other(anyhow::anyhow!("Offset `0` + buf_len `4` read until 4 which is out of bounds `3` for key `[10, 11, 12]`".to_string())));
}

#[test]
fn read_returns_from_view_buf_bigger_because_offset() {
// setup
let storage = InMemoryStorage::<Column>::default();
let mut view = storage.read_transaction();
let key = vec![0xA, 0xB, 0xC];
let value = Value::from([1, 2, 3]);
view.put(&key, Column::Metadata, value).unwrap();
// test
let mut buf = [0; 3];
let ret = view.read(&key, Column::Metadata, 1, &mut buf).unwrap_err();
// verify
assert_eq!(ret, crate::Error::Other(anyhow::anyhow!("Offset `1` + buf_len `3` read until 4 which is out of bounds `3` for key `[10, 11, 12]`".to_string())));
}

#[test]
fn get_returns_from_view() {
// setup
Expand Down
Loading