Skip to content

Commit

Permalink
feat: compiling CLI along with various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
grumbach committed Dec 11, 2024
1 parent f692b2e commit d3cdc3e
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 114 deletions.
207 changes: 122 additions & 85 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions ant-cli/src/commands/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ pub async fn cost(name: &str, peers: Vec<Multiaddr>) -> Result<()> {
let register_key = crate::keys::get_register_signing_key()
.wrap_err("The register key is required to perform this action")?;
let client = crate::actions::connect_to_network(peers).await?;
let wallet = load_wallet()?;

let cost = client
.register_cost(&wallet.network(), name.to_string(), register_key)
.register_cost(name.to_string(), register_key)
.await
.wrap_err("Failed to get cost for register")?;
info!("Estimated cost to create a register with name {name}: {cost}");
Expand Down
16 changes: 10 additions & 6 deletions ant-evm/src/data_payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ const LIVE_TIME_MARGIN: u64 = 10;
pub struct EncodedPeerId(Vec<u8>);

impl EncodedPeerId {
pub fn to_peer_id(&self) -> Result<PeerId, libp2p::identity::DecodingError> {
match PublicKey::try_decode_protobuf(&self.0) {
Ok(pub_key) => Ok(PeerId::from_public_key(&pub_key)),
Err(e) => Err(e),
}
pub fn to_peer_id(&self) -> Result<PeerId, libp2p::identity::ParseError> {
PeerId::from_bytes(&self.0)
}
}

// TODO: @anselme is this conversion right?
impl From<PeerId> for EncodedPeerId {
fn from(peer_id: PeerId) -> Self {
let bytes = peer_id.to_bytes();
Expand Down Expand Up @@ -322,6 +318,14 @@ mod tests {
use libp2p::identity::Keypair;
use std::{thread::sleep, time::Duration};

#[test]
fn test_encode_decode_peer_id() {
let id = PeerId::random();
let encoded = EncodedPeerId::from(id);
let decoded = encoded.to_peer_id().expect("decode to work");
assert_eq!(id, decoded);
}

#[test]
fn test_is_newer_than() {
let old_quote = PaymentQuote::zero();
Expand Down
21 changes: 11 additions & 10 deletions autonomi/src/client/external_signer.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
use crate::client::data::PutError;
use crate::client::utils::extract_quote_payments;
use crate::self_encryption::encrypt;
use crate::Client;
use ant_evm::{PaymentQuote, QuotePayment};
use ant_evm::QuotePayment;
use ant_protocol::storage::Chunk;
use bytes::Bytes;
use std::collections::HashMap;
use xor_name::XorName;

use crate::utils::cost_map_to_quotes;
#[allow(unused_imports)]
pub use ant_evm::external_signer::*;

use super::quote::QuoteForAddress;

impl Client {
/// Get quotes for data.
/// Returns a cost map, data payments to be executed and a list of free (already paid for) chunks.
pub async fn get_quotes_for_content_addresses(
&self,
content_addrs: impl Iterator<Item = XorName>,
content_addrs: impl Iterator<Item = XorName> + Clone,
) -> Result<
(
HashMap<XorName, PaymentQuote>,
HashMap<XorName, QuoteForAddress>,
Vec<QuotePayment>,
Vec<XorName>,
),
PutError,
> {
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);
let quote = self.get_store_quotes(content_addrs.clone()).await?;
let payments = quote.payments();
let free_chunks = content_addrs.filter(|addr| !quote.0.contains_key(addr)).collect();
let quotes_per_addr = quote.0.into_iter().collect();

debug!(
"Got the quotes , quote_payments and freechunks from the network {:?}",
(quotes.clone(), quote_payments.clone(), free_chunks.clone())
(quotes_per_addr.clone(), payments.clone(), free_chunks.clone())
);
Ok((quotes, quote_payments, free_chunks))
Ok((quotes_per_addr, payments, free_chunks))
}
}

Expand Down
6 changes: 5 additions & 1 deletion autonomi/src/client/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ pub struct StoreQuote(pub(crate) HashMap<XorName, QuoteForAddress>);

impl StoreQuote {
pub fn price(&self) -> Amount {
self.0.iter().map(|(_, quote)| quote.price()).sum()
self.0.values().map(|quote| quote.price()).sum()
}

pub fn len(&self) -> usize {
self.0.len()
}

pub fn is_empty(&self) -> bool {
self.0.is_empty()
}

pub fn payments(&self) -> Vec<QuotePayment> {
let mut quote_payments = vec![];
for (_address, quote) in self.0.iter() {
Expand Down
2 changes: 1 addition & 1 deletion autonomi/src/client/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl Client {
None => return Err(PutError::PaymentUnexpectedlyInvalid(scratch_address)),
};

total_cost = price.clone();
total_cost = *price;

Record {
key: scratch_key,
Expand Down
2 changes: 0 additions & 2 deletions autonomi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ pub use ant_evm::get_evm_network_from_env;
pub use ant_evm::EvmNetwork as Network;
pub use ant_evm::EvmWallet as Wallet;
pub use ant_evm::RewardsAddress;
#[cfg(feature = "external-signer")]
pub use utils::receipt_from_quotes_and_payments;

#[doc(no_inline)] // Place this under 'Re-exports' in the docs.
pub use bytes::Bytes;
Expand Down
6 changes: 2 additions & 4 deletions autonomi/tests/external_signer.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#![cfg(feature = "external-signer")]

use alloy::network::TransactionBuilder;
use alloy::providers::Provider;
use ant_evm::{QuoteHash, TxHash};
use ant_logging::LogBuilder;
use autonomi::client::external_signer::encrypt_data;
use autonomi::client::files::archive::{Metadata, PrivateArchive};
use autonomi::client::payment::Receipt;
use autonomi::client::vault::user_data::USER_DATA_VAULT_CONTENT_IDENTIFIER;
use autonomi::client::vault::VaultSecretKey;
use autonomi::{receipt_from_quotes_and_payments, Client, Wallet};
use autonomi::{Client, Wallet};
use bytes::Bytes;
use std::collections::BTreeMap;
use std::time::Duration;
Expand All @@ -34,7 +32,7 @@ async fn pay_for_data(client: &Client, wallet: &Wallet, data: Bytes) -> eyre::Re
async fn pay_for_content_addresses(
client: &Client,
wallet: &Wallet,
content_addrs: impl Iterator<Item = XorName>,
content_addrs: impl Iterator<Item = XorName> + Clone,
) -> eyre::Result<Receipt> {
let (quotes, quote_payments, _free_chunks) = client
.get_quotes_for_content_addresses(content_addrs)
Expand Down
10 changes: 7 additions & 3 deletions evmlib/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ use alloy::providers::ext::AnvilApi;
use alloy::providers::{ProviderBuilder, WalletProvider};
use alloy::signers::local::{LocalSigner, PrivateKeySigner};
use evmlib::common::{Amount, TxHash};
use evmlib::contract::payment_vault::MAX_TRANSFERS_PER_TRANSACTION;
use evmlib::contract::payment_vault::{verify_data_payment, MAX_TRANSFERS_PER_TRANSACTION};
use evmlib::quoting_metrics::QuotingMetrics;
use evmlib::testnet::{deploy_data_payments_contract, deploy_network_token_contract, start_node};
use evmlib::transaction::verify_data_payment;
use evmlib::wallet::{transfer_tokens, wallet_address, Wallet};
use evmlib::{CustomNetwork, Network};
use std::collections::HashSet;
Expand Down Expand Up @@ -90,7 +89,12 @@ async fn test_pay_for_quotes_and_data_payment_verification() {
for (quote_hash, reward_addr, _) in quote_payments.iter() {
let result = verify_data_payment(
&network,
vec![(*quote_hash, QuotingMetrics::default(), *reward_addr)]
vec![*quote_hash],
vec![(
*quote_hash,
QuotingMetrics::default(),
*reward_addr,
)],
)
.await;

Expand Down

0 comments on commit d3cdc3e

Please sign in to comment.