Skip to content

Commit

Permalink
Allow user overriding the Parachain Pallet Id
Browse files Browse the repository at this point in the history
fix #45
  • Loading branch information
chevdor committed May 30, 2022
1 parent c2e5b62 commit 84f3260
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
16 changes: 9 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,11 @@ 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`. This default behavior can be overriden by setting the `PARACHAIN_PALLET_ID_ENV` to the ID of your parachain pallet.

### 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 +178,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 +202,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 +221,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 Down
2 changes: 2 additions & 0 deletions README_src.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ include::doc/usage_get.adoc[]
include::doc/usage_info.adoc[]
----

NOTE: By default, the ID for the Parachain pallet is expected to be `0x01`. This default behavior can be overriden by setting the `PARACHAIN_PALLET_ID_ENV` to the ID of your parachain pallet.

=== Command: meta
----
include::doc/usage_meta.adoc[]
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
28 changes: 25 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,7 @@ 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";

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

pub fn get_parachainsystem_authorize_upgrade(wasm_blob: &[u8]) -> CalllHash {
let s = env::var(PARACHAIN_PALLET_ID_ENV).unwrap_or_else(|_| String::from("0x01")).replace("0x", "");
let decoded = <[u8; 1]>::from_hex(s).expect("Decoding failed");
let parachain_pallet_id = *decoded.first().expect("Failure while fecthing the Parachain Pallet ID");
let prefix_parachainsystem_authorize_upgrade: Prefix = (parachain_pallet_id, 0x03);
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 +78,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 +106,7 @@ mod tests {

#[test]
fn test_parachain_upgrade() {
env::set_var(PARACHAIN_PALLET_ID_ENV, "0x01");
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 +118,20 @@ mod tests {
);
}

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

Please sign in to comment.