From 312a55bacc1da42309c31d442b7f4b7e0ef08fd5 Mon Sep 17 00:00:00 2001 From: David Salami Date: Wed, 8 Jan 2025 10:26:53 +0000 Subject: [PATCH] delete duplicate leaves when canonicalizing --- Cargo.lock | 2 ++ modules/pallets/mmr/gadget/Cargo.toml | 2 ++ .../pallets/mmr/gadget/src/offchain_mmr.rs | 25 ++++++++++++++++--- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f92869079..e21b140f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10326,7 +10326,9 @@ version = "29.0.1" dependencies = [ "futures", "log", + "mmr-primitives", "pallet-ismp", + "pallet-mmr 0.1.1", "pallet-mmr-runtime-api", "parity-scale-codec", "sc-client-api 37.0.0", diff --git a/modules/pallets/mmr/gadget/Cargo.toml b/modules/pallets/mmr/gadget/Cargo.toml index 4e87d3518..06b67dbbc 100644 --- a/modules/pallets/mmr/gadget/Cargo.toml +++ b/modules/pallets/mmr/gadget/Cargo.toml @@ -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 } diff --git a/modules/pallets/mmr/gadget/src/offchain_mmr.rs b/modules/pallets/mmr/gadget/src/offchain_mmr.rs index 05882736e..932dce3a0 100644 --- a/modules/pallets/mmr/gadget/src/offchain_mmr.rs +++ b/modules/pallets/mmr/gadget/src/offchain_mmr.rs @@ -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}; @@ -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::::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 {:?}",