From 1bf43ef4e33304814aab03668bfd55620520481e Mon Sep 17 00:00:00 2001 From: aggstam Date: Mon, 3 Jul 2023 15:12:47 +0300 Subject: [PATCH] darkfid2: tests to use Darkfid as node structure --- bin/darkfid2/src/darkfid.rs | 29 +++++++++++++++ bin/darkfid2/src/main.rs | 36 ++++++++++++++++++- .../validator.rs => src/tests/harness.rs} | 31 +++++++--------- bin/darkfid2/src/tests/mod.rs | 34 ++++++++++++++++++ 4 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 bin/darkfid2/src/darkfid.rs rename bin/darkfid2/{tests/validator.rs => src/tests/harness.rs} (74%) create mode 100644 bin/darkfid2/src/tests/mod.rs diff --git a/bin/darkfid2/src/darkfid.rs b/bin/darkfid2/src/darkfid.rs new file mode 100644 index 000000000..e08928212 --- /dev/null +++ b/bin/darkfid2/src/darkfid.rs @@ -0,0 +1,29 @@ +/* This file is part of DarkFi (https://dark.fi) + * + * Copyright (C) 2020-2023 Dyne.org foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +use darkfi::validator::ValidatorPtr; + +pub struct Darkfid { + _validator: ValidatorPtr, +} + +impl Darkfid { + pub async fn new(_validator: ValidatorPtr) -> Self { + Self { _validator } + } +} diff --git a/bin/darkfid2/src/main.rs b/bin/darkfid2/src/main.rs index 451f81e28..43bdbeec3 100644 --- a/bin/darkfid2/src/main.rs +++ b/bin/darkfid2/src/main.rs @@ -20,7 +20,17 @@ use async_std::sync::Arc; use log::info; use structopt_toml::{serde::Deserialize, structopt::StructOpt, StructOptToml}; -use darkfi::{async_daemonize, cli_desc, Result}; +use darkfi::{ + async_daemonize, + blockchain::BlockInfo, + cli_desc, + util::time::TimeKeeper, + validator::{Validator, ValidatorConfig, ValidatorPtr}, + Result, +}; + +#[cfg(test)] +mod tests; const CONFIG_FILE: &str = "darkfid_config.toml"; const CONFIG_FILE_CONTENTS: &str = include_str!("../darkfid_config.toml"); @@ -42,6 +52,16 @@ struct Args { verbose: u8, } +pub struct Darkfid { + _validator: ValidatorPtr, +} + +impl Darkfid { + pub async fn new(_validator: ValidatorPtr) -> Self { + Self { _validator } + } +} + async_daemonize!(realmain); async fn realmain(args: Args, _ex: Arc>) -> Result<()> { info!("Initializing DarkFi node..."); @@ -55,10 +75,24 @@ async fn realmain(args: Args, _ex: Arc>) -> Result<()> { }) .unwrap(); + // NOTE: everything is dummy for now + // Initialize or open sled database + let sled_db = sled::Config::new().temporary(true).open()?; + + // Initialize validator configuration + let genesis_block = BlockInfo::default(); + let time_keeper = TimeKeeper::new(genesis_block.header.timestamp, 10, 90, 0); + let config = ValidatorConfig::new(time_keeper, genesis_block, vec![]); + if args.single_node { info!("Node is configured to run in single-node mode!"); } + // Initialize validator + let validator = Validator::new(&sled_db, config).await?; + + // Initialize node + let _darkfid = Darkfid::new(validator).await; info!("Node initialized successfully!"); // Wait for SIGINT diff --git a/bin/darkfid2/tests/validator.rs b/bin/darkfid2/src/tests/harness.rs similarity index 74% rename from bin/darkfid2/tests/validator.rs rename to bin/darkfid2/src/tests/harness.rs index dce0085c0..8820a3cec 100644 --- a/bin/darkfid2/tests/validator.rs +++ b/bin/darkfid2/src/tests/harness.rs @@ -19,18 +19,20 @@ use darkfi::{ blockchain::BlockInfo, util::time::TimeKeeper, - validator::{Validator, ValidatorConfig, ValidatorPtr}, + validator::{Validator, ValidatorConfig}, Result, }; -use darkfi_contract_test_harness::{init_logger, vks}; +use darkfi_contract_test_harness::vks; -struct Harness { - pub _alice: ValidatorPtr, - pub _bob: ValidatorPtr, +use crate::Darkfid; + +pub struct Harness { + pub _alice: Darkfid, + pub _bob: Darkfid, } impl Harness { - async fn new() -> Result { + pub async fn new() -> Result { // Generate default genesis block let genesis_block = BlockInfo::default(); @@ -43,22 +45,13 @@ impl Harness { // Generate validators using pregenerated vks let sled_db = sled::Config::new().temporary(true).open()?; vks::inject(&sled_db)?; - let _alice = Validator::new(&sled_db, config.clone()).await?; + let validator = Validator::new(&sled_db, config.clone()).await?; + let _alice = Darkfid::new(validator).await; let sled_db = sled::Config::new().temporary(true).open()?; vks::inject(&sled_db)?; - let _bob = Validator::new(&sled_db, config).await?; + let validator = Validator::new(&sled_db, config.clone()).await?; + let _bob = Darkfid::new(validator).await; Ok(Self { _alice, _bob }) } } - -#[async_std::test] -async fn add_blocks() -> Result<()> { - init_logger(); - - // Initialize harness - let _th = Harness::new().await?; - - // Thanks for reading - Ok(()) -} diff --git a/bin/darkfid2/src/tests/mod.rs b/bin/darkfid2/src/tests/mod.rs new file mode 100644 index 000000000..a56ee7aae --- /dev/null +++ b/bin/darkfid2/src/tests/mod.rs @@ -0,0 +1,34 @@ +/* This file is part of DarkFi (https://dark.fi) + * + * Copyright (C) 2020-2023 Dyne.org foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +use darkfi::Result; +use darkfi_contract_test_harness::init_logger; + +mod harness; +use harness::Harness; + +#[async_std::test] +async fn add_blocks() -> Result<()> { + init_logger(); + + // Initialize harness + let _th = Harness::new().await?; + + // Thanks for reading + Ok(()) +}