Skip to content

Commit

Permalink
feat: add more logs to autonomi
Browse files Browse the repository at this point in the history
  • Loading branch information
ermineJose committed Nov 20, 2024
1 parent 4f8c5ba commit 54345a7
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 26 deletions.
11 changes: 9 additions & 2 deletions autonomi/src/client/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ impl Archive {
.as_secs();
meta.modified = now;
self.map.insert(new_path.to_path_buf(), (data_addr, meta));
debug!("File renamed successfully in the archive");
Ok(())
}

/// Add a file to a local archive
/// Note that this does not upload the archive to the network
pub fn add_file(&mut self, path: PathBuf, data_addr: DataAddr, meta: Metadata) {
self.map.insert(path, (data_addr, meta));
debug!("Added file successfully to the archive");
}

/// List all files in the archive
Expand Down Expand Up @@ -148,6 +150,7 @@ impl Client {
/// Fetch an archive from the network
pub async fn archive_get(&self, addr: ArchiveAddr) -> Result<Archive, GetError> {
let data = self.data_get(addr).await?;
debug!("Fetched archive from network");
Ok(Archive::from_bytes(data)?)
}

Expand All @@ -160,14 +163,18 @@ impl Client {
let bytes = archive
.into_bytes()
.map_err(|e| PutError::Serialization(format!("Failed to serialize archive: {e:?}")))?;
self.data_put(bytes, wallet.into()).await
let result_archive = self.data_put(bytes, wallet.into()).await;
debug!("Uploaded archive to network");
result_archive
}

/// Get the cost to upload an archive
pub async fn archive_cost(&self, archive: Archive) -> Result<AttoTokens, CostError> {
let bytes = archive
.into_bytes()
.map_err(|e| CostError::Serialization(format!("Failed to serialize archive: {e:?}")))?;
self.data_cost(bytes).await
let result_cost = self.data_cost(bytes).await;
debug!("Got cost to upload archive");
result_cost
}
}
9 changes: 7 additions & 2 deletions autonomi/src/client/archive_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ impl PrivateArchive {
.as_secs();
meta.modified = now;
self.map.insert(new_path.to_path_buf(), (data_addr, meta));
debug!("File renamed successfully in the archive");
Ok(())
}

/// Add a file to a local archive
/// Note that this does not upload the archive to the network
pub fn add_file(&mut self, path: PathBuf, data_map: PrivateDataAccess, meta: Metadata) {
self.map.insert(path, (data_map, meta));
debug!("Added file successfully to the archive");
}

/// List all files in the archive
Expand Down Expand Up @@ -117,6 +119,7 @@ impl Client {
addr: PrivateArchiveAccess,
) -> Result<PrivateArchive, GetError> {
let data = self.private_data_get(addr).await?;
debug!("Private archive successfully fetched from the network");
Ok(PrivateArchive::from_bytes(data)?)
}

Expand All @@ -129,6 +132,8 @@ impl Client {
let bytes = archive
.into_bytes()
.map_err(|e| PutError::Serialization(format!("Failed to serialize archive: {e:?}")))?;
self.private_data_put(bytes, payment_option).await
let private_archive_access_result = self.private_data_put(bytes, payment_option).await;
debug!("Private archive successfully uploaded to the network");
private_archive_access_result
}
}
}
14 changes: 11 additions & 3 deletions autonomi/src/client/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl Client {
let data = self
.fetch_from_data_map_chunk(data_map_chunk.value())
.await?;

debug!("Successfully fetched a blob of data from the network");
Ok(data)
}

