Skip to content

Commit

Permalink
delete duplicate leaves when canonicalizing
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizdave97 committed Jan 8, 2025
1 parent d95da3c commit 312a55b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions modules/pallets/mmr/gadget/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ sc-offchain = { workspace = true, default-features = true }
sp-runtime = { workspace = true, default-features = true }
pallet-mmr-runtime-api = { workspace = true, default-features = true }
pallet-ismp = { workspace = true, default-features = true }
pallet-mmr = { workspace = true, default-features = true }
mmr-primitives = { workspace = true, default-features = true }

25 changes: 22 additions & 3 deletions modules/pallets/mmr/gadget/src/offchain_mmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,21 @@
#![warn(missing_docs)]

use crate::{aux_schema, HashFor, MmrClient, LOG_TARGET};
use codec::Decode;
use log::{debug, error, info, trace, warn};
use pallet_ismp::offchain::Leaf;
use mmr_primitives::DataOrHash;
use pallet_ismp::offchain::{FullLeaf, Leaf};
use pallet_mmr_runtime_api::MmrRuntimeApi;
use sc_client_api::{Backend, FinalityNotification};
use sc_offchain::OffchainDb;
use sp_blockchain::{CachedHeaderMetadata, ForkBackend};
use sp_core::offchain::{DbExternalities, StorageKind};
use sp_core::{
keccak_256,
offchain::{DbExternalities, StorageKind},
};
use sp_mmr_primitives::{utils::NodesUtils, LeafIndex, NodeIndex};
use sp_runtime::{
traits::{Block, Header, NumberFor, One},
traits::{Block, Header, Keccak256, NumberFor, One},
Saturating,
};
use std::{collections::VecDeque, sync::Arc};
Expand Down Expand Up @@ -236,6 +241,20 @@ where
let canon_key = self.node_canon_offchain_key(pos);
self.offchain_db.local_storage_set(StorageKind::PERSISTENT, &canon_key, &elem);
self.offchain_db.local_storage_clear(StorageKind::PERSISTENT, &temp_key);
// if it's a leaf node, also clear the duplicate that was added to no_op storage
if let Ok(data) = pallet_mmr::mmr::Node::<Keccak256, Leaf>::decode(&mut &*elem) {
match data {
DataOrHash::Data(leaf) => {
let pre_image = leaf.preimage();
let commitment = keccak_256(&pre_image);
let duplicate_key =
pallet_ismp::offchain::leaf_default_key(commitment.into());
self.offchain_db
.local_storage_clear(StorageKind::PERSISTENT, &duplicate_key);
},
_ => {},
}
}
debug!(
target: LOG_TARGET,
"Moved elem at pos {}, fork_identifier {:?} header_hash {:?} to canon key {:?}",
Expand Down

0 comments on commit 312a55b

Please sign in to comment.