Skip to content

Commit

Permalink
WIP: 3 consensus
Browse files Browse the repository at this point in the history
  • Loading branch information
coderofstuff committed Apr 9, 2024
1 parent b55e2d1 commit 5e17d74
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 33 deletions.
52 changes: 28 additions & 24 deletions simpa/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ struct Args {

/// Listen address
listen: Option<ContextualNetAddress>,
wait_time: Option<u64>,
add_peers: Vec<NetAddress>,
// halt_mining_after_blocks: Option<u64>,
}
Expand Down Expand Up @@ -192,6 +193,9 @@ fn main_impl(mut args: Args) {
.apply_args(|config| {
config.ram_scale = args.ram_scale;
config.disable_upnp = true;
if let Some(listen_address) = args.listen {
config.p2p_listen_address = listen_address;
}
})
.skip_proof_of_work()
.enable_sanity_checks();
Expand Down Expand Up @@ -250,6 +254,7 @@ fn main_impl(mut args: Args) {
args.rocksdb_stats_period_sec,
args.rocksdb_files_limit,
args.rocksdb_mem_budget,
args.wait_time.unwrap_or(0),
)
.run(until);

Expand Down Expand Up @@ -341,28 +346,6 @@ fn apply_args_to_consensus_params(args: &Args, params: &mut Params) {
params.mergeset_size_limit = 32;
params.pruning_depth = params.anticone_finalization_depth();
info!("Setting pruning depth to {}", params.pruning_depth);
//// x2
// params.pruning_proof_m = 32;
// params.legacy_difficulty_window_size = 128;
// params.legacy_timestamp_deviation_tolerance = 16;
// params.new_timestamp_deviation_tolerance = 16;
// // params.sampled_difficulty_window_size = params.sampled_difficulty_window_size.min(32);
// params.finality_depth = 256;
// params.merge_depth = 256;
// params.mergeset_size_limit = 64;
// params.pruning_depth = params.anticone_finalization_depth();
// info!("Setting pruning depth to {}", params.pruning_depth);
//// x4
// params.pruning_proof_m = 64;
// params.legacy_difficulty_window_size = 256;
// params.legacy_timestamp_deviation_tolerance = 16;
// params.new_timestamp_deviation_tolerance = 16;
// // params.sampled_difficulty_window_size = params.sampled_difficulty_window_size.min(32);
// params.finality_depth = 512;
// params.merge_depth = 512;
// params.mergeset_size_limit = 128;
// params.pruning_depth = params.anticone_finalization_depth();
// info!("Setting pruning depth to {}", params.pruning_depth);
}
}

