Skip to content

Commit

Permalink
Update AUTHORIZE_UPGRADE_PREFIX (#64)
Browse files Browse the repository at this point in the history
* Update `AUTHORIZE_UPGRADE_PREFIX`

* Make `const`
  • Loading branch information
aurexav authored Apr 13, 2023
1 parent 8e2ed46 commit cbf9282
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .env-sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
# POLKADOT_HTTP=http://localhost:9933
# POLKADOT_WS=ws://localhost:9944
# PARACHAIN_PALLET_ID=0x01
# AUTHORIZE_UPGRADE_PREFIX=0x03
# AUTHORIZE_UPGRADE_PREFIX=0x02
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ MacOS Homebrew users can use:
-h, --help Print help information
-V, --version Print version information

By default, the ID for the Parachain pallet is expected to be `0x01` and the call ID for `authorize_upgrade` is expected to be `0x03`.
By default, the ID for the Parachain pallet is expected to be `0x01` and the call ID for `authorize_upgrade` is expected to be `0x02`.
This default behavior can be overriden by setting the `PARACHAIN_PALLET_ID` to the ID of your parachain pallet and the `AUTHORIZE_UPGRADE_PREFIX` to the ID of your choice.

### Command: version
Expand Down Expand Up @@ -212,7 +212,7 @@ In addition to the command line flags, you can also pass one of the following EN
# POLKADOT_HTTP=http://localhost:9933
# POLKADOT_WS=ws://localhost:9944
# PARACHAIN_PALLET_ID=0x01
# AUTHORIZE_UPGRADE_PREFIX=0x03
# AUTHORIZE_UPGRADE_PREFIX=0x02

## Sample runs

Expand Down
2 changes: 1 addition & 1 deletion README_src.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ include::doc/usage_get.adoc[]
include::doc/usage_info.adoc[]
----

NOTE: By default, the ID for the Parachain pallet is expected to be `0x01` and the call ID for `authorize_upgrade` is expected to be `0x03`.
NOTE: By default, the ID for the Parachain pallet is expected to be `0x01` and the call ID for `authorize_upgrade` is expected to be `0x02`.
This default behavior can be overriden by setting the `PARACHAIN_PALLET_ID` to the ID of your parachain pallet and the `AUTHORIZE_UPGRADE_PREFIX` to the ID of your choice.

=== Command: version
Expand Down
25 changes: 16 additions & 9 deletions libs/substrate-runtime-proposal-hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ type Prefix = (u8, u8);

/// The PREFIX is prepended to the data before hashing
pub const PREFIX_SYSTEM_SETCODE: Prefix = (0x00, 0x03);

const PARACHAIN_PALLET_ID_ENV: &str = "PARACHAIN_PALLET_ID";
const DEFAULT_PARACHAIN_PALLET_ID: &str = "0x01";

const AUTHORIZE_UPGRADE_PREFIX_ENV: &str = "AUTHORIZE_UPGRADE_PREFIX";
const DEFAULT_AUTHORIZE_UPGRADE_PREFIX: &str = "0x02";

/// This struct is a container for whatever we calculated.
#[derive(Debug)]
Expand Down Expand Up @@ -62,8 +66,11 @@ pub fn get_system_setcode(wasm_blob: &[u8]) -> CalllHash {
}

pub fn get_parachainsystem_authorize_upgrade(wasm_blob: &[u8]) -> CalllHash {
let s1 = env::var(PARACHAIN_PALLET_ID_ENV).unwrap_or_else(|_| String::from("0x01")).replace("0x", "");
let s2 = env::var(AUTHORIZE_UPGRADE_PREFIX_ENV).unwrap_or_else(|_| String::from("0x03")).replace("0x", "");
let s1 =
env::var(PARACHAIN_PALLET_ID_ENV).unwrap_or_else(|_| DEFAULT_PARACHAIN_PALLET_ID.into()).replacen("0x", "", 1);
let s2 = env::var(AUTHORIZE_UPGRADE_PREFIX_ENV)
.unwrap_or_else(|_| DEFAULT_AUTHORIZE_UPGRADE_PREFIX.into())
.replacen("0x", "", 1);
let decoded1 = <[u8; 1]>::from_hex(s1).expect("Decoding failed");
let decoded2 = <[u8; 1]>::from_hex(s2).expect("Decoding failed");

Expand Down Expand Up @@ -111,30 +118,30 @@ mod prop_hash_tests {

#[test]
fn test_parachain_upgrade() {
env::set_var(PARACHAIN_PALLET_ID_ENV, "0x01");
env::set_var(AUTHORIZE_UPGRADE_PREFIX_ENV, "0x03");
env::set_var(PARACHAIN_PALLET_ID_ENV, DEFAULT_PARACHAIN_PALLET_ID);
env::set_var(AUTHORIZE_UPGRADE_PREFIX_ENV, DEFAULT_AUTHORIZE_UPGRADE_PREFIX);
assert_eq!(
get_parachainsystem_authorize_upgrade(&[
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x97, 0x03, 0x39, 0x60, 0x03, 0x7f, 0x7f
]),
[
136, 242, 183, 110, 31, 66, 126, 20, 192, 209, 151, 203, 156, 215, 131, 200, 97, 163, 230, 157, 86,
220, 102, 180, 58, 141, 176, 52, 178, 133, 149, 179
231, 116, 0, 171, 31, 105, 209, 55, 219, 85, 107, 244, 188, 127, 92, 82, 111, 152, 5, 80, 44, 48, 66,
9, 156, 175, 248, 163, 40, 92, 101, 11
]
);
}

#[test]
fn test_custom_parachain_upgrade() {
env::set_var(PARACHAIN_PALLET_ID_ENV, "0x32");
env::set_var(AUTHORIZE_UPGRADE_PREFIX_ENV, "0x03");
env::set_var(AUTHORIZE_UPGRADE_PREFIX_ENV, DEFAULT_AUTHORIZE_UPGRADE_PREFIX);
assert_eq!(
get_parachainsystem_authorize_upgrade(&[
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x97, 0x03, 0x39, 0x60, 0x03, 0x7f, 0x7f
]),
[
29, 53, 127, 234, 110, 75, 67, 238, 243, 171, 65, 93, 187, 246, 0, 84, 166, 88, 161, 205, 95, 62, 135,
99, 121, 139, 154, 39, 207, 121, 98, 87
51, 203, 30, 131, 48, 13, 150, 26, 217, 87, 213, 55, 43, 10, 200, 193, 248, 254, 202, 83, 165, 231, 4,
59, 213, 247, 98, 153, 119, 166, 175, 133
]
);
}
Expand Down

0 comments on commit cbf9282

Please sign in to comment.