Skip to content

Commit

Permalink
add network option, fix bug when submitting blocks, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-L2L committed Jul 8, 2024
1 parent 5246346 commit 4d4d4b5
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 21 deletions.
36 changes: 30 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ members = [
[workspace.package]
authors = [ "Ash Manning <[email protected]>" ]
edition = "2021"
version = "0.8.1"
version = "0.8.2"

[workspace.dependencies.bip300301]
git = "https://github.com/Ash-L2L/bip300301.git"
Expand Down
2 changes: 1 addition & 1 deletion app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ itertools = "0.11.0"
include_path = "0.1.1"
jsonrpsee = { version = "0.20.0", features = ["server"] }
parking_lot = "0.12.1"
plain_bitnames = { path = "../lib" }
plain_bitnames = { path = "../lib", features = ["clap"] }
plain_bitnames_app_cli = { path = "../cli" }
plain_bitnames_app_rpc_api = { path = "../rpc-api" }
poll-promise = { version = "0.3.0", features = ["tokio"] }
Expand Down
1 change: 1 addition & 0 deletions app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ impl App {
config.net_addr,
&config.datadir,
config.main_addr,
config.network,
&config.main_password,
&config.main_user,
local_pool.clone(),
Expand Down
7 changes: 6 additions & 1 deletion app/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
};

use clap::{Arg, Parser};
use plain_bitnames::node::THIS_SIDECHAIN;
use plain_bitnames::{node::THIS_SIDECHAIN, types::Network};

const fn ipv4_socket_addr(ipv4_octets: [u8; 4], port: u16) -> SocketAddr {
let [a, b, c, d] = ipv4_octets;
Expand Down Expand Up @@ -131,6 +131,9 @@ pub(super) struct Cli {
/// Socket address to use for P2P networking
#[arg(default_value_t = DEFAULT_NET_ADDR, long, short)]
net_addr: SocketAddr,
/// Set the network. Setting this may affect other defaults.
#[arg(default_value_t, long, value_enum)]
network: Network,
/// Socket address to host the RPC server
#[arg(default_value_t = DEFAULT_RPC_ADDR, long, short)]
rpc_addr: SocketAddr,
Expand Down Expand Up @@ -158,6 +161,7 @@ pub struct Config {
pub mnemonic_seed_phrase_path: Option<PathBuf>,
pub main_user: String,
pub net_addr: SocketAddr,
pub network: Network,
pub rpc_addr: SocketAddr,
#[cfg(all(not(target_os = "windows"), feature = "zmq"))]
pub zmq_addr: SocketAddr,
Expand Down Expand Up @@ -191,6 +195,7 @@ impl Cli {
main_user: self.user_main,
mnemonic_seed_phrase_path: self.mnemonic_seed_phrase_path,
net_addr: self.net_addr,
network: self.network,
rpc_addr: self.rpc_addr,
#[cfg(all(not(target_os = "windows"), feature = "zmq"))]
zmq_addr: self.zmq_addr,
Expand Down
3 changes: 3 additions & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ borsh = { version = "1.3.1", features = ["derive"] }
bs58 = { version = "0.5.0", features = ["check"] }
byteorder = "1.4.3"
bytes = "1.4.0"
clap = { version = "4.5.4", features = ["derive"], optional = true }
ed25519-dalek = { version = "2.1.1", features = ["batch", "serde"] }
ed25519-dalek-bip32 = "0.3.0"
educe = { version = "0.4.23", features = ["Hash"] }
Expand All @@ -36,6 +37,7 @@ serde = { version = "1.0.179", features = ["derive"] }
serde_json = "1.0.113"
serde_with = { version = "3.4.0", default-features = false }
sha256 = "1.2.2"
strum = { version = "0.26.3", features = ["derive"], optional = true}
thiserror = "1.0.44"
tiny-bip39 = "1.0.0"
tokio = { version = "1.29.1", features = ["rt-multi-thread", "sync"] }
Expand All @@ -51,6 +53,7 @@ rev = "61748742c2a91be4be24a227d109fd93492f7484"
optional = true

[features]
clap = ["dep:clap", "dep:strum"]
zmq = ["dep:async_zmq"]

[lib]
Expand Down
58 changes: 58 additions & 0 deletions lib/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,64 @@ impl Archive {
&block_hash,
&exponential_ancestors,
)?;
// Populate BMM verifications
{
let mut bmm_results = self.get_bmm_results(rwtxn, block_hash)?;
let parent_bmm_results =
self.get_bmm_results(rwtxn, header.prev_side_hash)?;
let main_blocks =
self.get_main_successors(rwtxn, header.prev_main_hash)?;
for main_block in main_blocks {
let Some(commitment) =
self.get_main_bmm_commitment(rwtxn, main_block)?
else {
tracing::trace!(%block_hash, "Failed BMM @ {main_block}: missing commitment");
bmm_results.insert(main_block, BmmResult::Failed);
continue;
};
if commitment != block_hash {
tracing::trace!(%block_hash, "Failed BMM @ {main_block}: commitment to other block ({commitment})");
bmm_results.insert(main_block, BmmResult::Failed);
continue;
}
let main_header = self.get_main_header(rwtxn, main_block)?;
if header.prev_main_hash != main_header.prev_blockhash {
tracing::trace!(%block_hash, "Failed BMM @ {main_block}: should be impossible?");
bmm_results.insert(main_block, BmmResult::Failed);
continue;
}
if header.prev_side_hash == BlockHash::default() {
tracing::trace!(%block_hash, "Verified BMM @ {main_block}: no parent");
bmm_results.insert(main_block, BmmResult::Verified);
continue;
}
// Check if there is a valid BMM commitment to the parent in the
// main ancestry
let main_ancestry_contains_valid_bmm_commitment_to_parent =
parent_bmm_results
.iter()
.map(Ok)
.transpose_into_fallible()
.any(|(bmm_block, bmm_result)| {
let parent_verified = *bmm_result
== BmmResult::Verified
&& self.is_main_descendant(
rwtxn, *bmm_block, main_block,
)?;
Result::<bool, Error>::Ok(parent_verified)
})?;
if main_ancestry_contains_valid_bmm_commitment_to_parent {
tracing::trace!(%block_hash, "Verified BMM @ {main_block}: verified parent");
bmm_results.insert(main_block, BmmResult::Verified);
continue;
} else {
tracing::trace!(%block_hash, "Failed BMM @ {main_block}: no valid BMM commitment to parent in main ancestry");
bmm_results.insert(main_block, BmmResult::Failed);
continue;
}
}
self.bmm_results.put(rwtxn, &block_hash, &bmm_results)?;
}
Ok(())
}

Expand Down
32 changes: 23 additions & 9 deletions lib/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ use quinn::{ClientConfig, Endpoint, ServerConfig};
use tokio_stream::StreamNotifyClose;

use crate::{
archive::Archive, node::THIS_SIDECHAIN, state::State,
types::AuthorizedTransaction,
archive::Archive,
node::THIS_SIDECHAIN,
state::State,
types::{AuthorizedTransaction, Network},
};

mod peer;
Expand Down Expand Up @@ -78,6 +80,21 @@ pub fn make_client_endpoint(bind_addr: SocketAddr) -> Result<Endpoint, Error> {
pub type PeerInfoRx =
mpsc::UnboundedReceiver<(SocketAddr, Option<PeerConnectionInfo>)>;

const SIGNET_SEED_NODE_ADDRS: &[SocketAddr] = {
const SEED_NODE_ADDR: SocketAddr = SocketAddr::new(
std::net::IpAddr::V4(std::net::Ipv4Addr::new(172, 105, 148, 135)),
4000 + THIS_SIDECHAIN as u16,
);
&[SEED_NODE_ADDR]
};

const fn seed_node_addrs(network: Network) -> &'static [SocketAddr] {
match network {
Network::Signet => SIGNET_SEED_NODE_ADDRS,
Network::Regtest => &[],
}
}

// State.
// Archive.

Expand Down Expand Up @@ -165,6 +182,7 @@ impl Net {
pub fn new(
env: &heed::Env,
archive: Archive,
network: Network,
state: State,
bind_addr: SocketAddr,
) -> Result<(Self, PeerInfoRx), Error> {
Expand All @@ -178,13 +196,9 @@ impl Net {
None => {
let known_peers =
env.create_database(&mut rwtxn, Some("known_peers"))?;
const SEED_NODE_ADDR: SocketAddr = SocketAddr::new(
std::net::IpAddr::V4(std::net::Ipv4Addr::new(
172, 105, 148, 135,
)),
4000 + THIS_SIDECHAIN as u16,
);
known_peers.put(&mut rwtxn, &SEED_NODE_ADDR, &())?;
for seed_node_addr in seed_node_addrs(network) {
known_peers.put(&mut rwtxn, seed_node_addr, &())?;
}
known_peers
}
};
Expand Down
8 changes: 5 additions & 3 deletions lib/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use crate::{
types::{
Address, Authorized, AuthorizedTransaction, BitName, BitNameData,
Block, BlockHash, BmmResult, Body, FilledOutput, FilledTransaction,
GetValue, Header, OutPoint, SpentOutput, Tip, Transaction, TxIn, Txid,
WithdrawalBundle,
GetValue, Header, Network, OutPoint, SpentOutput, Tip, Transaction,
TxIn, Txid, WithdrawalBundle,
},
util::Watchable,
};
Expand Down Expand Up @@ -152,10 +152,12 @@ pub struct Node {
}

impl Node {
#[allow(clippy::too_many_arguments)]
pub fn new(
bind_addr: SocketAddr,
datadir: &Path,
main_addr: SocketAddr,
network: Network,
password: &str,
user: &str,
local_pool: LocalPoolHandle,
Expand Down Expand Up @@ -194,7 +196,7 @@ impl Node {
drivechain.clone(),
);
let (net, peer_info_rx) =
Net::new(&env, archive.clone(), state.clone(), bind_addr)?;
Net::new(&env, archive.clone(), network, state.clone(), bind_addr)?;

let net_task = NetTaskHandle::new(
local_pool.clone(),
Expand Down
8 changes: 8 additions & 0 deletions lib/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,11 @@ impl Default for Tip {
}
}
}

#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum, strum::Display))]
pub enum Network {
#[default]
Signet,
Regtest,
}

0 comments on commit 4d4d4b5

Please sign in to comment.