Skip to content

Commit

Permalink
refactor(autonomi): move data/archive/fs modules
Browse files Browse the repository at this point in the history
  • Loading branch information
b-zee committed Dec 6, 2024
1 parent b6571f3 commit f8c4251
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,122 @@
// permissions and limitations relating to use of the SAFE Network Software.

use std::hash::{DefaultHasher, Hash, Hasher};
use std::sync::LazyLock;

use ant_evm::Amount;
use ant_evm::{Amount, EvmWalletError};
use ant_networking::NetworkError;
use ant_protocol::storage::Chunk;
use ant_protocol::NetworkAddress;
use bytes::Bytes;
use serde::{Deserialize, Serialize};
use xor_name::XorName;

use super::data::{GetError, PutError};
use crate::client::payment::PaymentOption;
use crate::client::{ClientEvent, UploadSummary};
use crate::{self_encryption::encrypt, Client};

pub mod public;

/// Number of chunks to upload in parallel.
/// Can be overridden by the `CHUNK_UPLOAD_BATCH_SIZE` environment variable.
pub static CHUNK_UPLOAD_BATCH_SIZE: LazyLock<usize> = LazyLock::new(|| {
let batch_size = std::env::var("CHUNK_UPLOAD_BATCH_SIZE")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
* 8,
);
info!("Chunk upload batch size: {}", batch_size);
batch_size
});

/// Number of retries to upload chunks.
pub const RETRY_ATTEMPTS: usize = 3;

/// Number of chunks to download in parallel.
/// Can be overridden by the `CHUNK_DOWNLOAD_BATCH_SIZE` environment variable.
pub static CHUNK_DOWNLOAD_BATCH_SIZE: LazyLock<usize> = LazyLock::new(|| {
let batch_size = std::env::var("CHUNK_DOWNLOAD_BATCH_SIZE")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
* 8,
);
info!("Chunk download batch size: {}", batch_size);
batch_size
});

/// Raw Data Address (points to a DataMap)
pub type DataAddr = XorName;
/// Raw Chunk Address (points to a [`Chunk`])
pub type ChunkAddr = XorName;