Expand Down Expand Up @@ -498,7 +481,7 @@ mod tests {
let task1 = std::thread::spawn(|| {
let mut args = Args::parse_from(std::iter::empty::<&str>());
args.bps = 1.0;
args.target_blocks = Some(4500);
args.target_blocks = Some(4120);
args.tpb = 1;
args.test_pruning = true;
args.listen = Some(ContextualNetAddress::from_str("0.0.0.0:1234").unwrap());
Expand All @@ -518,7 +501,7 @@ mod tests {
let mut args = Args::parse_from(std::iter::empty::<&str>());
args.bps = 1.0;
// Run the simulation long enough to go through some more pruning periods
args.sim_time = 30;
args.sim_time = 60;
args.tpb = 1;
args.test_pruning = true;
args.listen = Some(ContextualNetAddress::from_str("0.0.0.0:5678").unwrap());
Expand All @@ -529,7 +512,28 @@ mod tests {
main_impl(args);
});

// FIXME: This third task currently joins the network and IBDs, but for some reason
// only generates the one block. Maybe the first block's parents aren't properly set or something
// idk yet.
let task3 = std::thread::spawn(|| {
sleep(Duration::from_secs(50));
let mut args = Args::parse_from(std::iter::empty::<&str>());
args.bps = 1.0;
args.target_blocks = Some(3000);
args.tpb = 1;
args.test_pruning = true;
args.listen = Some(ContextualNetAddress::from_str("0.0.0.0:9876").unwrap());
args.ram_scale = 4.0;
args.add_peers = vec![ContextualNetAddress::from_str("127.0.0.1:5678").unwrap().normalize(5678)];
args.wait_time = Some(15);

kaspa_core::log::try_init_logger(&args.log_level);

main_impl(args);
});

let _ = task1.join().expect("Task1 failed");
let _ = task2.join().expect("Task2 failed");
let _ = task3.join().expect("Task3 failed");
}
}
14 changes: 12 additions & 2 deletions simpa/src/simulator/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use kaspa_consensus_core::tx::{
MutableTransaction, ScriptPublicKey, ScriptVec, Transaction, TransactionInput, TransactionOutpoint, TransactionOutput, UtxoEntry,
};
use kaspa_consensus_core::utxo::utxo_view::UtxoView;
use kaspa_core::trace;
use kaspa_core::{info, trace, warn};
use kaspa_p2p_lib::pb::kaspad_message::Payload;
use kaspa_p2p_lib::pb::InvRelayBlockMessage;
use kaspa_p2p_lib::{make_message, Hub};
Expand Down Expand Up @@ -87,6 +87,7 @@ pub struct Miner {
// hub
rt: Arc<Runtime>,
hub: Hub,
wait_time: u64,
}

impl Miner {
Expand All @@ -102,6 +103,7 @@ impl Miner {
target_blocks: Option<u64>,
rt: Arc<Runtime>,
hub: Hub,
wait_time: u64,
) -> Self {
let (schnorr_public_key, _) = pk.x_only_public_key();
let script_pub_key_script = once(0x20).chain(schnorr_public_key.serialize()).chain(once(0xac)).collect_vec(); // TODO: Use script builder when available to create p2pk properly
Expand All @@ -128,6 +130,7 @@ impl Miner {
),
rt,
hub,
wait_time,
}
}

Expand Down Expand Up @@ -217,6 +220,7 @@ impl Miner {
}

pub fn mine(&mut self, env: &mut Environment<Block>) -> Suspension {
info!("Mining");
let block = self.build_new_block(env.now());
env.broadcast(self.id, block);
self.sample_mining_interval()
Expand Down Expand Up @@ -277,7 +281,13 @@ impl Process<Block> for Miner {
match resumption {
Resumption::Initial => self.sample_mining_interval(),
Resumption::Scheduled => {
if self.num_blocks > 4000 {
if self.wait_time > 0 {
// FIXME: This is some hacky way to wait for the IBD to complete
warn!("wait time: {}", self.wait_time);
self.wait_time -= 1;
sleep(Duration::from_millis(1000));
return Suspension::Timeout(1000);
} else if self.num_blocks > 4000 {
// FIXME: Some hacky slowdown after a threshold
sleep(Duration::from_millis(100));
}
Expand Down
16 changes: 9 additions & 7 deletions simpa/src/simulator/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,12 @@ impl KaspaNetworkSimulator {
rocksdb_stats_period_sec: Option<u32>,
rocksdb_files_limit: Option<i32>,
rocksdb_mem_budget: Option<usize>,
wait_time: u64,
) -> &mut Self {
let secp = secp256k1::Secp256k1::new();
let mut rng = rand::thread_rng();
for i in 0..num_miners {
let mut builder = ConnBuilder::default().with_files_limit(fd_budget::limit() / 2 / num_miners as i32);
let mut builder = ConnBuilder::default().with_files_limit(fd_budget::limit() / 4 / num_miners as i32);
if let Some(rocksdb_files_limit) = rocksdb_files_limit {
builder = builder.with_files_limit(rocksdb_files_limit);
}
Expand Down Expand Up @@ -275,9 +276,9 @@ impl KaspaNetworkSimulator {
flow_context.clone(),
vec![],
self.add_peers.clone(),
self.config.p2p_listen_address.normalize(1234).into(),
1,
1,
self.config.p2p_listen_address.normalize(self.config.default_p2p_port()).into(),
8,
8,
Default::default(),
self.config.default_p2p_port(),
Default::default(),
Expand Down Expand Up @@ -307,6 +308,7 @@ impl KaspaNetworkSimulator {
self.target_blocks,
self.runtime.clone(),
flow_context.hub().clone(),
wait_time,
));
self.simulation.register(i, miner_process);
self.consensuses.push((consensus, handles, lifetime, services));
Expand Down Expand Up @@ -372,9 +374,9 @@ impl KaspaNetworkSimulator {
flow_context.clone(),
vec![],
self.add_peers.clone(),
self.config.p2p_listen_address.normalize(5678).into(),
1,
1,
self.config.p2p_listen_address.normalize(self.config.default_p2p_port()).into(),
8,
8,
Default::default(),
self.config.default_p2p_port(),
Default::default(),
Expand Down

0 comments on commit 5e17d74

Please sign in to comment.