use crate::{Config, Pallet, LOG_TARGET};
use frame_support::{
traits::{Get, OnRuntimeUpgrade, StorageVersion},
weights::Weight,
};
use xcm::prelude::{InteriorLocation, Location};
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);
pub struct OpenBridgeForLane<
T,
I,
Lane,
CreateLane,
SourceRelativeLocation,
BridgedUniversalLocation,
>(
core::marker::PhantomData<(
T,
I,
Lane,
CreateLane,
SourceRelativeLocation,
BridgedUniversalLocation,
)>,
);
impl<
T: Config<I>,
I: 'static,
Lane: Get<T::LaneId>,
CreateLane: Get<bool>,
SourceRelativeLocation: Get<Location>,
BridgedUniversalLocation: Get<InteriorLocation>,
> OnRuntimeUpgrade
for OpenBridgeForLane<T, I, Lane, CreateLane, SourceRelativeLocation, BridgedUniversalLocation>
{
fn on_runtime_upgrade() -> Weight {
let bridge_origin_relative_location = SourceRelativeLocation::get();
let bridge_destination_universal_location = BridgedUniversalLocation::get();
let lane_id = Lane::get();
let create_lane = CreateLane::get();
log::info!(
target: LOG_TARGET,
"OpenBridgeForLane - going to open bridge with lane_id: {lane_id:?} (create_lane: {create_lane:?}) \
between bridge_origin_relative_location: {bridge_origin_relative_location:?} and \
bridge_destination_universal_location: {bridge_destination_universal_location:?}",
);
let locations = match Pallet::<T, I>::bridge_locations(
bridge_origin_relative_location.clone(),
bridge_destination_universal_location.clone(),
) {
Ok(locations) => locations,
Err(e) => {
log::error!(
target: LOG_TARGET,
"OpenBridgeForLane - on_runtime_upgrade failed to construct bridge_locations with error: {e:?}"
);
return T::DbWeight::get().reads(0)
},
};
if let Some((bridge_id, bridge)) = Pallet::<T, I>::bridge_by_lane_id(&lane_id) {
log::info!(
target: LOG_TARGET,
"OpenBridgeForLane - bridge: {bridge:?} with bridge_id: {bridge_id:?} already exist for lane_id: {lane_id:?}!"
);
if &bridge_id != locations.bridge_id() {
log::warn!(
target: LOG_TARGET,
"OpenBridgeForLane - check you parameters, because a different bridge: {bridge:?} \
with bridge_id: {bridge_id:?} exist for lane_id: {lane_id:?} for requested \
bridge_origin_relative_location: {bridge_origin_relative_location:?} and \
bridge_destination_universal_location: {bridge_destination_universal_location:?} !",
);
}
return T::DbWeight::get().reads(2)
}
if let Err(e) = Pallet::<T, I>::do_open_bridge(locations, lane_id, create_lane) {
log::error!(target: LOG_TARGET, "OpenBridgeForLane - do_open_bridge failed with error: {e:?}");
T::DbWeight::get().reads(6)
} else {
log::info!(target: LOG_TARGET, "OpenBridgeForLane - do_open_bridge passed!");
T::DbWeight::get().reads_writes(6, 4)
}
}
#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: sp_std::vec::Vec<u8>) -> Result<(), sp_runtime::DispatchError> {
let bridge_origin_relative_location = SourceRelativeLocation::get();
let bridge_destination_universal_location = BridgedUniversalLocation::get();
let lane_id = Lane::get();
let Ok(locations) = Pallet::<T, I>::bridge_locations(
bridge_origin_relative_location.clone(),
bridge_destination_universal_location.clone(),
) else {
return Err(sp_runtime::DispatchError::Other("Invalid locations!"))
};
let Some((bridge_id, _)) = Pallet::<T, I>::bridge_by_lane_id(&lane_id) else {
return Err(sp_runtime::DispatchError::Other("Missing bridge!"))
};
frame_support::ensure!(
locations.bridge_id() == &bridge_id,
"Bridge is not stored correctly!"
);
log::info!(
target: LOG_TARGET,
"OpenBridgeForLane - post_upgrade found opened bridge with lane_id: {lane_id:?} \
between bridge_origin_relative_location: {bridge_origin_relative_location:?} and \
bridge_destination_universal_location: {bridge_destination_universal_location:?}",
);
Ok(())
}
}