Skip to content

Commit 5e17d74

Browse files
committed
WIP: 3 consensus
1 parent b55e2d1 commit 5e17d74

File tree

3 files changed

+49
-33
lines changed

3 files changed

+49
-33
lines changed

simpa/src/main.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ struct Args {
121121

122122
/// Listen address
123123
listen: Option<ContextualNetAddress>,
124+
wait_time: Option<u64>,
124125
add_peers: Vec<NetAddress>,
125126
// halt_mining_after_blocks: Option<u64>,
126127
}
@@ -192,6 +193,9 @@ fn main_impl(mut args: Args) {
192193
.apply_args(|config| {
193194
config.ram_scale = args.ram_scale;
194195
config.disable_upnp = true;
196+
if let Some(listen_address) = args.listen {
197+
config.p2p_listen_address = listen_address;
198+
}
195199
})
196200
.skip_proof_of_work()
197201
.enable_sanity_checks();
@@ -250,6 +254,7 @@ fn main_impl(mut args: Args) {
250254
args.rocksdb_stats_period_sec,
251255
args.rocksdb_files_limit,
252256
args.rocksdb_mem_budget,
257+
args.wait_time.unwrap_or(0),
253258
)
254259
.run(until);
255260

@@ -341,28 +346,6 @@ fn apply_args_to_consensus_params(args: &Args, params: &mut Params) {
341346
params.mergeset_size_limit = 32;
342347
params.pruning_depth = params.anticone_finalization_depth();
343348
info!("Setting pruning depth to {}", params.pruning_depth);
344-
//// x2
345-
// params.pruning_proof_m = 32;
346-
// params.legacy_difficulty_window_size = 128;
347-
// params.legacy_timestamp_deviation_tolerance = 16;
348-
// params.new_timestamp_deviation_tolerance = 16;
349-
// // params.sampled_difficulty_window_size = params.sampled_difficulty_window_size.min(32);
350-
// params.finality_depth = 256;
351-
// params.merge_depth = 256;
352-
// params.mergeset_size_limit = 64;
353-
// params.pruning_depth = params.anticone_finalization_depth();
354-
// info!("Setting pruning depth to {}", params.pruning_depth);
355-
//// x4
356-
// params.pruning_proof_m = 64;
357-
// params.legacy_difficulty_window_size = 256;
358-
// params.legacy_timestamp_deviation_tolerance = 16;
359-
// params.new_timestamp_deviation_tolerance = 16;
360-
// // params.sampled_difficulty_window_size = params.sampled_difficulty_window_size.min(32);
361-
// params.finality_depth = 512;
362-
// params.merge_depth = 512;
363-
// params.mergeset_size_limit = 128;
364-
// params.pruning_depth = params.anticone_finalization_depth();
365-
// info!("Setting pruning depth to {}", params.pruning_depth);
366349
}
367350
}
368351

