diff --git a/pallets/anchors/src/lib.rs b/pallets/anchors/src/lib.rs index 35b23e3891..41a3bef7dc 100644 --- a/pallets/anchors/src/lib.rs +++ b/pallets/anchors/src/lib.rs @@ -21,13 +21,16 @@ use codec::{Decode, Encode}; use frame_support::{ dispatch::{DispatchError, DispatchResult}, storage::child, - traits::{Currency, ReservableCurrency}, - RuntimeDebug, StateVersion, + traits::{Currency, Get, ReservableCurrency}, + BoundedVec, RuntimeDebug, StateVersion, }; pub use pallet::*; use scale_info::TypeInfo; use sp_arithmetic::traits::{CheckedAdd, CheckedMul}; -use sp_runtime::{traits::Hash, ArithmeticError}; +use sp_runtime::{ + traits::{Hash, Header}, + ArithmeticError, +}; use sp_std::vec::Vec; pub use weights::*; pub mod weights; @@ -48,6 +51,14 @@ pub mod migration; mod common; +pub struct RootHashSize(sp_std::marker::PhantomData); + +impl Get for RootHashSize { + fn get() -> u32 { + ::LENGTH as u32 + } +} + type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; @@ -94,7 +105,6 @@ pub mod pallet { use frame_support::{pallet_prelude::*, traits::ReservableCurrency}; use frame_system::pallet_prelude::*; use sp_runtime::traits::Hash; - use sp_std::vec::Vec; use super::*; @@ -169,7 +179,8 @@ pub mod pallet { /// evicted anchor. #[pallet::storage] #[pallet::getter(fn get_evicted_anchor_root_by_day)] - pub(super) type EvictedAnchorRoots = StorageMap<_, Blake2_256, u32, Vec>; + pub(super) type EvictedAnchorRoots = + StorageMap<_, Blake2_256, u32, BoundedVec>>; #[pallet::error] pub enum Error { @@ -460,7 +471,11 @@ impl Pallet { // exists before hand to ensure that it doesn't overwrite a root. .map(|(day, key)| { if !>::contains_key(day) { - >::insert(day, child::root(&key, StateVersion::V0)); + let root: BoundedVec<_, _> = child::root(&key, StateVersion::V0) + .try_into() + .expect("The output hash must use the block hasher"); + + >::insert(day, root); } key }) diff --git a/pallets/anchors/src/tests.rs b/pallets/anchors/src/tests.rs index 523ae4594c..9ba317ee39 100644 --- a/pallets/anchors/src/tests.rs +++ b/pallets/anchors/src/tests.rs @@ -562,7 +562,11 @@ fn anchor_evict_single_anchor_per_day_many_days() { Anchors::get_anchor_id_by_index((day - 1) as u64).unwrap_or_default(), H256([0; 32]) ); - assert!(Anchors::get_evicted_anchor_root_by_day((day - 1) as u32).unwrap() != [0; 32]); + assert!( + Anchors::get_evicted_anchor_root_by_day((day - 1) as u32) + .unwrap() + .to_vec() != [0; 32] + ); assert_eq!( Anchors::get_anchor_evict_date(anchors[day - 2]).unwrap_or_default(), 0 @@ -649,7 +653,11 @@ fn anchor_evict_single_anchor_per_day_many_days() { // verify anchor data has been removed until 520th anchor for i in (2 + FIRST_ONES) as usize..(2 + FIRST_ONES + MAX_LOOP_IN_TX) as usize { assert!(Anchors::get_anchor_by_id(anchors[i as usize - 2]).is_none()); - assert!(Anchors::get_evicted_anchor_root_by_day(i as u32).unwrap() != [0; 32]); + assert!( + Anchors::get_evicted_anchor_root_by_day(i as u32) + .unwrap() + .to_vec() != [0; 32] + ); } assert!(