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

chore: remove genesis hash from node configuration #316

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions cli/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use directories::ProjectDirs;
use libp2p::{identity, multiaddr::Protocol, Multiaddr};
use lumina_node::blockstore::RedbBlockstore;
use lumina_node::events::NodeEvent;
use lumina_node::network::{canonical_network_bootnodes, network_genesis, network_id, Network};
use lumina_node::network::{canonical_network_bootnodes, network_id, Network};
use lumina_node::node::{Node, NodeConfig};
use lumina_node::store::{RedbStore, Store};
use tokio::task::spawn_blocking;
Expand Down Expand Up @@ -54,7 +54,6 @@ pub(crate) async fn run(args: Params) -> Result<()> {
};

let network_id = network_id(network).to_owned();
let genesis_hash = network_genesis(network);

info!("Initializing store");
let db = open_db(args.store, &network_id).await?;
Expand All @@ -70,7 +69,6 @@ pub(crate) async fn run(args: Params) -> Result<()> {

let (_node, mut events) = Node::new_subscribed(NodeConfig {
network_id,
genesis_hash,
p2p_local_keypair,
p2p_bootnodes,
p2p_listen_on: args.listen_addrs,
Expand Down
5 changes: 1 addition & 4 deletions cli/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use celestia_types::hash::Hash;
use clap::Args;
use libp2p::multiaddr::Protocol;
use libp2p::Multiaddr;
use lumina_node::network::{canonical_network_bootnodes, network_genesis};
use lumina_node::network::canonical_network_bootnodes;
use rust_embed::RustEmbed;
use serde::Serialize;
use tokio::net::TcpListener;
Expand All @@ -25,7 +25,6 @@ const SERVER_DEFAULT_BIND_ADDR: &str = "127.0.0.1:9876";
struct WasmNodeArgs {
pub network: ArgNetwork,
pub bootnodes: Vec<Multiaddr>,
pub genesis_hash: Option<Hash>,
}

#[derive(RustEmbed)]
Expand Down Expand Up @@ -53,7 +52,6 @@ pub(crate) struct Params {

pub(crate) async fn run(args: Params) -> Result<()> {
let network = args.network.into();
let genesis_hash = network_genesis(network);
let bootnodes = if args.bootnodes.is_empty() {
canonical_network_bootnodes(network)
.filter(|addr| addr.iter().any(|proto| proto == Protocol::WebTransport))
Expand All @@ -65,7 +63,6 @@ pub(crate) async fn run(args: Params) -> Result<()> {
let state = WasmNodeArgs {
network: args.network,
bootnodes,
genesis_hash,
};

let app = Router::new()
Expand Down
15 changes: 2 additions & 13 deletions node-wasm/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use wasm_bindgen::prelude::*;
use web_sys::BroadcastChannel;

use lumina_node::blockstore::IndexedDbBlockstore;
use lumina_node::network::{canonical_network_bootnodes, network_genesis, network_id};
use lumina_node::network::{canonical_network_bootnodes, network_id};
use lumina_node::node::NodeConfig;
use lumina_node::store::IndexedDbStore;

Expand All @@ -27,9 +27,6 @@ const LUMINA_WORKER_NAME: &str = "lumina";
pub struct WasmNodeConfig {
/// A network to connect to.
pub network: Network,
/// Hash of the genesis block in the network.
#[wasm_bindgen(getter_with_clone)]
pub genesis_hash: Option<String>,
/// A list of bootstrap peers to connect to.
#[wasm_bindgen(getter_with_clone)]
pub bootnodes: Vec<String>,
Expand Down Expand Up @@ -347,11 +344,10 @@ impl NodeDriver {

#[wasm_bindgen(js_class = NodeConfig)]
impl WasmNodeConfig {
/// Get the configuration with default bootnodes and genesis hash for provided network
/// Get the configuration with default bootnodes for provided network
pub fn default(network: Network) -> WasmNodeConfig {
WasmNodeConfig {
network,
genesis_hash: network_genesis(network.into()).map(|h| h.to_string()),
bootnodes: canonical_network_bootnodes(network.into())
.filter(|addr| addr.iter().any(|proto| proto == Protocol::WebTransport))
.map(|addr| addr.to_string())
Expand All @@ -372,12 +368,6 @@ impl WasmNodeConfig {

let p2p_local_keypair = Keypair::generate_ed25519();

let genesis_hash = self
.genesis_hash
.map(|h| h.parse())
.transpose()
.context("genesis hash invalid")?;

let p2p_bootnodes = self
.bootnodes
.iter()
Expand All @@ -387,7 +377,6 @@ impl WasmNodeConfig {

Ok(NodeConfig {
network_id: network_id.to_string(),
genesis_hash,
p2p_bootnodes,
p2p_local_keypair,
p2p_listen_on: vec![],
Expand Down
31 changes: 0 additions & 31 deletions node/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::str::FromStr;

use celestia_types::hash::Hash;
use libp2p::Multiaddr;
use serde::{Deserialize, Serialize};
use thiserror::Error;
Expand Down Expand Up @@ -49,21 +48,6 @@ pub fn network_id(network: Network) -> &'static str {
}
}

/// Get the hash of a genesis block for the given network.
pub fn network_genesis(network: Network) -> Option<Hash> {
let hex = match network {
Network::Mainnet => "6BE39EFD10BA412A9DB5288488303F5DD32CF386707A5BEF33617F4C43301872",
Network::Arabica => "5904E55478BA4B3002EE885621E007A2A6A2399662841912219AECD5D5CBE393",
Network::Mocha => "B93BBE20A0FBFDF955811B6420F8433904664D45DB4BF51022BE4200C1A1680D",
Network::Private => return None,
};

let bytes = hex::decode(hex).expect("failed decoding genesis hash");
let array = bytes.try_into().expect("invalid genesis hash lenght");

Some(Hash::Sha256(array))
}

/// Get official Celestia and Lumina bootnodes for the given network.
pub fn canonical_network_bootnodes(network: Network) -> impl Iterator<Item = Multiaddr> {
let peers: &[_] = match network {
Expand Down Expand Up @@ -103,21 +87,6 @@ pub fn canonical_network_bootnodes(network: Network) -> impl Iterator<Item = Mul
mod tests {
use super::*;

#[test]
fn test_network_genesis() {
let mainnet = network_genesis(Network::Mainnet);
assert!(mainnet.is_some());

let arabica = network_genesis(Network::Arabica);
assert!(arabica.is_some());

let mocha = network_genesis(Network::Mocha);
assert!(mocha.is_some());

let private = network_genesis(Network::Private);
assert!(private.is_none());
}

#[test]
fn test_canonical_network_bootnodes() {
// canonical_network_bootnodes works on const data, test it doesn't panic and the data is there
Expand Down
2 changes: 0 additions & 2 deletions node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ where
{
/// An id of the network to connect to.
pub network_id: String,
/// The hash of the genesis block in network.
pub genesis_hash: Option<Hash>,
/// The keypair to be used as [`Node`]s identity.
pub p2p_local_keypair: Keypair,
/// List of bootstrap nodes to connect to and trust.
Expand Down
1 change: 0 additions & 1 deletion node/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ pub fn test_node_config() -> NodeConfig<InMemoryBlockstore, InMemoryStore> {
let node_keypair = identity::Keypair::generate_ed25519();
NodeConfig {
network_id: "private".to_string(),
genesis_hash: None,
p2p_local_keypair: node_keypair,
p2p_bootnodes: vec![],
p2p_listen_on: vec![],
Expand Down
Loading