Skip to content

Commit

Permalink
Refactor fuel-core to use version of StorageRead::read with offset
Browse files Browse the repository at this point in the history
  • Loading branch information
acerone85 committed Nov 14, 2024
1 parent ba0fa15 commit afbfef2
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 50 deletions.
27 changes: 9 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,6 @@ itertools = { version = "0.12", default-features = false }
insta = "1.8"
tempfile = "3.4"
tikv-jemallocator = "0.5"

[patch.crates-io]
fuel-vm = { git = "https://github.com/fuelLabs/fuel-vm" }
9 changes: 7 additions & 2 deletions crates/fuel-core/src/graphql_api/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,13 @@ impl StorageSize<BlobData> for ReadView {
}

impl StorageRead<BlobData> for ReadView {
fn read(&self, key: &BlobId, buf: &mut [u8]) -> Result<Option<usize>, Self::Error> {
StorageRead::<BlobData>::read(self.on_chain.as_ref(), key, buf)
fn read(
&self,
key: &BlobId,
offset: usize,
buf: &mut [u8],
) -> Result<Option<usize>, Self::Error> {
StorageRead::<BlobData>::read(self.on_chain.as_ref(), key, offset, buf)
}

fn read_alloc(&self, key: &BlobId) -> Result<Option<Vec<u8>>, Self::Error> {
Expand Down
3 changes: 2 additions & 1 deletion crates/fuel-core/src/state/data_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ where
&self,
key: &[u8],
column: Self::Column,
offset: usize,
buf: &mut [u8],
) -> StorageResult<Option<usize>> {
self.data.read(key, column, buf)
self.data.read(key, column, offset, buf)
}
}

Expand Down
12 changes: 9 additions & 3 deletions crates/fuel-core/src/state/generic_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,13 @@ where
M: Mappable,
StructuredStorage<Storage>: StorageRead<M, Error = StorageError>,
{
fn read(&self, key: &M::Key, buf: &mut [u8]) -> Result<Option<usize>, Self::Error> {
self.storage.storage::<M>().read(key, buf)
fn read(
&self,
key: &M::Key,
offset: usize,
buf: &mut [u8],
) -> Result<Option<usize>, Self::Error> {
self.storage.storage::<M>().read(key, offset, buf)
}

fn read_alloc(&self, key: &M::Key) -> Result<Option<Vec<u8>>, Self::Error> {
Expand Down Expand Up @@ -124,9 +129,10 @@ where
&self,
key: &[u8],
column: Self::Column,
offset: usize,
buf: &mut [u8],
) -> StorageResult<Option<usize>> {
KeyValueInspect::read(&self.storage, key, column, buf)
KeyValueInspect::read(&self.storage, key, column, offset, buf)
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/fuel-core/src/state/historical_rocksdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,11 @@ where
&self,
key: &[u8],
column: Self::Column,
offset: usize,
buf: &mut [u8],
) -> StorageResult<Option<usize>> {
self.db.read(key, Column::OriginalColumn(column), buf)
self.db
.read(key, Column::OriginalColumn(column), offset, buf)
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/fuel-core/src/state/iterable_key_value_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ where
&self,
key: &[u8],
column: Self::Column,
offset: usize,
buf: &mut [u8],
) -> StorageResult<Option<usize>> {
self.0.read(key, column, buf)
self.0.read(key, column, offset, buf)
}
}

Expand Down
3 changes: 2 additions & 1 deletion crates/fuel-core/src/state/key_value_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ where
&self,
key: &[u8],
column: Self::Column,
offset: usize,
buf: &mut [u8],
) -> StorageResult<Option<usize>> {
self.0.read(key, column, buf)
self.0.read(key, column, offset, buf)
}
}
15 changes: 11 additions & 4 deletions crates/fuel-core/src/state/rocks_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use fuel_core_storage::{
WriteOperation,
},
transactional::Changes,
Error as StorageError,
Result as StorageResult,
};
use itertools::Itertools;
Expand Down Expand Up @@ -732,6 +733,7 @@ where
&self,
key: &[u8],
column: Self::Column,
offset: usize,
mut buf: &mut [u8],
) -> StorageResult<Option<usize>> {
self.metrics.read_meter.inc();
Expand All @@ -743,10 +745,15 @@ where
.get_pinned_cf_opt(&self.cf(column), key, &self.read_options)
.map_err(|e| DatabaseError::Other(e.into()))?
.map(|value| {
let read = value.len();
std::io::Write::write_all(&mut buf, value.as_ref())
.map_err(|e| DatabaseError::Other(anyhow::anyhow!(e)))?;
StorageResult::Ok(read)
if let Some(read) = value.len().checked_sub(offset) {
std::io::Write::write_all(&mut buf, value[offset..].as_ref())
.map_err(|e| DatabaseError::Other(anyhow::anyhow!(e)))?;
StorageResult::Ok(read)
} else {
Err(StorageError::Other(anyhow::anyhow!(
"Offset is out of bounds"
)))
}
})
.transpose()?;

Expand Down
16 changes: 11 additions & 5 deletions crates/services/txpool_v2/src/tests/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,22 @@ impl StorageRead<BlobData> for MockDb {
fn read(
&self,
key: &<BlobData as Mappable>::Key,
offset: usize,
buf: &mut [u8],
) -> Result<Option<usize>, Self::Error> {
let table = self.data.lock().unwrap();
let bytes = table.blobs.get(key);

let len = bytes.map(|bytes| {
buf.copy_from_slice(bytes.0.as_slice());
bytes.0.len()
});
Ok(len)
bytes
.map(|bytes| {
if let Some(len) = bytes.0.len().checked_sub(offset) {
buf.copy_from_slice(&bytes.0[offset..]);
Ok(len)
} else {
Err(())
}
})
.transpose()
}

fn read_alloc(
Expand Down
23 changes: 15 additions & 8 deletions crates/storage/src/kv_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,24 @@ pub trait KeyValueInspect {
&self,
key: &[u8],
column: Self::Column,
offset: usize,
buf: &mut [u8],
) -> StorageResult<Option<usize>> {
self.get(key, column)?
.map(|value| {
let read = value.len();
if read != buf.len() {
return Err(StorageError::Other(anyhow::anyhow!(
"Buffer size is not equal to the value size"
)));
if let Some(read) = value.len().checked_sub(offset) {
if read != buf.len() {
return Err(StorageError::Other(anyhow::anyhow!(
"Buffer size is not equal to the value size"
)));
};
buf.copy_from_slice(value[offset..].as_ref());
Ok(buf.len())
} else {
Err(StorageError::Other(anyhow::anyhow!(
"Offset is out of bounds"
)))
}
buf.copy_from_slice(value.as_ref());
Ok(read)
})
.transpose()
}
Expand Down Expand Up @@ -111,9 +117,10 @@ where
&self,
key: &[u8],
column: Self::Column,
offset: usize,
buf: &mut [u8],
) -> StorageResult<Option<usize>> {
self.deref().read(key, column, buf)
self.deref().read(key, column, offset, buf)
}
}

Expand Down
12 changes: 9 additions & 3 deletions crates/storage/src/structured_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,10 @@ where
&self,
key: &[u8],
column: Self::Column,
offset: usize,
buf: &mut [u8],
) -> StorageResult<Option<usize>> {
self.inner.read(key, column, buf)
self.inner.read(key, column, offset, buf)
}
}

