Skip to content

Commit

Permalink
handling of the on-the-fly network change
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Jan 17, 2024
1 parent 85f8d0d commit 7b31fd4
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 76 deletions.
46 changes: 31 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,37 @@ egui-notify = "=0.11.0"
# kaspa-wrpc-server = { path = "../rusty-kaspa/rpc/wrpc/server" }
# kaspad = { path = "../rusty-kaspa/kaspad" }

kaspa-addresses = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
kaspa-alloc = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
kaspa-bip32 = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
kaspa-cli = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
kaspa-consensus-core = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
kaspa-core = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
kaspa-metrics-core = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
kaspa-notify = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
kaspa-rpc-core = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
kaspa-rpc-service = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
kaspa-utils = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
kaspa-wallet-core = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master", features=["no-unsafe-eval"] }
kaspa-wrpc-client = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master", features=["no-unsafe-eval"] }
kaspa-wrpc-server = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
kaspad = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
# kaspa-addresses = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
# kaspa-alloc = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
# kaspa-bip32 = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
# kaspa-cli = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
# kaspa-consensus-core = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
# kaspa-core = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
# kaspa-metrics-core = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
# kaspa-notify = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
# kaspa-rpc-core = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
# kaspa-rpc-service = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
# kaspa-utils = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
# kaspa-wallet-core = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master", features=["no-unsafe-eval"] }
# kaspa-wrpc-client = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master", features=["no-unsafe-eval"] }
# kaspa-wrpc-server = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }
# kaspad = { git = "https://github.com/kaspanet/rusty-kaspa.git", branch = "master" }

kaspa-addresses = { git = "https://github.com/aspectron/rusty-kaspa.git", branch = "dev" }
kaspa-alloc = { git = "https://github.com/aspectron/rusty-kaspa.git", branch = "dev" }
kaspa-bip32 = { git = "https://github.com/aspectron/rusty-kaspa.git", branch = "dev" }
kaspa-cli = { git = "https://github.com/aspectron/rusty-kaspa.git", branch = "dev" }
kaspa-consensus-core = { git = "https://github.com/aspectron/rusty-kaspa.git", branch = "dev" }
kaspa-core = { git = "https://github.com/aspectron/rusty-kaspa.git", branch = "dev" }
kaspa-metrics-core = { git = "https://github.com/aspectron/rusty-kaspa.git", branch = "dev" }
kaspa-notify = { git = "https://github.com/aspectron/rusty-kaspa.git", branch = "dev" }
kaspa-rpc-core = { git = "https://github.com/aspectron/rusty-kaspa.git", branch = "dev" }
kaspa-rpc-service = { git = "https://github.com/aspectron/rusty-kaspa.git", branch = "dev" }
kaspa-utils = { git = "https://github.com/aspectron/rusty-kaspa.git", branch = "dev" }
kaspa-wallet-core = { git = "https://github.com/aspectron/rusty-kaspa.git", branch = "dev", features=["no-unsafe-eval"] }
kaspa-wrpc-client = { git = "https://github.com/aspectron/rusty-kaspa.git", branch = "dev", features=["no-unsafe-eval"] }
kaspa-wrpc-server = { git = "https://github.com/aspectron/rusty-kaspa.git", branch = "dev" }
kaspad = { git = "https://github.com/aspectron/rusty-kaspa.git", branch = "dev" }

# _________________________________________________________
# _ _ _ ____ ____ _ _ ____ _ ____ _ _ _ ____ ____
Expand Down
104 changes: 68 additions & 36 deletions core/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ impl Core {
.unwrap();
}

pub fn network(&self) -> Network {
self.settings.node.network
}