Expand Down Expand Up @@ -176,6 +176,7 @@ impl Client {
.await
.inspect_err(|err| error!("Error uploading chunk {address:?} :{err:?}"))
});
debug!("Finished preparing upload tasks");
} else {
debug!("Chunk at {address:?} was already paid for so skipping");
}
Expand Down Expand Up @@ -205,6 +206,7 @@ impl Client {
record_count,
tokens_spent,
};
debug!("Sending upload summary: {:?}", summary);
if let Err(err) = channel.send(ClientEvent::UploadComplete(summary)).await {
error!("Failed to send client event: {err:?}");
}
Expand All @@ -218,26 +220,31 @@ impl Client {
info!("Getting chunk: {addr:?}");

let key = NetworkAddress::from_chunk_address(ChunkAddress::new(addr)).to_record_key();

debug!("Converted chunk address to network record key: {:?}", key);

let get_cfg = GetRecordCfg {
get_quorum: Quorum::One,
retry_strategy: None,
target_record: None,
expected_holders: HashSet::new(),
is_register: false,
};
debug!("GetRecordCfg created: {:?}", get_cfg);

let record = self
.network
.get_record_from_network(key, &get_cfg)
.await
.inspect_err(|err| error!("Error fetching chunk: {err:?}"))?;
debug!("Record fetched from network: {:?}", record);
let header = RecordHeader::from_record(&record)?;

debug!("Record header created: {:?}", header);
if let RecordKind::Chunk = header.kind {
let chunk: Chunk = try_deserialize_record(&record)?;
debug!("Chunk deserialized successfully: {:?}", chunk);
Ok(chunk)
} else {
error!("Record kind mismatch: expected Chunk, got {:?}", header.kind);
Err(NetworkError::RecordKindMismatch(RecordKind::Chunk).into())
}
}
Expand Down Expand Up @@ -271,6 +278,7 @@ impl Client {
.map(|quote| quote.2.cost.as_atto())
.sum::<Amount>(),
);
debug!("Total cost calculated: {:?}", total_cost);
Ok(total_cost)
}
}
2 changes: 1 addition & 1 deletion autonomi/src/client/data_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Client {
data_map.0.address()
);
let data = self.fetch_from_data_map_chunk(data_map.0.value()).await?;

debug!("Successfully fetched private data");
Ok(data)
}

Expand Down
2 changes: 1 addition & 1 deletion autonomi/src/client/external_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Client {
let cost_map = self.get_store_quotes(content_addrs).await?;
let (quote_payments, free_chunks) = extract_quote_payments(&cost_map);
let quotes = cost_map_to_quotes(cost_map);

debug!("Got the quotes for data chunks from the network");
Ok((quotes, quote_payments, free_chunks))
}
}
Expand Down
12 changes: 10 additions & 2 deletions autonomi/src/client/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::client::Client;
use bytes::Bytes;
use sn_evm::EvmWallet;
use sn_networking::target_arch::{Duration, SystemTime};
// use tracing_subscriber::field::debug;
use std::path::PathBuf;
use std::sync::LazyLock;

Expand Down Expand Up @@ -89,8 +90,10 @@ impl Client {
let data = self.data_get(data_addr).await?;
if let Some(parent) = to_dest.parent() {
tokio::fs::create_dir_all(parent).await?;
debug!("Created parent directories for {to_dest:?}");
}
tokio::fs::write(to_dest, data).await?;
tokio::fs::write(to_dest.clone(), data).await?;
debug!("Downloaded file to {to_dest:?}");
Ok(())
}

Expand All @@ -104,6 +107,7 @@ impl Client {
for (path, addr, _meta) in archive.iter() {
self.file_download(*addr, to_dest.join(path)).await?;
}
debug!("Downloaded directory to {to_dest:?}");
Ok(())
}

Expand Down Expand Up @@ -176,6 +180,7 @@ impl Client {
let data = tokio::fs::read(path).await?;
let data = Bytes::from(data);
let addr = self.data_put(data, wallet.into()).await?;
debug!("Uploaded file successfully");
Ok(addr)
}

Expand Down Expand Up @@ -217,6 +222,7 @@ impl Client {
let archive_cost = self.data_cost(Bytes::from(root_serialized)).await?;

total_cost += archive_cost.as_atto();
debug!("Total cost has been computed successfully {:?}", total_cost);
Ok(total_cost.into())
}
}
Expand All @@ -240,6 +246,8 @@ pub(crate) fn metadata_from_entry(entry: &walkdir::DirEntry) -> Metadata {
}
};

debug!("Processing the metadata for the entry");

