Skip to content

Commit

Permalink
refactor: custom_base_node in config instead of being stored in database
Browse files Browse the repository at this point in the history
  • Loading branch information
StriderDM committed Dec 8, 2021
1 parent 2adf81f commit c615b5c
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 164 deletions.
4 changes: 4 additions & 0 deletions applications/tari_app_utilities/src/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ pub fn init_configuration(
global_config.grpc_console_wallet_address = grpc_address;
}

if let Some(str) = bootstrap.custom_base_node.clone() {
global_config.wallet_custom_base_node = Some(str);
}

check_file_paths(&mut global_config, &bootstrap);

Ok((bootstrap, global_config, cfg))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ impl Display for ParsedCommand {
ExportSpentUtxos => "export-spent-utxos",
CountUtxos => "count-utxos",
SetBaseNode => "set-base-node",
SetCustomBaseNode => "set-custom-base-node",
ClearCustomBaseNode => "clear-custom-base-node",
InitShaAtomicSwap => "init-sha-atomic-swap",
FinaliseShaAtomicSwap => "finalise-sha-atomic-swap",
ClaimShaAtomicSwapRefund => "claim-sha-atomic-swap-refund",
Expand Down Expand Up @@ -128,8 +126,6 @@ pub fn parse_command(command: &str) -> Result<ParsedCommand, ParseError> {
ExportSpentUtxos => parse_export_spent_utxos(args)?, // todo: only show X number of utxos
CountUtxos => Vec::new(),
SetBaseNode => parse_public_key_and_address(args)?,
SetCustomBaseNode => parse_public_key_and_address(args)?,
ClearCustomBaseNode => Vec::new(),
InitShaAtomicSwap => parse_init_sha_atomic_swap(args)?,
FinaliseShaAtomicSwap => parse_finalise_sha_atomic_swap(args)?,
ClaimShaAtomicSwapRefund => parse_claim_htlc_refund_refund(args)?,
Expand Down
32 changes: 2 additions & 30 deletions applications/tari_console_wallet/src/automation/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ use tokio::{
};

use super::error::CommandError;
use crate::{
automation::command_parser::{ParsedArgument, ParsedCommand},
utils::db::{CUSTOM_BASE_NODE_ADDRESS_KEY, CUSTOM_BASE_NODE_PUBLIC_KEY_KEY},
};
use crate::automation::command_parser::{ParsedArgument, ParsedCommand};

pub const LOG_TARGET: &str = "wallet::automation::commands";

Expand All @@ -86,8 +83,6 @@ pub enum WalletCommand {
ExportSpentUtxos,
CountUtxos,
SetBaseNode,
SetCustomBaseNode,
ClearCustomBaseNode,
InitShaAtomicSwap,
FinaliseShaAtomicSwap,
ClaimShaAtomicSwapRefund,
Expand Down Expand Up @@ -289,6 +284,7 @@ async fn wait_for_comms(connectivity_requester: &ConnectivityRequester) -> Resul
}
}
}

