Skip to content

Commit ee2edf4

Browse files
apollo_proof_manager: calculate hash within the proof storage
1 parent 2b100d1 commit ee2edf4

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

crates/apollo_proof_manager/src/proof_manager.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use apollo_proof_manager_config::config::ProofManagerConfig;
2-
use starknet_api::transaction::fields::Proof;
3-
use starknet_types_core::felt::Felt;
2+
use starknet_api::transaction::fields::{Proof, ProofFacts};
43

54
use crate::proof_storage::{FsProofStorage, FsProofStorageError, ProofStorage};
65
/// Proof manager that wraps filesystem-based proof storage.
@@ -20,15 +19,15 @@ impl ProofManager {
2019
impl ProofStorage for ProofManager {
2120
type Error = FsProofStorageError;
2221

23-
fn set_proof(&self, facts_hash: Felt, proof: Proof) -> Result<(), Self::Error> {
24-
self.proof_storage.set_proof(facts_hash, proof)
22+
fn set_proof(&self, proof_facts: ProofFacts, proof: Proof) -> Result<(), Self::Error> {
23+
self.proof_storage.set_proof(proof_facts, proof)
2524
}
2625

27-
fn get_proof(&self, facts_hash: Felt) -> Result<Option<Proof>, Self::Error> {
28-
self.proof_storage.get_proof(facts_hash)
26+
fn get_proof(&self, proof_facts: ProofFacts) -> Result<Option<Proof>, Self::Error> {
27+
self.proof_storage.get_proof(proof_facts)
2928
}
3029

31-
fn contains_proof(&self, facts_hash: Felt) -> Result<bool, Self::Error> {
32-
self.proof_storage.contains_proof(facts_hash)
30+
fn contains_proof(&self, proof_facts: ProofFacts) -> Result<bool, Self::Error> {
31+
self.proof_storage.contains_proof(proof_facts)
3332
}
3433
}

crates/apollo_proof_manager/src/proof_storage.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::io::{BufWriter, Read, Write};
55
use std::path::{Path, PathBuf};
66
use std::sync::Arc;
77

8-
use starknet_api::transaction::fields::Proof;
8+
use starknet_api::transaction::fields::{Proof, ProofFacts};
99
use starknet_types_core::felt::Felt;
1010
use thiserror::Error;
1111

@@ -15,9 +15,9 @@ mod proof_storage_test;
1515

1616
pub trait ProofStorage: Send + Sync {
1717
type Error: Error;
18-
fn set_proof(&self, facts_hash: Felt, proof: Proof) -> Result<(), Self::Error>;
19-
fn get_proof(&self, facts_hash: Felt) -> Result<Option<Proof>, Self::Error>;
20-
fn contains_proof(&self, facts_hash: Felt) -> Result<bool, Self::Error>;
18+
fn set_proof(&self, proof_facts: ProofFacts, proof: Proof) -> Result<(), Self::Error>;
19+
fn get_proof(&self, proof_facts: ProofFacts) -> Result<Option<Proof>, Self::Error>;
20+
fn contains_proof(&self, proof_facts: ProofFacts) -> Result<bool, Self::Error>;
2121
}
2222

2323
#[derive(Debug, Error)]
@@ -150,20 +150,27 @@ impl FsProofStorage {
150150
std::fs::rename(tmp_dir, persistent_dir)?;
151151
Ok(())
152152
}
153+
154+
fn contains_proof_by_hash(&self, facts_hash: Felt) -> bool {
155+
self.get_persistent_dir(facts_hash).exists()
156+
}
153157
}
154158

155159
impl ProofStorage for FsProofStorage {
156160
type Error = FsProofStorageError;
157161

158-
fn set_proof(&self, facts_hash: Felt, proof: Proof) -> Result<(), Self::Error> {
159-
if self.contains_proof(facts_hash)? {
162+
fn set_proof(&self, proof_facts: ProofFacts, proof: Proof) -> Result<(), Self::Error> {
163+
let facts_hash = proof_facts.hash();
164+
165+
if self.contains_proof_by_hash(facts_hash) {
160166
return Ok(());
161167
}
162168
self.write_proof_atomically(facts_hash, proof)
163169
}
164170

165-
fn get_proof(&self, facts_hash: Felt) -> Result<Option<Proof>, Self::Error> {
166-
if !self.contains_proof(facts_hash)? {
171+
fn get_proof(&self, proof_facts: ProofFacts) -> Result<Option<Proof>, Self::Error> {
172+
let facts_hash = proof_facts.hash();
173+
if !self.contains_proof_by_hash(facts_hash) {
167174
return Ok(None);
168175
}
169176

@@ -176,7 +183,7 @@ impl ProofStorage for FsProofStorage {
176183
}
177184
}
178185

179-
fn contains_proof(&self, facts_hash: Felt) -> Result<bool, Self::Error> {
180-
Ok(self.get_persistent_dir(facts_hash).exists())
186+
fn contains_proof(&self, proof_facts: ProofFacts) -> Result<bool, Self::Error> {
187+
Ok(self.contains_proof_by_hash(proof_facts.hash()))
181188
}
182189
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::path::PathBuf;
22

3-
use starknet_api::transaction::fields::Proof;
3+
use starknet_api::transaction::fields::{Proof, ProofFacts};
44
use starknet_types_core::felt::Felt;
55

66
use crate::proof_storage::{FsProofStorage, ProofStorage};
@@ -11,8 +11,8 @@ fn new_fs_proof_storage() -> FsProofStorage {
1111
FsProofStorage::new(persistent_root).expect("Failed to create FsProofStorage")
1212
}
1313

14-
fn sample_facts_hash() -> Felt {
15-
Felt::from(0x1234_u64)
14+
fn sample_proof_facts() -> ProofFacts {
15+
ProofFacts::from(vec![Felt::from(0x1234_u64), Felt::from(0x5678_u64)])
1616
}
1717

1818
fn sample_proof() -> Proof {
@@ -22,21 +22,20 @@ fn sample_proof() -> Proof {
2222
#[test]
2323
fn fs_proof_storage_get_before_set_returns_none() {
2424
let storage = new_fs_proof_storage();
25-
let facts_hash = sample_facts_hash();
25+
let proof_facts = sample_proof_facts();
2626

27-
let res = storage.get_proof(facts_hash);
27+
let res = storage.get_proof(proof_facts);
2828
assert!(res.is_ok());
2929
assert!(res.unwrap().is_none());
3030
}
3131

3232
#[test]
3333
fn fs_proof_storage_roundtrip() {
3434
let storage = new_fs_proof_storage();
35-
let facts_hash = sample_facts_hash();
3635
let proof = sample_proof();
3736

38-
storage.set_proof(facts_hash, proof.clone()).unwrap();
37+
storage.set_proof(sample_proof_facts(), proof.clone()).unwrap();
3938

40-
let retrieved = storage.get_proof(sample_facts_hash()).unwrap();
39+
let retrieved = storage.get_proof(sample_proof_facts()).unwrap();
4140
assert_eq!(retrieved, Some(proof));
4241
}

0 commit comments

Comments
 (0)