Skip to content

Commit

Permalink
feat: enable data location test
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Oct 2, 2024
1 parent 3535a1c commit 8adce46
Show file tree
Hide file tree
Showing 18 changed files with 930 additions and 1,016 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions autonomi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ xor_name = "5.0.0"
[dev-dependencies]
eyre = "0.6.5"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
# Do not specify the version field. Release process expects even the local dev deps to be published.
# Removing the version field is a workaround.
test_utils = { path = "../test_utils" }
wasm-bindgen-test = "0.3.43"

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
8 changes: 4 additions & 4 deletions autonomi/src/client/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl Client {

/// Upload a piece of data to the network. This data will be self-encrypted,
/// and the data map XOR address will be returned.
pub async fn put(&mut self, data: Bytes, wallet: &Wallet) -> Result<XorName, PutError> {
pub async fn put(&self, data: Bytes, wallet: &Wallet) -> Result<XorName, PutError> {
let now = std::time::Instant::now();
let (data_map_chunk, chunks) = encrypt(data)?;

Expand Down Expand Up @@ -186,7 +186,7 @@ impl Client {
Ok(map_xor_name)
}

pub(crate) async fn cost(&mut self, data: Bytes) -> Result<AttoTokens, PayError> {
pub(crate) async fn cost(&self, data: Bytes) -> Result<AttoTokens, PayError> {
let now = std::time::Instant::now();
let (data_map_chunk, chunks) = encrypt(data)?;

Expand All @@ -210,7 +210,7 @@ impl Client {
}

pub(crate) async fn pay(
&mut self,
&self,
content_addrs: impl Iterator<Item = XorName>,
wallet: &Wallet,
) -> Result<(HashMap<XorName, ProofOfPayment>, Vec<XorName>), PayError> {
Expand All @@ -237,7 +237,7 @@ impl Client {
}

async fn get_store_quotes(
&mut self,
&self,
content_addrs: impl Iterator<Item = XorName>,
) -> Result<HashMap<XorName, PayeeQuote>, PayError> {
let futures: Vec<_> = content_addrs
Expand Down
2 changes: 1 addition & 1 deletion autonomi/src/client/registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Client {

/// Creates a new Register with an initial value and uploads it to the network.
pub async fn create_register(
&mut self,
&self,
value: Bytes,
name: XorName,
owner: SecretKey,
Expand Down
46 changes: 0 additions & 46 deletions autonomi/tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
use bytes::Bytes;
use const_hex::ToHexExt;
use evmlib::CustomNetwork;
use libp2p::Multiaddr;
use rand::Rng;
use sn_peers_acquisition::parse_peer_addr;
use std::env;

fn get_var_or_panic(var: &str) -> String {
env::var(var).unwrap_or_else(|_| panic!("{var} environment variable needs to be set"))
}

#[allow(dead_code)]
pub fn gen_random_data(len: usize) -> Bytes {
let mut data = vec![0u8; len];
Expand All @@ -36,43 +30,3 @@ pub fn peers_from_env() -> Result<Vec<Multiaddr>, libp2p::multiaddr::Error> {

peers_str.split(',').map(parse_peer_addr).collect()
}

pub fn evm_network_from_env() -> evmlib::Network {
let evm_network = env::var("EVM_NETWORK").ok();
let arbitrum_flag = evm_network.as_deref() == Some("arbitrum-one");

let (rpc_url, payment_token_address, chunk_payments_address) = if arbitrum_flag {
(
evmlib::Network::ArbitrumOne.rpc_url().to_string(),
evmlib::Network::ArbitrumOne
.payment_token_address()
.encode_hex_with_prefix(),
evmlib::Network::ArbitrumOne
.chunk_payments_address()
.encode_hex_with_prefix(),
)
} else {
(
get_var_or_panic("RPC_URL"),
get_var_or_panic("PAYMENT_TOKEN_ADDRESS"),
get_var_or_panic("CHUNK_PAYMENTS_ADDRESS"),
)
};

evmlib::Network::Custom(CustomNetwork::new(
&rpc_url,
&payment_token_address,
&chunk_payments_address,
))
}

pub fn evm_wallet_from_env_or_default(network: evmlib::Network) -> evmlib::wallet::Wallet {
// Default deployer wallet of the testnet.
const DEFAULT_WALLET_PRIVATE_KEY: &str =
"0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";

let private_key = env::var("EVM_PRIVATE_KEY").unwrap_or(DEFAULT_WALLET_PRIVATE_KEY.to_string());

evmlib::wallet::Wallet::new_from_private_key(network, &private_key)
.expect("Invalid private key")
}
11 changes: 3 additions & 8 deletions autonomi/tests/evm/file.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
#[cfg(feature = "evm-payments")]
mod test {

use crate::common;
use crate::common::{evm_network_from_env, evm_wallet_from_env_or_default};
use autonomi::Client;
use bytes::Bytes;
use eyre::bail;
use std::time::Duration;
use test_utils::evm::get_funded_wallet;
use tokio::time::sleep;

#[tokio::test]
async fn file() -> Result<(), Box<dyn std::error::Error>> {
common::enable_logging();

let network = evm_network_from_env();
let mut client = Client::connect(&[]).await.unwrap();
let mut wallet = evm_wallet_from_env_or_default(network);
let mut wallet = get_funded_wallet();

// let data = common::gen_random_data(1024 * 1024 * 1000);
// let user_key = common::gen_random_data(32);
Expand All @@ -41,13 +38,11 @@ mod test {
async fn file_into_vault() -> eyre::Result<()> {
common::enable_logging();

let network = evm_network_from_env();

let mut client = Client::connect(&[])
.await?
.with_vault_entropy(Bytes::from("at least 32 bytes of entropy here"))?;

let mut wallet = evm_wallet_from_env_or_default(network);
let mut wallet = get_funded_wallet();

let (root, addr) = client
.upload_from_dir("tests/file/test_dir".into(), &mut wallet)
Expand Down
13 changes: 7 additions & 6 deletions autonomi/tests/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

mod common;

use crate::common::{evm_network_from_env, evm_wallet_from_env_or_default};
#[cfg(feature = "files")]
use autonomi::Client;
#[cfg(feature = "files")]
use std::time::Duration;
#[cfg(feature = "files")]
use test_utils::evm::get_funded_wallet;
#[cfg(feature = "files")]
use tokio::time::sleep;

#[tokio::test]
async fn file() -> Result<(), Box<dyn std::error::Error>> {
common::enable_logging();

let network = evm_network_from_env();
let mut client = Client::connect(&[]).await.unwrap();
let wallet = evm_wallet_from_env_or_default(network);
let wallet = get_funded_wallet();

let (root, addr) = client
.upload_from_dir("tests/file/test_dir".into(), &wallet)
Expand All @@ -36,10 +39,8 @@ async fn file() -> Result<(), Box<dyn std::error::Error>> {
async fn file_into_vault() -> eyre::Result<()> {
common::enable_logging();

let network = evm_network_from_env();

let mut client = Client::connect(&[]).await?;
let mut wallet = evm_wallet_from_env_or_default(network);
let mut wallet = get_funded_wallet();
let client_sk = bls::SecretKey::random();

let (root, addr) = client
Expand Down
10 changes: 4 additions & 6 deletions autonomi/tests/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

mod common;

use std::time::Duration;

use crate::common::{evm_network_from_env, evm_wallet_from_env_or_default};
use autonomi::Client;
use std::time::Duration;
use test_utils::evm::get_funded_wallet;
use tokio::time::sleep;

#[tokio::test]
async fn put() {
common::enable_logging();

let network = evm_network_from_env();
let mut client = Client::connect(&[]).await.unwrap();
let wallet = evm_wallet_from_env_or_default(network);
let client = Client::connect(&[]).await.unwrap();
let wallet = get_funded_wallet();
let data = common::gen_random_data(1024 * 1024 * 10);

let addr = client.put(data.clone(), &wallet).await.unwrap();
Expand Down
16 changes: 10 additions & 6 deletions autonomi/tests/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@

mod common;

use std::time::Duration;

use crate::common::{evm_network_from_env, evm_wallet_from_env_or_default};
#[cfg(feature = "registers")]
use autonomi::Client;
#[cfg(feature = "registers")]
use bytes::Bytes;
#[cfg(feature = "registers")]
use std::time::Duration;
#[cfg(feature = "registers")]
use test_utils::evm::get_funded_wallet;
#[cfg(feature = "registers")]
use tokio::time::sleep;
#[cfg(feature = "registers")]
use xor_name::XorName;

#[tokio::test]
async fn register() {
common::enable_logging();

let network = evm_network_from_env();
let mut client = Client::connect(&[]).await.unwrap();
let wallet = evm_wallet_from_env_or_default(network);
let client = Client::connect(&[]).await.unwrap();
let wallet = get_funded_wallet();

// Owner key of the register.
let key = bls::SecretKey::random();
Expand Down
4 changes: 2 additions & 2 deletions autonomi/tests/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod common;

use crate::common::{evm_network_from_env, evm_wallet_from_env_or_default};
use const_hex::traits::FromHex;
use evmlib::common::{Address, Amount};
use evmlib::wallet::Wallet;
use test_utils::evm::{evm_network_from_env, get_funded_wallet};

#[tokio::test]
async fn from_private_key() {
Expand All @@ -20,7 +20,7 @@ async fn from_private_key() {
#[tokio::test]
async fn send_tokens() {
let network = evm_network_from_env();
let wallet = evm_wallet_from_env_or_default(network.clone());
let wallet = get_funded_wallet();

let receiving_wallet = Wallet::new_with_random_wallet(network);

Expand Down
17 changes: 3 additions & 14 deletions autonomi/tests/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::time::Duration;

use crate::common::{evm_network_from_env, evm_wallet_from_env_or_default};
use autonomi::Client;
use evmlib::{CustomNetwork, Network};
use test_utils::evm::get_funded_wallet;
use tokio::time::sleep;
use wasm_bindgen_test::*;

Expand All @@ -21,18 +20,8 @@ async fn file() -> Result<(), Box<dyn std::error::Error>> {
.expect("str to be valid multiaddr"),
];

let rpc_url = option_env!("RPC_URL").unwrap();
let payment_token_address = option_env!("PAYMENT_TOKEN_ADDRESS").unwrap();
let chunk_payments_address = option_env!("CHUNK_PAYMENTS_ADDRESS").unwrap();

let network = Network::Custom(CustomNetwork::new(
rpc_url,
payment_token_address,
chunk_payments_address,
));

let mut client = Client::connect(&peers).await.unwrap();
let wallet = evm_wallet_from_env_or_default(network);
let client = Client::connect(&peers).await.unwrap();
let wallet = get_funded_wallet();

let data = common::gen_random_data(1024 * 1024 * 10);

Expand Down
4 changes: 4 additions & 0 deletions sn_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ strum = { version = "0.26.2", features = ["derive"] }
color-eyre = "0.6.2"

[dev-dependencies]
evmlib = { path = "../evmlib", version = "0.1" }
autonomi = { path = "../autonomi", version = "0.1.0", features = [
"registers",
] }
reqwest = { version = "0.12.2", default-features = false, features = [
"rustls-tls-manual-roots",
] }
Expand Down
Loading

0 comments on commit 8adce46

Please sign in to comment.