Expand Down Expand Up @@ -359,15 +360,20 @@ where
fn read(
&self,
key: &<M as Mappable>::Key,
offset: usize,
buf: &mut [u8],
) -> Result<Option<usize>, Self::Error> {
let key_encoder =
<M::Blueprint as BlueprintInspect<M, StructuredStorage<S>>>::KeyCodec::encode(
key,
);
let key_bytes = key_encoder.as_bytes();
self.inner
.read(key_bytes.as_ref(), <M as TableWithBlueprint>::column(), buf)
self.inner.read(
key_bytes.as_ref(),
<M as TableWithBlueprint>::column(),
offset,
buf,
)
}

fn read_alloc(
Expand Down
3 changes: 2 additions & 1 deletion crates/storage/src/transactional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ where
&self,
key: &[u8],
column: Self::Column,
offset: usize,
buf: &mut [u8],
) -> StorageResult<Option<usize>> {
if let Some(operation) = self.get_from_changes(key, column) {
Expand All @@ -405,7 +406,7 @@ where
WriteOperation::Remove => Ok(None),
}
} else {
self.storage.read(key, column, buf)
self.storage.read(key, column, offset, buf)
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions crates/storage/src/vm_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,13 @@ impl<D, M: Mappable> StorageRead<M> for VmStorage<D>
where
D: StorageRead<M, Error = StorageError>,
{
fn read(&self, key: &M::Key, buf: &mut [u8]) -> Result<Option<usize>, Self::Error> {
StorageRead::<M>::read(&self.database, key, buf)
fn read(
&self,
key: &M::Key,
offset: usize,
buf: &mut [u8],
) -> Result<Option<usize>, Self::Error> {
StorageRead::<M>::read(&self.database, key, offset, buf)
}

fn read_alloc(
Expand Down

0 comments on commit afbfef2

Please sign in to comment.