Skip to content

Commit

Permalink
Allow user overriding the Parachain Pallet Id (#46)
Browse files Browse the repository at this point in the history
* Allow user overriding the Parachain Pallet Id

fix #45

* Allow customizing the `authorize_upgrade_prefix`
  • Loading branch information
chevdor authored Jun 21, 2022
1 parent c2e5b62 commit ce5b695
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 12 deletions.
6 changes: 5 additions & 1 deletion .env-sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# This is a sample env. Not needed if using defaults
# This is a sample .env file. It is not needed if you
# are using defaults if you want to use the default defined
# below.

# POLKADOT_HTTP=http://localhost:9933
# POLKADOT_WS=ws://localhost:9944
# PARACHAIN_PALLET_ID=0x01
# AUTHORIZE_UPGRADE_PREFIX=0x03
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ MacOS Homebrew users can use:

### Command: --help

subwasm 0.17.0
subwasm 0.17.1
chevdor <[email protected]>:Wilfried Kopp <[email protected]
`subwasm` allows fetching, parsing and calling some methods on WASM runtimes of Substrate based
chains
Expand Down Expand Up @@ -92,7 +92,7 @@ MacOS Homebrew users can use:

### Command: get

subwasm-get 0.17.0
subwasm-get 0.17.1
chevdor <[email protected]>:Wilfried Kopp <[email protected]
Get/Download the runtime wasm from a running node through rpc

Expand Down Expand Up @@ -122,7 +122,7 @@ MacOS Homebrew users can use:

### Command: info

subwasm-info 0.17.0
subwasm-info 0.17.1
chevdor <[email protected]>:Wilfried Kopp <[email protected]
The `info` command returns summarized information about a runtime

Expand All @@ -145,9 +145,12 @@ MacOS Homebrew users can use:
-j, --json Output as json
-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`.
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: meta

subwasm-metadata 0.17.0
subwasm-metadata 0.17.1
chevdor <[email protected]>:Wilfried Kopp <[email protected]
Returns the metadata as a json object. You may also use the "meta" alias

Expand Down Expand Up @@ -176,7 +179,7 @@ MacOS Homebrew users can use:

### Command: diff

subwasm-diff 0.17.0
subwasm-diff 0.17.1
chevdor <[email protected]>:Wilfried Kopp <[email protected]
Compare 2 runtimes

Expand All @@ -200,7 +203,7 @@ MacOS Homebrew users can use:

### Command: compress

subwasm-compress 0.17.0
subwasm-compress 0.17.1
chevdor <[email protected]>:Wilfried Kopp <[email protected]
Compress a given runtime wasm file. You will get an error if you try compressing a runtime that is
already compressed
Expand All @@ -219,7 +222,7 @@ MacOS Homebrew users can use:

### Command: decompress

subwasm-decompress 0.17.0
subwasm-decompress 0.17.1
chevdor <[email protected]>:Wilfried Kopp <[email protected]
Decompress a given runtime wasm file. You may pass a runtime that is uncompressed already. In that
case, you will get the same content as output. This is useful if you want to decompress "no matter
Expand All @@ -237,6 +240,19 @@ MacOS Homebrew users can use:
-j, --json Output as json
-V, --version Print version information

### Environment variables

In addition to the command line flags, you can also pass one of the following ENV variables:

# This is a sample .env file. It is not needed if you
# are using defaults if you want to use the default defined
# below.

# POLKADOT_HTTP=http://localhost:9933
# POLKADOT_WS=ws://localhost:9944
# PARACHAIN_PALLET_ID=0x01
# AUTHORIZE_UPGRADE_PREFIX=0x03

## Sample runs

**Fetch a runtime from a running node**
Expand Down
11 changes: 11 additions & 0 deletions README_src.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ 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`.
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: meta
----
include::doc/usage_meta.adoc[]
Expand All @@ -105,6 +108,14 @@ include::doc/usage_compress.adoc[]
include::doc/usage_decompress.adoc[]
----

=== Environment variables

In addition to the command line flags, you can also pass one of the following ENV variables:

----
include::.env-sample[]
----

== Sample runs

.Fetch a runtime from a running node
Expand Down
2 changes: 1 addition & 1 deletion libs/substrate-runtime-proposal-hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ version = "0.17.0"
blake2 = "0.10"
codec = {version = "2.3", package = "parity-scale-codec"}
frame-metadata = {version = "14", package = "frame-metadata", features = ["v12", "v13", "v14", "std"]}
hex = "0.4"
hex = "0.4.3"
sc-executor = "0.9"
sp-core = "3.0"
sp-io = "3.0"
Expand Down
35 changes: 32 additions & 3 deletions libs/substrate-runtime-proposal-hash/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use blake2::digest::{Update, VariableOutput};
use blake2::Blake2bVar;
use codec::Encode;
use hex::FromHex;
use sp_core::Hasher;
use sp_runtime::traits::BlakeTwo256;
use std::env;

/// Expected size of the hash
pub const SIZE: usize = 32;
Expand All @@ -14,7 +16,8 @@ type Prefix = (u8, u8);

/// The PREFIX is prepended to the data before hashing
pub const PREFIX_SYSTEM_SETCODE: Prefix = (0x00, 0x03);
pub const PREFIX_PARACHAINSYSTEM_AUTHORIZE_UPGRADE: Prefix = (0x01, 0x03);
const PARACHAIN_PALLET_ID_ENV: &str = "PARACHAIN_PALLET_ID";
const AUTHORIZE_UPGRADE_PREFIX_ENV: &str = "AUTHORIZE_UPGRADE_PREFIX";

/// This struct is a container for whatever we calculated.
#[derive(Debug)]
Expand Down Expand Up @@ -59,8 +62,16 @@ 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 decoded1 = <[u8; 1]>::from_hex(s1).expect("Decoding failed");
let decoded2 = <[u8; 1]>::from_hex(s2).expect("Decoding failed");

let parachain_pallet_id = *decoded1.first().expect("Failure while fecthing the Parachain Pallet ID");
let authorize_upgrade_prefix = *decoded2.first().expect("Failure while fecthing the Auhtorize upgrade ID");
let prefix_parachainsystem_authorize_upgrade: Prefix = (parachain_pallet_id, authorize_upgrade_prefix);
let code_hash = BlakeTwo256::hash(wasm_blob);
get_call_hash(PREFIX_PARACHAINSYSTEM_AUTHORIZE_UPGRADE, code_hash.as_bytes())
get_call_hash(prefix_parachainsystem_authorize_upgrade, code_hash.as_bytes())
}