/// Errors that can occur during the put operation.
#[derive(Debug, thiserror::Error)]
pub enum PutError {
#[error("Failed to self-encrypt data.")]
SelfEncryption(#[from] crate::self_encryption::Error),
#[error("A network error occurred.")]
Network(#[from] NetworkError),
#[error("Error occurred during cost estimation.")]
CostError(#[from] CostError),
#[error("Error occurred during payment.")]
PayError(#[from] PayError),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("A wallet error occurred.")]
Wallet(#[from] ant_evm::EvmError),
#[error("The vault owner key does not match the client's public key")]
VaultBadOwner,
#[error("Payment unexpectedly invalid for {0:?}")]
PaymentUnexpectedlyInvalid(NetworkAddress),
}

/// Errors that can occur during the pay operation.
#[derive(Debug, thiserror::Error)]
pub enum PayError {
#[error("Wallet error: {0:?}")]
EvmWalletError(#[from] EvmWalletError),
#[error("Failed to self-encrypt data.")]
SelfEncryption(#[from] crate::self_encryption::Error),
#[error("Cost error: {0:?}")]
Cost(#[from] CostError),
}

/// Errors that can occur during the get operation.
#[derive(Debug, thiserror::Error)]
pub enum GetError {
#[error("Could not deserialize data map.")]
InvalidDataMap(rmp_serde::decode::Error),
#[error("Failed to decrypt data.")]
Decryption(crate::self_encryption::Error),
#[error("Failed to deserialize")]
Deserialization(#[from] rmp_serde::decode::Error),
#[error("General networking error: {0:?}")]
Network(#[from] NetworkError),
#[error("General protocol error: {0:?}")]
Protocol(#[from] ant_protocol::Error),
}

/// Errors that can occur during the cost calculation.
#[derive(Debug, thiserror::Error)]
pub enum CostError {
#[error("Failed to self-encrypt data.")]
SelfEncryption(#[from] crate::self_encryption::Error),
#[error("Could not get store quote for: {0:?} after several retries")]
CouldNotGetStoreQuote(XorName),
#[error("Could not get store costs: {0:?}")]
CouldNotGetStoreCosts(NetworkError),
#[error("Failed to serialize {0}")]
Serialization(String),
}

/// Private data on the network can be accessed with this
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PrivateDataAccess(Chunk);
Expand Down
103 changes: 2 additions & 101 deletions autonomi/src/client/data.rs → autonomi/src/client/data/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,120 +10,21 @@ use bytes::Bytes;
use libp2p::kad::Quorum;

use std::collections::{HashMap, HashSet};
use std::sync::LazyLock;
use xor_name::XorName;

use crate::client::payment::PaymentOption;
use crate::client::utils::process_tasks_with_max_concurrency;
use crate::client::{ClientEvent, UploadSummary};
use crate::{self_encryption::encrypt, Client};
use ant_evm::ProofOfPayment;
use ant_evm::{Amount, AttoTokens};
use ant_evm::{EvmWalletError, ProofOfPayment};
use ant_networking::{GetRecordCfg, NetworkError};
use ant_protocol::{
storage::{try_deserialize_record, Chunk, ChunkAddress, RecordHeader, RecordKind},
NetworkAddress,
};

/// Number of chunks to upload in parallel.
/// Can be overridden by the `CHUNK_UPLOAD_BATCH_SIZE` environment variable.
pub static CHUNK_UPLOAD_BATCH_SIZE: LazyLock<usize> = LazyLock::new(|| {
let batch_size = std::env::var("CHUNK_UPLOAD_BATCH_SIZE")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
* 8,
);
info!("Chunk upload batch size: {}", batch_size);
batch_size
});

/// Number of retries to upload chunks.
pub const RETRY_ATTEMPTS: usize = 3;

/// Number of chunks to download in parallel.
/// Can be overridden by the `CHUNK_DOWNLOAD_BATCH_SIZE` environment variable.
pub static CHUNK_DOWNLOAD_BATCH_SIZE: LazyLock<usize> = LazyLock::new(|| {
let batch_size = std::env::var("CHUNK_DOWNLOAD_BATCH_SIZE")
.ok()
.and_then(|s| s.parse().ok())
.unwrap_or(
std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
* 8,
);
info!("Chunk download batch size: {}", batch_size);
batch_size
});

/// Raw Data Address (points to a DataMap)
pub type DataAddr = XorName;
/// Raw Chunk Address (points to a [`Chunk`])
pub type ChunkAddr = XorName;

/// Errors that can occur during the put operation.
#[derive(Debug, thiserror::Error)]
pub enum PutError {
#[error("Failed to self-encrypt data.")]
SelfEncryption(#[from] crate::self_encryption::Error),
#[error("A network error occurred.")]
Network(#[from] NetworkError),
#[error("Error occurred during cost estimation.")]
CostError(#[from] CostError),
#[error("Error occurred during payment.")]
PayError(#[from] PayError),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("A wallet error occurred.")]
Wallet(#[from] ant_evm::EvmError),
#[error("The vault owner key does not match the client's public key")]
VaultBadOwner,
#[error("Payment unexpectedly invalid for {0:?}")]
PaymentUnexpectedlyInvalid(NetworkAddress),
}

/// Errors that can occur during the pay operation.
#[derive(Debug, thiserror::Error)]
pub enum PayError {
#[error("Wallet error: {0:?}")]
EvmWalletError(#[from] EvmWalletError),
#[error("Failed to self-encrypt data.")]
SelfEncryption(#[from] crate::self_encryption::Error),
#[error("Cost error: {0:?}")]
Cost(#[from] CostError),
}

/// Errors that can occur during the get operation.
#[derive(Debug, thiserror::Error)]
pub enum GetError {
#[error("Could not deserialize data map.")]
InvalidDataMap(rmp_serde::decode::Error),
#[error("Failed to decrypt data.")]
Decryption(crate::self_encryption::Error),
#[error("Failed to deserialize")]
Deserialization(#[from] rmp_serde::decode::Error),
#[error("General networking error: {0:?}")]
Network(#[from] NetworkError),
#[error("General protocol error: {0:?}")]
Protocol(#[from] ant_protocol::Error),
}

/// Errors that can occur during the cost calculation.
#[derive(Debug, thiserror::Error)]
pub enum CostError {
#[error("Failed to self-encrypt data.")]
SelfEncryption(#[from] crate::self_encryption::Error),
#[error("Could not get store quote for: {0:?} after several retries")]
CouldNotGetStoreQuote(XorName),
#[error("Could not get store costs: {0:?}")]
CouldNotGetStoreCosts(NetworkError),
#[error("Failed to serialize {0}")]
Serialization(String),
}
use super::*;

impl Client {
/// Fetch a blob of data from the network
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ use std::{

use ant_networking::target_arch::{Duration, SystemTime, UNIX_EPOCH};

use super::{
data::{CostError, DataAddr, GetError, PutError},
Client,
};
use ant_evm::{AttoTokens, EvmWallet};
use bytes::Bytes;
use serde::{Deserialize, Serialize};
Expand All @@ -27,6 +23,11 @@ pub type ArchiveAddr = XorName;

use thiserror::Error;

use crate::{
client::data::{CostError, DataAddr, GetError, PutError},
Client,
};

#[derive(Error, Debug, PartialEq, Eq)]
pub enum RenameError {
#[error("File not found in archive: {0}")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ use std::{

use ant_networking::target_arch::{Duration, SystemTime, UNIX_EPOCH};

use super::{
archive::{Metadata, RenameError},
data::{GetError, PutError},
data_private::PrivateDataAccess,
use super::archive::{Metadata, RenameError};
use crate::{
client::{
data::{GetError, PrivateDataAccess, PutError},
payment::PaymentOption,
},
Client,
};
use crate::client::payment::PaymentOption;
use bytes::Bytes;
use serde::{Deserialize, Serialize};

Expand Down
6 changes: 2 additions & 4 deletions autonomi/src/client/fs.rs → autonomi/src/client/files/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::client::archive::Metadata;
use crate::client::data::CostError;
use crate::client::data::{CostError, DataAddr, GetError, PutError};
use crate::client::utils::process_tasks_with_max_concurrency;
use crate::client::Client;
use ant_evm::EvmWallet;
Expand All @@ -16,8 +15,7 @@ use bytes::Bytes;
use std::path::PathBuf;
use std::sync::LazyLock;

use super::archive::{Archive, ArchiveAddr};
use super::data::{DataAddr, GetError, PutError};
use super::archive::{Archive, ArchiveAddr, Metadata};

/// Number of files to upload in parallel.
/// Can be overridden by the `FILE_UPLOAD_BATCH_SIZE` environment variable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::client::data::PrivateDataAccess;
use crate::client::utils::process_tasks_with_max_concurrency;
use crate::client::Client;
use ant_evm::EvmWallet;
use bytes::Bytes;
use std::path::PathBuf;

use super::archive_private::{PrivateArchive, PrivateArchiveAccess};
use super::data_private::PrivateDataAccess;
use super::fs::{DownloadError, UploadError};

use super::fs::FILE_UPLOAD_BATCH_SIZE;
Expand Down
8 changes: 8 additions & 0 deletions autonomi/src/client/files/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pub mod archive;
pub mod archive_private;
#[cfg(feature = "fs")]
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub mod fs;
#[cfg(feature = "fs")]
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub mod fs_private;
10 changes: 1 addition & 9 deletions autonomi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,11 @@
pub mod address;
pub mod payment;

pub mod archive;
pub mod archive_private;
pub mod data;
pub mod data_private;
#[cfg(feature = "external-signer")]
#[cfg_attr(docsrs, doc(cfg(feature = "external-signer")))]
pub mod external_signer;
#[cfg(feature = "fs")]
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub mod fs;
#[cfg(feature = "fs")]
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
pub mod fs_private;
pub mod files;
#[cfg(feature = "registers")]
#[cfg_attr(docsrs, doc(cfg(feature = "registers")))]
pub mod registers;
Expand Down
4 changes: 2 additions & 2 deletions autonomi/src/client/vault/user_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

use std::collections::HashMap;

use crate::client::archive::ArchiveAddr;
use crate::client::archive_private::PrivateArchiveAccess;
use crate::client::data::GetError;
use crate::client::data::PutError;
use crate::client::files::archive::ArchiveAddr;
use crate::client::files::archive_private::PrivateArchiveAccess;
use crate::client::payment::PaymentOption;
use crate::client::registers::RegisterAddress;
use crate::client::vault::VaultError;
Expand Down
5 changes: 2 additions & 3 deletions autonomi/src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
#![allow(non_local_definitions)]

use crate::client::{
archive::ArchiveAddr,
archive_private::PrivateArchiveAccess,
data_private::PrivateDataAccess,
data::PrivateDataAccess,
files::{archive::ArchiveAddr, archive_private::PrivateArchiveAccess},
payment::PaymentOption as RustPaymentOption,
vault::{UserData, VaultSecretKey},
Client as RustClient,
Expand Down
Loading

0 comments on commit f8c4251

Please sign in to comment.