pub fn wallet(&self) -> &Arc<dyn WalletApi> {
&self.wallet
}
Expand Down Expand Up @@ -626,7 +630,13 @@ impl Core {
_ctx: &egui::Context,
_frame: &mut eframe::Frame,
) -> Result<()> {
// println!("event: {:?}", event);
match event {
Events::NetworkChange(network) => {
self.modules.clone().values().for_each(|module| {
module.network_change(self, network);
});
}
Events::UpdateStorage(_options) => {
#[cfg(not(target_arch = "wasm32"))]
self.storage
Expand Down Expand Up @@ -692,6 +702,18 @@ impl Core {
self.wallet_list = (*wallet_list).clone();
self.wallet_list.sort();
}
Events::WalletUpdate => {
if let Some(account_collection) = self.account_collection.as_ref() {
let mut account_manager = self
.modules
.get(&TypeId::of::<modules::AccountManager>())
.unwrap()
.clone();
account_manager
.get_mut::<modules::AccountManager>()
.update(account_collection);
}
}
Events::Notify {
user_notification: notification,
} => {
Expand All @@ -710,13 +732,22 @@ impl Core {
self.prv_key_data_map = Some(prv_key_data_info_map);
}
Events::Wallet { event } => {
// println!("event: {:?}", event);
match *event {
CoreWallet::Error { message } => {
// runtime().notify(UserNotification::error(message.as_str()));
println!("{message}");
}
CoreWallet::UtxoProcStart => {
self.state.error = None;

if self.state().is_open() {
let wallet = self.wallet().clone();
spawn(async move {
wallet.wallet_reload(false).await?;
Ok(())
});
}
}
CoreWallet::UtxoProcStop => {}
CoreWallet::UtxoProcError { message } => {
Expand All @@ -733,7 +764,9 @@ impl Core {
self.state.url = url;
self.state.network_id = Some(network_id);

self.module.clone().connect(self);
self.modules.clone().values().for_each(|module| {
module.connect(self, Network::from(network_id));
});
}
#[allow(unused_variables)]
CoreWallet::Disconnect {
Expand All @@ -751,7 +784,9 @@ impl Core {
self.metrics = None;
self.network_pressure.clear();

self.module.clone().disconnect(self);
self.modules.clone().values().for_each(|module| {
module.disconnect(self);
});
}
CoreWallet::UtxoIndexNotEnabled { url } => {
self.exception = Some(Exception::UtxoIndexNotEnabled { url });
Expand All @@ -774,28 +809,33 @@ impl Core {
self.hint = hint;
self.discard_hint = false;
}
// TODO remove reloading notification
CoreWallet::WalletReload { .. } => {}
CoreWallet::WalletReload {
wallet_descriptor,
account_descriptors,
} => {
self.wallet_descriptor = wallet_descriptor;
let network_id = self
.state
.network_id
.unwrap_or(self.settings.node.network.into());
let account_descriptors =
account_descriptors.ok_or(Error::WalletOpenAccountDescriptors)?;
self.load_accounts(network_id, account_descriptors)?;
}
CoreWallet::WalletOpen {
wallet_descriptor,
account_descriptors,
// }
// | CoreWallet::WalletReload {
// wallet_descriptor,
// account_descriptors,
} => {
self.state.is_open = true;

self.wallet_descriptor = wallet_descriptor;
// let network_id = self.state.network_id.ok_or(Error::WalletOpenNetworkId)?;
let network_id = self
.state
.network_id
.unwrap_or(self.settings.node.network.into());
let account_descriptors =
account_descriptors.ok_or(Error::WalletOpenAccountDescriptors)?;
self.load_accounts(network_id, account_descriptors)?;
// self.update_account_list();
}
CoreWallet::WalletCreate {
wallet_descriptor,
Expand All @@ -821,23 +861,7 @@ impl Core {
CoreWallet::AccountActivation { ids: _ } => {}
CoreWallet::AccountCreate {
account_descriptor: _,
} => {
// let account = Account::from(account_descriptor);
// self.account_collection
// .as_mut()
// .expect("account collection")
// .push_unchecked(account.clone());
// let device = self.device().clone();
// self.get_mut::<modules::AccountManager>()
// .select(Some(account.clone()), device);
// // self.select::<modules::AccountManager>();

// let wallet = self.wallet().clone();
// spawn(async move {
// wallet.accounts_activate(Some(vec![account.id()])).await?;
// Ok(())
// });
}
} => {}
CoreWallet::AccountUpdate { account_descriptor } => {
let account_id = account_descriptor.account_id();
if let Some(account_collection) = self.account_collection.as_ref() {
Expand Down Expand Up @@ -976,11 +1000,16 @@ impl Core {
network_id: NetworkId,
account_descriptors: Vec<AccountDescriptor>,
) -> Result<()> {
let network = Network::from(network_id);
if self.network() != network {
return Err(Error::InvalidNetwork(network.to_string()));
}

let application_events_sender = self.application_events_channel.sender.clone();

let account_list = account_descriptors
.into_iter()
.map(Account::from)
.map(|account_descriptor| Account::from(network, account_descriptor))
.collect::<Vec<_>>();

self.account_collection = Some(account_list.clone().into());
Expand Down Expand Up @@ -1019,7 +1048,7 @@ impl Core {
.map(|account_id| {
runtime
.wallet()
.transactions_data_get_range(account_id, network_id, 0..4096)
.transactions_data_get_range(account_id, network_id, 0..8192)
})
.collect::<Vec<_>>();

Expand Down Expand Up @@ -1048,6 +1077,8 @@ impl Core {

runtime.wallet().accounts_activate(None).await?;

application_events_sender.send(Events::WalletUpdate).await?;

Ok(())
});

Expand All @@ -1058,28 +1089,29 @@ impl Core {
&mut self,
account_descriptors: Vec<AccountDescriptor>,
) -> Vec<Account> {
let network = self.network();

let accounts = account_descriptors
.into_iter()
.map(Account::from)
.collect::<Vec<_>>(); //(account_descriptor);
.map(|account_descriptor| Account::from(network, account_descriptor))
.collect::<Vec<_>>();

self.account_collection
.as_mut()
.expect("account collection")
.extend_unchecked(accounts.clone());
// .push_unchecked(account.clone());

if let Some(first) = accounts.first() {
let device = self.device().clone();
self.get_mut::<modules::AccountManager>()
.select(Some(first.clone()), device);
}
// self.get_mut::<modules::AccountManager>()
// .select(Some(account.clone()), device);
// self.select::<modules::AccountManager>();

let account_ids = accounts
.iter()
.map(|account| account.id())
.collect::<Vec<_>>();
// let account_id = account.id();

let wallet = self.wallet().clone();
spawn(async move {
wallet.accounts_activate(Some(account_ids)).await?;
Expand Down
2 changes: 2 additions & 0 deletions core/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub type ApplicationEventsChannel = crate::runtime::channel::Channel<Events>;

#[derive(Clone, Debug)]
pub enum Events {
NetworkChange(Network),
UpdateStorage(StorageUpdateOptions),
VisibilityChange(VisibilityState),
VersionUpdate(Release),
Expand All @@ -29,6 +30,7 @@ pub enum Events {
Wallet {
event: Box<kaspa::Events>,
},
WalletUpdate,
PrvKeyDataInfo {
prv_key_data_info_map: HashMap<PrvKeyDataId, Arc<PrvKeyDataInfo>>,
},
Expand Down
2 changes: 2 additions & 0 deletions core/src/modules/account_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ impl AccountCreate {
}

impl ModuleT for AccountCreate {

fn modal(&self) -> bool { true }

fn style(&self) -> ModuleStyle {
ModuleStyle::Mobile
}
Expand Down
37 changes: 31 additions & 6 deletions core/src/modules/account_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub struct ManagerContext {
focus : FocusManager<Focus>,
wallet_secret : String,
payment_secret : String,
loading : bool,
}

impl ManagerContext {
Expand Down Expand Up @@ -218,12 +219,11 @@ impl ModuleT for AccountManager {
true
}

// fn reload(&mut self, core : &mut Core) {
// if let AccountManagerState::Overview { account } = self.state.clone() {
// let account_id = account.id();
// core.account_collection().get()
// }
// }
fn network_change(&mut self, _core: &mut Core, _network : Network) {
if let AccountManagerState::Overview { .. } = self.state.clone() {
self.context.loading = true;
}
}

fn render(
&mut self,
Expand Down Expand Up @@ -258,8 +258,21 @@ impl AccountManager {
}
} else {
self.state = AccountManagerState::Select;
self.context.loading = false;
}

}

pub fn update(&mut self, account_collection : &AccountCollection) {
if let AccountManagerState::Overview { account } = self.state.clone() {
if let Some(updated_account) = account_collection.get(&account.id()) {
self.state = AccountManagerState::Overview { account : updated_account.clone() };
} else {
self.state = AccountManagerState::Select;
}
}

self.context.loading = false;
}

pub fn section(&mut self, section : AccountManagerSection) {
Expand Down Expand Up @@ -343,6 +356,18 @@ impl AccountManager {
}

AccountManagerState::Overview { account } => {

if self.context.loading {
Panel::new(self)
.with_caption(i18n("Updating..."))
.with_body(|_this, ui| {
ui.add_space(64.);
ui.add(egui::Spinner::new().size(92.));
}).render(ui);

return Ok(());
}

let rc = RenderContext::new(&account, network_type, current_daa_score)?;
if core.device().single_pane() {
self.render_singular_layout(core,ui,&rc, self.section);
Expand Down
Loading

0 comments on commit 7b31fd4

Please sign in to comment.