Quellcode durchsuchen

bin/darkfid2: set foundation for implementing new darkfid

aggstam vor 3 Jahren
Ursprung
Commit
a3d04e0b46

+ 18 - 0
Cargo.lock

@@ -1796,6 +1796,24 @@ dependencies = [
  "url",
 ]
 
+[[package]]
+name = "darkfid2"
+version = "0.4.1"
+dependencies = [
+ "async-std",
+ "ctrlc",
+ "darkfi",
+ "darkfi-contract-test-harness",
+ "easy-parallel",
+ "log",
+ "serde",
+ "simplelog",
+ "sled",
+ "smol",
+ "structopt",
+ "structopt-toml",
+]
+
 [[package]]
 name = "darkirc"
 version = "0.4.1"

+ 1 - 0
Cargo.toml

@@ -22,6 +22,7 @@ members = [
     "bin/zkas",
     #"bin/cashierd",
     "bin/darkfid",
+    "bin/darkfid2",
     "bin/drk",
     "bin/faucetd",
     "bin/fud/fu",

+ 25 - 0
bin/darkfid2/Cargo.toml

@@ -0,0 +1,25 @@
+[package]
+name = "darkfid2"
+version = "0.4.1"
+homepage = "https://dark.fi"
+description = "DarkFi node daemon"
+authors = ["Dyne.org foundation <foundation@dyne.org>"]
+repository = "https://github.com/darkrenaissance/darkfi"
+license = "AGPL-3.0-only"
+edition = "2021"
+
+[dependencies]
+async-std = "1.12.0"
+ctrlc = { version = "3.4.0", features = ["termination"] }
+darkfi = {path = "../../", features = ["async-runtime", "util"]}
+darkfi-contract-test-harness = {path = "../../src/contract/test-harness"}
+easy-parallel = "3.3.0"
+log = "0.4.19"
+simplelog = "0.12.1"
+sled = "0.34.7"
+smol = "1.3.0"
+
+# Argument parsing
+serde = {version = "1.0.164", features = ["derive"]}
+structopt = "0.3.26"
+structopt-toml = "0.5.1"

+ 10 - 0
bin/darkfid2/darkfid_config.toml

@@ -0,0 +1,10 @@
+## darkfid configuration file
+##
+## Please make sure you go through all the settings so you can configure
+## your daemon properly.
+##
+## The default values are left commented. They can be overridden either by
+## uncommenting, or by using the command-line.
+
+# Enable single-node mode for local testing
+single_node = false

+ 70 - 0
bin/darkfid2/src/main.rs

@@ -0,0 +1,70 @@
+/* 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 async_std::sync::Arc;
+use log::info;
+use structopt_toml::{serde::Deserialize, structopt::StructOpt, StructOptToml};
+
+use darkfi::{async_daemonize, cli_desc, Result};
+
+const CONFIG_FILE: &str = "darkfid_config.toml";
+const CONFIG_FILE_CONTENTS: &str = include_str!("../darkfid_config.toml");
+
+#[derive(Clone, Debug, Deserialize, StructOpt, StructOptToml)]
+#[serde(default)]
+#[structopt(name = "darkfid", about = cli_desc!())]
+struct Args {
+    #[structopt(short, long)]
+    /// Configuration file to use
+    config: Option<String>,
+
+    #[structopt(long)]
+    /// Enable single-node mode for local testing
+    single_node: bool,
+
+    #[structopt(short, parse(from_occurrences))]
+    /// Increase verbosity (-vvv supported)
+    verbose: u8,
+}
+
+async_daemonize!(realmain);
+async fn realmain(args: Args, _ex: Arc<smol::Executor<'_>>) -> Result<()> {
+    info!("Initializing DarkFi node...");
+
+    // We use this handler to block this function after detaching all
+    // tasks, and to catch a shutdown signal, where we can clean up and
+    // exit gracefully.
+    let (signal, shutdown) = smol::channel::bounded::<()>(1);
+    ctrlc::set_handler(move || {
+        async_std::task::block_on(signal.send(())).unwrap();
+    })
+    .unwrap();
+
+    if args.single_node {
+        info!("Node is configured to run in single-node mode!");
+    }
+
+    info!("Node initialized successfully!");
+
+    // Wait for SIGINT
+    shutdown.recv().await?;
+    print!("\r");
+    info!("Caught termination signal, cleaning up and exiting...");
+
+    Ok(())
+}

+ 64 - 0
bin/darkfid2/tests/validator.rs

@@ -0,0 +1,64 @@
+/* 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::{
+    blockchain::BlockInfo,
+    util::time::TimeKeeper,
+    validator::{Validator, ValidatorConfig, ValidatorPtr},
+    Result,
+};
+use darkfi_contract_test_harness::{init_logger, vks};
+
+struct Harness {
+    pub _alice: ValidatorPtr,
+    pub _bob: ValidatorPtr,
+}
+
+impl Harness {
+    async fn new() -> Result<Self> {
+        // Generate default genesis block
+        let genesis_block = BlockInfo::default();
+
+        // Generate validators configuration
+        // NOTE: we are not using consensus constants here so we
+        // don't get circular dependencies.
+        let time_keeper = TimeKeeper::new(genesis_block.header.timestamp, 10, 90, 0);
+        let config = ValidatorConfig::new(time_keeper, genesis_block, vec![]);
+
+        // 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 sled_db = sled::Config::new().temporary(true).open()?;
+        vks::inject(&sled_db)?;
+        let _bob = Validator::new(&sled_db, config).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(())
+}

+ 1 - 1
src/contract/test-harness/src/lib.rs

@@ -54,7 +54,7 @@ use rand::rngs::OsRng;
 
 mod benchmarks;
 use benchmarks::TxActionBenchmarks;
-mod vks;
+pub mod vks;
 
 mod consensus_genesis_stake;
 mod consensus_proposal;

+ 1 - 1
src/contract/test-harness/src/vks.rs

@@ -129,7 +129,7 @@ fn read_or_gen_vks() -> Result<Vks> {
     Ok(vks)
 }
 
-pub(crate) fn inject(sled_db: &sled::Db) -> Result<()> {
+pub fn inject(sled_db: &sled::Db) -> Result<()> {
     // Use pregenerated vks
     let vks = read_or_gen_vks()?;
 

+ 1 - 0
src/validator/mod.rs

@@ -41,6 +41,7 @@ pub mod utils;
 use utils::deploy_native_contracts;
 
 /// Configuration for initializing [`Validator`]
+#[derive(Clone)]
 pub struct ValidatorConfig {
     /// Helper structure to calculate time related operations
     pub time_keeper: TimeKeeper,