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

Remove SMT nodes along the path during update #627

Merged
merged 33 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b8c05e5
Removed subtraction of predicate used gas from the gas limit.
xgreenx Nov 3, 2023
0b6d296
Updated values of teh GTF
xgreenx Nov 3, 2023
4ad331f
Implemented `Policies` structure.
xgreenx Nov 4, 2023
044b3a1
Applied change for VM.
xgreenx Nov 4, 2023
17242eb
Minor nits
xgreenx Nov 6, 2023
9d78719
Fix test in `fuel-core`
xgreenx Nov 7, 2023
482deaf
Merge branch 'master' into feature/transaction-policy
xgreenx Nov 7, 2023
db58054
Fixed review ocmments
xgreenx Nov 7, 2023
a36e555
Update fuel-tx/src/transaction/types/create.rs
xgreenx Nov 7, 2023
9ceebb5
Fixed comments from the PR.
xgreenx Nov 7, 2023
d4814bb
Merge remote-tracking branch 'origin/feature/transaction-policy' into…
xgreenx Nov 7, 2023
dfa9320
Added a test for refund
xgreenx Nov 7, 2023
0b7eaad
Merge branch 'master' into feature/transaction-policy
xgreenx Nov 8, 2023
19834b6
Remove SMT nodes along the path during `update`
xgreenx Nov 8, 2023
3cec325
Apply suggestions from code review
xgreenx Nov 8, 2023
3f87142
Updated CHANGELOG.md
xgreenx Nov 8, 2023
5f69fbd
Renamed `GasLimit` to `ScriptGasLimit`
xgreenx Nov 8, 2023
a72f086
Make CI happy
xgreenx Nov 8, 2023
ce05ce8
Merge branch 'feature/transaction-policy' into feature/remove-nodes-a…
xgreenx Nov 8, 2023
bb41836
Update CHANGELOG.md
xgreenx Nov 8, 2023
ac8047c
Update CHANGELOG.md
xgreenx Nov 8, 2023
3efa7dd
Renamed `gas_limit` to `script_gas_limit`
xgreenx Nov 8, 2023
9a488a6
Merge remote-tracking branch 'origin/feature/transaction-policy' into…
xgreenx Nov 8, 2023
089fdd9
Renamed `gas_limit` to `script_gas_limit`
xgreenx Nov 8, 2023
69c08ef
Use correct values for policies GTF
xgreenx Nov 8, 2023
e8d7bcc
Address comments
xgreenx Nov 8, 2023
379869e
Merge branch 'master' into feature/transaction-policy
xgreenx Nov 9, 2023
c76736b
Update CHANGELOG.md
xgreenx Nov 9, 2023
db65ca3
Fixed the issue during inserting hte same key
xgreenx Nov 9, 2023
9ca1872
Merge remote-tracking branch 'origin/feature/remove-nodes-alon-the-pa…
xgreenx Nov 9, 2023
25e0a5e
Merge branch 'master' into feature/transaction-policy
xgreenx Nov 9, 2023
59435ba
Merge branch 'feature/transaction-policy' into feature/remove-nodes-a…
xgreenx Nov 9, 2023
3e233f1
Merge branch 'master' into feature/remove-nodes-alon-the-path
xgreenx Nov 9, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- If the `witnessses_size > policies.witness_limit`, then transaction will be rejected.
- GTF opcode changed its hardcoded constants for fields. It should be updated according to the values from the specification on the Sway side.

### Fixed

- [#627](https://github.com/FuelLabs/fuel-vm/pull/627): Added removal of obsolete SMT nodes along the path during `update` and `delete` operations.

## [Version 0.41.0]

#### Breaking
Expand Down
8 changes: 8 additions & 0 deletions fuel-merkle/src/common/storage_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ where
map: Default::default(),
}
}

pub fn is_empty(&self) -> bool {
self.map.is_empty()
}

