Skip to content

Commit

Permalink
wasm: fix dump_to_idb lifetime issue
Browse files Browse the repository at this point in the history
error[E0521]: borrowed data escapes outside of closure
  --> network/src/services/dump_peer_store.rs:43:24
   |
42 |         self.network_state.with_peer_store_mut(|peer_store| {
   |                                                 ----------
   |                                                 |
   |                                                 `peer_store` is a reference that is only valid in the closure body
   |                                                 has type `&'1 mut PeerStore`
43 |             let task = peer_store.dump_to_idb(path);
   |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |                        |
   |                        `peer_store` escapes the closure body here
   |                        argument requires that `'1` must outlive `'static`

Signed-off-by: Eval EXEC <execvy@gmail.com>
  • Loading branch information
eval-exec committed Mar 5, 2025
1 parent 55035c8 commit 258dc84
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
1 change: 1 addition & 0 deletions network/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,7 @@ pub enum TransportType {
Wss,
}

#[allow(dead_code)]
pub(crate) fn find_type(addr: &Multiaddr) -> TransportType {
let mut iter = addr.iter();

Expand Down
1 change: 1 addition & 0 deletions network/src/peer_store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod types;
pub(crate) use crate::Behaviour;
pub use crate::SessionType;
use p2p::multiaddr::{Multiaddr, Protocol};
pub use peer_store_db::dump_to_idb;
pub use peer_store_impl::PeerStore;
pub(crate) use peer_store_impl::required_flags_filter;

Expand Down
48 changes: 26 additions & 22 deletions network/src/peer_store/peer_store_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,30 +194,34 @@ impl PeerStore {
move_file(tmp_ban_list, path.as_ref().join(DEFAULT_BAN_LIST_DB))?;
Ok(())
}
}

#[cfg(target_family = "wasm")]
pub fn dump_to_idb<P: AsRef<Path>>(&self, path: P) -> impl std::future::Future<Output = ()> {
use crate::peer_store::browser::get_db;
let ban_list = self.ban_list().dump_data();
let addr_manager = self.addr_manager().dump_data();
let addr_manager_path = path
.as_ref()
.join(DEFAULT_ADDR_MANAGER_DB)
.to_str()
.unwrap()
.to_owned();
let ban_list_path = path
.as_ref()
.join(DEFAULT_BAN_LIST_DB)
.to_str()
.unwrap()
.to_owned();
async {
let db = get_db(path).await;
#[cfg(target_family = "wasm")]
pub fn dump_to_idb<P: AsRef<Path>>(
ban_list: Vec<u8>,
addr_manager: Vec<u8>,
path: P,
) -> impl std::future::Future<Output = ()> {
use crate::peer_store::browser::get_db;
// let ban_list = self.ban_list().dump_data();
// let addr_manager = self.addr_manager().dump_data();
let addr_manager_path = path
.as_ref()
.join(DEFAULT_ADDR_MANAGER_DB)
.to_str()
.unwrap()
.to_owned();
let ban_list_path = path
.as_ref()
.join(DEFAULT_BAN_LIST_DB)
.to_str()
.unwrap()
.to_owned();
async {
let db = get_db(path).await;

let _ignore = db.put(addr_manager_path.into_bytes(), addr_manager).await;
let _ignore = db.put(ban_list_path.into_bytes(), ban_list).await;
}
let _ignore = db.put(addr_manager_path.into_bytes(), addr_manager).await;
let _ignore = db.put(ban_list_path.into_bytes(), ban_list).await;
}
}

Expand Down
4 changes: 3 additions & 1 deletion network/src/services/dump_peer_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ impl DumpPeerStoreService {
fn dump_peer_store(&self) {
let path = self.network_state.config.peer_store_path();
self.network_state.with_peer_store_mut(|peer_store| {
let task = peer_store.dump_to_idb(path);
let ban_list = peer_store.ban_list().dump_data();
let addr_manager = peer_store.addr_manager().dump_data();
let task = crate::peer_store::dump_to_idb(ban_list, addr_manager, path);
p2p::runtime::spawn(task)
});
}
Expand Down

0 comments on commit 258dc84

Please sign in to comment.