Skip to content

Commit

Permalink
wip: added cli download parameters, added startup chekc
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraccaman committed Jul 19, 2023
1 parent 9e2279d commit 27c19fa
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 33 deletions.
5 changes: 4 additions & 1 deletion apps/src/bin/namada-client/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,14 @@ pub async fn main() -> Result<()> {
utils::init_genesis_validator(global_args, args)
}
Utils::PkToTmAddress(PkToTmAddress(args)) => {
utils::download_masp_parameters(global_args, args).await
utils::pk_to_tm_address(global_args, args)
}
Utils::DefaultBaseDir(DefaultBaseDir(args)) => {
utils::default_base_dir(global_args, args)
}
Utils::FetchMaspParameters(FetchMaspParameters(args)) => {
utils::fetch_masp_parameters(global_args, args).await;
}
},
}
Ok(())
Expand Down
41 changes: 41 additions & 0 deletions apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,7 @@ pub mod cmds {
InitNetwork(InitNetwork),
InitGenesisValidator(InitGenesisValidator),
PkToTmAddress(PkToTmAddress),
FetchMaspParameters(FetchMaspParameters),
DefaultBaseDir(DefaultBaseDir),
}

Expand Down Expand Up @@ -2172,6 +2173,25 @@ pub mod cmds {
}
}

#[derive(Clone, Debug)]
pub struct FetchMaspParameters(pub args::FetchMaspParameters);

impl SubCmd for FetchMaspParameters {
const CMD: &'static str = "fetch-masp-parameters";

fn parse(matches: &ArgMatches) -> Option<Self> {
matches
.subcommand_matches(Self::CMD)
.map(|matches| Self(args::FetchMaspParameters::parse(matches)))
}

fn def() -> App {
App::new(Self::CMD)
.about("Fetch MASP parameters.")
.add_args::<args::FetchMaspParameters>()
}
}

#[derive(Clone, Debug)]
pub struct DefaultBaseDir(pub args::DefaultBaseDir);

Expand Down Expand Up @@ -4839,6 +4859,27 @@ pub mod args {
}
}

#[derive(Clone, Debug)]
pub struct FetchMaspParameters {
pub output_folder: Option<PathBuf>,
}

impl Args for FetchMaspParameters {
fn parse(matches: &ArgMatches) -> Self {
let output_folder: Option<PathBuf> =
OUT_FILE_PATH_OPT.parse(matches);
Self { output_folder }
}

fn def(app: App) -> App {
app.arg(
OUT_FILE_PATH_OPT
.def()
.help("The folder where the files will be downloaded."),
)
}
}

#[derive(Clone, Debug)]
pub struct DefaultBaseDir {}

Expand Down
49 changes: 36 additions & 13 deletions apps/src/lib/client/tx.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::collections::{HashSet, HashMap};
use std::{env, io};
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::path::PathBuf;
use std::{env, io};

use async_trait::async_trait;
use borsh::{BorshDeserialize, BorshSerialize};
Expand All @@ -26,7 +26,7 @@ use namada::types::storage::{Epoch, Key};
use namada::types::token;
use namada::types::transaction::governance::{ProposalType, VoteProposalData};
use namada::types::transaction::{InitValidator, TxType};
use sha2::{Sha256, Digest};
use sha2::{Digest, Sha256};

