Skip to content

Commit

Permalink
refactor(autonomi): add docs; expose types; local
Browse files Browse the repository at this point in the history
Use `local` if feature is enabled
  • Loading branch information
b-zee committed Dec 16, 2024
1 parent fe1e7c0 commit 02f20ab
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 18 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ jobs:

- name: Run autonomi tests
timeout-minutes: 25
run: cargo test --release --package autonomi --features full --lib
run: cargo test --release --package autonomi --features full,local --lib

- name: Run autonomi doc tests
timeout-minutes: 25
run: cargo test --release --package autonomi --features full --doc
run: cargo test --release --package autonomi --features full,local --doc

- name: Run bootstrap tests
timeout-minutes: 25
Expand Down
6 changes: 5 additions & 1 deletion autonomi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ repository = "https://github.com/maidsafe/autonomi"
name = "autonomi"
crate-type = ["cdylib", "rlib"]

[[example]]
name = "data_and_archive"
required-features = ["full"]

[[example]]
name = "put_and_dir_upload"
features = ["full"]
required-features = ["full"]

[features]
default = ["vault"]
Expand Down
37 changes: 37 additions & 0 deletions autonomi/examples/data_and_archive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use autonomi::{Bytes, Client, Metadata, PrivateArchive};
use test_utils::evm::get_funded_wallet;
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

#[tokio::main]
async fn main() -> eyre::Result<()> {
tracing_subscriber::registry()
.with(fmt::layer())
.with(EnvFilter::from_env("RUST_LOG"))
.init();

let client = Client::init().await?;
let wallet = get_funded_wallet();

// Upload 10MiB of random data and verify it by fetching it back.
let data = Bytes::from("Hello, World!");
let data_map = client.data_put(data.clone(), (&wallet).into()).await?;
let data_fetched = client.data_get(data_map.clone()).await?;
assert_eq!(data, data_fetched);

// Upload the data as part of an archive, giving it the name `test.txt`.
let mut archive = PrivateArchive::new();
archive.add_file(
"test.txt".into(),
data_map,
Metadata::new_with_size(data.len() as u64),
);

// Upload the archive to the network.
let archive_data_map = client.archive_put(&archive, (&wallet).into()).await?;
let archive_fetched = client.archive_get(archive_data_map).await?;
assert_eq!(archive, archive_fetched);

println!("Archive uploaded successfully");

Ok(())
}
14 changes: 9 additions & 5 deletions autonomi/examples/put_and_dir_upload.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use autonomi::{Bytes, Client, Wallet};
use autonomi::{Bytes, Client};
use test_utils::evm::get_funded_wallet;
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Default wallet of testnet.
let key = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
tracing_subscriber::registry()
.with(fmt::layer())
.with(EnvFilter::from_env("RUST_LOG"))
.init();

let client = Client::init_local().await?;
let wallet = Wallet::new_from_private_key(Default::default(), key)?;
let client = Client::init().await?;
let wallet = get_funded_wallet();

// Put and fetch data.
let data_addr = client
Expand Down
29 changes: 29 additions & 0 deletions autonomi/src/client/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,19 @@ fn hash_to_short_string(input: &str) -> String {

impl Client {
/// Fetch a blob of (private) data from the network
///
/// # Example
///
/// ```no_run
/// use autonomi::{Client, Bytes};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = Client::connect(&[]).await?;
/// # let data_map = todo!();
/// let data_fetched = client.data_get(data_map).await?;
/// # Ok(())
/// # }
/// ```
pub async fn data_get(&self, data_map: DataMapChunk) -> Result<Bytes, GetError> {
info!(
"Fetching private data from Data Map {:?}",
Expand All @@ -175,6 +188,22 @@ impl Client {
/// The [`DataMapChunk`] is not uploaded to the network, keeping the data private.
///
/// Returns the [`DataMapChunk`] containing the map to the encrypted chunks.
///
/// # Example
///
/// ```no_run
/// use autonomi::{Client, Bytes};
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let client = Client::connect(&[]).await?;
/// # let wallet = todo!();
/// let data = Bytes::from("Hello, World");
/// let data_map = client.data_put(data, wallet).await?;
/// let data_fetched = client.data_get(data_map).await?;
/// assert_eq!(data, data_fetched);
/// # Ok(())
/// # }
/// ```
pub async fn data_put(
&self,
data: Bytes,
Expand Down
2 changes: 1 addition & 1 deletion autonomi/src/client/files/archive_public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl Client {
/// # let wallet = todo!();
/// let mut archive = PublicArchive::new();
/// archive.add_file(PathBuf::from("file.txt"), DataAddr::random(&mut rand::thread_rng()), Metadata::new_with_size(0));
/// let address = client.archive_put_public(archive, &wallet).await?;
/// let address = client.archive_put_public(&archive, &wallet).await?;
/// # Ok(())
/// # }
/// ```
Expand Down
17 changes: 15 additions & 2 deletions autonomi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ mod utils;

use ant_bootstrap::{BootstrapCacheConfig, BootstrapCacheStore, PeersArgs};
pub use ant_evm::Amount;

use ant_evm::EvmNetwork;
use ant_networking::{interval, multiaddr_is_global, Network, NetworkBuilder, NetworkEvent};
use ant_protocol::version::IDENTIFY_PROTOCOL_STR;
Expand Down Expand Up @@ -74,9 +73,11 @@ pub struct Client {
}

/// Configuration for [`Client::init_with_config`].
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct ClientConfig {
/// Whether we're expected to connect to a local network.
///
/// If `local` feature is enabled, [`ClientConfig::default()`] will set this to `true`.
pub local: bool,

/// List of peers to connect to.
Expand All @@ -85,6 +86,18 @@ pub struct ClientConfig {
pub peers: Option<Vec<Multiaddr>>,
}

impl Default for ClientConfig {
fn default() -> Self {
Self {
#[cfg(feature = "local")]
local: true,
#[cfg(not(feature = "local"))]
local: false,
peers: None,
}
}
}

/// Error returned by [`Client::connect`].
#[derive(Debug, thiserror::Error)]
pub enum ConnectError {
Expand Down
2 changes: 1 addition & 1 deletion autonomi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub use bytes::Bytes;
pub use libp2p::Multiaddr;

#[doc(inline)]
pub use client::{files::archive::PrivateArchive, Client, ClientConfig};
pub use client::{files::archive::Metadata, files::archive::PrivateArchive, Client, ClientConfig};

#[cfg(feature = "extension-module")]
mod python;
7 changes: 1 addition & 6 deletions autonomi/tests/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,7 @@ async fn file_into_vault() -> Result<()> {
let archive = client.archive_get_public(addr).await?;
let set_version = 0;
client
.write_bytes_to_vault(
archive.into_bytes()?,
wallet.into(),
&client_sk,
set_version,
)
.write_bytes_to_vault(archive.to_bytes()?, wallet.into(), &client_sk, set_version)
.await?;

// now assert over the stored account packet
Expand Down

0 comments on commit 02f20ab

Please sign in to comment.