Răsfoiți Sursa

darkfid2: tests to use Darkfid as node structure

aggstam 3 ani în urmă
părinte
comite
1bf43ef4e3

+ 29 - 0
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 <https://www.gnu.org/licenses/>.
+ */
+
+use darkfi::validator::ValidatorPtr;
+
+pub struct Darkfid {
+    _validator: ValidatorPtr,
+}
+
+impl Darkfid {
+    pub async fn new(_validator: ValidatorPtr) -> Self {
+        Self { _validator }
+    }
+}

+ 35 - 1
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<smol::Executor<'_>>) -> Result<()> {
     info!("Initializing DarkFi node...");
@@ -55,10 +75,24 @@ async fn realmain(args: Args, _ex: Arc<smol::Executor<'_>>) -> 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

+ 12 - 19
bin/darkfid2/tests/validator.rs → 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<Self> {
+    pub async fn new() -> Result<Self> {
         // 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(())
-}

+ 34 - 0
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 <https://www.gnu.org/licenses/>.
+ */
+
+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(())
+}