Skip to content

Commit

Permalink
Use authorize_upgrade on Relay Chains (#29)
Browse files Browse the repository at this point in the history
* authorize on relay

* fix tests
  • Loading branch information
joepetrowski authored May 2, 2024
1 parent 35a5d6b commit 7614190
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
53 changes: 46 additions & 7 deletions src/build_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ pub(crate) struct UpgradeArgs {
#[clap(long = "only")]
pub(crate) only: bool,

/// Construct a call that will call `set_code` directly on the Relay Chain. This is generally
/// not recommended, as it involves submitting a large preimage (and therefore paying a large
/// fee). The default (false) uses `authorize_upgrade` instead, which only requires submitting
/// the hash. Anyone can then submit the actual runtime after it has been authorized.
#[clap(long = "set-relay-directly")]
pub(crate) set_relay_directly: bool,

/// The Fellowship release version. Should be semver and correspond to the release published.
#[clap(long = "relay-version")]
pub(crate) relay_version: Option<String>,
Expand Down Expand Up @@ -162,12 +169,16 @@ pub(crate) fn parse_inputs(prefs: UpgradeArgs) -> UpgradeDetails {
None => None,
};

let set_relay_directly = prefs.set_relay_directly;

// Get a version from one of the args. (This still feels dirty.)
let version = relay_version.clone().unwrap_or(asset_hub_version.unwrap_or(
bridge_hub_version.unwrap_or(encointer_version.unwrap_or(
collectives_version.unwrap_or(coretime_version.unwrap_or(String::from("no-version"))),
)),
));

// Set up a directory to store information fetched/written during this program.
let directory = format!("./upgrade-{}-{}/", &prefs.network, &version);
let output_file = if let Some(user_filename) = prefs.filename {
format!("{}{}", directory, user_filename)
Expand All @@ -177,7 +188,15 @@ pub(crate) fn parse_inputs(prefs: UpgradeArgs) -> UpgradeDetails {

make_version_directory(directory.as_str());

UpgradeDetails { relay, relay_version, networks, directory, output_file, additional }
UpgradeDetails {
relay,
relay_version,
networks,
directory,
output_file,
additional,
set_relay_directly,
}
}

// Create a directory into which to place runtime blobs and the final call data.
Expand Down Expand Up @@ -400,9 +419,19 @@ fn generate_relay_upgrade_call(upgrade_details: &UpgradeDetails) -> Option<CallI
let runtime_hash = blake2_256(&runtime);
println!("Kusama Relay Chain Runtime Hash: 0x{}", hex::encode(runtime_hash));

Some(CallInfo::from_runtime_call(NetworkRuntimeCall::Kusama(
KusamaRuntimeCall::System(SystemCall::set_code { code: runtime }),
)))
if !upgrade_details.set_relay_directly {
// authorize upgrade
Some(CallInfo::from_runtime_call(NetworkRuntimeCall::Kusama(
KusamaRuntimeCall::System(SystemCall::authorize_upgrade {
code_hash: H256(runtime_hash),
}),
)))
} else {
// set code directly
Some(CallInfo::from_runtime_call(NetworkRuntimeCall::Kusama(
KusamaRuntimeCall::System(SystemCall::set_code { code: runtime }),
)))
}
},
Network::Polkadot => {
use polkadot_relay::runtime_types::frame_system::pallet::Call as SystemCall;
Expand All @@ -415,9 +444,19 @@ fn generate_relay_upgrade_call(upgrade_details: &UpgradeDetails) -> Option<CallI
let runtime_hash = blake2_256(&runtime);
println!("Polkadot Relay Chain Runtime Hash: 0x{}", hex::encode(runtime_hash));

Some(CallInfo::from_runtime_call(NetworkRuntimeCall::Polkadot(
PolkadotRuntimeCall::System(SystemCall::set_code { code: runtime }),
)))
if !upgrade_details.set_relay_directly {
// authorize upgrade
Some(CallInfo::from_runtime_call(NetworkRuntimeCall::Polkadot(
PolkadotRuntimeCall::System(SystemCall::authorize_upgrade {
code_hash: H256(runtime_hash),
}),
)))
} else {
// set code directly
Some(CallInfo::from_runtime_call(NetworkRuntimeCall::Polkadot(
PolkadotRuntimeCall::System(SystemCall::set_code { code: runtime }),
)))
}
},
_ => panic!("Not a Relay Chain"),
}
Expand Down
3 changes: 3 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ fn upgrade_args_for_only_relay() -> UpgradeArgs {
UpgradeArgs {
network: String::from("polkadot"),
only: true,
set_relay_directly: true,
relay_version: Some(String::from("v1.2.0")),
asset_hub: None,
bridge_hub: None,
Expand All @@ -137,6 +138,7 @@ fn upgrade_args_for_only_asset_hub() -> UpgradeArgs {
UpgradeArgs {
network: String::from("polkadot"),
only: true,
set_relay_directly: true,
relay_version: None,
asset_hub: Some(String::from("v1.2.0")),
bridge_hub: None,
Expand All @@ -152,6 +154,7 @@ fn upgrade_args_for_all() -> UpgradeArgs {
UpgradeArgs {
network: String::from("polkadot"),
only: false,
set_relay_directly: true,
relay_version: Some(String::from("v1.2.0")),
asset_hub: None,
bridge_hub: None,
Expand Down
2 changes: 2 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ pub(super) struct UpgradeDetails {
pub(super) output_file: String,
// An additional call to be enacted in the same batch as the system upgrade.
pub(super) additional: Option<CallInfo>,
// Call `set_code` directly on the Relay Chain.
pub(super) set_relay_directly: bool,
}

// A network and the version to which it will upgrade.
Expand Down

0 comments on commit 7614190

Please sign in to comment.