Ver Fonte

bin/drk: expose drk as a library and make parse_blockchain_config accessible

oars há 4 meses atrás
pai
commit
64baac3c42
3 ficheiros alterados com 102 adições e 88 exclusões
  1. 4 0
      bin/drk/Cargo.toml
  2. 90 4
      bin/drk/src/cli_util.rs
  3. 8 84
      bin/drk/src/main.rs

+ 4 - 0
bin/drk/Cargo.toml

@@ -8,6 +8,10 @@ repository = "https://codeberg.org/darkrenaissance/darkfi"
 license = "AGPL-3.0-only"
 edition = "2021"
 
+[lib]
+path = "src/lib.rs"
+crate-type = ["cdylib", "rlib"]
+
 [dependencies]
 # Darkfi
 darkfi = {path = "../../", features = ["async-daemonize", "bs58", "rpc", "validator"]}

+ 90 - 4
bin/drk/src/cli_util.rs

@@ -24,19 +24,28 @@ use std::{
 };
 
 use rodio::{Decoder, OutputStreamBuilder, Sink};
-use smol::channel::Sender;
-use structopt_toml::clap::{App, Arg, Shell, SubCommand};
+use smol::{channel::Sender, fs::read_to_string};
+use structopt_toml::{
+    clap::{App, Arg, Shell, SubCommand},
+    structopt::StructOpt,
+    StructOptToml,
+};
+use url::Url;
 
 use darkfi::{
     cli_desc,
     tx::{ContractCallLeaf, Transaction, TransactionBuilder},
-    util::{encoding::base64, parse::decode_base10},
+    util::{encoding::base64, parse::decode_base10, path::get_config_path},
     zk::Proof,
     Error, Result,
 };
 use darkfi_money_contract::model::TokenId;
 use darkfi_sdk::{
-    crypto::{keypair::Address, pasta_prelude::PrimeField, FuncId, SecretKey},
+    crypto::{
+        keypair::{Address, Network},
+        pasta_prelude::PrimeField,
+        FuncId, SecretKey,
+    },
     dark_tree::DarkTree,
     pasta::pallas,
     ContractCallImport,
@@ -45,6 +54,83 @@ use darkfi_serial::deserialize_async;
 
 use crate::{money::BALANCE_BASE10_DECIMALS, Drk};
 
+/// Defines a blockchain network configuration.
+/// Default values correspond to a local network.
+#[derive(Clone, Debug, serde::Deserialize, structopt::StructOpt, structopt_toml::StructOptToml)]
+#[structopt()]
+pub struct BlockchainNetwork {
+    #[structopt(long, default_value = "~/.local/share/darkfi/drk/localnet/cache")]
+    /// Path to blockchain cache database
+    pub cache_path: String,
+
+    #[structopt(long, default_value = "~/.local/share/darkfi/drk/localnet/wallet.db")]
+    /// Path to wallet database
+    pub wallet_path: String,
+
+    #[structopt(long, default_value = "changeme")]
+    /// Password for the wallet database
+    pub wallet_pass: String,
+
+    #[structopt(short, long, default_value = "tcp://127.0.0.1:28345")]
+    /// darkfid JSON-RPC endpoint
+    pub endpoint: Url,
+
+    #[structopt(long, default_value = "~/.local/share/darkfi/drk/localnet/history.txt")]
+    /// Path to interactive shell history file
+    pub history_path: String,
+}
+
+/// Auxiliary function to parse darkfid configuration file and extract requested
+/// blockchain network config.
+pub async fn parse_blockchain_config(
+    config: Option<String>,
+    network: &str,
+    fallback: &str,
+) -> Result<(Network, BlockchainNetwork)> {
+    // Grab network
+    let used_net = match network {
+        "mainnet" | "localnet" => Network::Mainnet,
+        "testnet" => Network::Testnet,
+        _ => return Err(Error::ParseFailed("Invalid blockchain network")),
+    };
+
+    // Grab config path
+    let config_path = get_config_path(config, fallback)?;
+
+    // Parse TOML file contents
+    let contents = read_to_string(&config_path).await?;
+    let contents: toml::Value = match toml::from_str(&contents) {
+        Ok(v) => v,
+        Err(e) => {
+            eprintln!("Failed parsing TOML config: {e}");
+            return Err(Error::ParseFailed("Failed parsing TOML config"))
+        }
+    };
+
+    // Grab requested network config
+    let Some(table) = contents.as_table() else { return Err(Error::ParseFailed("TOML not a map")) };
+    let Some(network_configs) = table.get("network_config") else {
+        return Err(Error::ParseFailed("TOML does not contain network configurations"))
+    };
+    let Some(network_configs) = network_configs.as_table() else {
+        return Err(Error::ParseFailed("`network_config` not a map"))
+    };
+    let Some(network_config) = network_configs.get(network) else {
+        return Err(Error::ParseFailed("TOML does not contain requested network configuration"))
+    };
+    let network_config = toml::to_string(&network_config).unwrap();
+    let network_config =
+        match BlockchainNetwork::from_iter_with_toml::<Vec<String>>(&network_config, vec![]) {
+            Ok(v) => v,
+            Err(e) => {
+                eprintln!("Failed parsing requested network configuration: {e}");
+                return Err(Error::ParseFailed("Failed parsing requested network configuration"))
+            }
+        };
+
+    Ok((used_net, network_config))
+}
+
 /// Auxiliary function to parse a base64 encoded transaction from stdin.
 pub async fn parse_tx_from_stdin() -> Result<Transaction> {
     let mut buf = String::new();

+ 8 - 84
bin/drk/src/main.rs

@@ -24,7 +24,7 @@ use std::{
 
 use prettytable::{format, row, Table};
 use rand::rngs::OsRng;
-use smol::{channel::unbounded, fs::read_to_string, stream::StreamExt};
+use smol::{channel::unbounded, stream::StreamExt};
 use structopt_toml::{serde::Deserialize, structopt::StructOpt, StructOptToml};
 use tracing::info;
 use tracing_appender::non_blocking;
@@ -37,7 +37,7 @@ use darkfi::{
         encoding::base64,
         logger::{set_terminal_writer, ChannelWriter},
         parse::{decode_base10, encode_base10},
-        path::{expand_path, get_config_path},
+        path::expand_path,
     },
     zk::halo2::Field,
     Error, Result,
@@ -57,9 +57,9 @@ use darkfi_serial::{deserialize_async, serialize_async};
 
 use drk::{
     cli_util::{
-        display_mining_config, generate_completions, kaching, parse_calls_from_stdin,
-        parse_mining_config_from_stdin, parse_token_pair, parse_tree, parse_tx_from_stdin,
-        parse_value_pair, print_output, tx_from_calls_mapped,
+        display_mining_config, generate_completions, kaching, parse_blockchain_config,
+        parse_calls_from_stdin, parse_mining_config_from_stdin, parse_token_pair, parse_tree,
+        parse_tx_from_stdin, parse_value_pair, print_output, tx_from_calls_mapped,
     },
     common::*,
     dao::{DaoParams, ProposalRecord},
@@ -585,82 +585,6 @@ enum ContractSubcmd {
     },
 }
 
-/// Defines a blockchain network configuration.
-/// Default values correspond to a local network.
-#[derive(Clone, Debug, serde::Deserialize, structopt::StructOpt, structopt_toml::StructOptToml)]
-#[structopt()]
-struct BlockchainNetwork {
-    #[structopt(long, default_value = "~/.local/share/darkfi/drk/localnet/cache")]
-    /// Path to blockchain cache database
-    cache_path: String,
-
-    #[structopt(long, default_value = "~/.local/share/darkfi/drk/localnet/wallet.db")]
-    /// Path to wallet database
-    wallet_path: String,
-
-    #[structopt(long, default_value = "changeme")]
-    /// Password for the wallet database
-    wallet_pass: String,
-
-    #[structopt(short, long, default_value = "tcp://127.0.0.1:28345")]
-    /// darkfid JSON-RPC endpoint
-    endpoint: Url,
-
-    #[structopt(long, default_value = "~/.local/share/darkfi/drk/localnet/history.txt")]
-    /// Path to interactive shell history file
-    history_path: String,
-}
-
-/// Auxiliary function to parse darkfid configuration file and extract requested
-/// blockchain network config.
-async fn parse_blockchain_config(
-    config: Option<String>,
-    network: &str,
-) -> Result<(Network, BlockchainNetwork)> {
-    // Grab network
-    let used_net = match network {
-        "mainnet" | "localnet" => Network::Mainnet,
-        "testnet" => Network::Testnet,
-        _ => return Err(Error::ParseFailed("Invalid blockchain network")),
-    };
-
-    // Grab config path
-    let config_path = get_config_path(config, CONFIG_FILE)?;
-
-    // Parse TOML file contents
-    let contents = read_to_string(&config_path).await?;
-    let contents: toml::Value = match toml::from_str(&contents) {
-        Ok(v) => v,
-        Err(e) => {
-            eprintln!("Failed parsing TOML config: {e}");
-            return Err(Error::ParseFailed("Failed parsing TOML config"))
-        }
-    };
-
-    // Grab requested network config
-    let Some(table) = contents.as_table() else { return Err(Error::ParseFailed("TOML not a map")) };
-    let Some(network_configs) = table.get("network_config") else {
-        return Err(Error::ParseFailed("TOML does not contain network configurations"))
-    };
-    let Some(network_configs) = network_configs.as_table() else {
-        return Err(Error::ParseFailed("`network_config` not a map"))
-    };
-    let Some(network_config) = network_configs.get(network) else {
-        return Err(Error::ParseFailed("TOML does not contain requested network configuration"))
-    };
-    let network_config = toml::to_string(&network_config).unwrap();
-    let network_config =
-        match BlockchainNetwork::from_iter_with_toml::<Vec<String>>(&network_config, vec![]) {
-            Ok(v) => v,
-            Err(e) => {
-                eprintln!("Failed parsing requested network configuration: {e}");
-                return Err(Error::ParseFailed("Failed parsing requested network configuration"))
-            }
-        };
-
-    Ok((used_net, network_config))
-}
-
 /// Auxiliary function to create a `Drk` wallet for provided configuration.
 async fn new_wallet(
     network: Network,
@@ -690,9 +614,9 @@ async_daemonize!(realmain);
 async fn realmain(args: Args, ex: ExecutorPtr) -> Result<()> {
     // Grab blockchain network configuration
     let (network, blockchain_config) = match args.network.as_str() {
-        "localnet" => parse_blockchain_config(args.config, "localnet").await?,
-        "testnet" => parse_blockchain_config(args.config, "testnet").await?,
-        "mainnet" => parse_blockchain_config(args.config, "mainnet").await?,
+        "localnet" => parse_blockchain_config(args.config, "localnet", CONFIG_FILE).await?,
+        "testnet" => parse_blockchain_config(args.config, "testnet", CONFIG_FILE).await?,
+        "mainnet" => parse_blockchain_config(args.config, "mainnet", CONFIG_FILE).await?,
         _ => {
             eprintln!("Unsupported chain `{}`", args.network);
             return Err(Error::UnsupportedChain)