pub fn len(&self) -> usize {
self.map.len()
}
}

impl<Type> StorageInspect<Type> for StorageMap<Type>
Expand Down
89 changes: 89 additions & 0 deletions fuel-merkle/src/sparse/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ impl<TableType, StorageType> MerkleTree<TableType, StorageType> {
self.storage
}

pub fn storage(&self) -> &StorageType {
&self.storage
}

// PRIVATE

fn root_node(&self) -> &Node {
Expand Down Expand Up @@ -421,6 +425,10 @@ where
let path = requested_leaf_node.leaf_key();
let actual_leaf_node = &path_nodes[0];

if requested_leaf_node == actual_leaf_node {
return Ok(())
}

// Build the tree upwards starting with the requested leaf node.
let mut current_node = requested_leaf_node.clone();

Expand Down Expand Up @@ -465,6 +473,8 @@ where
self.storage
.insert(current_node.hash(), &current_node.as_ref().into())?;
}
} else {
self.storage.remove(actual_leaf_node.hash())?;
}

// Merge side nodes
Expand All @@ -474,6 +484,10 @@ where
.insert(current_node.hash(), &current_node.as_ref().into())?;
}

for node in path_nodes.iter().skip(1 /* leaf */) {
self.storage.remove(node.hash())?;
}

self.set_root_node(current_node);

Ok(())
Expand Down Expand Up @@ -924,6 +938,81 @@ mod test {
assert_eq!(hex::encode(root), expected_root);
}

#[test]
fn test_update_removes_old_entries() {
let mut storage = StorageMap::<TestTable>::new();
let mut tree = MerkleTree::new(&mut storage);
let tenth_index = 9u32;

for i in 0_u32..tenth_index {
let key = key(i.to_be_bytes());
tree.update(key, b"DATA").unwrap();
}
let size_before_tenth = tree.storage().len();
let tenth_key = key(tenth_index.to_be_bytes());

// Given
tree.update(tenth_key, b"DATA").unwrap();
let size_after_tenth = tree.storage().len();
assert_ne!(size_after_tenth, size_before_tenth);

// When
tree.update(tenth_key, b"ANOTHER_DATA").unwrap();

// Then
assert_eq!(tree.storage().len(), size_after_tenth);
}

#[test]
fn test_update_with_the_same_value_does_not_remove_old_entries() {
let mut storage = StorageMap::<TestTable>::new();
let mut tree = MerkleTree::new(&mut storage);
let tenth_index = 9u32;

for i in 0_u32..tenth_index {
let key = key(i.to_be_bytes());
tree.update(key, b"DATA").unwrap();
}
let size_before_tenth = tree.storage().len();
let tenth_key = key(tenth_index.to_be_bytes());

// Given
tree.update(tenth_key, b"DATA").unwrap();
let size_after_tenth = tree.storage().len();
assert_ne!(size_after_tenth, size_before_tenth);

// When
tree.update(tenth_key, b"DATA").unwrap();

// Then
assert_eq!(tree.storage().len(), size_after_tenth);
}

#[test]
fn test_delete_removes_path_entries() {
let mut storage = StorageMap::<TestTable>::new();
let mut tree = MerkleTree::new(&mut storage);
let tenth_index = 9u32;

for i in 0_u32..tenth_index {
let key = key(i.to_be_bytes());
tree.update(key, b"DATA").unwrap();
}
let size_before_tenth = tree.storage().len();
let tenth_key = key(tenth_index.to_be_bytes());

// Given
tree.update(tenth_key, b"DATA").unwrap();
let size_after_tenth = tree.storage().len();
assert_ne!(size_after_tenth, size_before_tenth);

// When
tree.delete(tenth_key).unwrap();

// Then
assert_eq!(tree.storage().len(), size_before_tenth);
}

#[test]
fn test_delete_sparse_union() {
let mut storage = StorageMap::<TestTable>::new();
Expand Down