Skip to content

Commit

Permalink
feat(node-wasm): Add more configuration options in NodeConfig (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique authored Dec 16, 2024
1 parent 9817a32 commit 5cfe280
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 22 deletions.
76 changes: 57 additions & 19 deletions node-wasm/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
use std::time::Duration;

use blockstore::EitherBlockstore;
use js_sys::Array;
use serde::{Deserialize, Serialize};
use serde_wasm_bindgen::to_value;
use tracing::{debug, error};
use wasm_bindgen::prelude::*;
use web_sys::BroadcastChannel;

use lumina_node::blockstore::IndexedDbBlockstore;
use lumina_node::blockstore::{InMemoryBlockstore, IndexedDbBlockstore};
use lumina_node::network;
use lumina_node::node::NodeBuilder;
use lumina_node::store::IndexedDbStore;
use lumina_node::node::{NodeBuilder, MIN_PRUNING_DELAY, MIN_SAMPLING_WINDOW};
use lumina_node::store::{EitherStore, InMemoryStore, IndexedDbStore};

use crate::commands::{CheckableResponseExt, NodeCommand, SingleHeaderQuery};
use crate::error::{Context, Result};
Expand All @@ -21,6 +22,7 @@ use crate::utils::{
is_safari, js_value_from_display, request_storage_persistence, resolve_dnsaddr_multiaddress,
timeout, Network,
};
use crate::worker::{WasmBlockstore, WasmStore};
use crate::wrapper::libp2p::NetworkInfoSnapshot;
use crate::wrapper::node::{PeerTrackerInfoSnapshot, SyncingInfoSnapshot};

Expand All @@ -33,9 +35,29 @@ pub struct WasmNodeConfig {
/// A list of bootstrap peers to connect to.
#[wasm_bindgen(getter_with_clone)]
pub bootnodes: Vec<String>,
/// Sampling window size, defines maximum age of headers considered for syncing and sampling.
/// Headers older than sampling window by more than an hour are eligible for pruning.
/// Whether to store data in persistent memory or not.
///
/// **Default value:** true
pub use_persistent_memory: bool,
/// Sampling window defines maximum age of a block considered for syncing and sampling.
///
/// If this is not set, then default value will apply:
///
/// * If `use_persistent_memory == true`, default value is 30 days.
/// * If `use_persistent_memory == false`, default value is 60 seconds.
///
/// The minimum value that can be set is 60 seconds.
pub custom_sampling_window_secs: Option<u32>,
/// Pruning delay defines how much time the pruner should wait after sampling window in
/// order to prune the block.
///
/// If this is not set, then default value will apply:
///
/// * If `use_persistent_memory == true`, default value is 1 hour.
/// * If `use_persistent_memory == false`, default value is 60 seconds.
///
/// The minimum value that can be set is 60 seconds.
pub custom_pruning_delay_secs: Option<u32>,
}

/// `NodeClient` is responsible for steering [`NodeWorker`] by sending it commands and receiving
Expand Down Expand Up @@ -382,28 +404,37 @@ impl WasmNodeConfig {
WasmNodeConfig {
network,
bootnodes,
use_persistent_memory: true,
custom_sampling_window_secs: None,
custom_pruning_delay_secs: None,
}
}

pub(crate) async fn into_node_builder(
self,
) -> Result<NodeBuilder<IndexedDbBlockstore, IndexedDbStore>> {
pub(crate) async fn into_node_builder(self) -> Result<NodeBuilder<WasmBlockstore, WasmStore>> {
let network = network::Network::from(self.network);
let network_id = network.id();

let store = IndexedDbStore::new(network_id)
.await
.context("Failed to open the store")?;
let blockstore = IndexedDbBlockstore::new(&format!("{network_id}-blockstore"))
.await
.context("Failed to open the blockstore")?;
let mut builder = if self.use_persistent_memory {
let store = IndexedDbStore::new(network_id)
.await
.context("Failed to open the store")?;
let blockstore = IndexedDbBlockstore::new(&format!("{network_id}-blockstore"))
.await
.context("Failed to open the blockstore")?;

NodeBuilder::new()
.store(EitherStore::Right(store))
.blockstore(EitherBlockstore::Right(blockstore))
} else {
NodeBuilder::new()
.store(EitherStore::Left(InMemoryStore::new()))
.blockstore(EitherBlockstore::Left(InMemoryBlockstore::new()))
// In-memory stores are memory hungry, so we lower sampling and pruining window.
.sampling_window(MIN_SAMPLING_WINDOW)
.pruning_delay(MIN_PRUNING_DELAY)
};

let mut builder = NodeBuilder::new()
.store(store)
.blockstore(blockstore)
.network(network)
.sync_batch_size(128);
builder = builder.network(network).sync_batch_size(128);

let mut bootnodes = Vec::with_capacity(self.bootnodes.len());

Expand All @@ -422,6 +453,11 @@ impl WasmNodeConfig {
builder = builder.sampling_window(dur);
}

if let Some(secs) = self.custom_pruning_delay_secs {
let dur = Duration::from_secs(secs.into());
builder = builder.pruning_delay(dur);
}

Ok(builder)
}
}
Expand Down Expand Up @@ -513,7 +549,9 @@ mod tests {
.start(&WasmNodeConfig {
network: Network::Private,
bootnodes,
use_persistent_memory: false,
custom_sampling_window_secs: None,
custom_pruning_delay_secs: None,
})
.await
.unwrap();
Expand Down
10 changes: 7 additions & 3 deletions node-wasm/src/worker.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::Debug;

use blockstore::EitherBlockstore;
use js_sys::Array;
use libp2p::{Multiaddr, PeerId};
use serde::{Deserialize, Serialize};
Expand All @@ -11,10 +12,10 @@ use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::spawn_local;
use web_sys::BroadcastChannel;

use lumina_node::blockstore::IndexedDbBlockstore;
use lumina_node::blockstore::{InMemoryBlockstore, IndexedDbBlockstore};
use lumina_node::events::{EventSubscriber, NodeEventInfo};
use lumina_node::node::{Node, SyncingInfo};
use lumina_node::store::{IndexedDbStore, SamplingMetadata};
use lumina_node::store::{EitherStore, InMemoryStore, IndexedDbStore, SamplingMetadata};

use crate::client::WasmNodeConfig;
use crate::commands::{NodeCommand, SingleHeaderQuery, WorkerResponse};
Expand All @@ -23,6 +24,9 @@ use crate::ports::{ClientMessage, WorkerServer};
use crate::utils::random_id;
use crate::wrapper::libp2p::NetworkInfoSnapshot;

pub(crate) type WasmBlockstore = EitherBlockstore<InMemoryBlockstore, IndexedDbBlockstore>;
pub(crate) type WasmStore = EitherStore<InMemoryStore, IndexedDbStore>;

#[derive(Debug, Serialize, Deserialize, Error)]
pub enum WorkerError {
/// Worker is initialised, but the node has not been started yet. Use [`NodeDriver::start`].
Expand Down Expand Up @@ -53,7 +57,7 @@ pub struct NodeWorker {
}

struct NodeWorkerInstance {
node: Node<IndexedDbBlockstore, IndexedDbStore>,
node: Node<WasmBlockstore, WasmStore>,
events_channel_name: String,
}

Expand Down

0 comments on commit 5cfe280

Please sign in to comment.