Skip to content

Commit

Permalink
feat(client): implement ClientOperationCfg to control any client oper…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
RolandSherwin committed Jan 23, 2025
1 parent 8ad5a4f commit 33b97f4
Show file tree
Hide file tree
Showing 24 changed files with 781 additions and 320 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

7 changes: 6 additions & 1 deletion ant-cli/src/actions/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::network::NetworkPeers;
use autonomi::client::config::ClientOperationConfig;
use autonomi::{get_evm_network, Client, ClientConfig};
use color_eyre::eyre::bail;
use color_eyre::eyre::Result;
use indicatif::ProgressBar;
use std::time::Duration;

pub async fn connect_to_network(peers: NetworkPeers) -> Result<Client> {
pub async fn connect_to_network(
peers: NetworkPeers,
client_operation_config: ClientOperationConfig,
) -> Result<Client> {
let progress_bar = ProgressBar::new_spinner();
progress_bar.enable_steady_tick(Duration::from_millis(120));
progress_bar.set_message("Connecting to The Autonomi Network...");
Expand All @@ -36,6 +40,7 @@ pub async fn connect_to_network(peers: NetworkPeers) -> Result<Client> {
local,
peers: peers_opt,
evm_network,
operation_config: client_operation_config,
};

let res = Client::init_with_config(config).await;
Expand Down
19 changes: 15 additions & 4 deletions ant-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod vault;
mod wallet;

use crate::opt::Opt;
use autonomi::ResponseQuorum;
use clap::{error::ErrorKind, CommandFactory as _, Subcommand};
use color_eyre::Result;

Expand Down Expand Up @@ -50,6 +51,8 @@ pub enum FileCmd {
/// Upload the file as public. Everyone can see public data on the Network.
#[arg(short, long)]
public: bool,
/// Optionally specify the quorum for the verification of the upload.
verification_quorum: Option<ResponseQuorum>,
},

/// Download a file from the given address.
Expand All @@ -58,6 +61,8 @@ pub enum FileCmd {
addr: String,
/// The destination file path.
dest_file: String,
/// Optionally specify the quorum for the verification of the download.
read_quorum: Option<ResponseQuorum>,
},

/// List previous uploads
Expand Down Expand Up @@ -127,10 +132,16 @@ pub async fn handle_subcommand(opt: Opt) -> Result<()> {
match cmd {
Some(SubCmd::File { command }) => match command {
FileCmd::Cost { file } => file::cost(&file, peers.await?).await,
FileCmd::Upload { file, public } => file::upload(&file, public, peers.await?).await,
FileCmd::Download { addr, dest_file } => {
file::download(&addr, &dest_file, peers.await?).await
}
FileCmd::Upload {
file,
public,
verification_quorum,
} => file::upload(&file, public, peers.await?, verification_quorum).await,
FileCmd::Download {
addr,
dest_file,
read_quorum,
} => file::download(&addr, &dest_file, peers.await?, read_quorum).await,
FileCmd::List => file::list(),
},
Some(SubCmd::Vault { command }) => match command {
Expand Down
34 changes: 28 additions & 6 deletions ant-cli/src/commands/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ use crate::network::NetworkPeers;
use crate::utils::collect_upload_summary;
use crate::wallet::load_wallet;
use autonomi::client::address::addr_to_str;
use autonomi::client::config::ClientOperationConfig;
use autonomi::ResponseQuorum;
use color_eyre::eyre::Context;
use color_eyre::eyre::Result;
use color_eyre::Section;
use std::path::PathBuf;

pub async fn cost(file: &str, peers: NetworkPeers) -> Result<()> {
let client = crate::actions::connect_to_network(peers).await?;
let client = crate::actions::connect_to_network(peers, Default::default()).await?;

println!("Getting upload cost...");
info!("Calculating cost for file: {file}");
Expand All @@ -31,9 +33,19 @@ pub async fn cost(file: &str, peers: NetworkPeers) -> Result<()> {
Ok(())
}

pub async fn upload(file: &str, public: bool, peers: NetworkPeers) -> Result<()> {
let mut client = crate::actions::connect_to_network(peers).await?;
let wallet = load_wallet(&client.evm_network)?;
pub async fn upload(
file: &str,
public: bool,
peers: NetworkPeers,
verification_quorum: Option<ResponseQuorum>,
) -> Result<()> {
let mut client_operation_config = ClientOperationConfig::default();
if let Some(verification_quorum) = verification_quorum {
client_operation_config.chunk_verification_quorum(verification_quorum);
}
let mut client = crate::actions::connect_to_network(peers, client_operation_config).await?;

let wallet = load_wallet(client.get_evm_network())?;
let event_receiver = client.enable_client_events();
let (upload_summary_thread, upload_completed_tx) = collect_upload_summary(event_receiver);

Expand Down Expand Up @@ -105,8 +117,18 @@ pub async fn upload(file: &str, public: bool, peers: NetworkPeers) -> Result<()>
Ok(())
}

pub async fn download(addr: &str, dest_path: &str, peers: NetworkPeers) -> Result<()> {
let mut client = crate::actions::connect_to_network(peers).await?;
pub async fn download(
addr: &str,
dest_path: &str,
peers: NetworkPeers,
read_quorum: Option<ResponseQuorum>,
) -> Result<()> {
let mut client_operation_config = ClientOperationConfig::default();
if let Some(read_quorum) = read_quorum {
client_operation_config.chunk_read_quorum(read_quorum);
}
let mut client = crate::actions::connect_to_network(peers, client_operation_config).await?;

crate::actions::download(addr, dest_path, &mut client).await
}

Expand Down
12 changes: 6 additions & 6 deletions ant-cli/src/commands/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use color_eyre::eyre::Result;
use color_eyre::Section;

pub async fn cost(peers: NetworkPeers) -> Result<()> {
let client = crate::actions::connect_to_network(peers).await?;
let client = crate::actions::connect_to_network(peers, Default::default()).await?;
let vault_sk = crate::keys::get_vault_secret_key()?;

println!("Getting cost to create a new vault...");
Expand All @@ -28,8 +28,8 @@ pub async fn cost(peers: NetworkPeers) -> Result<()> {
}

pub async fn create(peers: NetworkPeers) -> Result<()> {
let client = crate::actions::connect_to_network(peers).await?;
let wallet = load_wallet(&client.evm_network)?;
let client = crate::actions::connect_to_network(peers, Default::default()).await?;
let wallet = load_wallet(client.get_evm_network())?;
let vault_sk = crate::keys::get_vault_secret_key()?;

println!("Retrieving local user data...");
Expand All @@ -56,9 +56,9 @@ pub async fn create(peers: NetworkPeers) -> Result<()> {
}

pub async fn sync(force: bool, peers: NetworkPeers) -> Result<()> {
let client = crate::actions::connect_to_network(peers).await?;
let client = crate::actions::connect_to_network(peers, Default::default()).await?;
let vault_sk = crate::keys::get_vault_secret_key()?;
let wallet = load_wallet(&client.evm_network)?;
let wallet = load_wallet(client.get_evm_network())?;

println!("Fetching vault from network...");
let net_user_data = client
Expand Down Expand Up @@ -90,7 +90,7 @@ pub async fn sync(force: bool, peers: NetworkPeers) -> Result<()> {
}

pub async fn load(peers: NetworkPeers) -> Result<()> {
let client = crate::actions::connect_to_network(peers).await?;
let client = crate::actions::connect_to_network(peers, Default::default()).await?;
let vault_sk = crate::keys::get_vault_secret_key()?;

println!("Retrieving vault from network...");
Expand Down
1 change: 1 addition & 0 deletions ant-networking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async-trait = "0.1"
bls = { package = "blsttc", version = "8.0.2" }
bytes = { version = "1.0.1", features = ["serde"] }
custom_debug = "~0.6.1"
exponential-backoff = "2.0.0"
futures = "~0.3.13"
hex = "~0.4.3"
hkdf = "0.12"
Expand Down
14 changes: 8 additions & 6 deletions ant-networking/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{
config::GetRecordCfg,
driver::{PendingGetClosestType, SwarmDriver},
error::{NetworkError, Result},
event::TerminateNodeReason,
log_markers::Marker,
multiaddr_pop_p2p, GetRecordCfg, GetRecordError, MsgResponder, NetworkEvent, CLOSE_GROUP_SIZE,
multiaddr_pop_p2p, GetRecordError, MsgResponder, NetworkEvent, ResponseQuorum,
CLOSE_GROUP_SIZE,
};
use ant_evm::{PaymentQuote, QuotingMetrics, U256};
use ant_protocol::{
Expand All @@ -23,7 +25,7 @@ use ant_protocol::{
use libp2p::{
kad::{
store::{Error as StoreError, RecordStore},
KBucketDistance as Distance, Quorum, Record, RecordKey, K_VALUE,
KBucketDistance as Distance, Record, RecordKey, K_VALUE,
},
Multiaddr, PeerId,
};
Expand Down Expand Up @@ -205,14 +207,14 @@ pub enum NetworkSwarmCmd {
PutRecord {
record: Record,
sender: oneshot::Sender<Result<()>>,
quorum: Quorum,
quorum: ResponseQuorum,
},
/// Put record to specific node
PutRecordTo {
peers: Vec<PeerId>,
record: Record,
sender: oneshot::Sender<Result<()>>,
quorum: Quorum,
quorum: ResponseQuorum,
},
}

Expand Down Expand Up @@ -451,7 +453,7 @@ impl SwarmDriver {
.swarm
.behaviour_mut()
.kademlia
.put_record(record, quorum)
.put_record(record, quorum.get_kad_quorum())
{
Ok(request_id) => {
debug!("Sent record {record_key:?} to network. Request id: {request_id:?} to network");
Expand Down Expand Up @@ -483,7 +485,7 @@ impl SwarmDriver {
let request_id = self.swarm.behaviour_mut().kademlia.put_record_to(
record,
peers.into_iter(),
quorum,
quorum.get_kad_quorum(),
);
debug!("Sent record {record_key:?} to {peers_count:?} peers. Request id: {request_id:?}");

Expand Down
Loading

0 comments on commit 33b97f4

Please sign in to comment.