Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional contract testing and logging #2608

Merged
merged 5 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ant-node-manager/src/bin/cli/subcommands/evm_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub enum EvmNetworkCommand {
/// Use the Arbitrum Sepolia network
EvmArbitrumSepolia,

/// Use the Arbitrum Sepolia network with test contracts
EvmArbitrumSepoliaTest,

/// Use a custom network
EvmCustom {
/// The RPC URL for the custom network
Expand All @@ -45,6 +48,7 @@ impl TryInto<EvmNetwork> for EvmNetworkCommand {
match self {
Self::EvmArbitrumOne => Ok(EvmNetwork::ArbitrumOne),
Self::EvmArbitrumSepolia => Ok(EvmNetwork::ArbitrumSepolia),
Self::EvmArbitrumSepoliaTest => Ok(EvmNetwork::ArbitrumSepoliaTest),
Self::EvmLocal => {
if !cfg!(feature = "local") {
return Err(color_eyre::eyre::eyre!(
Expand Down
4 changes: 4 additions & 0 deletions ant-node/src/bin/antnode/subcommands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub(crate) enum EvmNetworkCommand {
/// Use the Arbitrum Sepolia network
EvmArbitrumSepolia,

/// Use the Arbitrum Sepolia network with test contracts
EvmArbitrumSepoliaTest,

/// Use a custom network
EvmCustom {
/// The RPC URL for the custom network
Expand All @@ -32,6 +35,7 @@ impl Into<EvmNetwork> for EvmNetworkCommand {
match self {
Self::EvmArbitrumOne => EvmNetwork::ArbitrumOne,
Self::EvmArbitrumSepolia => EvmNetwork::ArbitrumSepolia,
Self::EvmArbitrumSepoliaTest => EvmNetwork::ArbitrumSepoliaTest,
Self::EvmCustom {
rpc_url,
payment_token_address,
Expand Down
9 changes: 9 additions & 0 deletions autonomi/src/client/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ impl Client {
.into_iter()
.map(|content_addr| fetch_store_quote_with_retries(&self.network, content_addr))
.collect();

let raw_quotes_per_addr = futures::future::try_join_all(futures).await?;

// choose the quotes to pay for each address
Expand All @@ -70,9 +71,15 @@ impl Client {
let mut rate_limiter = RateLimiter::new();

for (content_addr, raw_quotes) in raw_quotes_per_addr {
debug!(
"fetching market price for content_addr: {content_addr}, with {} quotes.",
raw_quotes.len()
);

// FIXME: find better way to deal with paid content addrs and feedback to the user
// assume that content addr is already paid for and uploaded
if raw_quotes.is_empty() {
debug!("content_addr: {content_addr} is already paid for. No need to fetch market price.");
continue;
}

Expand All @@ -90,6 +97,8 @@ impl Client {
)
.await?;

debug!("market prices: {all_prices:?}");

let mut prices: Vec<(PeerId, PaymentQuote, Amount)> = all_prices
.into_iter()
.zip(raw_quotes.into_iter())
Expand Down
16 changes: 14 additions & 2 deletions evmlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub mod testnet;
pub mod utils;
pub mod wallet;

/// Timeout for transactions
const TX_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60);

static PUBLIC_ARBITRUM_ONE_HTTP_RPC_URL: LazyLock<reqwest::Url> = LazyLock::new(|| {
"https://arb1.arbitrum.io/rpc"
.parse()
Expand All @@ -45,15 +48,18 @@ const ARBITRUM_ONE_PAYMENT_TOKEN_ADDRESS: Address =
const ARBITRUM_SEPOLIA_PAYMENT_TOKEN_ADDRESS: Address =
address!("BE1802c27C324a28aeBcd7eeC7D734246C807194");

const ARBITRUM_SEPOLIA_TEST_PAYMENT_TOKEN_ADDRESS: Address =
address!("4bc1aCE0E66170375462cB4E6Af42Ad4D5EC689C");

// Should be updated when the smart contract changes!
const ARBITRUM_ONE_DATA_PAYMENTS_ADDRESS: Address =
address!("607483B50C5F06c25cDC316b6d1E071084EeC9f5");

const ARBITRUM_SEPOLIA_DATA_PAYMENTS_ADDRESS: Address =
address!("993C7739f50899A997fEF20860554b8a28113634");

/// Timeout for transactions
const TX_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60);
const ARBITRUM_SEPOLIA_TEST_DATA_PAYMENTS_ADDRESS: Address =
address!("7f0842a78f7d4085d975ba91d630d680f91b1295");

Check failure

Code scanning / devskim

A token or key was found in source code. If this represents a secret, it should be moved somewhere else.

Do not store tokens or keys in source code.

#[serde_as]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -81,6 +87,7 @@ pub enum Network {
#[default]
ArbitrumOne,
ArbitrumSepolia,
ArbitrumSepoliaTest,
Custom(CustomNetwork),
}

Expand All @@ -89,6 +96,7 @@ impl std::fmt::Display for Network {
match self {
Network::ArbitrumOne => write!(f, "evm-arbitrum-one"),
Network::ArbitrumSepolia => write!(f, "evm-arbitrum-sepolia"),
Network::ArbitrumSepoliaTest => write!(f, "evm-arbitrum-sepolia-test"),
Network::Custom(_) => write!(f, "evm-custom"),
}
}
Expand All @@ -107,6 +115,7 @@ impl Network {
match self {
Network::ArbitrumOne => "arbitrum-one",
Network::ArbitrumSepolia => "arbitrum-sepolia",
Network::ArbitrumSepoliaTest => "arbitrum-sepolia-test",
Network::Custom(_) => "custom",
}
}
Expand All @@ -115,6 +124,7 @@ impl Network {
match self {
Network::ArbitrumOne => &PUBLIC_ARBITRUM_ONE_HTTP_RPC_URL,
Network::ArbitrumSepolia => &PUBLIC_ARBITRUM_SEPOLIA_HTTP_RPC_URL,
Network::ArbitrumSepoliaTest => &PUBLIC_ARBITRUM_SEPOLIA_HTTP_RPC_URL,
Network::Custom(custom) => &custom.rpc_url_http,
}
}
Expand All @@ -123,6 +133,7 @@ impl Network {
match self {
Network::ArbitrumOne => &ARBITRUM_ONE_PAYMENT_TOKEN_ADDRESS,
Network::ArbitrumSepolia => &ARBITRUM_SEPOLIA_PAYMENT_TOKEN_ADDRESS,
Network::ArbitrumSepoliaTest => &ARBITRUM_SEPOLIA_TEST_PAYMENT_TOKEN_ADDRESS,
Network::Custom(custom) => &custom.payment_token_address,
}
}
Expand All @@ -131,6 +142,7 @@ impl Network {
match self {
Network::ArbitrumOne => &ARBITRUM_ONE_DATA_PAYMENTS_ADDRESS,
Network::ArbitrumSepolia => &ARBITRUM_SEPOLIA_DATA_PAYMENTS_ADDRESS,
Network::ArbitrumSepoliaTest => &ARBITRUM_SEPOLIA_TEST_DATA_PAYMENTS_ADDRESS,
Network::Custom(custom) => &custom.data_payments_address,
}
}
Expand Down
7 changes: 7 additions & 0 deletions evmlib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,19 @@ pub fn get_evm_network_from_env() -> Result<Network, Error> {
.map(|v| v == "arbitrum-sepolia")
.unwrap_or(false);

let use_arbitrum_sepolia_test = std::env::var("EVM_NETWORK")
.map(|v| v == "arbitrum-sepolia-test")
.unwrap_or(false);

if use_arbitrum_one {
info!("Using Arbitrum One EVM network as EVM_NETWORK is set to 'arbitrum-one'");
Ok(Network::ArbitrumOne)
} else if use_arbitrum_sepolia {
info!("Using Arbitrum Sepolia EVM network as EVM_NETWORK is set to 'arbitrum-sepolia'");
Ok(Network::ArbitrumSepolia)
} else if use_arbitrum_sepolia_test {
info!("Using Arbitrum Sepolia Test EVM network as EVM_NETWORK is set to 'arbitrum-sepolia-test'");
Ok(Network::ArbitrumSepoliaTest)
} else if let Ok(evm_vars) = evm_vars {
info!("Using custom EVM network from environment variables");
Ok(Network::Custom(CustomNetwork::new(
Expand Down
78 changes: 52 additions & 26 deletions evmlib/tests/payment_vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ async fn test_deploy() {
}

#[tokio::test]
async fn test_proxy_reachable() {
let network = Network::ArbitrumOne;
async fn test_proxy_reachable_on_arb_sepolia() {
let network = Network::ArbitrumSepolia;
let provider = http_provider(network.rpc_url().clone());
let payment_vault = PaymentVaultHandler::new(*network.data_payments_address(), provider);

Expand All @@ -130,12 +130,38 @@ async fn test_proxy_reachable() {
}

#[tokio::test]
async fn test_verify_payment() {
async fn test_get_quote_on_arb_sepolia_test() {
let network = Network::ArbitrumSepoliaTest;
let provider = http_provider(network.rpc_url().clone());
let payment_vault = PaymentVaultHandler::new(*network.data_payments_address(), provider);

let quoting_metrics = QuotingMetrics {
close_records_stored: 10,
max_records: 16 * 1024,
received_payment_count: 0,
live_time: 1400,
network_density: Some([
4, 4, 224, 228, 247, 252, 14, 44, 67, 21, 153, 47, 244, 18, 232, 1, 152, 195, 44, 43,
29, 135, 19, 217, 240, 129, 64, 245, 240, 227, 129, 162,
]),
network_size: Some(240),
};

let amount = payment_vault
.get_quote(vec![quoting_metrics])
.await
.unwrap();

assert_eq!(amount, vec![Amount::from(610678225049958_u64)]);
}

#[tokio::test]
async fn test_pay_for_quotes_on_local() {
let (_anvil, network_token, mut payment_vault) = setup().await;

let mut quote_payments = vec![];

for _ in 0..5 {
for _ in 0..MAX_TRANSFERS_PER_TRANSACTION {
let quote_payment = random_quote_payment();
quote_payments.push(quote_payment);
}
Expand All @@ -149,36 +175,18 @@ async fn test_verify_payment() {
// so we set it to the same as the network token contract
payment_vault.set_provider(network_token.contract.provider().clone());

let result = payment_vault.pay_for_quotes(quote_payments.clone()).await;
let result = payment_vault.pay_for_quotes(quote_payments).await;

assert!(result.is_ok(), "Failed with error: {:?}", result.err());

let payment_verifications: Vec<_> = quote_payments
.into_iter()
.map(|v| interface::IPaymentVault::PaymentVerification {
metrics: QuotingMetrics::default().into(),
rewardsAddress: v.1,
quoteHash: v.0,
})
.collect();

let results = payment_vault
.verify_payment(payment_verifications)
.await
.expect("Verify payment failed");

for result in results {
assert!(result.isValid);
}
}

#[tokio::test]
async fn test_pay_for_quotes() {
async fn test_verify_payment_on_local() {
let (_anvil, network_token, mut payment_vault) = setup().await;

let mut quote_payments = vec![];

for _ in 0..MAX_TRANSFERS_PER_TRANSACTION {
for _ in 0..5 {
let quote_payment = random_quote_payment();
quote_payments.push(quote_payment);
}
Expand All @@ -192,7 +200,25 @@ async fn test_pay_for_quotes() {
// so we set it to the same as the network token contract
payment_vault.set_provider(network_token.contract.provider().clone());

let result = payment_vault.pay_for_quotes(quote_payments).await;
let result = payment_vault.pay_for_quotes(quote_payments.clone()).await;

assert!(result.is_ok(), "Failed with error: {:?}", result.err());

let payment_verifications: Vec<_> = quote_payments
.into_iter()
.map(|v| interface::IPaymentVault::PaymentVerification {
metrics: QuotingMetrics::default().into(),
rewardsAddress: v.1,
quoteHash: v.0,
})
.collect();

let results = payment_vault
.verify_payment(payment_verifications)
.await
.expect("Verify payment failed");

for result in results {
assert!(result.isValid);
}
}
Loading