fn get_call_hash(prefix: Prefix, wasm_blob: &[u8]) -> CalllHash {
Expand All @@ -72,8 +83,9 @@ fn get_call_hash(prefix: Prefix, wasm_blob: &[u8]) -> CalllHash {
}

#[cfg(test)]
mod tests {
mod prop_hash_tests {
use super::*;
use std::env;

#[test]
fn test_proposal_hash() {
Expand All @@ -99,6 +111,8 @@ mod tests {

#[test]
fn test_parachain_upgrade() {
env::set_var(PARACHAIN_PALLET_ID_ENV, "0x01");
env::set_var(AUTHORIZE_UPGRADE_PREFIX_ENV, "0x03");
assert_eq!(
get_parachainsystem_authorize_upgrade(&[
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x97, 0x03, 0x39, 0x60, 0x03, 0x7f, 0x7f
Expand All @@ -110,6 +124,21 @@ mod tests {
);
}

#[test]
fn test_custom_parachain_upgrade() {
env::set_var(PARACHAIN_PALLET_ID_ENV, "0x32");
env::set_var(AUTHORIZE_UPGRADE_PREFIX_ENV, "0x03");
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
]
);
}

#[test]
fn test_hash_length() {
assert_eq!(32, get_proposal_hash(&[0]).len());
Expand Down

0 comments on commit ce5b695

Please sign in to comment.