Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit 31955eb

Browse files
committed
1 parent f3f07a3 commit 31955eb

File tree

7 files changed

+247
-68
lines changed

7 files changed

+247
-68
lines changed

modules/grandpa/src/lib.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,16 @@ mod storage_types;
5353
use finality_grandpa::voter_set::VoterSet;
5454
// darwinia-network
5555
use bp_header_chain::{justification::GrandpaJustification, InitializationData};
56-
use bp_runtime::{BlockNumberOf, Chain, HashOf, HasherOf, HeaderOf, OwnedBridgeModule};
56+
use bp_runtime::{
57+
BlockNumberOf, BoundedStorageValue, Chain, HashOf, HasherOf, HeaderOf, OwnedBridgeModule,
58+
};
59+
use storage_types::StoredAuthoritySet;
5760
// paritytech
5861
use frame_support::{ensure, fail, log};
5962
use frame_system::ensure_signed;
6063
use sp_finality_grandpa::{ConsensusLog, GRANDPA_ENGINE_ID};
6164
use sp_runtime::traits::{Header as HeaderT, Zero};
6265
use sp_std::{boxed::Box, convert::TryInto};
63-
use storage_types::{StoredAuthoritySet, StoredBridgedHeader};
6466

6567
/// The target that will be used when publishing logs related to this pallet.
6668
pub const LOG_TARGET: &str = "runtime::bridge-grandpa";
@@ -73,6 +75,9 @@ pub type BridgedBlockHash<T, I> = HashOf<<T as Config<I>>::BridgedChain>;
7375
pub type BridgedBlockHasher<T, I> = HasherOf<<T as Config<I>>::BridgedChain>;
7476
/// Header of the bridged chain.
7577
pub type BridgedHeader<T, I> = HeaderOf<<T as Config<I>>::BridgedChain>;
78+
/// Stored header of the bridged chain.
79+
pub type StoredBridgedHeader<T, I> =
80+
BoundedStorageValue<<T as Config<I>>::MaxBridgedHeaderSize, BridgedHeader<T, I>>;
7681

