Skip to content

Commit

Permalink
feat: Add estimate of machine power to networking_state
Browse files Browse the repository at this point in the history
Estimate which kind of proofs the host-machine is capable of generating:
LockScript (very cheap), ProofCollection (possible on regular
computers), SingleProof (only possible on very powerful machines).

What to do with transactions before sending them to peers or inserting
them into the mempool will be determined based on this parameter.

Co-authored-by: Alan Szepieniec <[email protected]>
  • Loading branch information
Sword-Smith and aszepieniec committed Oct 1, 2024
1 parent 6b2d21f commit 1702caa
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 5 deletions.
81 changes: 79 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ sha3 = "0.10.8"
readonly = "0.2.12"
thiserror = "1.0.59"
systemstat = "0.2.3"
sysinfo = "0.31.4"

[dev-dependencies]
test-strategy = "0.3"
Expand Down
6 changes: 6 additions & 0 deletions src/config_models/cli_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use clap::builder::RangedI64ValueParser;
use clap::Parser;

use super::network::Network;
use crate::models::state::tx_proving_capability::TxProvingCapability;

/// The `neptune-core` command-line program starts a Neptune node.
#[derive(Parser, Debug, Clone)]
Expand Down Expand Up @@ -127,6 +128,11 @@ pub struct Args {
#[clap(long, default_value = "false")]
pub privacy: bool,

/// Configure how complicated proofs this machine is capable of producing.
/// If no value is set, this parameter is estimated.
#[clap(long)]
pub tx_proving_capability: Option<TxProvingCapability>,

/// Enable tokio tracing for consumption by the tokio-console application
/// note: this will attempt to connect to localhost:6669
#[structopt(long, name = "tokio-console", default_value = "false")]
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ pub async fn initialize(cli_args: cli_args::Args) -> Result<()> {
// Create handshake data which is used when connecting to outgoing peers specified in the
// CLI arguments
let syncing = false;
let networking_state = NetworkingState::new(peer_map, peer_databases, syncing);
let networking_state = NetworkingState::new(
peer_map,
peer_databases,
syncing,
cli_args.tx_proving_capability,
);

let light_state: LightState = LightState::from(latest_block.clone());
let blockchain_archival_state = BlockchainArchivalState {
Expand Down
44 changes: 43 additions & 1 deletion src/models/state/networking_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::net::IpAddr;
use std::net::SocketAddr;

use anyhow::Result;
use num_traits::Zero;
use sysinfo::System;

use super::tx_proving_capability::TxProvingCapability;
use crate::config_models::data_directory::DataDirectory;
use crate::database::create_db_if_missing;
use crate::database::NeptuneLevelDb;
Expand Down Expand Up @@ -35,15 +38,54 @@ pub struct NetworkingState {

// Read-only value set during startup
pub instance_id: u128,

/// The capabilities of this machine to produce STARK proofs
pub tx_proving_capability: TxProvingCapability,
}

impl NetworkingState {
pub fn new(peer_map: PeerMap, peer_databases: PeerDatabases, syncing: bool) -> Self {
pub(crate) fn new(
peer_map: PeerMap,
peer_databases: PeerDatabases,
syncing: bool,
tx_proving_capability: Option<TxProvingCapability>,
) -> Self {
println!("{tx_proving_capability:?}");
let tx_proving_capability =
tx_proving_capability.unwrap_or_else(Self::estimate_proving_power);
println!("{tx_proving_capability:?}");
Self {
peer_map,
peer_databases,
syncing,
instance_id: rand::random(),
tx_proving_capability,
}
}

pub(crate) fn estimate_proving_power() -> TxProvingCapability {
const SINGLE_PROOF_CORE_REQ: usize = 19;
const SINGLE_PROOF_MEMORY_USAGE: u64 = (1u64 << 30) * 128;
const PROOF_COLLECTION_CORE_REQ: usize = 2;
const PROOF_COLLECTION_MEMORY_USAGE: u64 = (1u64 << 30) * 16;

let s = System::new_all();
let total_memory = s.total_memory();
assert!(
!total_memory.is_zero(),
"Total memory reported illegal value of 0"
);

let physical_core_count = s.physical_core_count().unwrap_or(1);

if total_memory > SINGLE_PROOF_MEMORY_USAGE && physical_core_count > SINGLE_PROOF_CORE_REQ {
TxProvingCapability::SingleProof
} else if total_memory > PROOF_COLLECTION_MEMORY_USAGE
&& physical_core_count > PROOF_COLLECTION_CORE_REQ
{
TxProvingCapability::ProofCollection
} else {
TxProvingCapability::LockScript
}
}

Expand Down
27 changes: 27 additions & 0 deletions src/models/state/tx_proving_capability.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::str::FromStr;

use clap::error::ErrorKind;
use clap::Parser;

#[derive(Parser, Debug, Clone, Copy)]
pub enum TxProvingCapability {
LockScript,
ProofCollection,
SingleProof,
}

impl FromStr for TxProvingCapability {
type Err = clap::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"lockscript" => Ok(TxProvingCapability::LockScript),
"proofcollection" => Ok(TxProvingCapability::ProofCollection),
"singleproof" => Ok(TxProvingCapability::SingleProof),
_ => Err(clap::Error::raw(
ErrorKind::InvalidValue,
"Invalid machine proving power",
)),
}
}
}
2 changes: 1 addition & 1 deletion src/tests/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ pub async fn mock_genesis_global_state(
std::net::SocketAddr::from_str(&format!("123.123.123.{}:8080", i)).unwrap();
peer_map.insert(peer_address, get_dummy_peer(peer_address));
}
let networking_state = NetworkingState::new(peer_map, peer_db, syncing);
let networking_state = NetworkingState::new(peer_map, peer_db, syncing, None);
let genesis_block = archival_state.get_tip().await;

// Sanity check
Expand Down

0 comments on commit 1702caa

Please sign in to comment.