diff --git a/crates/jstzd/src/config.rs b/crates/jstzd/src/config.rs index 1914dfdf..f66a0a12 100644 --- a/crates/jstzd/src/config.rs +++ b/crates/jstzd/src/config.rs @@ -77,7 +77,7 @@ pub(crate) async fn build_config( ) -> Result<(u16, JstzdConfig)> { let mut config = match config_path { Some(p) => parse_config(p).await?, - None => default_config(), + None => Config::default(), }; let octez_node_config = config.octez_node.build()?; let octez_client_config = match config.octez_client { @@ -130,15 +130,6 @@ pub(crate) async fn build_config( )) } -fn default_config() -> Config { - let mut config = Config::default(); - config.protocol.set_bootstrap_accounts([ - BootstrapAccount::new(ROLLUP_OPERATOR_PK, 60_000_000_000).unwrap(), - BootstrapAccount::new(ACTIVATOR_PK, 40_000_000_000).unwrap(), - ]); - config -} - fn populate_baker_config( mut config_builder: OctezBakerConfigBuilder, octez_node_config: &OctezNodeConfig, @@ -192,6 +183,20 @@ async fn build_protocol_params( contracts.push(contract); } + // Insert necessary bootstrap accounts. These accounts will be overwriten + // when bootstrap accounts in users' parameter file collide with these bootstrap accounts. + let mut accounts = builder + .bootstrap_accounts() + .iter() + .map(|v| (*v).to_owned()) + .collect::>(); + for account in [ + BootstrapAccount::new(ROLLUP_OPERATOR_PK, 60_000_000_000).unwrap(), + BootstrapAccount::new(ACTIVATOR_PK, 40_000_000_000).unwrap(), + ] { + accounts.push(account); + } + builder .set_bootstrap_smart_rollups([BootstrapSmartRollup::new( JSTZ_ROLLUP_ADDRESS, @@ -205,6 +210,7 @@ async fn build_protocol_params( ) .unwrap()]) .set_bootstrap_contracts(contracts) + .set_bootstrap_accounts(accounts) .build() } @@ -234,17 +240,21 @@ mod tests { use super::Config; - async fn read_bootstrap_contracts_from_param_file( - path: PathBuf, - ) -> Vec { + async fn read_param_file(path: &PathBuf) -> serde_json::Value { let mut buf = String::new(); - tokio::fs::File::open(&path) + tokio::fs::File::open(path) .await .unwrap() .read_to_string(&mut buf) .await .unwrap(); - let params_json = serde_json::from_str::(&buf).unwrap(); + serde_json::from_str::(&buf).unwrap() + } + + async fn read_bootstrap_contracts_from_param_file( + path: &PathBuf, + ) -> Vec { + let params_json = read_param_file(path).await; params_json .as_object() .unwrap() @@ -257,6 +267,22 @@ mod tests { .collect::>() } + async fn read_bootstrap_accounts_from_param_file( + path: &PathBuf, + ) -> Vec { + let params_json = read_param_file(path).await; + params_json + .as_object() + .unwrap() + .get("bootstrap_accounts") + .unwrap() + .as_array() + .unwrap() + .iter() + .map(|v| serde_json::from_value::(v.to_owned()).unwrap()) + .collect::>() + } + #[tokio::test] async fn parse_config() { let mut tmp_file = NamedTempFile::new().unwrap(); @@ -478,20 +504,6 @@ mod tests { ); } - #[test] - fn default_config() { - let config = super::default_config(); - let accounts = config.protocol.bootstrap_accounts(); - assert_eq!(accounts.len(), 2); - let expected_accounts = [ - BootstrapAccount::new(super::ROLLUP_OPERATOR_PK, 60_000_000_000).unwrap(), - BootstrapAccount::new(super::ACTIVATOR_PK, 40_000_000_000).unwrap(), - ]; - assert!(expected_accounts - .iter() - .all(|expected| { accounts.iter().any(|account| **account == *expected) })); - } - #[tokio::test] async fn build_config() { let mut tmp_file = NamedTempFile::new().unwrap(); @@ -519,16 +531,17 @@ mod tests { ); assert_eq!(port, super::DEFAULT_JSTZD_SERVER_PORT); - let contracts = read_bootstrap_contracts_from_param_file( - config - .protocol_params() - .parameter_file() - .path() - .to_path_buf(), - ) - .await; + let config_path = config + .protocol_params() + .parameter_file() + .path() + .to_path_buf(); + let contracts = read_bootstrap_contracts_from_param_file(&config_path).await; assert_eq!(contracts.len(), 2); + let accounts = read_bootstrap_accounts_from_param_file(&config_path).await; + assert_eq!(accounts.len(), 3); + assert_eq!( config.octez_rollup_config().address.to_base58_check(), JSTZ_ROLLUP_ADDRESS @@ -654,7 +667,7 @@ mod tests { .unwrap()]); let params = super::build_protocol_params(builder).await.unwrap(); let mut addresses = read_bootstrap_contracts_from_param_file( - params.parameter_file().path().to_path_buf(), + ¶ms.parameter_file().path().to_path_buf(), ) .await .iter() @@ -685,7 +698,7 @@ mod tests { .set_bootstrap_contracts([dummy_contract.clone()]); let params = super::build_protocol_params(builder).await.unwrap(); let mut contracts = read_bootstrap_contracts_from_param_file( - params.parameter_file().path().to_path_buf(), + ¶ms.parameter_file().path().to_path_buf(), ) .await; assert_eq!(contracts.len(), 2); diff --git a/crates/jstzd/tests/main_test.rs b/crates/jstzd/tests/main_test.rs index 3d285fca..52afec05 100644 --- a/crates/jstzd/tests/main_test.rs +++ b/crates/jstzd/tests/main_test.rs @@ -41,7 +41,9 @@ fn default_config() { fn valid_config_file() { let port = unused_port(); let mut tmp_file = NamedTempFile::new().unwrap(); - tmp_file.write_all(format!(r#"{{"protocol":{{"bootstrap_accounts":[["edpkuSLWfVU1Vq7Jg9FucPyKmma6otcMHac9zG4oU1KMHSTBpJuGQ2","15000000000"]]}},"server_port":{}}}"#, port).as_bytes()).unwrap(); + tmp_file + .write_all(format!(r#"{{"server_port":{}}}"#, port).as_bytes()) + .unwrap(); let handle = thread::spawn(move || { Command::cargo_bin("jstzd") @@ -82,14 +84,14 @@ fn valid_config_file() { fn bad_config_file() { let mut cmd = Command::cargo_bin("jstzd").unwrap(); let mut tmp_file = NamedTempFile::new().unwrap(); - tmp_file.write_all("{}".as_bytes()).unwrap(); + tmp_file + .write_all("{\"protocol\":{\"protocol\":\"foo\"}}".as_bytes()) + .unwrap(); cmd.args(["run", &tmp_file.path().to_string_lossy()]) .assert() .failure() - .stderr(predicate::str::contains( - "should have at least one bootstrap account with at least 6000 tez", - )); + .stderr(predicate::str::contains("failed to build config")); } #[test]