diff --git a/Cargo.lock b/Cargo.lock index 03e3e81864fb6d7b9a8d6baf4838ed1897a214ef..c2ab5ccc8dcafac419901ebdcfd919b210b325cd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1268,6 +1268,7 @@ dependencies = [ "sp-consensus-babe", "sp-core", "sp-finality-grandpa", + "sp-io", "sp-keyring", "sp-keystore", "sp-membership", diff --git a/Cargo.toml b/Cargo.toml index 6d9f96550140d542435b5c88b5b541c22c3c77fa..de1092a02b882c41581f5c72d6fecb7641b41b83 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,6 +84,7 @@ sp-consensus = { git = "https://github.com/librelois/substrate.git", branch = "d sp-consensus-babe = { git = "https://github.com/librelois/substrate.git", branch = "duniter-monthly-2022-01" } sp-core = { git = "https://github.com/librelois/substrate.git", branch = "duniter-monthly-2022-01" } sp-finality-grandpa = { git = "https://github.com/librelois/substrate.git", branch = "duniter-monthly-2022-01" } +sp-io = { git = "https://github.com/librelois/substrate.git", branch = "duniter-monthly-2022-01" } sp-offchain = { git = "https://github.com/librelois/substrate.git", branch = "duniter-monthly-2022-01" } sp-keyring = { git = "https://github.com/librelois/substrate.git", branch = "duniter-monthly-2022-01" } sp-keystore = { git = "https://github.com/librelois/substrate.git", branch = "duniter-monthly-2022-01" } diff --git a/node/src/cli.rs b/node/src/cli.rs index ed59dbc65c907ee79d6ad400f467e8fb0031ed74..a77a01830c29c4108dd82b43d921abc4076ec879 100644 --- a/node/src/cli.rs +++ b/node/src/cli.rs @@ -15,6 +15,7 @@ // along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>. pub mod key; +pub mod utils; use sc_cli::RunCmd; use std::str::FromStr; @@ -60,6 +61,9 @@ pub enum Subcommand { /// Revert the chain to a previous state. Revert(sc_cli::RevertCmd), + /// Some tools for developers and advanced testers + Utils(utils::UtilsSubCommand), + /// The custom benchmark subcommmand benchmarking runtime pallets. #[structopt(name = "benchmark", about = "Benchmark runtime pallets.")] Benchmark(frame_benchmarking_cli::BenchmarkCmd), diff --git a/node/src/cli/utils.rs b/node/src/cli/utils.rs new file mode 100644 index 0000000000000000000000000000000000000000..cd872564f807f6b9139663f9adf847141524150d --- /dev/null +++ b/node/src/cli/utils.rs @@ -0,0 +1,68 @@ +// Copyright 2021 Axiom-Team +// +// This file is part of Substrate-Libre-Currency. +// +// Substrate-Libre-Currency is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, version 3 of the License. +// +// Substrate-Libre-Currency is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with Substrate-Libre-Currency. If not, see <https://www.gnu.org/licenses/>. + +use sc_cli::{Error, SubstrateCli}; +use sp_io::hashing::twox_128; +use structopt::StructOpt; + +#[derive(Debug, StructOpt)] +pub enum UtilsSubCommand { + /// Compute the raw storage key prefix + StorageKeyPrefix(StorageKeyPrefixCmd), +} + +impl UtilsSubCommand { + /// Run the command + pub fn run<C: SubstrateCli>(&self, cli: &C) -> Result<(), Error> { + match self { + Self::StorageKeyPrefix(cmd) => cmd.run(cli), + } + } +} + +#[derive(Debug, StructOpt)] +pub struct StorageKeyPrefixCmd { + /// Pallet name + #[structopt(short = "p", long)] + pallet_name: Option<String>, + /// Storage item name + #[structopt(short = "i", long)] + item_name: Option<String>, +} + +impl StorageKeyPrefixCmd { + /// Run the command + pub fn run<C: SubstrateCli>(&self, _cli: &C) -> Result<(), Error> { + let mut key_prefix = Vec::new(); + let mut print_key_prefix = false; + if let Some(ref pallet_name) = self.pallet_name { + print_key_prefix = true; + let pallet_prefix = twox_128(pallet_name.as_bytes()); + println!("Pallet prefix: 0x{}", hex::encode(&pallet_prefix)); + key_prefix.extend_from_slice(&pallet_prefix[..]); + } + if let Some(ref item_name) = self.item_name { + let item_prefix = twox_128(item_name.as_bytes()); + println!("Item prefix: 0x{}", hex::encode(item_prefix)); + key_prefix.extend_from_slice(&item_prefix[..]); + } + if print_key_prefix { + println!("Key prefix: 0x{}", hex::encode(key_prefix)); + } + + Ok(()) + } +} diff --git a/node/src/command.rs b/node/src/command.rs index 4a671429fe80eb882ecc54bfb6ec30674c1a3825..3d16d9cab3a38c228433f9ce47281d7b8e944421 100644 --- a/node/src/command.rs +++ b/node/src/command.rs @@ -147,6 +147,7 @@ pub fn run() -> sc_cli::Result<()> { match &cli.subcommand { Some(Subcommand::Key(cmd)) => cmd.run(&cli), + Some(Subcommand::Utils(cmd)) => cmd.run(&cli), Some(Subcommand::BuildSpec(cmd)) => { let runner = cli.create_runner(cmd)?; runner.sync_run(|config| {