Skip to content

Commit

Permalink
chore: Simplify InterpreterStorage (#683)
Browse files Browse the repository at this point in the history
* Simplify InterpreterStorage

* Update CHANGELOG.md
  • Loading branch information
Brandon Vrooman authored Feb 22, 2024
1 parent 77f44f8 commit de18292
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 125 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Changed
#### Breaking

- [#683](https://github.com/FuelLabs/fuel-vm/pull/683): Simplify `InterpreterStorage` by removing dependency on `MerkleRootStorage` and removing `merkle_` prefix from method names.
- [#672](https://github.com/FuelLabs/fuel-vm/pull/672): Remove `GasPrice` policy
- [#672](https://github.com/FuelLabs/fuel-vm/pull/672): Add `gas_price` field to transaction execution

Expand Down
14 changes: 7 additions & 7 deletions fuel-vm/src/interpreter/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ where

let _ = self
.storage
.merkle_contract_asset_id_balance_insert(contract_id, &asset_id, balance)
.contract_asset_id_balance_insert(contract_id, &asset_id, balance)
.map_err(RuntimeError::Storage)?;

let receipt = Receipt::burn(*sub_id, *contract_id, a, *self.pc, *self.is);
Expand Down Expand Up @@ -746,7 +746,7 @@ where

let old_value = self
.storage
.merkle_contract_asset_id_balance_insert(contract_id, &asset_id, balance)
.contract_asset_id_balance_insert(contract_id, &asset_id, balance)
.map_err(RuntimeError::Storage)?;

if old_value.is_none() {
Expand Down Expand Up @@ -1024,7 +1024,7 @@ pub(crate) fn state_read_word<S: InterpreterStorage>(
let key = Bytes32::from_bytes_ref(key.read(memory));

let value = storage
.merkle_contract_state(contract, key)
.contract_state(contract, key)
.map_err(RuntimeError::Storage)?
.map(|bytes| {
Word::from_be_bytes(
Expand Down Expand Up @@ -1085,7 +1085,7 @@ pub(crate) fn state_write_word<S: InterpreterStorage>(
value[..WORD_SIZE].copy_from_slice(&c.to_be_bytes());

let result = storage
.merkle_contract_state_insert(contract, key, &value)
.contract_state_insert(contract, key, &value)
.map_err(RuntimeError::Storage)?;

*created_new = result.is_none() as Word;
Expand Down Expand Up @@ -1263,7 +1263,7 @@ fn state_read_qword<S: InterpreterStorage>(

let mut all_set = true;
let result: Vec<u8> = storage
.merkle_contract_state_range(contract_id, origin_key, input.num_slots)
.contract_state_range(contract_id, origin_key, input.num_slots)
.map_err(RuntimeError::Storage)?
.into_iter()
.flat_map(|bytes| match bytes {
Expand Down Expand Up @@ -1338,7 +1338,7 @@ fn state_write_qword<'vm, S: InterpreterStorage>(
.collect();

let unset_count = storage
.merkle_contract_state_insert_range(contract_id, destination_key, &values)
.contract_state_insert_range(contract_id, destination_key, &values)
.map_err(RuntimeError::Storage)?;
*result_register = unset_count as Word;

Expand Down Expand Up @@ -1402,7 +1402,7 @@ fn state_clear_qword<S: InterpreterStorage>(
Bytes32::from_bytes_ref(input.start_storage_key_memory_range.read(memory));

let all_previously_set = storage
.merkle_contract_state_remove_range(contract_id, start_key, input.num_slots)
.contract_state_remove_range(contract_id, start_key, input.num_slots)
.map_err(RuntimeError::Storage)?
.is_some();

Expand Down
8 changes: 4 additions & 4 deletions fuel-vm/src/interpreter/blockchain/other_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn test_burn(
let initialize = initialize.into();
if let Some(initialize) = initialize {
let old_balance = storage
.merkle_contract_asset_id_balance_insert(&contract_id, &asset_id, initialize)
.contract_asset_id_balance_insert(&contract_id, &asset_id, initialize)
.unwrap();
assert!(old_balance.is_none());
}
Expand Down Expand Up @@ -76,7 +76,7 @@ fn test_burn(
.burn(amount, ContractId::LEN as Word)?;
assert_eq!(pc, 8);
let result = storage
.merkle_contract_asset_id_balance(&contract_id, &asset_id)
.contract_asset_id_balance(&contract_id, &asset_id)
.unwrap()
.unwrap();
assert_eq!(result, initialize.unwrap_or(0) - amount);
Expand Down Expand Up @@ -124,7 +124,7 @@ fn test_mint(
let initialize = initialize.into();
if let Some(initialize) = initialize {
let old_balance = storage
.merkle_contract_asset_id_balance_insert(&contract_id, &asset_id, initialize)
.contract_asset_id_balance_insert(&contract_id, &asset_id, initialize)
.unwrap();
assert!(old_balance.is_none());
}
Expand Down Expand Up @@ -166,7 +166,7 @@ fn test_mint(
.mint(amount, ContractId::LEN as Word)?;
assert_eq!(pc, 8);
let result = storage
.merkle_contract_asset_id_balance(&contract_id, &asset_id)
.contract_asset_id_balance(&contract_id, &asset_id)
.unwrap()
.unwrap();
assert_eq!(result, initialize.unwrap_or(0) + amount);
Expand Down
2 changes: 1 addition & 1 deletion fuel-vm/src/interpreter/blockchain/smo_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ fn test_smo(
let mut tx = Create::default();
let mut storage = MemoryStorage::new(Default::default(), Default::default());
let old_balance = storage
.merkle_contract_asset_id_balance_insert(
.contract_asset_id_balance_insert(
&ContractId::default(),
&base_asset_id,
initial_balance,
Expand Down
6 changes: 3 additions & 3 deletions fuel-vm/src/interpreter/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ where
S: ContractsAssetsStorage + ?Sized,
{
Ok(storage
.merkle_contract_asset_id_balance(contract, asset_id)
.contract_asset_id_balance(contract, asset_id)
.map_err(RuntimeError::Storage)?
.unwrap_or_default())
}
Expand All @@ -446,7 +446,7 @@ where
.ok_or(PanicReason::BalanceOverflow)?;

let old_value = storage
.merkle_contract_asset_id_balance_insert(contract, asset_id, balance)
.contract_asset_id_balance_insert(contract, asset_id, balance)
.map_err(RuntimeError::Storage)?;

Ok((balance, old_value.is_none()))
Expand All @@ -467,7 +467,7 @@ where
.checked_sub(amount)
.ok_or(PanicReason::NotEnoughBalance)?;
let _ = storage
.merkle_contract_asset_id_balance_insert(contract, asset_id, balance)
.contract_asset_id_balance_insert(contract, asset_id, balance)
.map_err(RuntimeError::Storage)?;
Ok(balance)
}
24 changes: 10 additions & 14 deletions fuel-vm/src/interpreter/contract/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ fn test_contract_balance(b: Word, c: Word) -> IoResult<(), Infallible> {
let contract_id = ContractId::from([3u8; 32]);
let mut storage = MemoryStorage::new(Default::default(), Default::default());
let old_balance = storage
.merkle_contract_asset_id_balance_insert(
&contract_id,
&AssetId::from([2u8; 32]),
33,
)
.contract_asset_id_balance_insert(&contract_id, &AssetId::from([2u8; 32]), 33)
.unwrap();
assert!(old_balance.is_none());
let mut pc = 4;
Expand Down Expand Up @@ -106,7 +102,7 @@ fn test_transfer(
let initial_recipient_contract_balance = 0;
let initial_source_contract_balance = 60;
let old_balance = storage
.merkle_contract_asset_id_balance_insert(
.contract_asset_id_balance_insert(
&SOURCE_CONTRACT_ID,
&ASSET_ID,
initial_source_contract_balance,
Expand Down Expand Up @@ -167,12 +163,12 @@ fn test_transfer(
// Then

let final_recipient_contract_balance = storage
.merkle_contract_asset_id_balance(&RECIPIENT_CONTRACT_ID, &ASSET_ID)
.contract_asset_id_balance(&RECIPIENT_CONTRACT_ID, &ASSET_ID)
.unwrap()
.unwrap();

let final_source_contract_balance = storage
.merkle_contract_asset_id_balance(&SOURCE_CONTRACT_ID, &ASSET_ID)
.contract_asset_id_balance(&SOURCE_CONTRACT_ID, &ASSET_ID)
.unwrap()
.unwrap();

Expand Down Expand Up @@ -249,7 +245,7 @@ fn test_transfer_output(
let initial_contract_balance = 60;

let old_balance = storage
.merkle_contract_asset_id_balance_insert(
.contract_asset_id_balance_insert(
&SOURCE_CONTRACT_ID,
&ASSET_ID,
initial_contract_balance,
Expand Down Expand Up @@ -321,7 +317,7 @@ fn test_transfer_output(
// Then

let final_contract_balance = storage
.merkle_contract_asset_id_balance(&SOURCE_CONTRACT_ID, &ASSET_ID)
.contract_asset_id_balance(&SOURCE_CONTRACT_ID, &ASSET_ID)
.unwrap()
.unwrap();

Expand Down Expand Up @@ -367,7 +363,7 @@ fn test_balance_increase(
let initial = initial.into();
if let Some(initial) = initial {
let old_balance = storage
.merkle_contract_asset_id_balance_insert(&contract_id, &asset_id, initial)
.contract_asset_id_balance_insert(&contract_id, &asset_id, initial)
.unwrap();
assert!(old_balance.is_none());
}
Expand All @@ -380,7 +376,7 @@ fn test_balance_increase(
assert_eq!(result, initial + amount);

let result = storage
.merkle_contract_asset_id_balance(&contract_id, &asset_id)
.contract_asset_id_balance(&contract_id, &asset_id)
.unwrap()
.unwrap();

Expand All @@ -405,7 +401,7 @@ fn test_balance_decrease(
let initial = initial.into();
if let Some(initial) = initial {
let old_balance = storage
.merkle_contract_asset_id_balance_insert(&contract_id, &asset_id, initial)
.contract_asset_id_balance_insert(&contract_id, &asset_id, initial)
.unwrap();
assert!(old_balance.is_none());
}
Expand All @@ -416,7 +412,7 @@ fn test_balance_decrease(
assert_eq!(result, initial - amount);

let result = storage
.merkle_contract_asset_id_balance(&contract_id, &asset_id)
.contract_asset_id_balance(&contract_id, &asset_id)
.unwrap()
.unwrap();

Expand Down
1 change: 0 additions & 1 deletion fuel-vm/src/interpreter/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use hashbrown::{
use fuel_asm::Word;
use fuel_storage::{
Mappable,
MerkleRootStorage,
StorageInspect,
StorageMutate,
};
Expand Down
22 changes: 6 additions & 16 deletions fuel-vm/src/interpreter/diff/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,6 @@ where
}
}

impl<Key, Type: StorageType, S> MerkleRootStorage<Key, Type> for Record<S>
where
S: InterpreterStorage,
S: MerkleRootStorage<Key, Type>,
{
fn root(&self, key: &Key) -> Result<fuel_storage::MerkleRoot, Self::Error> {
<S as MerkleRootStorage<Key, Type>>::root(&self.0, key)
}
}

impl<S: ContractsAssetsStorage + InterpreterStorage> ContractsAssetsStorage
for Record<S>
{
Expand Down Expand Up @@ -393,33 +383,33 @@ where
self.0.coinbase()
}

fn merkle_contract_state_range(
fn contract_state_range(
&self,
id: &ContractId,
start_key: &Bytes32,
range: usize,
) -> Result<Vec<Option<alloc::borrow::Cow<Bytes32>>>, Self::DataError> {
self.0.merkle_contract_state_range(id, start_key, range)
self.0.contract_state_range(id, start_key, range)
}

fn merkle_contract_state_insert_range(
fn contract_state_insert_range(
&mut self,
contract: &ContractId,
start_key: &Bytes32,
values: &[Bytes32],
) -> Result<usize, Self::DataError> {
self.0
.merkle_contract_state_insert_range(contract, start_key, values)
.contract_state_insert_range(contract, start_key, values)
}

fn merkle_contract_state_remove_range(
fn contract_state_remove_range(
&mut self,
contract: &ContractId,
start_key: &Bytes32,
range: usize,
) -> Result<Option<()>, S::DataError> {
self.0
.merkle_contract_state_remove_range(contract, start_key, range)
.contract_state_remove_range(contract, start_key, range)
}
}

Expand Down
2 changes: 1 addition & 1 deletion fuel-vm/src/interpreter/flow/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ fn test_prepare_call(input: Input) -> Result<Output, RuntimeError<Infallible>> {
}
for (a, n) in storage_balance.iter() {
let old_balance = storage
.merkle_contract_asset_id_balance_insert(&ContractId::default(), a, *n)
.contract_asset_id_balance_insert(&ContractId::default(), a, *n)
.unwrap();
assert!(old_balance.is_none());
}
Expand Down
Loading

0 comments on commit de18292

Please sign in to comment.