use super::rpc;
use crate::cli::context::WalletAddress;
Expand Down Expand Up @@ -429,40 +429,63 @@ pub struct CLIShieldedUtils {
impl CLIShieldedUtils {
/// Initialize a shielded transaction context that identifies notes
/// decryptable by any viewing key in the given set
pub fn new(context_dir: PathBuf, masp_parameters: HashMap<String, String>) -> masp::ShieldedContext<Self> {
pub fn new(
context_dir: PathBuf,
masp_parameters: HashMap<String, String>,
) -> masp::ShieldedContext<Self> {
// Make sure that MASP parameters are downloaded to enable MASP
// transaction building and verification later on

let params_dir = masp::get_params_dir();
let spend_path = params_dir.join(masp::SPEND_NAME);
let convert_path = params_dir.join(masp::CONVERT_NAME);
let output_path = params_dir.join(masp::OUTPUT_NAME);

let spend_hash = masp_parameters.get("spend").expect("Spend parameter hash should be present.");
let output_hash = masp_parameters.get("output").expect("output parameter hash should be present.");
let convert_hash = masp_parameters.get("convert").expect("convert parameter hash should be present.");
let spend_hash = masp_parameters
.get("spend")
.expect("Spend parameter hash should be present.");
let output_hash = masp_parameters
.get("output")
.expect("output parameter hash should be present.");
let convert_hash = masp_parameters
.get("convert")
.expect("convert parameter hash should be present.");

let parameters_check = vec![
(spend_path, spend_hash),
(convert_path, convert_hash),
(output_path, output_hash),
];

tracing::info!("Checking MASP parameter integrity at {}.", params_dir.display());
tracing::info!(
"Checking MASP parameter integrity at {}.",
params_dir.display()
);

for (path, genesis_hash) in parameters_check {
if path.exists() {
let mut hasher = Sha256::new();
let mut file = File::open(&path).expect("File should be redable.");
io::copy(&mut file, &mut hasher).expect("File should be redable.");
let mut file =
File::open(&path).expect("File should be redable.");
io::copy(&mut file, &mut hasher)
.expect("File should be redable.");
let hash_bytes = hasher.finalize();
let hash = format!("{:X}", hash_bytes);
if !hash.eq_ignore_ascii_case(genesis_hash) {
tracing::error!("MASP parameter check failed for {}. Expect hash is {} but got {}", path.display() , genesis_hash, hash);
tracing::error!(
"MASP parameter check failed for {}. Expect hash is \
{} but got {}",
path.display(),
genesis_hash,
hash
);
safe_exit(1)
}
} else {
tracing::error!("MASP parameter wasn't found at path {}", path.display());
tracing::error!(
"MASP parameter wasn't found at path {}",
path.display()
);
safe_exit(1)
}
}
Expand Down
12 changes: 6 additions & 6 deletions apps/src/lib/client/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use borsh::BorshSerialize;
use flate2::read::GzDecoder;
use flate2::write::GzEncoder;
use flate2::Compression;
use futures::StreamExt;
use namada::ledger::masp;
use namada::ledger::wallet::Wallet;
use namada::types::address;
Expand All @@ -22,7 +23,6 @@ use rand::thread_rng;
use serde_json::json;
use sha2::{Digest, Sha256};
use tokio::io::AsyncWriteExt;
use futures::StreamExt;

use crate::cli::context::ENV_VAR_WASM_DIR;
use crate::cli::{self, args, safe_exit};
Expand Down Expand Up @@ -993,21 +993,21 @@ pub fn pk_to_tm_address(
println!("{tm_addr}");
}

pub async fn download_masp_parameters(
pub async fn fetch_masp_parameters(
_global_args: args::Global,
args::PkToTmAddress { public_key }: args::PkToTmAddress,
args::FetchMaspParameters { output_folder }: args::FetchMaspParameters,
) {
let params_dir: PathBuf = masp::get_params_dir();
let params_dir = output_folder.unwrap_or_else(masp::get_params_dir);
let parameters = vec![
"masp-convert.params",
"masp-output.params",
"masp-spend.params"
"masp-spend.params",
];

for parameter in parameters {
println!("Downloading {}...", parameter);
let url = format!("{}{}", masp::DOWNLOAD_URL, parameter);
let path = params_dir.join(&parameter);
let path = params_dir.join(parameter);
download_file_streaming(&url, path.to_string_lossy().as_ref()).await;
}
println!("Done!");
Expand Down
14 changes: 7 additions & 7 deletions apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub mod genesis_config {
// Wasm definitions
pub wasm: HashMap<String, WasmConfig>,
// Masp parameters
pub masp: MaspParameters
pub masp: MaspParameters,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -317,7 +317,7 @@ pub mod genesis_config {
pub struct MaspParameters {
convert: String,
spend: String,
output: String
output: String,
}

fn load_validator(
Expand Down Expand Up @@ -552,7 +552,7 @@ pub mod genesis_config {
gov_params,
wasm,
ethereum_bridge_params,
masp
masp,
} = config;

let native_token = Address::decode(
Expand Down Expand Up @@ -612,7 +612,7 @@ pub mod genesis_config {
let masp_parameters = HashMap::from_iter([
("convert".to_string(), masp.convert),
("spend".to_string(), masp.spend),
("output".to_string(), masp.output)
("output".to_string(), masp.output),
]);

let min_duration: i64 =
Expand Down Expand Up @@ -705,7 +705,7 @@ pub mod genesis_config {
pos_params,
gov_params,
ethereum_bridge_params,
masp: masp_parameters
masp: masp_parameters,
};
genesis.init();
genesis
Expand Down Expand Up @@ -760,7 +760,7 @@ pub struct Genesis {
pub gov_params: GovParams,
// Ethereum bridge config
pub ethereum_bridge_params: Option<EthereumBridgeConfig>,
pub masp: HashMap<String, String>
pub masp: HashMap<String, String>,
}

impl Genesis {
Expand Down Expand Up @@ -1135,7 +1135,7 @@ pub fn genesis(num_validators: u64) -> Genesis {
faucet_pow_difficulty: None,
#[cfg(not(feature = "mainnet"))]
faucet_withdrawal_limit: None,
masp: HashMap::new()
masp: HashMap::new(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion genesis/dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ max_proposal_content_size = 10000
# minimum epochs between end and grace epoch
min_proposal_grace_epochs = 6

# Computable via shasum -a 256 <file>
# Computable via shasum -a 256 <file> (https://github.com/anoma/masp-mpc/releases)
[masp]
spend = "62b3c60ca54bd99eb390198e949660624612f7db7942db84595fa9f1b4a29fd8"
output = "ed8b5d354017d808cfaf7b31eca5c511936e65ef6d276770251f5234ec5328b8"
Expand Down
2 changes: 1 addition & 1 deletion genesis/e2e-tests-single-node.toml
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ max_proposal_content_size = 10000
# minimum epochs between end and grace epoch
min_proposal_grace_epochs = 6

# Computable via shasum -a 256 <file>
# Computable via shasum -a 256 <file> (https://github.com/anoma/masp-mpc/releases)
[masp]
spend = "62b3c60ca54bd99eb390198e949660624612f7db7942db84595fa9f1b4a29fd8"
output = "ed8b5d354017d808cfaf7b31eca5c511936e65ef6d276770251f5234ec5328b8"
Expand Down
3 changes: 2 additions & 1 deletion shared/src/ledger/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ pub const OUTPUT_NAME: &str = "masp-output.params";
/// Convert circuit name
pub const CONVERT_NAME: &str = "masp-convert.params";
/// Default donwload url
pub const DOWNLOAD_URL: &str = "https://github.com/anoma/masp-mpc/releases/download/namada-trusted-setup/";
pub const DOWNLOAD_URL: &str =
"https://github.com/anoma/masp-mpc/releases/download/namada-trusted-setup/";

fn load_pvks() -> (
PreparedVerifyingKey<Bls12>,
Expand Down
6 changes: 3 additions & 3 deletions tests/src/e2e/ledger_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ fn ledger_txs_and_queries() -> Result<()> {
#[test]
fn masp_txs_and_queries() -> Result<()> {
// Download the shielded pool parameters before starting node
let _ = CLIShieldedUtils::new(PathBuf::new());
let _ = CLIShieldedUtils::default();
// Lengthen epoch to ensure that a transaction can be constructed and
// submitted within the same block. Necessary to ensure that conversion is
// not invalidated.
Expand Down Expand Up @@ -929,7 +929,7 @@ fn masp_txs_and_queries() -> Result<()> {
#[test]
fn masp_pinned_txs() -> Result<()> {
// Download the shielded pool parameters before starting node
let _ = CLIShieldedUtils::new(PathBuf::new());
let _ = CLIShieldedUtils::default();
// Lengthen epoch to ensure that a transaction can be constructed and
// submitted within the same block. Necessary to ensure that conversion is
// not invalidated.
Expand Down Expand Up @@ -1106,7 +1106,7 @@ fn masp_incentives() -> Result<()> {
// The number of decimal places used by ETH amounts.
const ETH_DENOMINATION: u8 = 18;
// Download the shielded pool parameters before starting node
let _ = CLIShieldedUtils::new(PathBuf::new());
let _ = CLIShieldedUtils::default();
// Lengthen epoch to ensure that a transaction can be constructed and
// submitted within the same block. Necessary to ensure that conversion is
// not invalidated.
Expand Down

0 comments on commit 27c19fa

Please sign in to comment.