-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Support MMR Pruning #9700
Support MMR Pruning #9700
Conversation
After support pruning, In this test, root in runtime storage has already been changed, and leaves for generating that root have already been pruned after OffchainStorage might be need to be used for generating and verifying proof. |
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.
Cool stuff, thanks!
I think the idea of removing nodes on every insertion is sensible - it's going to give us much more predictable on_initialize
weight compared to defered pruning.
We have to be careful though, to asses what the actual weight is going to be. The computation time is now (slowly) growing depending on the MMR size (more/less peaks), so the formula and benchmarking needs to be updated accordingly.
let elems = elems | ||
.into_iter() | ||
.enumerate() | ||
.map(|(i, elem)| (size + i as NodeIndex, elem)) | ||
.collect::<Vec<_>>(); | ||
let leaves = elems.iter().fold(leaves, |acc, (pos, elem)| { | ||
// Indexing API is used to store the full leaf content. | ||
let key = Pallet::<T, I>::offchain_key(size); | ||
elem.using_encoded(|elem| sp_io::offchain_index::set(&key, elem)); | ||
size += 1; | ||
elem.using_encoded(|elem| { | ||
offchain_index::set(&Pallet::<T, I>::offchain_key(*pos), elem) | ||
}); | ||
|
||
if let Node::Data(..) = elem { | ||
leaves += 1; | ||
acc + 1 | ||
} else { | ||
acc | ||
} | ||
} | ||
}); |
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.
These changes seem unnecessary and imho increase both code complexity and the number of iterations + extra allocation.
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.
I don't like mutable variable. IMO this simplify the code.
But yes, a little bit more allocation here. Could you accept this?
47c5cbd
to
1cbe99a
Compare
How do we benchmark this? |
@tomusdrw Please review. |
@tomusdrw We really need this feature! 🙏🏻 |
@AurevoirXavier apologies for the delay, reviewing this PR is high on my list, hoping to get this done this week. |
@tomusdrw Any progress? |
@adoerr Could this be merged? |
@tomusdrw is giving it another look right now |
Proposed my changes here: darwinia-network#123
|
Simplification of insertion & pruning code
@AurevoirXavier could you run |
bot merge |
Waiting for commit status. |
Just a note here, the weights were not getting updated. Could you mention me while updating the weights? |
* Use `0.3.2` * Replace `u64` with `NodeIndex` * Fix Typo * Add Pruning Logic * Fix Some Tests * Remove Comment * Log Only Under STD * Return while No Element to Append * Optimize Pruning Algorithm * Update Doc * Update Doc * Zero Copy Algorithm * Import Missing Type * Fix Merge Mistake * Import Missing Item * Make `verify` Off-Chain * `cargo fmt` * Avoid using NodeIndex in incorrect places. * Simplify pruning. * Format Co-authored-by: Tomasz Drwięga <[email protected]>
* Use `0.3.2` * Replace `u64` with `NodeIndex` * Fix Typo * Add Pruning Logic * Fix Some Tests * Remove Comment * Log Only Under STD * Return while No Element to Append * Optimize Pruning Algorithm * Update Doc * Update Doc * Zero Copy Algorithm * Import Missing Type * Fix Merge Mistake * Import Missing Item * Make `verify` Off-Chain * `cargo fmt` * Avoid using NodeIndex in incorrect places. * Simplify pruning. * Format Co-authored-by: Tomasz Drwięga <[email protected]>
* Use `0.3.2` * Replace `u64` with `NodeIndex` * Fix Typo * Add Pruning Logic * Fix Some Tests * Remove Comment * Log Only Under STD * Return while No Element to Append * Optimize Pruning Algorithm * Update Doc * Update Doc * Zero Copy Algorithm * Import Missing Type * Fix Merge Mistake * Import Missing Item * Make `verify` Off-Chain * `cargo fmt` * Avoid using NodeIndex in incorrect places. * Simplify pruning. * Format Co-authored-by: Tomasz Drwięga <[email protected]>
Add pruning process to the MMR pallet.
But I have encountered a problem with the
verify_leaf
part.should_verify_on_the_next_block_since_there_is_no_pruning_yet
this failed.We can't get the root at some moment (except the latest one) if we perform pruning on the
Nodes
.@tomusdrw Any idea on that?
Closes paritytech/grandpa-bridge-gadget#64