Skip to content

Reinitialize bridge command #1410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
54 changes: 51 additions & 3 deletions control/preimage/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ use crate::bridge_hub_runtime::runtime_types::{
snowbridge_pallet_outbound_queue, snowbridge_pallet_system,
};
use crate::bridge_hub_runtime::RuntimeCall as BridgeHubRuntimeCall;
use hex_literal::hex;
use sp_crypto_hashing::twox_64;

const ASSET_HUB_CHANNEL_ID: [u8; 32] = hex!("c173fac324158e77fb5840738a1a541f633cbec8884c6a601c567d2b376a0539");

#[cfg(feature = "polkadot")]
pub mod asset_hub_polkadot_types {
Expand Down Expand Up @@ -149,12 +153,21 @@ pub fn outbound_queue_operating_mode(param: &OperatingModeEnum) -> BridgeHubRunt
}

pub fn upgrade(params: &UpgradeArgs) -> BridgeHubRuntimeCall {
let initializer = if params.initializer {
Some((
params.initializer_params.as_ref().unwrap().clone(),
params.initializer_gas.unwrap(),
))
} else {
None
};

BridgeHubRuntimeCall::EthereumSystem(snowbridge_pallet_system::pallet::Call::upgrade {
impl_address: params.logic_address.into_array().into(),
impl_code_hash: params.logic_code_hash.0.into(),
initializer: Some(Initializer {
params: params.initializer_params.clone().into(),
maximum_required_gas: params.initializer_gas,
initializer: initializer.map(|(params, gas)| Initializer {
params: params.into(),
maximum_required_gas: gas,
}),
})
}
Expand Down Expand Up @@ -263,6 +276,41 @@ pub fn set_assethub_fee(fee: u128) -> AssetHubRuntimeCall {
)
}

pub fn set_inbound_nonce() -> BridgeHubRuntimeCall {
set_nonce("EthereumInboundQueue")
}

pub fn set_outbound_nonce() -> BridgeHubRuntimeCall {
set_nonce("EthereumOutboundQueue")
}

fn set_nonce(pallet_name: &str) -> BridgeHubRuntimeCall {
let new_nonce: Vec<u8> = 0u64.encode();

let mut key = Vec::new();
let pallet_prefix = pallet_name.as_bytes();
key.extend_from_slice(&twox_128(&pallet_prefix));

let storage_prefix = b"Nonce";
key.extend_from_slice(&twox_128(storage_prefix));

let encoded_id = ASSET_HUB_CHANNEL_ID.encode();
let hash_id_64 = twox_64(&encoded_id);
key.extend_from_slice(&hash_id_64);
key.extend_from_slice(&encoded_id);

println!("nonce key for {}: 0x{}", pallet_name, hex::encode(&key));

BridgeHubRuntimeCall::System(
crate::bridge_hub_runtime::runtime_types::frame_system::pallet::Call::set_storage {
items: vec![(
key,
new_nonce,
)],
},
)
}

pub fn force_checkpoint(params: &ForceCheckpointArgs) -> BridgeHubRuntimeCall {
let mut file = File::open(params.checkpoint.clone()).expect("File not found");
let mut data = String::new();
Expand Down
67 changes: 55 additions & 12 deletions control/preimage/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub enum Command {
TreasuryProposal2024(TreasuryProposal2024Args),
/// Governance update 202501
GovUpdate202501(GovUpdate202501Args),
/// Reinitialize bridge
ReinitializeBridge(ReinitializeBridgeArgs),
}

#[derive(Debug, Args)]
Expand Down Expand Up @@ -112,13 +114,17 @@ pub struct UpgradeArgs {
#[arg(long, value_name = "HASH", value_parser=parse_hex_bytes32)]
logic_code_hash: FixedBytes<32>,

/// Initialize the logic contract
#[arg(long, requires_all=["initializer_params", "initializer_gas"])]
initializer: bool,

/// ABI-encoded params to pass to initializer
#[arg(long, value_name = "BYTES", value_parser=parse_hex_bytes)]
initializer_params: Bytes,
#[arg(long, requires = "initializer", value_name = "BYTES", value_parser=parse_hex_bytes)]
initializer_params: Option<Bytes>,

/// Maximum gas required by the initializer
#[arg(long, value_name = "GAS")]
initializer_gas: u64,
#[arg(long, requires = "initializer", value_name = "GAS")]
initializer_gas: Option<u64>,
}

#[derive(Debug, Args)]
Expand Down Expand Up @@ -242,6 +248,16 @@ pub struct RegisterEtherArgs {
ether_decimals: u8,
}

#[derive(Debug, Args)]
pub struct ReinitializeBridgeArgs {
//#[arg(long, value_name = "FILE")]
// pub checkpoint: PathBuf,
#[command(flatten)]
register_ether: RegisterEtherArgs,
#[command(flatten)]
gateway_address: GatewayAddressArgs,
}

#[derive(Debug, Args)]
pub struct ApiEndpoints {
#[arg(long, value_name = "URL")]
Expand Down Expand Up @@ -318,14 +334,14 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
.bridge_hub_api
.unwrap_or(BRIDGE_HUB_API.to_owned()),
)
.await?;
.await?;

let asset_hub_api: OnlineClient<PolkadotConfig> = OnlineClient::from_url(
cli.api_endpoints
.asset_hub_api
.unwrap_or(ASSET_HUB_API.to_owned()),
)
.await?;
.await?;

let relay_api: OnlineClient<PolkadotConfig> =
OnlineClient::from_url(cli.api_endpoints.relay_api.unwrap_or(RELAY_API.to_owned())).await?;
Expand Down Expand Up @@ -355,7 +371,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
commands::force_checkpoint(&params.force_checkpoint),
],
)
.await?;
.await?;
let (register_ether_call, set_ether_metadata_call) =
commands::register_ether(&params.register_ether);
let asset_hub_call = send_xcm_asset_hub(
Expand All @@ -367,7 +383,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
set_ethereum_fee,
],
)
.await?;
.await?;
utility_force_batch(vec![bridge_hub_call, asset_hub_call])
}
Command::UpdateAsset(params) => {
Expand All @@ -378,7 +394,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
commands::force_set_metadata(params),
],
)
.await?
.await?
}
Command::GatewayOperatingMode(params) => {
let call = commands::gateway_operating_mode(&params.gateway_operating_mode);
Expand Down Expand Up @@ -448,9 +464,9 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
}
Command::TreasuryProposal2024(params) => treasury_commands::treasury_proposal(&params),
Command::GovUpdate202501(GovUpdate202501Args {
pricing_parameters,
register_ether,
}) => {
pricing_parameters,
register_ether,
}) => {
let (set_pricing_parameters, set_ethereum_fee) =
commands::pricing_parameters(&context, pricing_parameters).await?;

Expand All @@ -470,6 +486,33 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
ah_set_pricing_call,
ah_register_ether_call,
])
},
Command::ReinitializeBridge(params) => {
//let force_checkpoint_call = commands::force_checkpoint(&ForceCheckpointArgs{
// checkpoint: params.checkpoint.clone(),
// });
let gateway_address_call = commands::set_gateway_address(&params.gateway_address);
let inbound_nonce_call = commands::set_inbound_nonce();
let outbound_nonce_call = commands::set_outbound_nonce();
let (register_ether_call, set_ether_metadata_call) =
commands::register_ether(&params.register_ether);

let bridge_hub_call = send_xcm_bridge_hub(&context, vec![
// force_checkpoint_call,
gateway_address_call,
inbound_nonce_call,
outbound_nonce_call,
]).await?;

let asset_hub_call = send_xcm_asset_hub(
&context,
vec![
register_ether_call,
set_ether_metadata_call,
],
)
.await?;
utility_force_batch(vec![bridge_hub_call, asset_hub_call])
}
};

Expand Down
Binary file modified control/runtimes/bridge-hub-paseo/bridge-hub-metadata.bin
Binary file not shown.
Binary file modified control/runtimes/paseo/polkadot-metadata.bin
Binary file not shown.
Loading