Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
oblique committed Dec 5, 2024
1 parent ff66ea4 commit 3259ff4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
54 changes: 29 additions & 25 deletions node-wasm/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
use std::time::Duration;

use js_sys::Array;
use libp2p::identity::Keypair;
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::node::NodeConfig;
use lumina_node::network;
use lumina_node::node::NodeBuilder;
use lumina_node::store::IndexedDbStore;

use crate::commands::{CheckableResponseExt, NodeCommand, SingleHeaderQuery};
Expand Down Expand Up @@ -374,51 +374,55 @@ impl NodeClient {
impl WasmNodeConfig {
/// Get the configuration with default bootnodes for provided network
pub fn default(network: Network) -> WasmNodeConfig {
let bootnodes = network::Network::from(network)
.canonical_bootnodes()
.map(|addr| addr.to_string())
.collect::<Vec<_>>();

WasmNodeConfig {
network,
bootnodes: canonical_network_bootnodes(network.into())
.map(|addr| addr.to_string())
.collect::<Vec<_>>(),
bootnodes,
custom_syncing_window_secs: None,
}
}

pub(crate) async fn into_node_config(
pub(crate) async fn into_node_builder(
self,
) -> Result<NodeConfig<IndexedDbBlockstore, IndexedDbStore>> {
let network_id = network_id(self.network.into());
) -> Result<NodeBuilder<IndexedDbBlockstore, IndexedDbStore>> {
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 p2p_local_keypair = Keypair::generate_ed25519();
let mut builder = NodeBuilder::new()
.store(store)
.blockstore(blockstore)
.network(self.network.into())
.sync_batch_size(128);

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

let mut p2p_bootnodes = Vec::with_capacity(self.bootnodes.len());
for addr in self.bootnodes {
let addr = addr
.parse()
.with_context(|| format!("invalid multiaddr: '{addr}"))?;
let resolved_addrs = resolve_dnsaddr_multiaddress(addr).await?;
p2p_bootnodes.extend(resolved_addrs.into_iter());
bootnodes.extend(resolved_addrs.into_iter());
}

builder = builder.bootnodes(bootnodes);

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

let syncing_window = self
.custom_syncing_window_secs
.map(|d| Duration::from_secs(d.into()));

Ok(NodeConfig {
network_id: network_id.to_string(),
p2p_bootnodes,
p2p_local_keypair,
p2p_listen_on: vec![],
sync_batch_size: 128,
custom_syncing_window: syncing_window,
blockstore,
store,
})
Ok(builder)
}
}

Expand Down
5 changes: 3 additions & 2 deletions node-wasm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl From<Network> for network::Network {
Network::Mainnet => network::Network::Mainnet,
Network::Arabica => network::Network::Arabica,
Network::Mocha => network::Network::Mocha,
Network::Private => network::Network::Private,
Network::Private => network::Network::custom("private").expect("invalid network id"),
}
}
}
Expand All @@ -72,7 +72,8 @@ impl From<network::Network> for Network {
network::Network::Mainnet => Network::Mainnet,
network::Network::Arabica => Network::Arabica,
network::Network::Mocha => Network::Mocha,
network::Network::Private => Network::Private,
network::Network::Custom(n) if n.as_str() == "private" => Network::Private,
network::Network::Custom(_) => panic!("todo"),
}
}
}
Expand Down
12 changes: 2 additions & 10 deletions node-wasm/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use web_sys::BroadcastChannel;
use lumina_node::blockstore::IndexedDbBlockstore;
use lumina_node::events::{EventSubscriber, NodeEventInfo};
use lumina_node::node::{Node, SyncingInfo};
use lumina_node::store::{IndexedDbStore, SamplingMetadata, Store};
use lumina_node::store::{IndexedDbStore, SamplingMetadata};

use crate::client::WasmNodeConfig;
use crate::commands::{NodeCommand, SingleHeaderQuery, WorkerResponse};
Expand Down Expand Up @@ -123,15 +123,7 @@ impl NodeWorker {

impl NodeWorkerInstance {
async fn new(events_channel_name: &str, config: WasmNodeConfig) -> Result<Self> {
let config = config.into_node_config().await?;

if let Ok(store_height) = config.store.head_height().await {
info!("Initialised store with head height: {store_height}");
} else {
info!("Initialised new empty store");
}

let (node, events_sub) = Node::new_subscribed(config).await?;
let (node, events_sub) = config.into_node_builder().await?.start_subscribed().await?;

let events_channel = BroadcastChannel::new(events_channel_name)
.context("Failed to allocate BroadcastChannel")?;
Expand Down
4 changes: 4 additions & 0 deletions node/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ impl NetworkId {
Ok(NetworkId(id.to_owned()))
}
}

pub fn as_str(&self) -> &str {
&self.0
}
}

impl Deref for NetworkId {
Expand Down

0 comments on commit 3259ff4

Please sign in to comment.