let unix_time = |property: &'static str, time: std::io::Result<SystemTime>| {
time.inspect_err(|err| {
tracing::warn!(
Expand All @@ -260,7 +268,7 @@ pub(crate) fn metadata_from_entry(entry: &walkdir::DirEntry) -> Metadata {
};
let created = unix_time("created", fs_metadata.created());
let modified = unix_time("modified", fs_metadata.modified());

debug!("successfully computed the metadata for the entry");
Metadata {
uploaded: SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
Expand Down
6 changes: 5 additions & 1 deletion autonomi/src/client/fs_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ impl Client {
let data = self.private_data_get(data_access).await?;
if let Some(parent) = to_dest.parent() {
tokio::fs::create_dir_all(parent).await?;
debug!("Created parent directories for {to_dest:?}");
}
tokio::fs::write(to_dest, data).await?;
tokio::fs::write(to_dest.clone(), data).await?;
debug!("Downloaded private file to {to_dest:?}");
Ok(())
}

Expand All @@ -52,6 +54,7 @@ impl Client {
self.private_file_download(addr.clone(), to_dest.join(path))
.await?;
}
debug!("Downloaded private directory to {to_dest:?}");
Ok(())
}

Expand Down Expand Up @@ -129,6 +132,7 @@ impl Client {
let data = tokio::fs::read(path).await?;
let data = Bytes::from(data);
let addr = self.private_data_put(data, wallet.into()).await?;
debug!("Private file uploaded to the network successfully");
Ok(addr)
}
}
7 changes: 4 additions & 3 deletions autonomi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Client {
let local = !peers.iter().any(multiaddr_is_global);

let (network, event_receiver) = build_client_and_run_swarm(local);

debug!("Client is built and swarm driver initiated");
// Spawn task to dial to the given peers
let network_clone = network.clone();
let peers = peers.to_vec();
Expand All @@ -115,7 +115,7 @@ impl Client {
sn_networking::target_arch::spawn(handle_event_receiver(event_receiver, sender));

receiver.await.expect("sender should not close")?;

debug!("Client is connected to the Network");
Ok(Self {
network,
client_event_sender: Arc::new(None),
Expand All @@ -127,6 +127,7 @@ impl Client {
let (client_event_sender, client_event_receiver) =
tokio::sync::mpsc::channel(CLIENT_EVENT_CHANNEL_SIZE);
self.client_event_sender = Arc::new(Some(client_event_sender));
debug!("All events to the clients are enabled");
client_event_receiver
}
}
Expand All @@ -140,7 +141,7 @@ fn build_client_and_run_swarm(local: bool) -> (Network, mpsc::Receiver<NetworkEv
network_builder.build_client().expect("mdns to succeed");

let _swarm_driver = sn_networking::target_arch::spawn(swarm_driver.run());

debug!("Network, event_reciever and swarm driver are built and initiated");
(network, event_receiver)
}

Expand Down
14 changes: 8 additions & 6 deletions autonomi/src/client/registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl Register {
if let Some(value) = initial_value {
register.write_atop(&value, &owner)?;
}

debug!("Instantiated a new register");
Ok(register)
}

Expand Down Expand Up @@ -170,7 +170,7 @@ impl Client {
return Err(RegisterError::Write(err));
}
}

debug!("Fetched register from network");
Ok(Register {
signed_reg,
crdt_reg,
Expand Down Expand Up @@ -222,7 +222,7 @@ impl Client {
register.address()
)
})?;

debug!("Register value is updated on the network");
Ok(())
}

Expand All @@ -249,7 +249,7 @@ impl Client {
.map(|quote| quote.2.cost.as_atto())
.sum::<Amount>(),
);

debug!("Register cost is successfully computed");
Ok(total_cost)
}

Expand All @@ -273,8 +273,10 @@ impl Client {
let pk = owner.public_key();
let permissions = Permissions::new_with([pk]);

self.register_create_with_permissions(value, name, owner, permissions, wallet)
.await
let register_created =
self.register_create_with_permissions(value, name, owner, permissions, wallet).await;
debug!("Register created successfully");
register_created
}

/// Creates a new Register with a name and an initial value and uploads it to the network.
Expand Down
Loading

0 comments on commit 54345a7

Please sign in to comment.