// Copyright (C) Parity Technologies (UK) Ltd. // This file is part of Parity Bridges Common. // Parity Bridges Common is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // Parity Bridges Common is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with Parity Bridges Common. If not, see . //! A module that is responsible for migration of storage. use crate::{Config, Pezpallet, LOG_TARGET}; use pezframe_support::{ traits::{Get, OnRuntimeUpgrade, StorageVersion}, weights::Weight, }; use xcm::prelude::{InteriorLocation, Location}; /// The in-code storage version. pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(0); /// This migration does not modify storage but can be used to open a bridge and link it to the /// specified LaneId. This is useful when we want to open a bridge and use a custom LaneId instead /// of the pre-calculated one provided by the `fn open_bridge extrinsic`. /// Or perhaps if you want to ensure that your runtime (e.g., for testing) always has an open /// bridge. pub struct OpenBridgeForLane< T, I, Lane, CreateLane, SourceRelativeLocation, BridgedUniversalLocation, >( core::marker::PhantomData<( T, I, Lane, CreateLane, SourceRelativeLocation, BridgedUniversalLocation, )>, ); impl< T: Config, I: 'static, Lane: Get, CreateLane: Get, SourceRelativeLocation: Get, BridgedUniversalLocation: Get, > OnRuntimeUpgrade for OpenBridgeForLane { 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(); tracing::info!( target: LOG_TARGET, ?lane_id, ?create_lane, ?bridge_origin_relative_location, ?bridge_destination_universal_location, "OpenBridgeForLane - going to open bridge" ); let locations = match Pezpallet::::bridge_locations( bridge_origin_relative_location.clone(), bridge_destination_universal_location.clone(), ) { Ok(locations) => locations, Err(e) => { tracing::error!( target: LOG_TARGET, error=?e, "OpenBridgeForLane - on_runtime_upgrade failed to construct bridge_locations" ); return T::DbWeight::get().reads(0); }, }; // check if already exists if let Some((bridge_id, bridge)) = Pezpallet::::bridge_by_lane_id(&lane_id) { tracing::info!( target: LOG_TARGET, ?bridge, ?bridge_id, ?lane_id, "OpenBridgeForLane - already exist!" ); if &bridge_id != locations.bridge_id() { tracing::warn!( target: LOG_TARGET, ?bridge, ?bridge_id, ?lane_id, ?bridge_origin_relative_location, ?bridge_destination_universal_location, "OpenBridgeForLane - check you parameters, because a different bridge exist for requested!" ); } return T::DbWeight::get().reads(2); } if let Err(e) = Pezpallet::::do_open_bridge(locations, lane_id, create_lane) { tracing::error!(target: LOG_TARGET, error=?e, "OpenBridgeForLane - do_open_bridge failed"); T::DbWeight::get().reads(6) } else { tracing::info!(target: LOG_TARGET, "OpenBridgeForLane - do_open_bridge passed!"); T::DbWeight::get().reads_writes(6, 4) } } #[cfg(feature = "try-runtime")] fn post_upgrade(_state: pezsp_std::vec::Vec) -> Result<(), pezsp_runtime::DispatchError> { let bridge_origin_relative_location = SourceRelativeLocation::get(); let bridge_destination_universal_location = BridgedUniversalLocation::get(); let lane_id = Lane::get(); // check that requested bridge is stored let Ok(locations) = Pezpallet::::bridge_locations( bridge_origin_relative_location.clone(), bridge_destination_universal_location.clone(), ) else { return Err(pezsp_runtime::DispatchError::Other("Invalid locations!")); }; let Some((bridge_id, _)) = Pezpallet::::bridge_by_lane_id(&lane_id) else { return Err(pezsp_runtime::DispatchError::Other("Missing bridge!")); }; pezframe_support::ensure!( locations.bridge_id() == &bridge_id, "Bridge is not stored correctly!" ); tracing::info!( target: LOG_TARGET, ?lane_id, ?bridge_origin_relative_location, ?bridge_destination_universal_location, "OpenBridgeForLane - post_upgrade found opened bridge" ); Ok(()) } }