stratum: wallet selection, wallet api port saving, fix stratum config saving, modules optimization

This commit is contained in:
ardocrat
2024-08-01 01:59:51 +03:00
parent c64477e870
commit 38bb68736b
24 changed files with 396 additions and 188 deletions
+15 -1
View File
@@ -18,6 +18,7 @@ use std::string::ToString;
use grin_core::global::ChainTypes;
use grin_wallet_libwallet::{Slate};
use rand::Rng;
use serde_derive::{Deserialize, Serialize};
use crate::{AppConfig, Settings};
@@ -41,7 +42,9 @@ pub struct WalletConfig {
/// Flag to use Dandelion to broadcast transactions.
pub use_dandelion: Option<bool>,
/// Flag to enable Tor listener on start.
pub enable_tor_listener: Option<bool>
pub enable_tor_listener: Option<bool>,
/// Wallet API port.
pub api_port: Option<u16>,
}
/// Base wallets directory name.
@@ -77,6 +80,7 @@ impl WalletConfig {
min_confirmations: MIN_CONFIRMATIONS_DEFAULT,
use_dandelion: Some(true),
enable_tor_listener: Some(true),
api_port: Some(rand::thread_rng().gen_range(10000..30000)),
};
Settings::write_to_file(&config, config_path);
config
@@ -102,6 +106,16 @@ impl WalletConfig {
None
}
/// Get wallet API port by provided identifier.
pub fn api_port_by_id(id: i64) -> Option<u16> {
let mut wallet_dir = WalletConfig::get_base_path(AppConfig::chain_type());
wallet_dir.push(id.to_string());
if let Some(cfg) = Self::load(wallet_dir) {
return cfg.api_port;
}
None
}
/// Save wallet config.
pub fn save(&self) {
let config_path = Self::get_config_file_path(self.chain_type, self.id);
+9 -8
View File
@@ -24,7 +24,6 @@ use std::thread::Thread;
use std::time::Duration;
use futures::channel::oneshot;
use serde_json::{json, Value};
use rand::Rng;
use grin_api::{ApiServer, Router};
use grin_chain::SyncStatus;
@@ -42,6 +41,7 @@ use grin_wallet_impls::{DefaultLCProvider, DefaultWalletImpl, HTTPNodeClient};
use grin_wallet_libwallet::{address, Error, InitTxArgs, IssueInvoiceTxArgs, NodeClient, RetrieveTxQueryArgs, RetrieveTxQuerySortField, RetrieveTxQuerySortOrder, Slate, SlatepackAddress, SlateState, SlateVersion, StatusMessage, TxLogEntry, TxLogEntryType, VersionedSlate, WalletInst, WalletLCProvider};
use grin_wallet_libwallet::api_impl::owner::{cancel_tx, retrieve_summary_info, retrieve_txs};
use grin_wallet_util::OnionV3Address;
use rand::Rng;
use crate::AppConfig;
use crate::node::{Node, NodeConfig};
@@ -1450,18 +1450,19 @@ fn sync_wallet_data(wallet: &Wallet, from_node: bool) {
/// Start Foreign API server to receive txs over transport and mining rewards.
fn start_api_server(wallet: &Wallet) -> Result<(ApiServer, u16), Error> {
let host = "127.0.0.1";
// Find free port.
let port = if wallet.get_config().chain_type == ChainTypes::Mainnet {
rand::thread_rng().gen_range(37000..40000)
} else {
rand::thread_rng().gen_range(47000..50000)
};
let port = wallet.get_config().api_port.unwrap_or(rand::thread_rng().gen_range(10000..30000));
let free_port = (port..).find(|port| {
return match TcpListener::bind((host, port.to_owned())) {
Ok(_) => {
let node_p2p_port = NodeConfig::get_p2p_port();
let node_api_port = NodeConfig::get_api_ip_port().1;
port.to_string() != node_p2p_port && port.to_string() != node_api_port
let free = port.to_string() != node_p2p_port && port.to_string() != node_api_port;
if free {
let mut config = wallet.config.write();
config.api_port = Some(*port);
config.save();
}
free
},
Err(_) => false
}