-
Notifications
You must be signed in to change notification settings - Fork 36
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
feat(SMT): reverse mutations generation, mutations serialization #355
Open
polydez
wants to merge
3
commits into
next
Choose a base branch
from
polydez-mutations-revert
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+227
−26
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ use alloc::{collections::BTreeMap, vec::Vec}; | |
use core::mem; | ||
|
||
use num::Integer; | ||
use winter_utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable}; | ||
|
||
use super::{EmptySubtreeRoots, InnerNodeInfo, MerkleError, MerklePath, NodeIndex}; | ||
use crate::{ | ||
|
@@ -149,7 +150,7 @@ pub(crate) trait SparseMerkleTree<const DEPTH: u8> { | |
if node_hash == *EmptySubtreeRoots::entry(DEPTH, node_depth) { | ||
// If a subtree is empty, when can remove the inner node, since it's equal to the | ||
// default value | ||
self.remove_inner_node(index) | ||
self.remove_inner_node(index); | ||
} else { | ||
self.insert_inner_node(index, InnerNode { left, right }); | ||
} | ||
|
@@ -256,7 +257,7 @@ pub(crate) trait SparseMerkleTree<const DEPTH: u8> { | |
} | ||
|
||
/// Apply the prospective mutations computed with [`SparseMerkleTree::compute_mutations()`] to | ||
/// this tree. | ||
/// this tree. Return reverse mutation set. | ||
/// | ||
/// # Errors | ||
/// If `mutations` was computed on a tree with a different root than this one, returns | ||
|
@@ -266,7 +267,7 @@ pub(crate) trait SparseMerkleTree<const DEPTH: u8> { | |
fn apply_mutations( | ||
&mut self, | ||
mutations: MutationSet<DEPTH, Self::Key, Self::Value>, | ||
) -> Result<(), MerkleError> | ||
) -> Result<MutationSet<DEPTH, Self::Key, Self::Value>, MerkleError> | ||
where | ||
Self: Sized, | ||
{ | ||
|
@@ -287,20 +288,41 @@ pub(crate) trait SparseMerkleTree<const DEPTH: u8> { | |
}); | ||
} | ||
|
||
let mut reverse_mutations = BTreeMap::new(); | ||
for (index, mutation) in node_mutations { | ||
match mutation { | ||
Removal => self.remove_inner_node(index), | ||
Addition(node) => self.insert_inner_node(index, node), | ||
Removal => { | ||
if let Some(node) = self.remove_inner_node(index) { | ||
reverse_mutations.insert(index, Addition(node)); | ||
} | ||
}, | ||
Addition(node) => { | ||
if let Some(old_node) = self.insert_inner_node(index, node) { | ||
reverse_mutations.insert(index, Addition(old_node)); | ||
} else { | ||
reverse_mutations.insert(index, Removal); | ||
} | ||
}, | ||
} | ||
} | ||
|
||
let mut reverse_pairs = BTreeMap::new(); | ||
for (key, value) in new_pairs { | ||
self.insert_value(key, value); | ||
if let Some(old_value) = self.insert_value(key.clone(), value) { | ||
reverse_pairs.insert(key, old_value); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we also rely on the computation of mutations set, that it didn't generate useless update where new value is the same as previous (actually it does generate unnecessary changes in mutations set). Otherwise we would need to clone |
||
} else { | ||
reverse_pairs.insert(key, Self::EMPTY_VALUE); | ||
} | ||
} | ||
|
||
self.set_root(new_root); | ||
|
||
Ok(()) | ||
Ok(MutationSet { | ||
old_root: new_root, | ||
node_mutations: reverse_mutations, | ||
new_pairs: reverse_pairs, | ||
new_root: old_root, | ||
}) | ||
} | ||
|
||
// REQUIRED METHODS | ||
|
@@ -326,10 +348,10 @@ pub(crate) trait SparseMerkleTree<const DEPTH: u8> { | |
fn get_inner_node(&self, index: NodeIndex) -> InnerNode; | ||
|
||
/// Inserts an inner node at the given index | ||
fn insert_inner_node(&mut self, index: NodeIndex, inner_node: InnerNode); | ||
fn insert_inner_node(&mut self, index: NodeIndex, inner_node: InnerNode) -> Option<InnerNode>; | ||
|
||
/// Removes an inner node at the given index | ||
fn remove_inner_node(&mut self, index: NodeIndex); | ||
fn remove_inner_node(&mut self, index: NodeIndex) -> Option<InnerNode>; | ||
|
||
/// Inserts a leaf node, and returns the value at the key if already exists | ||
fn insert_value(&mut self, key: Self::Key, value: Self::Value) -> Option<Self::Value>; | ||
|
@@ -606,8 +628,78 @@ impl<const DEPTH: u8, K, V> MutationSet<DEPTH, K, V> { | |
} | ||
} | ||
|
||
// SERIALIZATION | ||
// ================================================================================================ | ||
|
||
impl Serializable for InnerNode { | ||
fn write_into<W: ByteWriter>(&self, target: &mut W) { | ||
self.left.write_into(target); | ||
self.right.write_into(target); | ||
} | ||
} | ||
|
||
impl Deserializable for InnerNode { | ||
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> { | ||
let left = source.read()?; | ||
let right = source.read()?; | ||
|
||
Ok(Self { left, right }) | ||
} | ||
} | ||
|
||
impl Serializable for NodeMutation { | ||
fn write_into<W: ByteWriter>(&self, target: &mut W) { | ||
match self { | ||
NodeMutation::Removal => target.write_bool(false), | ||
NodeMutation::Addition(inner_node) => { | ||
target.write_bool(true); | ||
inner_node.write_into(target); | ||
}, | ||
} | ||
} | ||
} | ||
|
||
impl Deserializable for NodeMutation { | ||
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> { | ||
if source.read_bool()? { | ||
let inner_node = source.read()?; | ||
return Ok(NodeMutation::Addition(inner_node)); | ||
} | ||
|
||
Ok(NodeMutation::Removal) | ||
} | ||
} | ||
|
||
impl<const DEPTH: u8, K: Serializable, V: Serializable> Serializable for MutationSet<DEPTH, K, V> { | ||
fn write_into<W: ByteWriter>(&self, target: &mut W) { | ||
target.write(self.old_root); | ||
target.write(self.new_root); | ||
self.node_mutations.write_into(target); | ||
self.new_pairs.write_into(target); | ||
} | ||
} | ||
|
||
impl<const DEPTH: u8, K: Deserializable + Ord, V: Deserializable> Deserializable | ||
for MutationSet<DEPTH, K, V> | ||
{ | ||
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> { | ||
let old_root = source.read()?; | ||
let new_root = source.read()?; | ||
let node_mutations = source.read()?; | ||
let new_pairs = source.read()?; | ||
|
||
Ok(Self { | ||
old_root, | ||
node_mutations, | ||
new_pairs, | ||
new_root, | ||
}) | ||
} | ||
} | ||
|
||
// SUBTREES | ||
// ================================================================================================ | ||
|
||
/// A subtree is of depth 8. | ||
const SUBTREE_DEPTH: u8 = 8; | ||
|
||
|
@@ -797,5 +889,6 @@ pub fn build_subtree_for_bench( | |
|
||
// TESTS | ||
// ================================================================================================ | ||
|
||
#[cfg(test)] | ||
mod tests; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we rely on the computation of mutations set, that it didn't generate useless update where new value is the same as previous (actually it does generate unnecessary changes in mutations set). Otherwise we would need to clone
node
and compare it withold_node
.