Skip to content

Commit

Permalink
clears state in flush_updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentine1898 committed Oct 6, 2023
1 parent 0411e74 commit 08d6a6a
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions crates/wasm/src/view_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub struct ViewServer {
denoms: BTreeMap<Id, DenomMetadata>,
nct: Tree,
storage: IndexedDBStorage,
last_position: Option<StoredPosition>,
last_forgotten: Option<Forgotten>,
}

#[wasm_bindgen]
Expand Down Expand Up @@ -94,6 +96,8 @@ impl ViewServer {
nct: tree,
swaps: Default::default(),
storage: IndexedDBStorage::new(constants).await?,
last_position: None,
last_forgotten: None,
};
Ok(view_server)
}
Expand Down Expand Up @@ -248,35 +252,33 @@ impl ViewServer {

/// Get new notes, swaps, SCT state updates
/// Function also clears state
/// Arguments:
/// last_position: `Option<StoredPosition>`
/// last_forgotten: `Option<Forgotten>`
/// Returns: `ScanBlockResult`
#[wasm_bindgen]
pub fn flush_updates(
&mut self,
last_position: JsValue,
last_forgotten: JsValue,
) -> WasmResult<JsValue> {
let stored_position: Option<StoredPosition> =
serde_wasm_bindgen::from_value(last_position)?;
let stored_forgotten: Option<Forgotten> = serde_wasm_bindgen::from_value(last_forgotten)?;

let nct_updates: Updates = self
.nct
.updates(
stored_position.unwrap_or_default(),
stored_forgotten.unwrap_or_default(),
self.last_position.unwrap_or_default(),
self.last_forgotten.unwrap_or_default(),
)
.collect::<Updates>();

let updates = ScanBlockResult {
height: self.latest_height,
nct_updates,
nct_updates: nct_updates.clone(),
new_notes: self.notes.clone().into_values().collect(),
new_swaps: self.swaps.clone().into_values().collect(),
};

self.notes = Default::default();
self.swaps = Default::default();

self.last_position= nct_updates.set_position;
self.last_forgotten = nct_updates.set_forgotten;

let result = serde_wasm_bindgen::to_value(&updates)?;
Ok(result)
}
Expand Down

0 comments on commit 08d6a6a

Please sign in to comment.