Skip to content

Commit

Permalink
fix(jstzd): insert bootstrap accounts into config
Browse files Browse the repository at this point in the history
  • Loading branch information
huancheng-trili committed Dec 19, 2024
1 parent 3cf7e48 commit 92bfc65
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 44 deletions.
91 changes: 52 additions & 39 deletions crates/jstzd/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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::<Vec<BootstrapAccount>>();
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,
Expand All @@ -205,6 +210,7 @@ async fn build_protocol_params(
)
.unwrap()])
.set_bootstrap_contracts(contracts)
.set_bootstrap_accounts(accounts)
.build()
}

Expand Down Expand Up @@ -234,17 +240,21 @@ mod tests {

use super::Config;

async fn read_bootstrap_contracts_from_param_file(
path: PathBuf,
) -> Vec<BootstrapContract> {
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::<serde_json::Value>(&buf).unwrap();
serde_json::from_str::<serde_json::Value>(&buf).unwrap()
}

async fn read_bootstrap_contracts_from_param_file(
path: &PathBuf,
) -> Vec<BootstrapContract> {
let params_json = read_param_file(path).await;
params_json
.as_object()
.unwrap()
Expand All @@ -257,6 +267,22 @@ mod tests {
.collect::<Vec<BootstrapContract>>()
}

async fn read_bootstrap_accounts_from_param_file(
path: &PathBuf,
) -> Vec<BootstrapAccount> {
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::<BootstrapAccount>(v.to_owned()).unwrap())
.collect::<Vec<BootstrapAccount>>()
}

#[tokio::test]
async fn parse_config() {
let mut tmp_file = NamedTempFile::new().unwrap();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(),
&params.parameter_file().path().to_path_buf(),
)
.await
.iter()
Expand Down Expand Up @@ -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(),
&params.parameter_file().path().to_path_buf(),
)
.await;
assert_eq!(contracts.len(), 2);
Expand Down
12 changes: 7 additions & 5 deletions crates/jstzd/tests/main_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 92bfc65

Please sign in to comment.