async fn set_base_node_peer(
mut wallet: WalletSqlite,
args: &[ParsedArgument],
Expand All @@ -308,7 +304,6 @@ async fn set_base_node_peer(
wallet
.set_base_node_peer(public_key.clone(), net_address.to_string())
.await?;

Ok((public_key, net_address))
}

Expand Down Expand Up @@ -744,29 +739,6 @@ pub async fn command_runner(
SetBaseNode => {
set_base_node_peer(wallet.clone(), &parsed.args).await?;
},
SetCustomBaseNode => {
let (public_key, net_address) = set_base_node_peer(wallet.clone(), &parsed.args).await?;
wallet
.db
.set_client_key_value(CUSTOM_BASE_NODE_PUBLIC_KEY_KEY.to_string(), public_key.to_string())
.await?;
wallet
.db
.set_client_key_value(CUSTOM_BASE_NODE_ADDRESS_KEY.to_string(), net_address.to_string())
.await?;
println!("Custom base node peer saved in wallet database.");
},
ClearCustomBaseNode => {
wallet
.db
.clear_client_value(CUSTOM_BASE_NODE_PUBLIC_KEY_KEY.to_string())
.await?;
wallet
.db
.clear_client_value(CUSTOM_BASE_NODE_ADDRESS_KEY.to_string())
.await?;
println!("Custom base node peer cleared from wallet database.");
},
InitShaAtomicSwap => {
let (tx_id, pre_image, output) =
init_sha_atomic_swap(transaction_service.clone(), parsed.clone().args).await?;
Expand Down
22 changes: 13 additions & 9 deletions applications/tari_console_wallet/src/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ use tari_wallet::{
WalletSqlite,
};

use crate::{
utils::db::get_custom_base_node_peer_from_db,
wallet_modes::{PeerConfig, WalletMode},
};
use crate::wallet_modes::{PeerConfig, WalletMode};

pub const LOG_TARGET: &str = "wallet::console_wallet::init";
/// The minimum buffer size for a tari application pubsub_connector channel
Expand Down Expand Up @@ -148,12 +145,19 @@ pub async fn change_password(
/// 1. The custom peer in the wallet if it exists
/// 2. The service peers defined in config they exist
/// 3. The peer seeds defined in config
pub async fn get_base_node_peer_config(
config: &GlobalConfig,
wallet: &mut WalletSqlite,
) -> Result<PeerConfig, ExitCodes> {
pub async fn get_base_node_peer_config(config: &GlobalConfig) -> Result<PeerConfig, ExitCodes> {
// custom
let base_node_custom = get_custom_base_node_peer_from_db(wallet).await;
let mut base_node_custom = None;
if let Some(custom) = config.wallet_custom_base_node.clone() {
match SeedPeer::from_str(&custom) {
Ok(node) => {
base_node_custom = Some(Peer::from(node));
},
Err(err) => {
return Err(ExitCodes::ConfigError(format!("Malformed custom base node: {}", err)));
},
}
}

// config
let base_node_peers = config
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_console_wallet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ fn main_inner() -> Result<(), ExitCodes> {
}

// get base node/s
let base_node_config = runtime.block_on(get_base_node_peer_config(&global_config, &mut wallet))?;
let base_node_config = runtime.block_on(get_base_node_peer_config(&global_config))?;
let base_node_selected = base_node_config.get_base_node_peer()?;

let wallet_mode = wallet_mode(&bootstrap, boot_mode);
Expand Down
23 changes: 0 additions & 23 deletions applications/tari_console_wallet/src/ui/state/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ use crate::{
UiContact,
UiError,
},
utils::db::{CUSTOM_BASE_NODE_ADDRESS_KEY, CUSTOM_BASE_NODE_PUBLIC_KEY_KEY},
wallet_modes::PeerConfig,
};

Expand Down Expand Up @@ -792,19 +791,6 @@ impl AppStateInner {
.insert(0, ("Custom Base Node".to_string(), peer.clone()));
self.updated = true;

// persist the custom node in wallet db
self.wallet
.db
.set_client_key_value(CUSTOM_BASE_NODE_PUBLIC_KEY_KEY.to_string(), peer.public_key.to_string())
.await?;
self.wallet
.db
.set_client_key_value(
CUSTOM_BASE_NODE_ADDRESS_KEY.to_string(),
peer.addresses.first().ok_or(UiError::NoAddress)?.to_string(),
)
.await?;

info!(
target: LOG_TARGET,
"Setting custom base node peer for wallet: {}::{}",
Expand All @@ -831,15 +817,6 @@ impl AppStateInner {
self.data.base_node_list.remove(0);
self.updated = true;

// clear from wallet db
self.wallet
.db
.clear_client_value(CUSTOM_BASE_NODE_PUBLIC_KEY_KEY.to_string())
.await?;
self.wallet
.db
.clear_client_value(CUSTOM_BASE_NODE_ADDRESS_KEY.to_string())
.await?;
Ok(())
}

Expand Down
90 changes: 0 additions & 90 deletions applications/tari_console_wallet/src/utils/db.rs

This file was deleted.

1 change: 0 additions & 1 deletion applications/tari_console_wallet/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

pub mod crossterm_events;
pub mod db;
pub mod events;
pub mod formatting;

Expand Down
8 changes: 2 additions & 6 deletions applications/tari_console_wallet/src/wallet_modes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use crate::{
recovery::wallet_recovery,
ui,
ui::App,
utils::db::get_custom_base_node_peer_from_db,
};

pub const LOG_TARGET: &str = "wallet::app::main";
Expand Down Expand Up @@ -224,7 +223,7 @@ fn wallet_or_exit(config: WalletModeConfig, wallet: WalletSqlite) -> Result<(),

pub fn tui_mode(config: WalletModeConfig, mut wallet: WalletSqlite) -> Result<(), ExitCodes> {
let WalletModeConfig {
mut base_node_config,
base_node_config,
mut base_node_selected,
global_config,
handle,
Expand All @@ -236,10 +235,7 @@ pub fn tui_mode(config: WalletModeConfig, mut wallet: WalletSqlite) -> Result<()

let notifier = Notifier::new(notify_script, handle.clone(), wallet.clone());

// update the selected/custom base node since it may have been changed by script/command mode
let base_node_custom = handle.block_on(get_custom_base_node_peer_from_db(&mut wallet));
base_node_config.base_node_custom = base_node_custom.clone();
if let Some(peer) = base_node_custom {
if let Some(peer) = base_node_config.base_node_custom.clone() {
base_node_selected = peer;
} else if let Some(peer) = handle.block_on(wallet.get_base_node_peer()) {
base_node_selected = peer;
Expand Down
3 changes: 3 additions & 0 deletions common/src/configuration/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ pub struct ConfigBootstrap {
/// Metrics push endpoint (prometheus push)
#[structopt(long)]
pub metrics_push_endpoint: Option<String>,
#[structopt(long, alias = "custom_base_node")]
pub custom_base_node: Option<String>,
}

fn normalize_path(path: PathBuf) -> PathBuf {
Expand Down Expand Up @@ -203,6 +205,7 @@ impl Default for ConfigBootstrap {
wallet_grpc_address: None,
metrics_server_bind_addr: None,
metrics_push_endpoint: None,
custom_base_node: None,
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions common/src/configuration/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub struct GlobalConfig {
pub wallet_command_send_wait_stage: String,
pub wallet_command_send_wait_timeout: u64,
pub wallet_base_node_service_peers: Vec<String>,
pub wallet_custom_base_node: Option<String>,
pub wallet_base_node_service_refresh_interval: u64,
pub wallet_base_node_service_request_max_age: u64,
pub wallet_balance_enquiry_cooldown_period: u64,
Expand Down Expand Up @@ -526,6 +527,13 @@ fn convert_node_config(
},
};

let key = config_string("wallet", net_str, "custom_base_node");
let custom_peer: Option<String> = match cfg.get_int(&key) {
Ok(peer) => Some(peer.to_string()),
Err(ConfigError::NotFound(_)) => None,
Err(e) => return Err(ConfigurationError::new(&key, &e.to_string())),
};

let key = "wallet.password";
let console_wallet_password = optional(cfg.get_str(key))?;

Expand Down Expand Up @@ -795,6 +803,7 @@ fn convert_node_config(
wallet_command_send_wait_stage,
wallet_command_send_wait_timeout,
wallet_base_node_service_peers,
wallet_custom_base_node: custom_peer,
wallet_base_node_service_refresh_interval,
wallet_base_node_service_request_max_age,
wallet_balance_enquiry_cooldown_period,
Expand Down

0 comments on commit c615b5c

Please sign in to comment.