From 638988bd486321d18108bf741b17fa0898a14a49 Mon Sep 17 00:00:00 2001 From: Ori Newman Date: Sat, 26 Aug 2023 08:27:14 +0000 Subject: [PATCH 1/2] Add mining_integration_test --- consensus/core/src/config/params.rs | 2 +- kaspad/src/daemon.rs | 8 +- kaspad/src/main.rs | 2 +- testing/integration/src/integration_tests.rs | 100 +++++++++++++------ 4 files changed, 75 insertions(+), 37 deletions(-) diff --git a/consensus/core/src/config/params.rs b/consensus/core/src/config/params.rs index 9131ab593..c8088eb1b 100644 --- a/consensus/core/src/config/params.rs +++ b/consensus/core/src/config/params.rs @@ -497,7 +497,7 @@ pub const SIMNET_PARAMS: Params = Params { deflationary_phase_daa_score: 15778800 - 259200, pre_deflationary_phase_base_subsidy: 50000000000, coinbase_maturity: 100, - skip_proof_of_work: false, + skip_proof_of_work: true, max_block_level: 250, pruning_proof_m: 1000, }; diff --git a/kaspad/src/daemon.rs b/kaspad/src/daemon.rs index 6e51af673..5eef3377d 100644 --- a/kaspad/src/daemon.rs +++ b/kaspad/src/daemon.rs @@ -119,12 +119,12 @@ impl Runtime { } } -pub fn create_core(args: Args) -> Arc { +pub fn create_core(args: Args) -> (Arc, Arc) { let rt = Runtime::from_args(&args); create_core_with_runtime(&rt, &args) } -pub fn create_core_with_runtime(runtime: &Runtime, args: &Args) -> Arc { +pub fn create_core_with_runtime(runtime: &Runtime, args: &Args) -> (Arc, Arc) { let network = args.network(); let config = Arc::new( @@ -290,7 +290,7 @@ do you confirm? (answer y/n or pass --yes to the Kaspad command line to confirm mining_manager, flow_context, index_service.as_ref().map(|x| x.utxoindex().unwrap()), - config, + config.clone(), core.clone(), processing_counters, wrpc_borsh_counters.clone(), @@ -339,5 +339,5 @@ do you confirm? (answer y/n or pass --yes to the Kaspad command line to confirm core.bind(consensus_manager); core.bind(async_runtime); - core + (core, config) } diff --git a/kaspad/src/main.rs b/kaspad/src/main.rs index 844990dc2..7428a494f 100644 --- a/kaspad/src/main.rs +++ b/kaspad/src/main.rs @@ -12,7 +12,7 @@ pub fn main() { let _profiler = dhat::Profiler::builder().file_name("kaspad-heap.json").build(); let args = parse_args(); - let core = create_core(args); + let (core, _) = create_core(args); // Bind the keyboard signal to the core Arc::new(Signals::new(&core)).init(); diff --git a/testing/integration/src/integration_tests.rs b/testing/integration/src/integration_tests.rs index 1b4e35c9c..ae555d965 100644 --- a/testing/integration/src/integration_tests.rs +++ b/testing/integration/src/integration_tests.rs @@ -3,6 +3,7 @@ //! use async_channel::unbounded; +use kaspa_addresses::Address; use kaspa_consensus::config::genesis::GENESIS; use kaspa_consensus::config::{Config, ConfigBuilder}; use kaspa_consensus::consensus::factory::Factory as ConsensusFactory; @@ -53,6 +54,7 @@ use kaspa_database::prelude::ConnBuilder; use kaspa_index_processor::service::IndexService; use kaspa_math::Uint256; use kaspa_muhash::MuHash; +use kaspa_rpc_core::api::rpc::RpcApi; use kaspa_rpc_core::notify::mode::NotificationMode; use kaspa_utxoindex::api::{UtxoIndexApi, UtxoIndexProxy}; use kaspa_utxoindex::UtxoIndex; @@ -61,8 +63,10 @@ use kaspad::daemon::create_core_with_runtime; use serde::{Deserialize, Serialize}; use std::cmp::{max, Ordering}; use std::collections::HashSet; +use std::ops::Deref; use std::path::Path; use std::sync::Arc; +use std::thread::JoinHandle; use std::time::Duration; use std::{ collections::HashMap, @@ -1712,33 +1716,44 @@ async fn staging_consensus_test() { #[tokio::test] async fn sanity_integration_test() { - let core1 = DaemonWithRpc::new_random(); - let (workers1, rpc_client1) = core1.start().await; - - let core2 = DaemonWithRpc::new_random(); - let (workers2, rpc_client2) = core2.start().await; + let mut kaspad1 = DaemonWithRpc::new_random(); + let mut kaspad2 = DaemonWithRpc::new_random(); + kaspad1.start().await; + kaspad2.start().await; + assert!(kaspad1.get_info().await.is_ok()); + assert!(kaspad2.get_info().await.is_ok()); +} +#[tokio::test] +async fn mining_integration_test() { + kaspa_core::log::init_logger(None, "INFO"); + + let mut kaspad1 = DaemonWithRpc::new_random(); + let mut kaspad2 = DaemonWithRpc::new_random(); + kaspad1.start().await; + kaspad2.start().await; + + kaspad2.add_peer(format!("127.0.0.1:{}", kaspad1.p2p_port).try_into().unwrap(), true).await.unwrap(); + tokio::time::sleep(Duration::from_secs(1)).await; // Let it connect + assert_eq!(kaspad2.get_connected_peer_info().await.unwrap().peer_info.len(), 1); + kaspad1.mine_blocks(10).await; tokio::time::sleep(Duration::from_secs(1)).await; - rpc_client1.disconnect().await.unwrap(); - drop(rpc_client1); - core1.core.shutdown(); - core1.core.join(workers1); - - rpc_client2.disconnect().await.unwrap(); - drop(rpc_client2); - core2.core.shutdown(); - core2.core.join(workers2); + assert_eq!(kaspad2.get_block_dag_info().await.unwrap().block_count, 10); } struct DaemonWithRpc { core: Arc, + config: Arc, rpc_port: u16, + p2p_port: u16, + workers: Option>>, + rpc_client: Option, _appdir_tempdir: TempDir, } impl DaemonWithRpc { fn new_random() -> DaemonWithRpc { - let mut args = Args { devnet: true, ..Default::default() }; + let mut args = Args { simnet: true, unsafe_rpc: true, enable_unsynced_mining: true, ..Default::default() }; // This should ask the OS to allocate free port for socket 1 to 4. let socket1 = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); @@ -1765,23 +1780,46 @@ impl DaemonWithRpc { let appdir_tempdir = tempdir().unwrap(); args.appdir = Some(appdir_tempdir.path().to_str().unwrap().to_owned()); - let core = create_core_with_runtime(&Default::default(), &args); - DaemonWithRpc { core, rpc_port, _appdir_tempdir: appdir_tempdir } + let (core, config) = create_core_with_runtime(&Default::default(), &args); + DaemonWithRpc { core, config, rpc_port, p2p_port, workers: None, rpc_client: None, _appdir_tempdir: appdir_tempdir } } - async fn start(&self) -> (Vec>, GrpcClient) { - let workers = self.core.start(); - tokio::time::sleep(Duration::from_secs(1)).await; - let rpc_client = GrpcClient::connect( - NotificationMode::Direct, - format!("grpc://localhost:{}", self.rpc_port), - true, - None, - false, - Some(500_000), - ) - .await - .unwrap(); - (workers, rpc_client) + async fn start(&mut self) { + self.workers = Some(self.core.start()); + tokio::time::sleep(Duration::from_secs(1)).await; // Wait for the node to start before connecting to RPC + self.rpc_client = Some( + GrpcClient::connect( + NotificationMode::Direct, + format!("grpc://localhost:{}", self.rpc_port), + true, + None, + false, + Some(500_000), + ) + .await + .unwrap(), + ); + } + + async fn mine_blocks(&self, num_blocks: usize) { + for _ in 0..num_blocks { + self.mine_block().await; + } + } + + async fn mine_block(&self) { + let template = self + .get_block_template(Address::new(self.config.prefix(), kaspa_addresses::Version::PubKey, &[0; 32]), vec![]) + .await + .unwrap(); + self.submit_block(template.block, false).await.unwrap(); + } +} + +impl Deref for DaemonWithRpc { + type Target = GrpcClient; + + fn deref(&self) -> &Self::Target { + self.rpc_client.as_ref().unwrap() } } From f31b94ef13758df02ef661fa14b8db319f11a52c Mon Sep 17 00:00:00 2001 From: Ori Newman Date: Sun, 27 Aug 2023 08:44:39 +0000 Subject: [PATCH 2/2] Fix mining_integration_test to use try_init_logger --- testing/integration/src/integration_tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/integration/src/integration_tests.rs b/testing/integration/src/integration_tests.rs index ae555d965..7ade35219 100644 --- a/testing/integration/src/integration_tests.rs +++ b/testing/integration/src/integration_tests.rs @@ -1726,7 +1726,7 @@ async fn sanity_integration_test() { #[tokio::test] async fn mining_integration_test() { - kaspa_core::log::init_logger(None, "INFO"); + kaspa_core::log::try_init_logger("INFO"); let mut kaspad1 = DaemonWithRpc::new_random(); let mut kaspad2 = DaemonWithRpc::new_random();