Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions pallets/anchors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -48,6 +51,14 @@ pub mod migration;

mod common;

pub struct RootHashSize<H>(sp_std::marker::PhantomData<H>);

impl<H: Header> Get<u32> for RootHashSize<H> {
fn get() -> u32 {
<H::Hashing as sp_core::Hasher>::LENGTH as u32
}
}

type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

Expand Down Expand Up @@ -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::*;

Expand Down Expand Up @@ -169,7 +179,8 @@ pub mod pallet {
/// evicted anchor.
#[pallet::storage]
#[pallet::getter(fn get_evicted_anchor_root_by_day)]
pub(super) type EvictedAnchorRoots<T: Config> = StorageMap<_, Blake2_256, u32, Vec<u8>>;
pub(super) type EvictedAnchorRoots<T: Config> =
StorageMap<_, Blake2_256, u32, BoundedVec<u8, RootHashSize<T::Header>>>;

#[pallet::error]
pub enum Error<T> {
Expand Down Expand Up @@ -460,7 +471,11 @@ impl<T: Config> Pallet<T> {
// exists before hand to ensure that it doesn't overwrite a root.
.map(|(day, key)| {
if !<EvictedAnchorRoots<T>>::contains_key(day) {
<EvictedAnchorRoots<T>>::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");

<EvictedAnchorRoots<T>>::insert(day, root);
}
key
})
Expand Down
12 changes: 10 additions & 2 deletions pallets/anchors/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!(
Expand Down