Skip to content

Commit

Permalink
opt: update deps + reduce data .clone()
Browse files Browse the repository at this point in the history
  • Loading branch information
canewsin committed Jan 30, 2024
1 parent 1a3f048 commit ac2f51b
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 70 deletions.
30 changes: 22 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ version = "1.17.0"

[dependencies.zeronet_cryptography]
git = "https://github.com/decentnetwork/zeronet_cryptography.git"
branch = "zeronetx"

[dependencies.zeronet_protocol]
git = "https://github.com/decentnetwork/zeronet_protocol.git"
branch = "decentnet"

[dependencies.zerucontent]
git = "https://github.com/decentnetwork/zerucontent.git"
branch = "decentnet"

[dependencies.decentnet_protocol]
git = "https://github.com/decentnetwork/decentnet_protocol.git"
Expand Down
6 changes: 3 additions & 3 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub async fn site_sign(site: &mut Site, private_key: String) -> Result<(), Error
info!("No changes to sign");
} else {
let content = {
let mut content = site.content(None).unwrap();
let mut content = site.content(None).unwrap().clone();
let mut files = content.files;
for (inner_path, file) in changes {
if files.insert(inner_path, file).is_none() {
Expand Down Expand Up @@ -145,7 +145,7 @@ pub async fn site_need_file(site: &mut Site, inner_path: String) -> Result<(), E
} else {
site.load_content().await?;
let content = site.content(None).unwrap();
let files = content.files;
let files = &content.files;
let files_res = files.keys().any(|path| path == &inner_path);
let includes_res = content.includes.keys().any(|path| path == &inner_path);
let users_res = content
Expand Down Expand Up @@ -199,7 +199,7 @@ pub async fn peer_exchange(site: &mut Site) -> Result<(), Error> {

pub async fn fetch_changes(site: &mut Site) -> Result<(), Error> {
site.load_content().await?;
let modified = site.content(None).unwrap().modified;
let modified = &site.content(None).unwrap().modified;
info!("{:?}", modified);
let changes = site.fetch_changes(1421043090).await?;
info!("{:#?}", changes);
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ impl ConnectionController {
true
} else {
let site_content_modified =
site.content(Some(inner_path)).unwrap().modified;
site.content(Some(inner_path)).unwrap().modified.clone();
content_modified > site_content_modified.into()
}
}
Expand Down
20 changes: 12 additions & 8 deletions src/controllers/sites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub async fn run() -> Result<Addr<SitesController>, Error> {
.extend_sites_from_sitedata(site_storage.clone())
.await;
for site in site_storage.keys().clone() {
if let Some(site) = site_controller.get_site(site) {
site_controller.get(site.addr())?;
if let Some(addr) = site_controller.get_site_addr(site).cloned() {
site_controller.get(&addr)?;
}
}
let site_controller_addr = site_controller.start();
Expand Down Expand Up @@ -58,7 +58,7 @@ impl SitesController {
}
}

pub fn get(&mut self, address: Address) -> Result<(Address, Addr<Site>), Error> {
pub fn get(&mut self, address: &Address) -> Result<(Address, Addr<Site>), Error> {
let address_str = address.address.clone();
let mut site;
let site = if let Some(site) = self.sites.get_mut(&address_str) {
Expand All @@ -69,7 +69,7 @@ impl SitesController {
};
if let Some(addr) = self.sites_addr.get(&address) {

Check warning on line 70 in src/controllers/sites.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/controllers/sites.rs:70:49 | 70 | if let Some(addr) = self.sites_addr.get(&address) { | ^^^^^^^^ help: change this to: `address` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
if site.content_path().is_file() {
return Ok((address, addr.clone()));
return Ok((address.clone(), addr.clone()));
}
}
trace!(
Expand Down Expand Up @@ -99,7 +99,7 @@ impl SitesController {
// TODO: Decide whether to spawn actors in syncArbiter
let addr = site.clone().start();
self.sites_addr.insert(address.clone(), addr.clone());
Ok((address, addr))
Ok((address.clone(), addr))
}

pub fn get_by_key(&mut self, key: String) -> Result<(Address, Addr<Site>), Error> {
Expand All @@ -113,14 +113,18 @@ impl SitesController {
}

pub fn add_site(&mut self, site: Site) {
self.sites.insert(site.address(), site);
self.sites.insert(site.address().into(), site);
self.update_sites_changed();
}

pub fn get_site(&self, site_addr: &str) -> Option<&Site> {
self.sites.get(site_addr)
}

pub fn get_site_addr(&self, site_addr: &str) -> Option<&Address> {
self.sites.get(site_addr).map(|site| site.addr())
}

pub fn get_site_mut(&mut self, site_addr: &str) -> Option<&mut Site> {
self.sites.get_mut(site_addr)
}
Expand All @@ -140,9 +144,9 @@ impl SitesController {
if res.is_ok() {
self.sites.insert(address, site.clone());
self.nonce
.insert(site_storage.keys.wrapper_key, site.addr());
.insert(site_storage.keys.wrapper_key, site.addr().clone());
self.ajax_keys
.insert(site_storage.keys.ajax_key, site.addr());
.insert(site_storage.keys.ajax_key, site.addr().clone());
} else {
//TODO! Start Downloading Site Content
error!(
Expand Down
11 changes: 5 additions & 6 deletions src/core/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ impl Site {
})
}

pub fn addr(&self) -> Addr {
self.address.clone()
pub fn addr(&self) -> &Addr {
&self.address
}

pub fn address(&self) -> String {
self.address.address.clone()
pub fn address(&self) -> &str {
&self.address.address
}

pub fn content_exists(&self) -> bool {
Expand All @@ -133,10 +133,9 @@ impl Site {
self.content.contains_key(inner_path)
}

pub fn content(&self, inner_path: Option<&str>) -> Option<Content> {
pub fn content(&self, inner_path: Option<&str>) -> Option<&Content> {
self.content
.get(inner_path.unwrap_or("content.json"))
.cloned()
}

pub fn content_mut(&mut self, inner_path: Option<&str>) -> Option<&mut Content> {
Expand Down
6 changes: 3 additions & 3 deletions src/io/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl ContentMod for Site {
) -> Result<(), Error> {
let content = self.content_mut(inner_path).unwrap();
content.modified = current_unix_epoch().into();
let sign = content.sign(private_key.to_string());
let sign = content.sign(private_key);
let address = zeronet_cryptography::privkey_to_pubkey(private_key)?;
content.signs.insert(address, sign);
Ok(())
Expand All @@ -62,7 +62,7 @@ impl ContentMod for Site {
.keys()
.into_iter()

Check warning on line 63 in src/io/content.rs

View workflow job for this annotation

GitHub Actions / clippy

useless conversion to the same type: `std::collections::btree_map::Keys<'_, std::string::String, std::string::String>`

warning: useless conversion to the same type: `std::collections::btree_map::Keys<'_, std::string::String, std::string::String>` --> src/io/content.rs:60:24 | 60 | let verified = content | ________________________^ 61 | | .signs 62 | | .keys() 63 | | .into_iter() | |________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion = note: `#[warn(clippy::useless_conversion)]` on by default help: consider removing `.into_iter()` | 60 ~ let verified = content 61 + .signs 62 + .keys() |
.find_map(|key| {
if content.verify(key.to_string()) {
if content.verify(key) {
Some(true)
} else {
None
Expand Down Expand Up @@ -196,7 +196,7 @@ impl Site {
}
if content.user_contents.is_some() {
if let Value::Object(mut user_contents) =
json!(content.user_contents.unwrap())
json!(content.user_contents.as_ref().unwrap())
{
map.append(&mut user_contents);
} else {
Expand Down
39 changes: 20 additions & 19 deletions src/io/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
impl Site {
pub async fn create(&mut self, addr_idx: u32, private_key: &str) -> Result<(), Error> {
let mut content = Content::create(self.address(), addr_idx);
content.zeronet_version = ENV.version.clone();
content.meta.zeronet_version = Some(ENV.version.clone());
content.signs_required = 1;
content.signers_sign =
zeronet_cryptography::sign(format!("1:{}", self.address()), private_key)?;
Expand Down Expand Up @@ -71,7 +71,7 @@ impl Site {
let message = Protocol::new(peer.connection_mut().unwrap())
.get_file(
self.address(),
inner_path.clone(),
&inner_path,
file_size,
downloaded,
Some(def_read_bytes),
Expand Down Expand Up @@ -101,7 +101,7 @@ impl Site {
Ok(bytes)
} else {
let message = Protocol::new(peer.connection_mut().unwrap())
.get_file(self.address(), inner_path.clone(), file_size, 0, None)
.get_file(self.address(), &inner_path, file_size, 0, None)
.await;
if let Err(e) = &message {
let err = format!(
Expand Down Expand Up @@ -202,18 +202,19 @@ impl Site {
}

async fn download_site_files(&self) -> Result<(), Error> {
let files = self.content(None).unwrap().files;
let content = self.content(None).unwrap();
let files = content.files.clone();
let mut tasks = Vec::new();
let mut inner_paths = Vec::new();
for (inner_path, file) in files {
inner_paths.push(inner_path.clone());
let task = self.download_file(inner_path, Some(file), None);
tasks.push(task);
}
let includes = self.content(None).unwrap().includes;
let includes = &content.includes;
for (inner_path, _file) in includes {

Check warning on line 215 in src/io/site.rs

View workflow job for this annotation

GitHub Actions / clippy

you seem to want to iterate on a map's keys

warning: you seem to want to iterate on a map's keys --> src/io/site.rs:215:36 | 215 | for (inner_path, _file) in includes { | ^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#for_kv_map = note: `#[warn(clippy::for_kv_map)]` on by default help: use the corresponding method | 215 | for inner_path in includes.keys() { | ~~~~~~~~~~ ~~~~~~~~~~~~~~~
inner_paths.push(inner_path.clone());
let task = self.download_file(inner_path, None, None);
let task = self.download_file(inner_path.clone(), None, None);
tasks.push(task);
}
//TODO!: Other client may not have an up-to-date site files
Expand Down Expand Up @@ -248,7 +249,7 @@ impl Site {
let mut files = vec![];
content_res.iter_mut().for_each(|content| {
let content = content.as_ref().unwrap();
let path = Path::new(&content.inner_path);
let path = Path::new(&content.meta.inner_path);
if let Some(parent) = path.parent() {
let files_inner = content.files.clone();
for (path, file) in files_inner {
Expand Down Expand Up @@ -297,7 +298,7 @@ impl Site {
}
let content = self.content(None).unwrap();
//TODO! Verify inner content also
let verified = content.verify(self.address());
let verified = content.verify(&self.address());

Check warning on line 301 in src/io/site.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/io/site.rs:301:43 | 301 | let verified = content.verify(&self.address()); | ^^^^^^^^^^^^^^^ help: change this to: `self.address()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
if !verified {
Err(Error::Err(format!(
"Content verification failed for {}",
Expand All @@ -311,13 +312,13 @@ impl Site {

pub async fn check_site_integrity(&self) -> Result<Vec<(String, zerucontent::File)>, Error> {
let content = self.content(None).unwrap();
let files = content.files;
let files = &content.files;
let mut tasks = Vec::new();
for (inner_path, file) in files {
let hash = file.sha512.clone();
let hash = &file.sha512;
let (site_path, inner_path) = if !PATH_PROVIDER_PLUGINS.read().unwrap().is_empty() {
let path = get_storage_path().into();
(path, hash.clone())
(path, hash)
} else {
(self.site_path(), inner_path)
};
Expand All @@ -342,7 +343,7 @@ impl Site {
if *v {
None
} else {
Some((i.clone(), h.clone()))
Some((i.to_string(), h.clone()))
}
} else {
unreachable!()
Expand Down Expand Up @@ -381,10 +382,10 @@ impl Site {
}

pub async fn fetch_peers(&mut self) -> Result<Vec<String>, Error> {
let addr = self.address().clone();
let addr = self.address();
let mut peer = self.peers.values().next().unwrap().clone();
let res = Protocol::new((peer.connection_mut()).unwrap())
.pex(addr.clone())
.pex(addr)
.await?
.peers
.iter()
Expand All @@ -397,15 +398,15 @@ impl Site {
}

pub async fn update(&mut self, inner_path: &str, diff: Option<HashMap<String, Vec<Value>>>) {
let addr = self.address();
let addr = self.address().to_string();
let path = self.site_path().join(inner_path);
let modified = self.content(None).unwrap().modified;
let modified = (&self.content(None).unwrap().modified).clone();

Check warning on line 403 in src/io/site.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression borrows a value the compiler would automatically borrow

warning: this expression borrows a value the compiler would automatically borrow --> src/io/site.rs:403:24 | 403 | let modified = (&self.content(None).unwrap().modified).clone(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: change this to: `self.content(None).unwrap().modified` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
let peer = self.peers.values_mut().next().unwrap();
let content = fs::read(path).await.unwrap();
let res = Protocol::new(peer.connection_mut().unwrap())
.update(
addr,
inner_path.to_owned(),
&addr,
inner_path,
ByteBuf::from(content),
diff.unwrap_or_default(),
modified.into(),
Expand Down Expand Up @@ -498,7 +499,7 @@ impl SiteIO for Site {
remove_file(file_path).await?;
let file_path = ENV.data_path.join("sites.json");
let mut file = File::create(&file_path).await?;
let mut sites: HashMap<String, serde_json::Value> = serde_json::from_str(&content)?;
let mut sites: HashMap<&str, serde_json::Value> = serde_json::from_str(&content)?;
sites.insert(self.address(), serde_json::to_value(storage)?);
let bytes = serde_json::to_vec_pretty(&sites)?;
let len = file.write(&bytes).await?;
Expand Down
Loading

0 comments on commit ac2f51b

Please sign in to comment.