Skip to content

Commit

Permalink
feat(octez): store bootstrap accounts in protocol parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
huancheng-trili committed Dec 31, 2024
1 parent c47fc90 commit 4208aed
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion crates/octez/src/async/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl BootstrapAccount {
}
}

#[derive(Default, Debug, PartialEq)]
#[derive(Default, Debug, PartialEq, Clone)]
pub struct BootstrapAccounts {
accounts: HashMap<String, BootstrapAccount>,
}
Expand Down
29 changes: 25 additions & 4 deletions crates/octez/src/async/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pub struct ProtocolParameterFile;
#[derive(Clone)]
pub struct ProtocolParameter {
protocol: Protocol,
bootstrap_accounts: BootstrapAccounts,
parameter_file: Arc<tempfile::NamedTempFile>,
}

Expand All @@ -119,6 +120,10 @@ impl ProtocolParameter {
pub fn parameter_file(&self) -> &tempfile::NamedTempFile {
&self.parameter_file
}

pub fn bootstrap_accounts(&self) -> Vec<&BootstrapAccount> {
self.bootstrap_accounts.accounts()
}
}

#[derive(Deserialize, Default, PartialEq, Debug)]
Expand Down Expand Up @@ -217,7 +222,7 @@ impl ProtocolParameterBuilder {
"Failed to convert loaded json file into a json object"
))?;

self.merge_bootstrap_accounts(json)?;
let merged_bootstrap_accounts = self.merge_bootstrap_accounts(json)?;
self.bootstrap_accounts = BootstrapAccounts::default();
self.merge_bootstrap_contracts(json)?;
self.bootstrap_contracts = BootstrapContracts::default();
Expand All @@ -231,6 +236,7 @@ impl ProtocolParameterBuilder {
Ok(ProtocolParameter {
protocol,
parameter_file: Arc::new(output_file),
bootstrap_accounts: merged_bootstrap_accounts,
})
}

Expand Down Expand Up @@ -276,7 +282,7 @@ impl ProtocolParameterBuilder {
fn merge_bootstrap_accounts(
&mut self,
json: &mut serde_json::Map<String, Value>,
) -> anyhow::Result<()> {
) -> anyhow::Result<BootstrapAccounts> {
let mut accounts = BootstrapAccounts::default();
if let Some(value) = json.get("bootstrap_accounts") {
let existing_accounts = serde_json::from_value(value.clone())?;
Expand All @@ -303,9 +309,9 @@ impl ProtocolParameterBuilder {

json.insert(
"bootstrap_accounts".to_owned(),
serde_json::to_value(accounts)?,
serde_json::to_value(&accounts)?,
);
Ok(())
Ok(accounts)
}

fn merge_bootstrap_contracts(
Expand Down Expand Up @@ -794,4 +800,19 @@ mod tests {
.to_string()
.contains("unknown protocol 'foobar'"));
}

#[test]
fn bootstrap_accounts_in_parameters() {
let account = BootstrapAccount::new(
ACCOUNT_PUBLIC_KEY,
ProtocolParameterBuilder::MIN_BOOTSTRAP_ACCOUNT_BALANCE_MUTEZ,
)
.unwrap();
let params = ProtocolParameterBuilder::new()
.set_bootstrap_accounts([account.clone()])
.build()
.unwrap();
let accounts = params.bootstrap_accounts();
assert_eq!(accounts, vec![&account]);
}
}

0 comments on commit 4208aed

Please sign in to comment.