Skip to content

Commit 0831be5

Browse files
committed
Refactor parsing to function
1 parent 4631b61 commit 0831be5

1 file changed

Lines changed: 16 additions & 16 deletions

File tree

operator/mina/lib/src/pickles_preproc/state_proof.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,32 +145,32 @@ impl TryFrom<HexScalar> for WrapScalar {
145145
}
146146
}
147147

148-
pub fn parse(proof_json: &serde_json::Value) -> Result<StateProof, String> {
149-
serde_json::from_value(proof_json.to_owned())
150-
.map_err(|err| format!("Could not parse proof: {err}"))
148+
pub fn parse(mina_state_proof_vk_query_str: &str) -> Result<StateProof, String> {
149+
let mina_state_proof_vk_query: serde_json::Map<String, serde_json::Value> =
150+
serde_json::from_str(mina_state_proof_vk_query_str)
151+
.map_err(|err| format!("Could not parse mina state proof vk query: {err}"))?;
152+
let protocol_state_proof_json = mina_state_proof_vk_query
153+
.get("data")
154+
.and_then(|d| d.get("bestChain"))
155+
.and_then(|d| d.get(0))
156+
.and_then(|d| d.get("protocolStateProof"))
157+
.and_then(|d| d.get("json"))
158+
.ok_or("Could not parse protocol state proof: JSON structure upto protocolStateProof is unexpected")?;
159+
160+
serde_json::from_value(protocol_state_proof_json.to_owned())
161+
.map_err(|err| format!("Could not parse mina state proof: {err}"))
151162
}
152163

153164
#[cfg(test)]
154165
mod tests {
155-
use super::StateProof;
166+
use super::parse;
156167

157168
const MINA_STATE_PROOF_VK_QUERY: &str = include_str!(
158169
"../../../../../batcher/aligned/test_files/mina/mina_state_proof_vk_query.json"
159170
);
160171

161172
#[test]
162173
fn parse_protocol_state_proof() {
163-
let mina_state_proof_vk_query: serde_json::Map<String, serde_json::Value> =
164-
serde_json::from_str(MINA_STATE_PROOF_VK_QUERY).expect("Could not parse JSON query");
165-
let protocol_state_proof_json = mina_state_proof_vk_query
166-
.get("data")
167-
.and_then(|d| d.get("bestChain"))
168-
.and_then(|d| d.get(0))
169-
.and_then(|d| d.get("protocolStateProof"))
170-
.and_then(|d| d.get("json"))
171-
.expect("Could not parse protocol state proof: JSON structure upto protocolStateProof is unexpected");
172-
173-
let _state_proof: StateProof =
174-
serde_json::from_value(protocol_state_proof_json.to_owned()).unwrap();
174+
parse(MINA_STATE_PROOF_VK_QUERY).unwrap();
175175
}
176176
}

0 commit comments

Comments
 (0)