Skip to content

Commit

Permalink
auto sort commitments
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfirefist committed Apr 25, 2024
1 parent bd27b0c commit 7dc3aaa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
2 changes: 1 addition & 1 deletion apps/fortuna/src/command/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
for (chain_id, chain_config) in &config.chains {
let contract = Arc::new(PythContract::from_config(&chain_config)?);
let provider_chain_config = provider_config.get_chain_config(chain_id)?;
let provider_commitments = &provider_chain_config.commitments;
let provider_commitments = &provider_chain_config.get_sorted_commitments();
let provider_info = contract.get_provider_info(opts.provider).call().await?;
let latest_metadata =
bincode::deserialize::<CommitmentMetadata>(&provider_info.commitment_metadata)?;
Expand Down
28 changes: 8 additions & 20 deletions apps/fortuna/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,9 @@ impl ProviderConfig {
/// Gets the provider chain config from the Hashmap. It will check that the commitments are in order.
/// If not, it will return an Err. Else, the config will be returned.
pub fn get_chain_config(&self, chain_id: &ChainId) -> Result<ProviderChainConfig> {
let provider_chain_config = self.chains.get(chain_id).map(|x| x.clone()).ok_or(anyhow!(
self.chains.get(chain_id).map(|x| x.clone()).ok_or(anyhow!(
"Could not find chain id {} in the configuration",
&chain_id
))?;

if provider_chain_config.check() {
return Ok(provider_chain_config);
}

Err(anyhow!(
"{}: error loading provider config. The commitments are not in order",
&chain_id
))
}
}
Expand All @@ -209,16 +200,13 @@ pub struct ProviderChainConfig {
}

impl ProviderChainConfig {
pub fn check(&self) -> bool {
let commitments = &self.commitments;
for i in 0..commitments.len() - 1 {
if commitments[i].original_commitment_sequence_number
> commitments[i + 1].original_commitment_sequence_number
{
return false;
}
}
true
pub fn get_sorted_commitments(&self) -> Vec<Commitment> {
let mut sorted_commitments = self.commitments.clone();
sorted_commitments.sort_by(|c1, c2| {
c1.original_commitment_sequence_number
.cmp(&c2.original_commitment_sequence_number)
});
sorted_commitments
}
}

Expand Down

0 comments on commit 7dc3aaa

Please sign in to comment.