7782
#[frame_support::pallet]
7883
pub mod pallet {
@@ -202,8 +207,18 @@ pub mod pallet {
202207

203208
let is_authorities_change_enacted =
204209
try_enact_authority_change::<T, I>(&finality_target, set_id)?;
205-
let finality_target =
206-
StoredBridgedHeader::<T, I>::try_from_bridged_header(*finality_target)?;
210+
let finality_target = StoredBridgedHeader::<T, I>::try_from_inner(*finality_target)
211+
.map_err(|e| {
212+
log::error!(
213+
target: LOG_TARGET,
214+
"Size of header {:?} ({}) is larger that the configured value {}",
215+
hash,
216+
e.value_size,
217+
e.maximal_size,
218+
);
219+
220+
Error::<T, I>::TooLargeHeader
221+
})?;
207222
<RequestCount<T, I>>::mutate(|count| *count += 1);
208223
insert_header::<T, I>(finality_target, hash);
209224
log::info!(
@@ -507,7 +522,8 @@ pub mod pallet {
507522
init_params;
508523
let authority_set = StoredAuthoritySet::<T, I>::try_new(authority_list, set_id)
509524
.map_err(|_| Error::TooManyAuthoritiesInSet)?;
510-
let header = StoredBridgedHeader::<T, I>::try_from_bridged_header(*header)?;
525+
let header = StoredBridgedHeader::<T, I>::try_from_inner(*header)
526+
.map_err(|_| Error::<T, I>::TooLargeHeader)?;
511527

512528
let initial_hash = header.hash();
513529
<InitialHash<T, I>>::put(initial_hash);
@@ -540,7 +556,7 @@ pub mod pallet {
540556
);
541557
let hash = header.hash();
542558
insert_header::<T, I>(
543-
StoredBridgedHeader::try_from_bridged_header(header)
559+
StoredBridgedHeader::<T, I>::try_from_inner(header)
544560
.expect("only used from benchmarks; benchmarks are correct; qed"),
545561
hash,
546562
);
@@ -556,7 +572,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
556572
/// if the pallet has not been initialized yet.
557573
pub fn best_finalized() -> Option<BridgedHeader<T, I>> {
558574
let (_, hash) = <BestFinalized<T, I>>::get()?;
559-
<ImportedHeaders<T, I>>::get(hash).map(|h| h.0)
575+
<ImportedHeaders<T, I>>::get(hash).map(|h| h.into_inner())
560576
}
561577

562578
/// Check if a particular header is known to the bridge pallet.
@@ -1106,7 +1122,7 @@ mod tests {
11061122
<BestFinalized<TestRuntime>>::put((2, hash));
11071123
<ImportedHeaders<TestRuntime>>::insert(
11081124
hash,
1109-
StoredBridgedHeader::try_from_bridged_header(header).unwrap(),
1125+
StoredBridgedHeader::<TestRuntime, ()>::try_from_inner(header).unwrap(),
11101126
);
11111127

11121128
assert_ok!(

modules/grandpa/src/storage_types.rs

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use crate::{BridgedHeader, Config, Error};
2020

2121
use bp_header_chain::AuthoritySet;
2222
use codec::{Decode, Encode, MaxEncodedLen};
23-
use frame_support::{traits::Get, BoundedVec, RuntimeDebugNoBound};
24-
use scale_info::{Type, TypeInfo};
23+
use frame_support::{BoundedVec, RuntimeDebugNoBound};
24+
use scale_info::TypeInfo;
2525
use sp_finality_grandpa::{AuthorityId, AuthorityList, AuthorityWeight, SetId};
2626

2727
/// A bounded list of Grandpa authorities with associated weights.
@@ -64,43 +64,3 @@ impl<T: Config<I>, I: 'static> From<StoredAuthoritySet<T, I>> for AuthoritySet {
6464
AuthoritySet { authorities: t.authorities.into(), set_id: t.set_id }
6565
}
6666
}
67-
68-
/// A bounded chain header.
69-
#[derive(Clone, Decode, Encode, Eq, PartialEq, RuntimeDebugNoBound)]
70-
pub struct StoredBridgedHeader<T: Config<I>, I: 'static>(pub BridgedHeader<T, I>);
71-
72-
impl<T: Config<I>, I: 'static> StoredBridgedHeader<T, I> {
73-
/// Construct `StoredBridgedHeader` from the `BridgedHeader` with all required checks.
74-
pub fn try_from_bridged_header(header: BridgedHeader<T, I>) -> Result<Self, Error<T, I>> {
75-
// this conversion is heavy (since we do encoding here), so we may want to optimize it later
76-
// (e.g. by introducing custom Encode implementation, and turning `StoredBridgedHeader` into
77-
// `enum StoredBridgedHeader { Decoded(BridgedHeader), Encoded(Vec<u8>) }`)
78-
if header.encoded_size() > T::MaxBridgedHeaderSize::get() as usize {
79-
Err(Error::TooLargeHeader)
80-
} else {
81-
Ok(StoredBridgedHeader(header))
82-
}
83-
}
84-
}
85-
86-
impl<T: Config<I>, I: 'static> sp_std::ops::Deref for StoredBridgedHeader<T, I> {
87-
type Target = BridgedHeader<T, I>;
88-
89-
fn deref(&self) -> &Self::Target {
90-
&self.0
91-
}
92-
}
93-
94-
impl<T: Config<I>, I: 'static> TypeInfo for StoredBridgedHeader<T, I> {
95-
type Identity = Self;
96-
97-
fn type_info() -> Type {
98-
BridgedHeader::<T, I>::type_info()
99-
}
100-
}
101-
102-
impl<T: Config<I>, I: 'static> MaxEncodedLen for StoredBridgedHeader<T, I> {
103-
fn max_encoded_len() -> usize {
104-
T::MaxBridgedHeaderSize::get() as usize
105-
}
106-
}

0 commit comments

Comments
 (0)