mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 04:01:02 +00:00
Match substrate's fmt (#1148)
* Alter gitlab. * Use substrate's rustfmt.toml * cargo +nightly fmt --all * Fix spellcheck. * cargo +nightly fmt --all * format. * Fix spellcheck and fmt * fmt? * Fix spellcheck Co-authored-by: Tomasz Drwięga <tomasz@parity.io>
This commit is contained in:
@@ -38,8 +38,7 @@
|
||||
|
||||
use crate::weights::WeightInfo;
|
||||
|
||||
use bp_header_chain::justification::GrandpaJustification;
|
||||
use bp_header_chain::InitializationData;
|
||||
use bp_header_chain::{justification::GrandpaJustification, InitializationData};
|
||||
use bp_runtime::{BlockNumberOf, Chain, HashOf, HasherOf, HeaderOf};
|
||||
use finality_grandpa::voter_set::VoterSet;
|
||||
use frame_support::{ensure, fail};
|
||||
@@ -136,10 +135,7 @@ pub mod pallet {
|
||||
ensure_operational::<T, I>()?;
|
||||
let _ = ensure_signed(origin)?;
|
||||
|
||||
ensure!(
|
||||
Self::request_count() < T::MaxRequests::get(),
|
||||
<Error<T, I>>::TooManyRequests
|
||||
);
|
||||
ensure!(Self::request_count() < T::MaxRequests::get(), <Error<T, I>>::TooManyRequests);
|
||||
|
||||
let (hash, number) = (finality_target.hash(), finality_target.number());
|
||||
log::trace!(target: "runtime::bridge-grandpa", "Going to try and finalize header {:?}", finality_target);
|
||||
@@ -153,27 +149,29 @@ pub mod pallet {
|
||||
finality_target,
|
||||
);
|
||||
fail!(<Error<T, I>>::NotInitialized);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// We do a quick check here to ensure that our header chain is making progress and isn't
|
||||
// "travelling back in time" (which could be indicative of something bad, e.g a hard-fork).
|
||||
// "travelling back in time" (which could be indicative of something bad, e.g a
|
||||
// hard-fork).
|
||||
ensure!(best_finalized.number() < number, <Error<T, I>>::OldHeader);
|
||||
|
||||
let authority_set = <CurrentAuthoritySet<T, I>>::get();
|
||||
let set_id = authority_set.set_id;
|
||||
verify_justification::<T, I>(&justification, hash, *number, authority_set)?;
|
||||
|
||||
let is_authorities_change_enacted = try_enact_authority_change::<T, I>(&finality_target, set_id)?;
|
||||
let is_authorities_change_enacted =
|
||||
try_enact_authority_change::<T, I>(&finality_target, set_id)?;
|
||||
<RequestCount<T, I>>::mutate(|count| *count += 1);
|
||||
insert_header::<T, I>(*finality_target, hash);
|
||||
log::info!(target: "runtime::bridge-grandpa", "Succesfully imported finalized header with hash {:?}!", hash);
|
||||
|
||||
// mandatory header is a header that changes authorities set. The pallet can't go further
|
||||
// without importing this header. So every bridge MUST import mandatory headers.
|
||||
// mandatory header is a header that changes authorities set. The pallet can't go
|
||||
// further without importing this header. So every bridge MUST import mandatory headers.
|
||||
//
|
||||
// We don't want to charge extra costs for mandatory operations. So relayer is not paying
|
||||
// fee for mandatory headers import transactions.
|
||||
// We don't want to charge extra costs for mandatory operations. So relayer is not
|
||||
// paying fee for mandatory headers import transactions.
|
||||
let is_mandatory_header = is_authorities_change_enacted;
|
||||
let pays_fee = if is_mandatory_header { Pays::No } else { Pays::Yes };
|
||||
|
||||
@@ -183,8 +181,8 @@ pub mod pallet {
|
||||
/// Bootstrap the bridge pallet with an initial header and authority set from which to sync.
|
||||
///
|
||||
/// The initial configuration provided does not need to be the genesis header of the bridged
|
||||
/// chain, it can be any arbitrary header. You can also provide the next scheduled set change
|
||||
/// if it is already know.
|
||||
/// chain, it can be any arbitrary header. You can also provide the next scheduled set
|
||||
/// change if it is already know.
|
||||
///
|
||||
/// This function is only allowed to be called from a trusted origin and writes to storage
|
||||
/// with practically no checks in terms of the validity of the data. It is important that
|
||||
@@ -213,17 +211,20 @@ pub mod pallet {
|
||||
///
|
||||
/// May only be called either by root, or by `PalletOwner`.
|
||||
#[pallet::weight((T::DbWeight::get().reads_writes(1, 1), DispatchClass::Operational))]
|
||||
pub fn set_owner(origin: OriginFor<T>, new_owner: Option<T::AccountId>) -> DispatchResultWithPostInfo {
|
||||
pub fn set_owner(
|
||||
origin: OriginFor<T>,
|
||||
new_owner: Option<T::AccountId>,
|
||||
) -> DispatchResultWithPostInfo {
|
||||
ensure_owner_or_root::<T, I>(origin)?;
|
||||
match new_owner {
|
||||
Some(new_owner) => {
|
||||
PalletOwner::<T, I>::put(&new_owner);
|
||||
log::info!(target: "runtime::bridge-grandpa", "Setting pallet Owner to: {:?}", new_owner);
|
||||
}
|
||||
},
|
||||
None => {
|
||||
PalletOwner::<T, I>::kill();
|
||||
log::info!(target: "runtime::bridge-grandpa", "Removed Owner of pallet.");
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
Ok(().into())
|
||||
@@ -233,7 +234,10 @@ pub mod pallet {
|
||||
///
|
||||
/// May only be called either by root, or by `PalletOwner`.
|
||||
#[pallet::weight((T::DbWeight::get().reads_writes(1, 1), DispatchClass::Operational))]
|
||||
pub fn set_operational(origin: OriginFor<T>, operational: bool) -> DispatchResultWithPostInfo {
|
||||
pub fn set_operational(
|
||||
origin: OriginFor<T>,
|
||||
operational: bool,
|
||||
) -> DispatchResultWithPostInfo {
|
||||
ensure_owner_or_root::<T, I>(origin)?;
|
||||
<IsHalted<T, I>>::put(operational);
|
||||
|
||||
@@ -260,11 +264,13 @@ pub mod pallet {
|
||||
|
||||
/// Hash of the header used to bootstrap the pallet.
|
||||
#[pallet::storage]
|
||||
pub(super) type InitialHash<T: Config<I>, I: 'static = ()> = StorageValue<_, BridgedBlockHash<T, I>, ValueQuery>;
|
||||
pub(super) type InitialHash<T: Config<I>, I: 'static = ()> =
|
||||
StorageValue<_, BridgedBlockHash<T, I>, ValueQuery>;
|
||||
|
||||
/// Hash of the best finalized header.
|
||||
#[pallet::storage]
|
||||
pub(super) type BestFinalized<T: Config<I>, I: 'static = ()> = StorageValue<_, BridgedBlockHash<T, I>, ValueQuery>;
|
||||
pub(super) type BestFinalized<T: Config<I>, I: 'static = ()> =
|
||||
StorageValue<_, BridgedBlockHash<T, I>, ValueQuery>;
|
||||
|
||||
/// A ring buffer of imported hashes. Ordered by the insertion time.
|
||||
#[pallet::storage]
|
||||
@@ -273,7 +279,8 @@ pub mod pallet {
|
||||
|
||||
/// Current ring buffer position.
|
||||
#[pallet::storage]
|
||||
pub(super) type ImportedHashesPointer<T: Config<I>, I: 'static = ()> = StorageValue<_, u32, ValueQuery>;
|
||||
pub(super) type ImportedHashesPointer<T: Config<I>, I: 'static = ()> =
|
||||
StorageValue<_, u32, ValueQuery>;
|
||||
|
||||
/// Headers which have been imported into the pallet.
|
||||
#[pallet::storage]
|
||||
@@ -292,7 +299,8 @@ pub mod pallet {
|
||||
/// runtime methods may still be used to do that (i.e. democracy::referendum to update halt
|
||||
/// flag directly or call the `halt_operations`).
|
||||
#[pallet::storage]
|
||||
pub(super) type PalletOwner<T: Config<I>, I: 'static = ()> = StorageValue<_, T::AccountId, OptionQuery>;
|
||||
pub(super) type PalletOwner<T: Config<I>, I: 'static = ()> =
|
||||
StorageValue<_, T::AccountId, OptionQuery>;
|
||||
|
||||
/// If true, all pallet transactions are failed immediately.
|
||||
#[pallet::storage]
|
||||
@@ -309,10 +317,7 @@ pub mod pallet {
|
||||
#[cfg(feature = "std")]
|
||||
impl<T: Config<I>, I: 'static> Default for GenesisConfig<T, I> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
owner: None,
|
||||
init_data: None,
|
||||
}
|
||||
Self { owner: None, init_data: None }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -419,29 +424,35 @@ pub mod pallet {
|
||||
) -> Result<(), sp_runtime::DispatchError> {
|
||||
use bp_header_chain::justification::verify_justification;
|
||||
|
||||
let voter_set = VoterSet::new(authority_set.authorities).ok_or(<Error<T, I>>::InvalidAuthoritySet)?;
|
||||
let voter_set =
|
||||
VoterSet::new(authority_set.authorities).ok_or(<Error<T, I>>::InvalidAuthoritySet)?;
|
||||
let set_id = authority_set.set_id;
|
||||
|
||||
Ok(
|
||||
verify_justification::<BridgedHeader<T, I>>((hash, number), set_id, &voter_set, justification).map_err(
|
||||
|e| {
|
||||
log::error!(
|
||||
target: "runtime::bridge-grandpa",
|
||||
"Received invalid justification for {:?}: {:?}",
|
||||
hash,
|
||||
e,
|
||||
);
|
||||
<Error<T, I>>::InvalidJustification
|
||||
},
|
||||
)?,
|
||||
Ok(verify_justification::<BridgedHeader<T, I>>(
|
||||
(hash, number),
|
||||
set_id,
|
||||
&voter_set,
|
||||
justification,
|
||||
)
|
||||
.map_err(|e| {
|
||||
log::error!(
|
||||
target: "runtime::bridge-grandpa",
|
||||
"Received invalid justification for {:?}: {:?}",
|
||||
hash,
|
||||
e,
|
||||
);
|
||||
<Error<T, I>>::InvalidJustification
|
||||
})?)
|
||||
}
|
||||
|
||||
/// Import a previously verified header to the storage.
|
||||
///
|
||||
/// Note this function solely takes care of updating the storage and pruning old entries,
|
||||
/// but does not verify the validity of such import.
|
||||
pub(crate) fn insert_header<T: Config<I>, I: 'static>(header: BridgedHeader<T, I>, hash: BridgedBlockHash<T, I>) {
|
||||
pub(crate) fn insert_header<T: Config<I>, I: 'static>(
|
||||
header: BridgedHeader<T, I>,
|
||||
hash: BridgedBlockHash<T, I>,
|
||||
) {
|
||||
let index = <ImportedHashesPointer<T, I>>::get();
|
||||
let pruning = <ImportedHashes<T, I>>::try_get(index);
|
||||
<BestFinalized<T, I>>::put(hash);
|
||||
@@ -461,12 +472,7 @@ pub mod pallet {
|
||||
pub(crate) fn initialize_bridge<T: Config<I>, I: 'static>(
|
||||
init_params: super::InitializationData<BridgedHeader<T, I>>,
|
||||
) {
|
||||
let super::InitializationData {
|
||||
header,
|
||||
authority_list,
|
||||
set_id,
|
||||
is_halted,
|
||||
} = init_params;
|
||||
let super::InitializationData { header, authority_list, set_id, is_halted } = init_params;
|
||||
|
||||
let initial_hash = header.hash();
|
||||
<InitialHash<T, I>>::put(initial_hash);
|
||||
@@ -506,7 +512,9 @@ pub mod pallet {
|
||||
fn ensure_owner_or_root<T: Config<I>, I: 'static>(origin: T::Origin) -> Result<(), BadOrigin> {
|
||||
match origin.into() {
|
||||
Ok(RawOrigin::Root) => Ok(()),
|
||||
Ok(RawOrigin::Signed(ref signer)) if Some(signer) == <PalletOwner<T, I>>::get().as_ref() => Ok(()),
|
||||
Ok(RawOrigin::Signed(ref signer))
|
||||
if Some(signer) == <PalletOwner<T, I>>::get().as_ref() =>
|
||||
Ok(()),
|
||||
_ => Err(BadOrigin),
|
||||
}
|
||||
}
|
||||
@@ -553,14 +561,17 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
parse: impl FnOnce(bp_runtime::StorageProofChecker<BridgedBlockHasher<T, I>>) -> R,
|
||||
) -> Result<R, sp_runtime::DispatchError> {
|
||||
let header = <ImportedHeaders<T, I>>::get(hash).ok_or(Error::<T, I>::UnknownHeader)?;
|
||||
let storage_proof_checker = bp_runtime::StorageProofChecker::new(*header.state_root(), storage_proof)
|
||||
.map_err(|_| Error::<T, I>::StorageRootMismatch)?;
|
||||
let storage_proof_checker =
|
||||
bp_runtime::StorageProofChecker::new(*header.state_root(), storage_proof)
|
||||
.map_err(|_| Error::<T, I>::StorageRootMismatch)?;
|
||||
|
||||
Ok(parse(storage_proof_checker))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn find_scheduled_change<H: HeaderT>(header: &H) -> Option<sp_finality_grandpa::ScheduledChange<H::Number>> {
|
||||
pub(crate) fn find_scheduled_change<H: HeaderT>(
|
||||
header: &H,
|
||||
) -> Option<sp_finality_grandpa::ScheduledChange<H::Number>> {
|
||||
use sp_runtime::generic::OpaqueDigestItemId;
|
||||
|
||||
let id = OpaqueDigestItemId::Consensus(&GRANDPA_ENGINE_ID);
|
||||
@@ -599,7 +610,8 @@ pub(crate) fn find_forced_change<H: HeaderT>(
|
||||
pub fn initialize_for_benchmarks<T: Config<I>, I: 'static>(header: BridgedHeader<T, I>) {
|
||||
initialize_bridge::<T, I>(InitializationData {
|
||||
header: Box::new(header),
|
||||
authority_list: sp_std::vec::Vec::new(), // we don't verify any proofs in external benchmarks
|
||||
authority_list: sp_std::vec::Vec::new(), /* we don't verify any proofs in external
|
||||
* benchmarks */
|
||||
set_id: 0,
|
||||
is_halted: false,
|
||||
});
|
||||
@@ -608,14 +620,15 @@ pub fn initialize_for_benchmarks<T: Config<I>, I: 'static>(header: BridgedHeader
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::mock::{run_test, test_header, Origin, TestHash, TestHeader, TestNumber, TestRuntime};
|
||||
use crate::mock::{
|
||||
run_test, test_header, Origin, TestHash, TestHeader, TestNumber, TestRuntime,
|
||||
};
|
||||
use bp_test_utils::{
|
||||
authority_list, make_default_justification, make_justification_for_header, JustificationGeneratorParams, ALICE,
|
||||
BOB,
|
||||
authority_list, make_default_justification, make_justification_for_header,
|
||||
JustificationGeneratorParams, ALICE, BOB,
|
||||
};
|
||||
use codec::Encode;
|
||||
use frame_support::weights::PostDispatchInfo;
|
||||
use frame_support::{assert_err, assert_noop, assert_ok};
|
||||
use frame_support::{assert_err, assert_noop, assert_ok, weights::PostDispatchInfo};
|
||||
use sp_runtime::{Digest, DigestItem, DispatchError};
|
||||
|
||||
fn initialize_substrate_bridge() {
|
||||
@@ -624,7 +637,10 @@ mod tests {
|
||||
|
||||
fn init_with_origin(
|
||||
origin: Origin,
|
||||
) -> Result<InitializationData<TestHeader>, sp_runtime::DispatchErrorWithPostInfo<PostDispatchInfo>> {
|
||||
) -> Result<
|
||||
InitializationData<TestHeader>,
|
||||
sp_runtime::DispatchErrorWithPostInfo<PostDispatchInfo>,
|
||||
> {
|
||||
let genesis = test_header(0);
|
||||
|
||||
let init_data = InitializationData {
|
||||
@@ -641,7 +657,11 @@ mod tests {
|
||||
let header = test_header(header.into());
|
||||
let justification = make_default_justification(&header);
|
||||
|
||||
Pallet::<TestRuntime>::submit_finality_proof(Origin::signed(1), Box::new(header), justification)
|
||||
Pallet::<TestRuntime>::submit_finality_proof(
|
||||
Origin::signed(1),
|
||||
Box::new(header),
|
||||
justification,
|
||||
)
|
||||
}
|
||||
|
||||
fn next_block() {
|
||||
@@ -653,10 +673,11 @@ mod tests {
|
||||
}
|
||||
|
||||
fn change_log(delay: u64) -> Digest<TestHash> {
|
||||
let consensus_log = ConsensusLog::<TestNumber>::ScheduledChange(sp_finality_grandpa::ScheduledChange {
|
||||
next_authorities: vec![(ALICE.into(), 1), (BOB.into(), 1)],
|
||||
delay,
|
||||
});
|
||||
let consensus_log =
|
||||
ConsensusLog::<TestNumber>::ScheduledChange(sp_finality_grandpa::ScheduledChange {
|
||||
next_authorities: vec![(ALICE.into(), 1), (BOB.into(), 1)],
|
||||
delay,
|
||||
});
|
||||
|
||||
Digest::<TestHash> {
|
||||
logs: vec![DigestItem::Consensus(GRANDPA_ENGINE_ID, consensus_log.encode())],
|
||||
@@ -821,14 +842,16 @@ mod tests {
|
||||
|
||||
let header = test_header(1);
|
||||
|
||||
let params = JustificationGeneratorParams::<TestHeader> {
|
||||
set_id: 2,
|
||||
..Default::default()
|
||||
};
|
||||
let params =
|
||||
JustificationGeneratorParams::<TestHeader> { set_id: 2, ..Default::default() };
|
||||
let justification = make_justification_for_header(params);
|
||||
|
||||
assert_err!(
|
||||
Pallet::<TestRuntime>::submit_finality_proof(Origin::signed(1), Box::new(header), justification,),
|
||||
Pallet::<TestRuntime>::submit_finality_proof(
|
||||
Origin::signed(1),
|
||||
Box::new(header),
|
||||
justification,
|
||||
),
|
||||
<Error<TestRuntime>>::InvalidJustification
|
||||
);
|
||||
})
|
||||
@@ -844,7 +867,11 @@ mod tests {
|
||||
justification.round = 42;
|
||||
|
||||
assert_err!(
|
||||
Pallet::<TestRuntime>::submit_finality_proof(Origin::signed(1), Box::new(header), justification,),
|
||||
Pallet::<TestRuntime>::submit_finality_proof(
|
||||
Origin::signed(1),
|
||||
Box::new(header),
|
||||
justification,
|
||||
),
|
||||
<Error<TestRuntime>>::InvalidJustification
|
||||
);
|
||||
})
|
||||
@@ -869,7 +896,11 @@ mod tests {
|
||||
let justification = make_default_justification(&header);
|
||||
|
||||
assert_err!(
|
||||
Pallet::<TestRuntime>::submit_finality_proof(Origin::signed(1), Box::new(header), justification,),
|
||||
Pallet::<TestRuntime>::submit_finality_proof(
|
||||
Origin::signed(1),
|
||||
Box::new(header),
|
||||
justification,
|
||||
),
|
||||
<Error<TestRuntime>>::InvalidAuthoritySet
|
||||
);
|
||||
})
|
||||
@@ -942,7 +973,11 @@ mod tests {
|
||||
|
||||
// Should not be allowed to import this header
|
||||
assert_err!(
|
||||
Pallet::<TestRuntime>::submit_finality_proof(Origin::signed(1), Box::new(header), justification),
|
||||
Pallet::<TestRuntime>::submit_finality_proof(
|
||||
Origin::signed(1),
|
||||
Box::new(header),
|
||||
justification
|
||||
),
|
||||
<Error<TestRuntime>>::UnsupportedScheduledChange
|
||||
);
|
||||
})
|
||||
@@ -963,7 +998,11 @@ mod tests {
|
||||
|
||||
// Should not be allowed to import this header
|
||||
assert_err!(
|
||||
Pallet::<TestRuntime>::submit_finality_proof(Origin::signed(1), Box::new(header), justification),
|
||||
Pallet::<TestRuntime>::submit_finality_proof(
|
||||
Origin::signed(1),
|
||||
Box::new(header),
|
||||
justification
|
||||
),
|
||||
<Error<TestRuntime>>::UnsupportedScheduledChange
|
||||
);
|
||||
})
|
||||
@@ -1021,7 +1060,11 @@ mod tests {
|
||||
let mut invalid_justification = make_default_justification(&header);
|
||||
invalid_justification.round = 42;
|
||||
|
||||
Pallet::<TestRuntime>::submit_finality_proof(Origin::signed(1), Box::new(header), invalid_justification)
|
||||
Pallet::<TestRuntime>::submit_finality_proof(
|
||||
Origin::signed(1),
|
||||
Box::new(header),
|
||||
invalid_justification,
|
||||
)
|
||||
};
|
||||
|
||||
initialize_substrate_bridge();
|
||||
|
||||
Reference in New Issue
Block a user