@@ -498,7 +481,7 @@ mod tests {
498481
let task1 = std::thread::spawn(|| {
499482
let mut args = Args::parse_from(std::iter::empty::<&str>());
500483
args.bps = 1.0;
501-
args.target_blocks = Some(4500);
484+
args.target_blocks = Some(4120);
502485
args.tpb = 1;
503486
args.test_pruning = true;
504487
args.listen = Some(ContextualNetAddress::from_str("0.0.0.0:1234").unwrap());
@@ -518,7 +501,7 @@ mod tests {
518501
let mut args = Args::parse_from(std::iter::empty::<&str>());
519502
args.bps = 1.0;
520503
// Run the simulation long enough to go through some more pruning periods
521-
args.sim_time = 30;
504+
args.sim_time = 60;
522505
args.tpb = 1;
523506
args.test_pruning = true;
524507
args.listen = Some(ContextualNetAddress::from_str("0.0.0.0:5678").unwrap());
@@ -529,7 +512,28 @@ mod tests {
529512
main_impl(args);
530513
});
531514

515+
// FIXME: This third task currently joins the network and IBDs, but for some reason
516+
// only generates the one block. Maybe the first block's parents aren't properly set or something
517+
// idk yet.
518+
let task3 = std::thread::spawn(|| {
519+
sleep(Duration::from_secs(50));
520+
let mut args = Args::parse_from(std::iter::empty::<&str>());
521+
args.bps = 1.0;
522+
args.target_blocks = Some(3000);
523+
args.tpb = 1;
524+
args.test_pruning = true;
525+
args.listen = Some(ContextualNetAddress::from_str("0.0.0.0:9876").unwrap());
526+
args.ram_scale = 4.0;
527+
args.add_peers = vec![ContextualNetAddress::from_str("127.0.0.1:5678").unwrap().normalize(5678)];
528+
args.wait_time = Some(15);
529+
530+
kaspa_core::log::try_init_logger(&args.log_level);
531+
532+
main_impl(args);
533+
});
534+
532535
let _ = task1.join().expect("Task1 failed");
533536
let _ = task2.join().expect("Task2 failed");
537+
let _ = task3.join().expect("Task3 failed");
534538
}
535539
}

simpa/src/simulator/miner.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use kaspa_consensus_core::tx::{
1313
MutableTransaction, ScriptPublicKey, ScriptVec, Transaction, TransactionInput, TransactionOutpoint, TransactionOutput, UtxoEntry,
1414
};
1515
use kaspa_consensus_core::utxo::utxo_view::UtxoView;
16-
use kaspa_core::trace;
16+
use kaspa_core::{info, trace, warn};
1717
use kaspa_p2p_lib::pb::kaspad_message::Payload;
1818
use kaspa_p2p_lib::pb::InvRelayBlockMessage;
1919
use kaspa_p2p_lib::{make_message, Hub};
@@ -87,6 +87,7 @@ pub struct Miner {
8787
// hub
8888
rt: Arc<Runtime>,
8989
hub: Hub,
90+
wait_time: u64,
9091
}
9192

9293
impl Miner {
@@ -102,6 +103,7 @@ impl Miner {
102103
target_blocks: Option<u64>,
103104
rt: Arc<Runtime>,
104105
hub: Hub,
106+
wait_time: u64,
105107
) -> Self {
106108
let (schnorr_public_key, _) = pk.x_only_public_key();
107109
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
@@ -128,6 +130,7 @@ impl Miner {
128130
),
129131
rt,
130132
hub,
133+
wait_time,
131134
}
132135
}
133136

@@ -217,6 +220,7 @@ impl Miner {
217220
}
218221

219222
pub fn mine(&mut self, env: &mut Environment<Block>) -> Suspension {
223+
info!("Mining");
220224
let block = self.build_new_block(env.now());
221225
env.broadcast(self.id, block);
222226
self.sample_mining_interval()
@@ -277,7 +281,13 @@ impl Process<Block> for Miner {
277281
match resumption {
278282
Resumption::Initial => self.sample_mining_interval(),
279283
Resumption::Scheduled => {
280-
if self.num_blocks > 4000 {
284+
if self.wait_time > 0 {
285+
// FIXME: This is some hacky way to wait for the IBD to complete
286+
warn!("wait time: {}", self.wait_time);
287+
self.wait_time -= 1;
288+
sleep(Duration::from_millis(1000));
289+
return Suspension::Timeout(1000);
290+
} else if self.num_blocks > 4000 {
281291
// FIXME: Some hacky slowdown after a threshold
282292
sleep(Duration::from_millis(100));
283293
}

simpa/src/simulator/network.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,12 @@ impl KaspaNetworkSimulator {
212212
rocksdb_stats_period_sec: Option<u32>,
213213
rocksdb_files_limit: Option<i32>,
214214
rocksdb_mem_budget: Option<usize>,
215+
wait_time: u64,
215216
) -> &mut Self {
216217
let secp = secp256k1::Secp256k1::new();
217218
let mut rng = rand::thread_rng();
218219
for i in 0..num_miners {
219-
let mut builder = ConnBuilder::default().with_files_limit(fd_budget::limit() / 2 / num_miners as i32);
220+
let mut builder = ConnBuilder::default().with_files_limit(fd_budget::limit() / 4 / num_miners as i32);
220221
if let Some(rocksdb_files_limit) = rocksdb_files_limit {
221222
builder = builder.with_files_limit(rocksdb_files_limit);
222223
}
@@ -275,9 +276,9 @@ impl KaspaNetworkSimulator {
275276
flow_context.clone(),
276277
vec![],
277278
self.add_peers.clone(),
278-
self.config.p2p_listen_address.normalize(1234).into(),
279-
1,
280-
1,
279+
self.config.p2p_listen_address.normalize(self.config.default_p2p_port()).into(),
280+
8,
281+
8,
281282
Default::default(),
282283
self.config.default_p2p_port(),
283284
Default::default(),
@@ -307,6 +308,7 @@ impl KaspaNetworkSimulator {
307308
self.target_blocks,
308309
self.runtime.clone(),
309310
flow_context.hub().clone(),
311+
wait_time,
310312
));
311313
self.simulation.register(i, miner_process);
312314
self.consensuses.push((consensus, handles, lifetime, services));
@@ -372,9 +374,9 @@ impl KaspaNetworkSimulator {
372374
flow_context.clone(),
373375
vec![],
374376
self.add_peers.clone(),
375-
self.config.p2p_listen_address.normalize(5678).into(),
376-
1,
377-
1,
377+
self.config.p2p_listen_address.normalize(self.config.default_p2p_port()).into(),
378+
8,
379+
8,
378380
Default::default(),
379381
self.config.default_p2p_port(),
380382
Default::default(),

0 commit comments

Comments
 (0)