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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

130 changes: 23 additions & 107 deletions modules/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ pub mod benchmarking;
pub use pallet::*;
pub use weights::WeightInfo;

/// The target that will be used when publishing logs related to this pallet.
const LOG_TARGET: &str = "runtime::bridge-grandpa";

/// Block number of the bridged chain.
pub type BridgedBlockNumber<T, I> = BlockNumberOf<<T as Config<I>>::BridgedChain>;
/// Block hash of the bridged chain.
Expand Down Expand Up @@ -119,7 +122,7 @@ pub mod pallet {
}

impl<T: Config<I>, I: 'static> OwnedBridgeModule<T> for Pallet<T, I> {
const LOG_TARGET: &'static str = "runtime::bridge-grandpa";
const LOG_TARGET: &'static str = LOG_TARGET;
type OwnerStorage = PalletOwner<T, I>;
type OperatingMode = BasicOperatingMode;
type OperatingModeStorage = PalletOperatingMode<T, I>;
Expand Down Expand Up @@ -149,7 +152,11 @@ pub mod pallet {
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);
log::trace!(
target: LOG_TARGET,
"Going to try and finalize header {:?}",
finality_target
);

let best_finalized = BestFinalized::<T, I>::get();
let best_finalized =
Expand All @@ -158,7 +165,7 @@ pub mod pallet {
Some(best_finalized) => best_finalized,
None => {
log::error!(
target: "runtime::bridge-grandpa",
target: LOG_TARGET,
"Cannot finalize header {:?} because pallet is not yet initialized",
finality_target,
);
Expand All @@ -179,7 +186,11 @@ pub mod pallet {
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", "Successfully imported finalized header with hash {:?}!", hash);
log::info!(
target: LOG_TARGET,
"Successfully 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.
Expand Down Expand Up @@ -213,7 +224,7 @@ pub mod pallet {
initialize_bridge::<T, I>(init_data.clone());

log::info!(
target: "runtime::bridge-grandpa",
target: LOG_TARGET,
"Pallet has been initialized with the following parameters: {:?}",
init_data
);
Expand Down Expand Up @@ -392,7 +403,7 @@ pub mod pallet {
change_enacted = true;

log::info!(
target: "runtime::bridge-grandpa",
target: LOG_TARGET,
"Transitioned from authority set {} to {}! New authorities are: {:?}",
current_set_id,
current_set_id + 1,
Expand Down Expand Up @@ -429,7 +440,7 @@ pub mod pallet {
)
.map_err(|e| {
log::error!(
target: "runtime::bridge-grandpa",
target: LOG_TARGET,
"Received invalid justification for {:?}: {:?}",
hash,
e,
Expand All @@ -455,7 +466,7 @@ pub mod pallet {
// Update ring buffer pointer and remove old header.
<ImportedHashesPointer<T, I>>::put((index + 1) % T::HeadersToKeep::get());
if let Ok(hash) = pruning {
log::debug!(target: "runtime::bridge-grandpa", "Pruning old header: {:?}.", hash);
log::debug!(target: LOG_TARGET, "Pruning old header: {:?}.", hash);
<ImportedHeaders<T, I>>::remove(hash);
}
}
Expand Down Expand Up @@ -589,8 +600,8 @@ mod tests {
use crate::mock::{run_test, test_header, Origin, TestHeader, TestNumber, TestRuntime};
use bp_runtime::BasicOperatingMode;
use bp_test_utils::{
authority_list, make_default_justification, make_justification_for_header,
JustificationGeneratorParams, ALICE, BOB,
authority_list, generate_owned_bridge_module_tests, make_default_justification,
make_justification_for_header, JustificationGeneratorParams, ALICE, BOB,
};
use codec::Encode;
use frame_support::{
Expand Down Expand Up @@ -704,103 +715,6 @@ mod tests {
})
}

#[test]
fn pallet_owner_may_change_owner() {
run_test(|| {
PalletOwner::<TestRuntime>::put(2);

assert_ok!(Pallet::<TestRuntime>::set_owner(Origin::root(), Some(1)));
assert_noop!(
Pallet::<TestRuntime>::set_operating_mode(
Origin::signed(2),
BasicOperatingMode::Halted
),
DispatchError::BadOrigin,
);
assert_ok!(Pallet::<TestRuntime>::set_operating_mode(
Origin::root(),
BasicOperatingMode::Halted
));

assert_ok!(Pallet::<TestRuntime>::set_owner(Origin::signed(1), None));
assert_noop!(
Pallet::<TestRuntime>::set_operating_mode(
Origin::signed(1),
BasicOperatingMode::Normal
),
DispatchError::BadOrigin,
);
assert_noop!(
Pallet::<TestRuntime>::set_operating_mode(
Origin::signed(2),
BasicOperatingMode::Normal
),
DispatchError::BadOrigin,
);
assert_ok!(Pallet::<TestRuntime>::set_operating_mode(
Origin::root(),
BasicOperatingMode::Normal
));
});
}

#[test]
fn pallet_may_be_halted_by_root() {
run_test(|| {
assert_ok!(Pallet::<TestRuntime>::set_operating_mode(
Origin::root(),
BasicOperatingMode::Halted
));
assert_ok!(Pallet::<TestRuntime>::set_operating_mode(
Origin::root(),
BasicOperatingMode::Normal
));
});
}

#[test]
fn pallet_may_be_halted_by_owner() {
run_test(|| {
PalletOwner::<TestRuntime>::put(2);

assert_ok!(Pallet::<TestRuntime>::set_operating_mode(
Origin::signed(2),
BasicOperatingMode::Halted
));
assert_ok!(Pallet::<TestRuntime>::set_operating_mode(
Origin::signed(2),
BasicOperatingMode::Normal
));

assert_noop!(
Pallet::<TestRuntime>::set_operating_mode(
Origin::signed(1),
BasicOperatingMode::Halted
),
DispatchError::BadOrigin,
);
assert_noop!(
Pallet::<TestRuntime>::set_operating_mode(
Origin::signed(1),
BasicOperatingMode::Normal
),
DispatchError::BadOrigin,
);

assert_ok!(Pallet::<TestRuntime>::set_operating_mode(
Origin::signed(2),
BasicOperatingMode::Halted
));
assert_noop!(
Pallet::<TestRuntime>::set_operating_mode(
Origin::signed(1),
BasicOperatingMode::Normal
),
DispatchError::BadOrigin,
);
});
}

#[test]
fn pallet_rejects_transactions_if_halted() {
run_test(|| {
Expand Down Expand Up @@ -1173,4 +1087,6 @@ mod tests {
bp_header_chain::storage_keys::best_finalized_key("Grandpa").0,
);
}

generate_owned_bridge_module_tests!(BasicOperatingMode::Normal, BasicOperatingMode::Halted);
}
1 change: 1 addition & 0 deletions modules/messages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ sp-std = { git = "https://github.com/paritytech/substrate", branch = "master", d
[dev-dependencies]
sp-io = { git = "https://github.com/paritytech/substrate", branch = "master" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "master" }
bp-test-utils = { path = "../../primitives/test-utils" }

[features]
default = ["std"]
Expand Down
Loading