Skip to content

Commit

Permalink
Make BlsCache locking more granular
Browse files Browse the repository at this point in the history
  • Loading branch information
hrxi committed Feb 6, 2025
1 parent 3c0bfcb commit dd306c2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
23 changes: 16 additions & 7 deletions web-client/src/client/bls_cache.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::cell::RefCell;

use idb::{Database, Error, KeyPath, ObjectStore, TransactionMode};
use nimiq_bls::{LazyPublicKey, PublicKey};
use nimiq_serde::{Deserialize, Serialize};

/// Caches decompressed BlsPublicKeys in an IndexedDB
pub(crate) struct BlsCache {
db: Option<Database>,
keys: Vec<LazyPublicKey>,
keys: RefCell<Vec<LazyPublicKey>>,
}

#[derive(Deserialize, Serialize)]
Expand Down Expand Up @@ -34,7 +36,10 @@ impl BlsCache {
}
};

BlsCache { db, keys: vec![] }
BlsCache {
db,
keys: RefCell::new(vec![]),
}
}

/// Add the given keys into IndexedDB.
Expand Down Expand Up @@ -66,18 +71,22 @@ impl BlsCache {

/// Fetches all bls keys from the IndexedDB and stores them, which makes
/// the decompressed keys available in other places.
pub async fn init(&mut self) -> Result<(), Error> {
pub async fn init(&self) -> Result<(), Error> {
if let Some(db) = &self.db {
let transaction = db.transaction(&[BLS_KEYS], TransactionMode::ReadOnly)?;
let bls_keys_store = transaction.object_store(BLS_KEYS)?;

let js_keys = bls_keys_store.get_all(None, None)?.await?;
log::info!(num = js_keys.len(), "loaded keys from idb");

for js_key in &js_keys {
let value: BlsKeyEntry = serde_wasm_bindgen::from_value(js_key.clone()).unwrap();
let public_key = PublicKey::trusted_deserialize(&value.public_key);
self.keys.push(LazyPublicKey::from(public_key));
{
let mut keys = self.keys.borrow_mut();
for js_key in &js_keys {
let value: BlsKeyEntry =
serde_wasm_bindgen::from_value(js_key.clone()).unwrap();
let public_key = PublicKey::trusted_deserialize(&value.public_key);
keys.push(LazyPublicKey::from(public_key));
}
}
transaction.await?;
} else {
Expand Down
8 changes: 4 additions & 4 deletions web-client/src/client/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub struct Client {
/// Used to await transaction events in `send_transaction`.
transaction_oneshots: Rc<RefCell<HashMap<String, oneshot::Sender<PlainTransactionDetails>>>>,

bls_cache: Rc<RefCell<BlsCache>>,
bls_cache: Rc<BlsCache>,
}

#[wasm_bindgen]
Expand Down Expand Up @@ -198,7 +198,7 @@ impl Client {
peer_changed_listeners: Rc::new(RefCell::new(HashMap::with_capacity(1))),
transaction_listeners: Rc::new(RefCell::new(HashMap::new())),
transaction_oneshots: Rc::new(RefCell::new(HashMap::new())),
bls_cache: Rc::new(RefCell::new(bls_cache)),
bls_cache: Rc::new(bls_cache),
};

client.setup_offline_online_event_handlers();
Expand All @@ -207,7 +207,7 @@ impl Client {
client.setup_network_events();
client.setup_transaction_events().await;

if let Err(err) = client.bls_cache.borrow_mut().init().await {
if let Err(err) = client.bls_cache.init().await {
log::warn!("Failed loading bls cache {}", err);
}

Expand Down Expand Up @@ -1125,7 +1125,7 @@ impl Client {
.iter()
.map(|validator| validator.voting_key.clone())
.collect::<Vec<LazyPublicKey>>();
if let Err(error) = bls_cache.borrow_mut().add_keys(bls_keys).await {
if let Err(error) = bls_cache.add_keys(bls_keys).await {
log::warn!(%error, "failed caching BLS keys");
}
}
Expand Down

0 comments on commit dd306c2

Please sign in to comment.