Skip to content

Commit

Permalink
Merge pull request #2712 from grumbach/api_cleanup
Browse files Browse the repository at this point in the history
feat: API cleanup for release
  • Loading branch information
grumbach authored Feb 6, 2025
2 parents 9fbbb81 + 47ba0a6 commit 5fdda2f
Show file tree
Hide file tree
Showing 32 changed files with 225 additions and 184 deletions.
8 changes: 4 additions & 4 deletions ant-cli/src/actions/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ async fn download_private(
client: &Client,
) -> Result<()> {
let archive = client
.archive_get(private_address)
.archive_get(&private_address)
.await
.wrap_err("Failed to fetch data from address")?;

let progress_bar = get_progress_bar(archive.iter().count() as u64)?;
let mut all_errs = vec![];
for (path, access, _meta) in archive.iter() {
progress_bar.println(format!("Fetching file: {path:?}..."));
let bytes = match client.data_get(access.clone()).await {
let bytes = match client.data_get(access).await {
Ok(bytes) => bytes,
Err(e) => {
let err = format!("Failed to fetch file {path:?}: {e}");
Expand Down Expand Up @@ -89,15 +89,15 @@ async fn download_public(
client: &Client,
) -> Result<()> {
let archive = client
.archive_get_public(address)
.archive_get_public(&address)
.await
.wrap_err("Failed to fetch data from address")?;

let progress_bar = get_progress_bar(archive.iter().count() as u64)?;
let mut all_errs = vec![];
for (path, addr, _meta) in archive.iter() {
progress_bar.println(format!("Fetching file: {path:?}..."));
let bytes = match client.data_get_public(*addr).await {
let bytes = match client.data_get_public(addr).await {
Ok(bytes) => bytes,
Err(e) => {
let err = format!("Failed to fetch file {path:?}: {e}");
Expand Down
4 changes: 2 additions & 2 deletions ant-cli/src/commands/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ pub async fn upload(
// upload dir
let local_addr;
let archive = if public {
let xor_name = client
let (_cost, xor_name) = client
.dir_and_archive_upload_public(dir_path, &wallet)
.await
.wrap_err("Failed to upload file")?;
local_addr = addr_to_str(xor_name);
local_addr.clone()
} else {
let private_data_access = client
let (_cost, private_data_access) = client
.dir_and_archive_upload(dir_path, &wallet)
.await
.wrap_err("Failed to upload dir and archive")?;
Expand Down
10 changes: 5 additions & 5 deletions ant-node/src/put_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,14 @@ impl Node {
}
RecordKind::DataOnly(DataTypes::Pointer) => {
let pointer = try_deserialize_record::<Pointer>(&record)?;
let net_addr = NetworkAddress::from_pointer_address(pointer.network_address());
let net_addr = NetworkAddress::from_pointer_address(pointer.address());
let pretty_key = PrettyPrintRecordKey::from(&record.key);
let already_exists = self
.validate_key_and_existence(&net_addr, &record.key)
.await?;

if !already_exists {
warn!("Pointer at address: {:?}, key: {:?} does not exist locally, rejecting PUT without payment", pointer.network_address(), pretty_key);
warn!("Pointer at address: {:?}, key: {:?} does not exist locally, rejecting PUT without payment", pointer.address(), pretty_key);
return Err(Error::InvalidPutWithoutPayment(
PrettyPrintRecordKey::from(&record.key).into_owned(),
));
Expand All @@ -283,7 +283,7 @@ impl Node {
let (payment, pointer) =
try_deserialize_record::<(ProofOfPayment, Pointer)>(&record)?;

let net_addr = NetworkAddress::from_pointer_address(pointer.network_address());
let net_addr = NetworkAddress::from_pointer_address(pointer.address());
let pretty_key = PrettyPrintRecordKey::from(&record.key);
let already_exists = self
.validate_key_and_existence(&net_addr, &record.key)
Expand Down Expand Up @@ -797,14 +797,14 @@ impl Node {
}

// Check if the pointer's address matches the record key
let net_addr = NetworkAddress::from_pointer_address(pointer.network_address());
let net_addr = NetworkAddress::from_pointer_address(pointer.address());
if key != net_addr.to_record_key() {
warn!("Pointer address does not match record key");
return Err(Error::RecordKeyMismatch);
}

// Keep the pointer with the highest counter
if let Some(local_pointer) = self.get_local_pointer(pointer.network_address()).await {
if let Some(local_pointer) = self.get_local_pointer(pointer.address()).await {
if pointer.counter() <= local_pointer.counter() {
info!(
"Ignoring Pointer PUT at {key:?} with counter less than or equal to the current counter ({} <= {})",
Expand Down
10 changes: 5 additions & 5 deletions ant-node/tests/data_with_churn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ fn create_graph_entry_task(

let mut retries = 1;
loop {
match client.graph_entry_get(*addr).await {
match client.graph_entry_get(addr).await {
Ok(graph_entry) => {
println!("Fetched graph_entry at {addr:?}");

Expand Down Expand Up @@ -683,7 +683,7 @@ fn store_chunks_task(
println!("Error to put chunk: {err:?}");
error!("Error to put chunk: {err:?}")
}) {
Ok(data_map) => {
Ok((_cost, data_map)) => {
println!("Stored Chunk/s at {data_map:?} after a delay of: {delay:?}");
info!("Stored Chunk/s at {data_map:?} after a delay of: {delay:?}");

Expand Down Expand Up @@ -872,15 +872,15 @@ async fn final_retry_query_content(
async fn query_content(client: &Client, net_addr: &NetworkAddress) -> Result<()> {
match net_addr {
NetworkAddress::ChunkAddress(addr) => {
client.data_get_public(*addr.xorname()).await?;
client.data_get_public(addr.xorname()).await?;
Ok(())
}
NetworkAddress::PointerAddress(addr) => {
let _ = client.pointer_get(*addr).await?;
let _ = client.pointer_get(addr).await?;
Ok(())
}
NetworkAddress::GraphEntryAddress(addr) => {
let _ = client.graph_entry_get(*addr).await?;
let _ = client.graph_entry_get(addr).await?;
Ok(())
}
NetworkAddress::ScratchpadAddress(addr) => {
Expand Down
7 changes: 6 additions & 1 deletion ant-protocol/src/storage/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,14 @@ impl Chunk {
}

/// Returns size of this chunk after serialisation.
pub fn serialised_size(&self) -> usize {
pub fn size(&self) -> usize {
self.value.len()
}

/// Returns true if the chunk is too big
pub fn is_too_big(&self) -> bool {
self.size() > Self::DEFAULT_MAX_SIZE
}
}

impl Serialize for Chunk {
Expand Down
10 changes: 5 additions & 5 deletions ant-protocol/src/storage/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ impl Pointer {
PointerAddress::from_owner(self.owner)
}

/// Get the owner of the pointer
pub fn owner(&self) -> &PublicKey {
&self.owner
}

/// Get the target of the pointer
pub fn target(&self) -> &PointerTarget {
&self.target
Expand All @@ -111,11 +116,6 @@ impl Pointer {
self.counter
}

/// Get the network address for this pointer
pub fn network_address(&self) -> PointerAddress {
PointerAddress::from_owner(self.owner)
}

/// Verifies if the pointer has a valid signature
pub fn verify_signature(&self) -> bool {
let bytes = self.bytes_for_signature();
Expand Down
4 changes: 2 additions & 2 deletions ant-protocol/src/storage/scratchpad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ impl Scratchpad {
XorName::from_content(&self.encrypted_data)
}

/// Returns the owner.
/// Returns the owner of the scratchpad
pub fn owner(&self) -> &PublicKey {
self.address.owner()
}

/// Returns the address.
/// Returns the address of the scratchpad
pub fn address(&self) -> &ScratchpadAddress {
&self.address
}
Expand Down
2 changes: 1 addition & 1 deletion autonomi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let data_addr = client
.data_put_public(Bytes::from("Hello, World"), (&wallet).into())
.await?;
let _data_fetched = client.data_get_public(data_addr).await?;
let _data_fetched = client.data_get_public(&data_addr).await?;

// Put and fetch directory from local file system.
let dir_addr = client.dir_and_archive_upload_public("files/to/upload".into(), &wallet).await?;
Expand Down
11 changes: 6 additions & 5 deletions autonomi/examples/data_and_archive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use autonomi::{Bytes, Client, Metadata, PrivateArchive};
use autonomi::files::{Metadata, PrivateArchive};
use autonomi::{Bytes, Client};
use test_utils::evm::get_funded_wallet;
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};

Expand All @@ -14,8 +15,8 @@ async fn main() -> eyre::Result<()> {

// 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?;
let (_cost, data_map) = client.data_put(data.clone(), (&wallet).into()).await?;
let data_fetched = client.data_get(&data_map).await?;
assert_eq!(data, data_fetched);

// Upload the data as part of an archive, giving it the name `test.txt`.
Expand All @@ -27,8 +28,8 @@ async fn main() -> eyre::Result<()> {
);

// 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?;
let (_cost, 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");
Expand Down
8 changes: 4 additions & 4 deletions autonomi/examples/put_and_dir_upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let wallet = get_funded_wallet();

// Put and fetch data.
let data_addr = client
let (_cost, data_addr) = client
.data_put_public(Bytes::from("Hello, World"), (&wallet).into())
.await?;
let _data_fetched = client.data_get_public(data_addr).await?;
let _data_fetched = client.data_get_public(&data_addr).await?;

// Put and fetch directory from local file system.
let dir_addr = client
let (_cost, dir_addr) = client
.dir_and_archive_upload_public("files/to/upload".into(), &wallet)
.await?;
client
.dir_download_public(dir_addr, "files/downloaded".into())
.dir_download_public(&dir_addr, "files/downloaded".into())
.await?;

Ok(())
Expand Down
9 changes: 5 additions & 4 deletions autonomi/src/client/data_types/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub static CHUNK_DOWNLOAD_BATCH_SIZE: LazyLock<usize> = LazyLock::new(|| {
});

/// Private data on the network can be accessed with this
/// Uploading this data in a chunk makes it publicly accessible from the address of that Chunk
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct DataMapChunk(pub(crate) Chunk);

Expand Down Expand Up @@ -105,10 +106,10 @@ fn hash_to_short_string(input: &str) -> String {

impl Client {
/// Get a chunk from the network.
pub async fn chunk_get(&self, addr: ChunkAddress) -> Result<Chunk, GetError> {
pub async fn chunk_get(&self, addr: &ChunkAddress) -> Result<Chunk, GetError> {
info!("Getting chunk: {addr:?}");

let key = NetworkAddress::from_chunk_address(addr).to_record_key();
let key = NetworkAddress::from_chunk_address(*addr).to_record_key();
debug!("Fetching chunk from network at: {key:?}");

let get_cfg = self.config.chunks.get_cfg();
Expand Down Expand Up @@ -146,7 +147,7 @@ impl Client {
let (payment_proofs, _skipped_payments) = self
.pay_for_content_addrs(
DataTypes::Chunk,
std::iter::once((xor_name, chunk.serialised_size())),
std::iter::once((xor_name, chunk.size())),
payment_option,
)
.await
Expand Down Expand Up @@ -366,7 +367,7 @@ impl Client {
for info in data_map.infos() {
download_tasks.push(async move {
match self
.chunk_get(ChunkAddress::new(info.dst_hash))
.chunk_get(&ChunkAddress::new(info.dst_hash))
.await
.inspect_err(|err| {
error!(
Expand Down
4 changes: 2 additions & 2 deletions autonomi/src/client/data_types/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ impl Client {
/// Fetches a GraphEntry from the network.
pub async fn graph_entry_get(
&self,
address: GraphEntryAddress,
address: &GraphEntryAddress,
) -> Result<GraphEntry, GraphError> {
let key = NetworkAddress::from_graph_entry_address(address).to_record_key();
let key = NetworkAddress::from_graph_entry_address(*address).to_record_key();
let get_cfg = self.config.graph_entry.get_cfg();
let record = self
.network
Expand Down
13 changes: 7 additions & 6 deletions autonomi/src/client/data_types/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ pub enum PointerError {

impl Client {
/// Get a pointer from the network
pub async fn pointer_get(&self, address: PointerAddress) -> Result<Pointer, PointerError> {
let key = NetworkAddress::from_pointer_address(address).to_record_key();
pub async fn pointer_get(&self, address: &PointerAddress) -> Result<Pointer, PointerError> {
let key = NetworkAddress::from_pointer_address(*address).to_record_key();
debug!("Fetching pointer from network at: {key:?}");

let get_cfg = self.config.pointer.get_cfg();
Expand Down Expand Up @@ -117,7 +117,7 @@ impl Client {
pointer: Pointer,
payment_option: PaymentOption,
) -> Result<(AttoTokens, PointerAddress), PointerError> {
let address = pointer.network_address();
let address = pointer.address();

// pay for the pointer storage
let xor_name = *address.xorname();
Expand Down Expand Up @@ -203,13 +203,14 @@ impl Client {
/// Update an existing pointer to point to a new target on the network
/// The pointer needs to be created first with [`Client::pointer_put`]
/// This operation is free as the pointer was already paid for at creation
/// Only the latest version of the pointer is kept on the Network, previous versions will be overwritten and unrecoverable
pub async fn pointer_update(
&self,
owner: &SecretKey,
target: PointerTarget,
) -> Result<(), PointerError> {
let address = PointerAddress::from_owner(owner.public_key());
let current = match self.pointer_get(address).await {
let current = match self.pointer_get(&address).await {
Ok(pointer) => Some(pointer),
Err(PointerError::Network(NetworkError::GetRecordError(
GetRecordError::RecordNotFound,
Expand Down Expand Up @@ -257,10 +258,10 @@ impl Client {
}

/// Calculate the cost of storing a pointer
pub async fn pointer_cost(&self, key: PublicKey) -> Result<AttoTokens, CostError> {
pub async fn pointer_cost(&self, key: &PublicKey) -> Result<AttoTokens, CostError> {
trace!("Getting cost for pointer of {key:?}");

let address = PointerAddress::from_owner(key);
let address = PointerAddress::from_owner(*key);
let xor = *address.xorname();
let store_quote = self
.get_store_quotes(DataTypes::Pointer, std::iter::once((xor, Pointer::size())))
Expand Down
6 changes: 3 additions & 3 deletions autonomi/src/client/data_types/scratchpad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub enum ScratchpadError {

impl Client {
/// Get Scratchpad from the Network
/// It is stored at the owner's public key
/// A Scratchpad is stored at the owner's public key so we can derive the address from it
pub async fn scratchpad_get_from_public_key(
&self,
public_key: &PublicKey,
Expand All @@ -58,7 +58,6 @@ impl Client {
}

/// Get Scratchpad from the Network
/// It is stored at the owner's public key
pub async fn scratchpad_get(
&self,
address: &ScratchpadAddress,
Expand Down Expand Up @@ -252,7 +251,8 @@ impl Client {
}

/// Update an existing scratchpad to the network
/// This operation is free but requires the scratchpad to be already created on the network
/// The scratchpad needs to be created first with [`Client::scratchpad_create`]
/// This operation is free as the scratchpad was already paid for at creation
/// Only the latest version of the scratchpad is kept on the Network, previous versions will be overwritten and unrecoverable
pub async fn scratchpad_update(
&self,
Expand Down
Loading

0 comments on commit 5fdda2f

Please sign in to comment.