Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mining_integration_test #249

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion consensus/core/src/config/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
8 changes: 4 additions & 4 deletions kaspad/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ impl Runtime {
}
}

pub fn create_core(args: Args) -> Arc<Core> {
pub fn create_core(args: Args) -> (Arc<Core>, Arc<Config>) {
let rt = Runtime::from_args(&args);
create_core_with_runtime(&rt, &args)
}

pub fn create_core_with_runtime(runtime: &Runtime, args: &Args) -> Arc<Core> {
pub fn create_core_with_runtime(runtime: &Runtime, args: &Args) -> (Arc<Core>, Arc<Config>) {
let network = args.network();

let config = Arc::new(
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion kaspad/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
100 changes: 69 additions & 31 deletions testing/integration/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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::try_init_logger("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<Core>,
config: Arc<Config>,
rpc_port: u16,
p2p_port: u16,
workers: Option<Vec<JoinHandle<()>>>,
rpc_client: Option<GrpcClient>,
_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();
Expand All @@ -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<std::thread::JoinHandle<()>>, 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()
}
}