Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
MitchTurner committed Feb 29, 2024
1 parent d0edfb3 commit 5bb98b4
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 42 deletions.
32 changes: 16 additions & 16 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fuel-core-tests = { version = "0.0.0", path = "./tests" }
fuel-core-xtask = { version = "0.0.0", path = "./xtask" }

# Fuel dependencies
fuel-vm-private = { version = "0.46.0", package = "fuel-vm", default-features = false }
fuel-vm-private = { version = "0.47.0", package = "fuel-vm", default-features = false }

# Common dependencies
anyhow = "1.0"
Expand Down
52 changes: 43 additions & 9 deletions crates/storage/src/vm_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ use fuel_core_types::{
fuel_vm::InterpreterStorage,
tai64::Tai64,
};
use fuel_vm_private::{
fuel_storage::StorageWrite,
storage::ContractsStateData,
};
use itertools::Itertools;
use primitive_types::U256;
use std::borrow::Cow;
Expand Down Expand Up @@ -161,6 +165,27 @@ where
}
}

impl<D, M: Mappable> StorageWrite<M> for VmStorage<D>
where
D: StorageWrite<M, Error = StorageError>,
{
fn write(&mut self, key: &M::Key, buf: &[u8]) -> Result<usize, Self::Error> {
StorageWrite::<M>::write(&mut self.database, key, buf)
}

fn replace(
&mut self,
key: &M::Key,
buf: &[u8],
) -> Result<(usize, Option<Vec<u8>>), Self::Error> {
StorageWrite::<M>::replace(&mut self.database, key, buf)
}

fn take(&mut self, key: &M::Key) -> Result<Option<Vec<u8>>, Self::Error> {
StorageWrite::<M>::take(&mut self.database, key)
}
}

impl<D, K, M: Mappable> MerkleRootStorage<K, M> for VmStorage<D>
where
D: MerkleRootStorage<K, M, Error = StorageError>,
Expand All @@ -185,7 +210,10 @@ where
+ StorageMutate<ContractsState, Error = StorageError>
+ StorageRead<ContractsRawCode, Error = StorageError>
+ MerkleRootStorage<ContractId, ContractsAssets, Error = StorageError>
+ VmStorageRequirements<Error = StorageError>,
+ VmStorageRequirements<Error = StorageError>
+ StorageRead<ContractsState>
+ StorageWrite<ContractsState>
+ StorageWrite<ContractsRawCode>,
{
type DataError = StorageError;

Expand Down Expand Up @@ -237,12 +265,12 @@ where
)
}

fn merkle_contract_state_range(
fn contract_state_range(
&self,
contract_id: &ContractId,
start_key: &Bytes32,
range: usize,
) -> Result<Vec<Option<Cow<Bytes32>>>, Self::DataError> {
) -> Result<Vec<Option<Cow<ContractsStateData>>>, Self::DataError> {
use crate::StorageAsRef;

let mut key = U256::from_big_endian(start_key.as_ref());
Expand All @@ -258,16 +286,22 @@ where
Ok(results)
}

fn merkle_contract_state_insert_range(
fn contract_state_insert_range<'a, I>(
&mut self,
contract_id: &ContractId,
start_key: &Bytes32,
values: &[Bytes32],
) -> Result<usize, Self::DataError> {
values: I,
) -> Result<usize, Self::DataError>
where
I: Iterator<Item = &'a [u8]>,
{
let mut current_key = U256::from_big_endian(start_key.as_ref());

// TODO: Is there a safe way to do this?
let (values_len, _) = values.size_hint();
// verify key is in range
current_key
.checked_add(U256::from(values.len()))
.checked_add(U256::from(values_len))
.ok_or_else(|| anyhow!("range op exceeded available keyspace"))?;

let mut key_bytes = Bytes32::zeroed();
Expand All @@ -292,7 +326,7 @@ where
Ok(found_unset as usize)
}

fn merkle_contract_state_remove_range(
fn contract_state_remove_range(
&mut self,
contract_id: &ContractId,
start_key: &Bytes32,
Expand Down Expand Up @@ -376,6 +410,6 @@ where
let slots = slots
.map(|(key, value)| (ContractsStateKey::new(contract_id, &key), value))
.collect_vec();
self.init_storage(slots.iter().map(|kv| (&kv.0, &kv.1)))
self.init_storage(slots.iter().map(|kv| (&kv.0, kv.1.as_ref())))
}
}
16 changes: 0 additions & 16 deletions crates/types/src/services/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,6 @@ pub enum PoolTransaction {
}

impl PoolTransaction {
/// Returns the gas price.
pub fn price(&self) -> Word {
match self {
PoolTransaction::Script(script) => script.transaction().price(),
PoolTransaction::Create(create) => create.transaction().price(),
}
}

/// Returns the maximum amount of gas that the transaction can consume.
pub fn max_gas(&self) -> Word {
match self {
PoolTransaction::Script(script) => script.metadata().fee.max_gas(),
PoolTransaction::Create(create) => create.metadata().fee.max_gas(),
}
}

/// Used for accounting purposes when charging byte based fees.
pub fn metered_bytes_size(&self) -> usize {
match self {
Expand Down

0 comments on commit 5bb98b4

Please sign in to comment.