|
| 1 | +// Copyright (C) 2019-2025 Provable Inc. |
| 2 | +// This file is part of the Provable SDK library. |
| 3 | + |
| 4 | +// The Provable SDK library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// The Provable SDK library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with the Provable SDK library. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +use crate::types::native::CurrentNetwork; |
| 18 | +use snarkvm_console::{network::Network, program::StatePath, types::Field}; |
| 19 | +use snarkvm_ledger_query::QueryTrait; |
| 20 | + |
| 21 | +use anyhow::anyhow; |
| 22 | +use async_trait::async_trait; |
| 23 | +use indexmap::IndexMap; |
| 24 | +use serde::{Deserialize, Serialize}; |
| 25 | +use wasm_bindgen::prelude::wasm_bindgen; |
| 26 | + |
| 27 | +use std::str::FromStr; |
| 28 | + |
| 29 | +/// A snapshot-based query object used to pin the block height, state root, |
| 30 | +/// and state paths to a single ledger view during online execution. |
| 31 | +#[wasm_bindgen] |
| 32 | +#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] |
| 33 | +pub struct SnapshotQuery { |
| 34 | + block_height: u32, |
| 35 | + state_paths: IndexMap<Field<CurrentNetwork>, StatePath<CurrentNetwork>>, |
| 36 | + state_root: <CurrentNetwork as Network>::StateRoot, |
| 37 | +} |
| 38 | + |
| 39 | +#[wasm_bindgen] |
| 40 | +impl SnapshotQuery { |
| 41 | + #[wasm_bindgen(constructor)] |
| 42 | + pub fn new(block_height: u32, state_root: &str) -> Result<SnapshotQuery, String> { |
| 43 | + let state_root = <CurrentNetwork as Network>::StateRoot::from_str(state_root) |
| 44 | + .map_err(|e| e.to_string())?; |
| 45 | + Ok(Self { |
| 46 | + block_height, |
| 47 | + state_paths: IndexMap::new(), |
| 48 | + state_root, |
| 49 | + }) |
| 50 | + } |
| 51 | + |
| 52 | + #[wasm_bindgen(js_name = "addBlockHeight")] |
| 53 | + pub fn add_block_height(&mut self, block_height: u32) { |
| 54 | + self.block_height = block_height; |
| 55 | + } |
| 56 | + |
| 57 | + #[wasm_bindgen(js_name = "addStatePath")] |
| 58 | + pub fn add_state_path(&mut self, commitment: &str, state_path: &str) -> Result<(), String> { |
| 59 | + let commitment = Field::from_str(commitment).map_err(|e| e.to_string())?; |
| 60 | + let state_path = StatePath::from_str(state_path).map_err(|e| e.to_string())?; |
| 61 | + self.state_paths.insert(commitment, state_path); |
| 62 | + Ok(()) |
| 63 | + } |
| 64 | + |
| 65 | + #[wasm_bindgen(js_name = "toString")] |
| 66 | + #[allow(clippy::inherent_to_string)] |
| 67 | + pub fn to_string(&self) -> String { |
| 68 | + serde_json::to_string(&self).unwrap() |
| 69 | + } |
| 70 | + |
| 71 | + #[wasm_bindgen(js_name = "fromString")] |
| 72 | + pub fn from_string(s: &str) -> Result<SnapshotQuery, String> { |
| 73 | + serde_json::from_str(s).map_err(|e| e.to_string()) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +#[async_trait(?Send)] |
| 78 | +impl QueryTrait<CurrentNetwork> for SnapshotQuery { |
| 79 | + fn current_state_root(&self) -> anyhow::Result<<CurrentNetwork as Network>::StateRoot> { |
| 80 | + Ok(self.state_root) |
| 81 | + } |
| 82 | + |
| 83 | + async fn current_state_root_async(&self) -> anyhow::Result<<CurrentNetwork as Network>::StateRoot> { |
| 84 | + Ok(self.state_root) |
| 85 | + } |
| 86 | + |
| 87 | + fn get_state_path_for_commitment( |
| 88 | + &self, |
| 89 | + commitment: &Field<CurrentNetwork>, |
| 90 | + ) -> anyhow::Result<StatePath<CurrentNetwork>> { |
| 91 | + self.state_paths |
| 92 | + .get(commitment) |
| 93 | + .cloned() |
| 94 | + .ok_or(anyhow!("State path not found for commitment")) |
| 95 | + } |
| 96 | + |
| 97 | + async fn get_state_path_for_commitment_async( |
| 98 | + &self, |
| 99 | + commitment: &Field<CurrentNetwork>, |
| 100 | + ) -> anyhow::Result<StatePath<CurrentNetwork>> { |
| 101 | + self.state_paths |
| 102 | + .get(commitment) |
| 103 | + .cloned() |
| 104 | + .ok_or(anyhow!("State path not found for commitment")) |
| 105 | + } |
| 106 | + |
| 107 | + fn current_block_height(&self) -> anyhow::Result<u32> { |
| 108 | + Ok(self.block_height) |
| 109 | + } |
| 110 | + |
| 111 | + async fn current_block_height_async(&self) -> anyhow::Result<u32> { |
| 112 | + Ok(self.block_height) |
| 113 | + } |
| 114 | +} |
0 commit comments