Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3080 passing indexeddb constans to wasm #3097

71 changes: 50 additions & 21 deletions crates/wasm/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,44 @@ use penumbra_proto::view::v1alpha1::{NotesRequest, SwapRecord};
use penumbra_proto::DomainType;
use penumbra_sct::Nullifier;
use penumbra_shielded_pool::{note, Note};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct IndexedDbConstants {
name: String,
version: u32,
tables: Tables,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Tables {
assets: String,
chain_parameters: String,
fmd_parameters: String,
notes: String,
spendable_notes: String,
swaps: String,
}

pub struct IndexedDBStorage {
db: IdbDatabase,
constants: IndexedDbConstants,
}

impl IndexedDBStorage {
pub async fn new() -> WasmResult<Self> {
let db_req: OpenDbRequest = IdbDatabase::open_u32("penumbra", 12)?;
pub async fn new(constants: IndexedDbConstants) -> WasmResult<Self> {
Valentine1898 marked this conversation as resolved.
Show resolved Hide resolved
let db_req: OpenDbRequest = IdbDatabase::open_u32(&constants.name, constants.version)?;

let db: IdbDatabase = db_req.into_future().await?;

Ok(IndexedDBStorage { db })
Ok(IndexedDBStorage { db, constants })
}

pub async fn get_notes(&self, request: NotesRequest) -> WasmResult<Vec<SpendableNoteRecord>> {
let idb_tx = self.db.transaction_on_one("spendable_notes")?;
let store = idb_tx.object_store("spendable_notes")?;
let idb_tx = self
.db
.transaction_on_one(&self.constants.tables.spendable_notes)?;
let store = idb_tx.object_store(&self.constants.tables.spendable_notes)?;

let values = store.get_all()?.await?;

Expand All @@ -52,8 +73,8 @@ impl IndexedDBStorage {
}

pub async fn get_asset(&self, id: &Id) -> WasmResult<Option<DenomMetadata>> {
let tx = self.db.transaction_on_one("assets")?;
let store = tx.object_store("assets")?;
let tx = self.db.transaction_on_one(&self.constants.tables.assets)?;
let store = tx.object_store(&self.constants.tables.assets)?;

Ok(store
.get_owned(base64::Engine::encode(
Expand All @@ -69,8 +90,10 @@ impl IndexedDBStorage {
&self,
commitment: &note::StateCommitment,
) -> WasmResult<Option<SpendableNoteRecord>> {
let tx = self.db.transaction_on_one("spendable_notes")?;
let store = tx.object_store("spendable_notes")?;
let tx = self
.db
.transaction_on_one(&self.constants.tables.spendable_notes)?;
let store = tx.object_store(&self.constants.tables.spendable_notes)?;

Ok(store
.get_owned(base64::Engine::encode(
Expand All @@ -86,8 +109,10 @@ impl IndexedDBStorage {
&self,
nullifier: &Nullifier,
) -> WasmResult<Option<SpendableNoteRecord>> {
let tx = self.db.transaction_on_one("spendable_notes")?;
let store = tx.object_store("spendable_notes")?;
let tx = self
.db
.transaction_on_one(&self.constants.tables.spendable_notes)?;
let store = tx.object_store(&self.constants.tables.spendable_notes)?;

Ok(store
.index("nullifier")?
Expand All @@ -101,8 +126,8 @@ impl IndexedDBStorage {
}

pub async fn store_advice(&self, note: Note) -> WasmResult<()> {
let tx = self.db.transaction_on_one("notes")?;
let store = tx.object_store("notes")?;
let tx = self.db.transaction_on_one(&self.constants.tables.notes)?;
let store = tx.object_store(&self.constants.tables.notes)?;

let note_proto: penumbra_proto::core::component::shielded_pool::v1alpha1::Note =
note.clone().try_into()?;
Expand All @@ -118,8 +143,8 @@ impl IndexedDBStorage {
}

pub async fn read_advice(&self, commitment: note::StateCommitment) -> WasmResult<Option<Note>> {
let tx = self.db.transaction_on_one("notes")?;
let store = tx.object_store("notes")?;
let tx = self.db.transaction_on_one(&self.constants.tables.notes)?;
let store = tx.object_store(&self.constants.tables.notes)?;

let commitment_proto = commitment.to_proto();

Expand All @@ -133,8 +158,10 @@ impl IndexedDBStorage {
}

pub async fn get_chain_parameters(&self) -> WasmResult<Option<ChainParameters>> {
let tx = self.db.transaction_on_one("chain_parameters")?;
let store = tx.object_store("chain_parameters")?;
let tx = self
.db
.transaction_on_one(&self.constants.tables.chain_parameters)?;
let store = tx.object_store(&self.constants.tables.chain_parameters)?;

Ok(store
.get_owned("chain_parameters")?
Expand All @@ -144,8 +171,10 @@ impl IndexedDBStorage {
}

pub async fn get_fmd_parameters(&self) -> WasmResult<Option<FmdParameters>> {
let tx = self.db.transaction_on_one("fmd_parameters")?;
let store = tx.object_store("fmd_parameters")?;
let tx = self
.db
.transaction_on_one(&self.constants.tables.fmd_parameters)?;
let store = tx.object_store(&self.constants.tables.fmd_parameters)?;

Ok(store
.get_owned("fmd")?
Expand All @@ -158,8 +187,8 @@ impl IndexedDBStorage {
&self,
swap_commitment: StateCommitment,
) -> WasmResult<Option<SwapRecord>> {
let tx = self.db.transaction_on_one("swaps")?;
let store = tx.object_store("swaps")?;
let tx = self.db.transaction_on_one(&self.constants.tables.swaps)?;
let store = tx.object_store(&self.constants.tables.swaps)?;

Ok(store
.get_owned(base64::Engine::encode(
Expand Down
15 changes: 11 additions & 4 deletions crates/wasm/src/tx.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::error::WasmResult;
use crate::storage::IndexedDBStorage;
use crate::storage::IndexedDbConstants;
use crate::view_server::{load_tree, StoredTree};
use penumbra_keys::keys::SpendKey;
use penumbra_keys::FullViewingKey;
Expand Down Expand Up @@ -88,21 +89,27 @@ pub fn build_tx(
/// Arguments:
/// full_viewing_key: `bech32 String`
/// tx: `pbt::Transaction`
/// idb_constants: IndexedDbConstants
/// Returns: `TxInfoResponse`
#[wasm_bindgen]
pub async fn transaction_info(full_viewing_key: &str, tx: JsValue) -> Result<JsValue, Error> {
pub async fn transaction_info(
full_viewing_key: &str,
tx: JsValue,
idb_constants: JsValue,
) -> Result<JsValue, Error> {
let transaction = serde_wasm_bindgen::from_value(tx)?;
let response = transaction_info_inner(full_viewing_key, transaction).await?;
let constants = serde_wasm_bindgen::from_value(idb_constants)?;
let response = transaction_info_inner(full_viewing_key, transaction, constants).await?;

serde_wasm_bindgen::to_value(&response)
}

/// deprecated
pub async fn transaction_info_inner(
full_viewing_key: &str,
tx: Transaction,
idb_constants: IndexedDbConstants,
) -> WasmResult<TxInfoResponse> {
let storage = IndexedDBStorage::new().await?;
let storage = IndexedDBStorage::new(idb_constants).await?;

let fvk = FullViewingKey::from_str(full_viewing_key)?;

Expand Down
12 changes: 11 additions & 1 deletion crates/wasm/src/view_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,25 @@ pub struct ViewServer {

#[wasm_bindgen]
impl ViewServer {
/// Create new instances of `ViewServer`
/// Function opens a connection to indexedDb
/// Arguments:
/// full_viewing_key: `bech32 string`
/// epoch_duration: `u64`
/// stored_tree: `StoredTree`
/// idb_constants: `IndexedDbConstants`
/// Returns: `ViewServer`
#[wasm_bindgen]
pub async fn new(
full_viewing_key: &str,
epoch_duration: u64,
stored_tree: JsValue,
idb_constants: JsValue,
) -> WasmResult<ViewServer> {
let fvk = FullViewingKey::from_str(full_viewing_key)?;
let stored_tree: StoredTree = serde_wasm_bindgen::from_value(stored_tree)?;
let tree = load_tree(stored_tree);
let constants = serde_wasm_bindgen::from_value(idb_constants)?;
let view_server = Self {
latest_height: u64::MAX,
fvk,
Expand All @@ -85,7 +95,7 @@ impl ViewServer {
denoms: Default::default(),
nct: tree,
swaps: Default::default(),
storage: IndexedDBStorage::new().await?,
storage: IndexedDBStorage::new(constants).await?,
};
Ok(view_server)
}
Expand Down
15 changes: 10 additions & 5 deletions crates/wasm/src/wasm_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ pub struct WasmPlanner {

#[wasm_bindgen]
impl WasmPlanner {
#[wasm_bindgen(constructor)]
pub async fn new() -> Result<WasmPlanner, Error> {
/// Create new instances of `WasmPlanner`
/// Function opens a connection to indexedDb
/// Arguments:
/// idb_constants: `IndexedDbConstants`
/// Returns: `WasmPlanner`
#[wasm_bindgen]
pub async fn new(idb_constants: JsValue) -> Result<WasmPlanner, Error> {
let constants = serde_wasm_bindgen::from_value(idb_constants)?;
let planner = WasmPlanner {
planner: Planner::new(OsRng),
storage: IndexedDBStorage::new().await?,
storage: IndexedDBStorage::new(constants).await?,
};
Ok(planner)
}
Expand Down Expand Up @@ -167,9 +173,8 @@ impl WasmPlanner {

let (spendable_requests, _) = self.planner.notes_requests();

let idb_storage = IndexedDBStorage::new().await?;
for request in spendable_requests {
let notes = idb_storage.get_notes(request);
let notes = self.storage.get_notes(request);
spendable_notes.extend(notes.await?);
}

Expand Down