Skip to content

Commit

Permalink
Merge pull request #2499 from b-zee/refactor-autonomi-private-public
Browse files Browse the repository at this point in the history
refactor(autonomi): restructure modules and private/public naming
  • Loading branch information
b-zee authored Dec 6, 2024
2 parents cedab29 + 725b967 commit 53cbf8d
Show file tree
Hide file tree
Showing 33 changed files with 619 additions and 612 deletions.
3 changes: 1 addition & 2 deletions ant-cli/src/access/user_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use std::collections::HashMap;

use autonomi::client::{
address::{addr_to_str, str_to_addr},
archive::ArchiveAddr,
archive_private::PrivateArchiveAccess,
files::{archive::PrivateArchiveAccess, archive_public::ArchiveAddr},
registers::{RegisterAddress, RegisterSecretKey},
vault::UserData,
};
Expand Down
13 changes: 8 additions & 5 deletions ant-cli/src/actions/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

use super::get_progress_bar;
use autonomi::{
client::{address::str_to_addr, archive::ArchiveAddr, archive_private::PrivateArchiveAccess},
client::{
address::str_to_addr,
files::{archive::PrivateArchiveAccess, archive_public::ArchiveAddr},
},
Client,
};
use color_eyre::{
Expand Down Expand Up @@ -40,15 +43,15 @@ async fn download_private(
client: &mut Client,
) -> Result<()> {
let archive = client
.private_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.private_data_get(access.clone()).await {
let bytes = match client.data_get(access.clone()).await {
Ok(bytes) => bytes,
Err(e) => {
let err = format!("Failed to fetch file {path:?}: {e}");
Expand Down Expand Up @@ -86,15 +89,15 @@ async fn download_public(
client: &mut Client,
) -> Result<()> {
let archive = client
.archive_get(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(*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
7 changes: 4 additions & 3 deletions ant-cli/src/commands/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,17 @@ pub async fn upload(file: &str, public: bool, peers: Vec<Multiaddr>) -> Result<(
let local_addr;
let archive = if public {
let xor_name = client
.dir_upload(dir_path, &wallet)
.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
.private_dir_upload(dir_path, &wallet)
.dir_and_archive_upload(dir_path, &wallet)
.await
.wrap_err("Failed to upload file")?;
.wrap_err("Failed to upload dir and archive")?;

local_addr = private_data_access.address();
private_data_access.to_hex()
};
Expand Down
4 changes: 2 additions & 2 deletions ant-node/tests/data_with_churn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ fn store_chunks_task(
let mut retries = 1;
loop {
match client
.data_put(random_data.clone(), (&wallet).into())
.data_put_public(random_data.clone(), (&wallet).into())
.await
.inspect_err(|err| {
println!("Error to put chunk: {err:?}");
Expand Down Expand Up @@ -537,7 +537,7 @@ async fn query_content(client: &Client, net_addr: &NetworkAddress) -> Result<()>
Ok(())
}
NetworkAddress::ChunkAddress(addr) => {
client.data_get(*addr.xorname()).await?;
client.data_get_public(*addr.xorname()).await?;
Ok(())
}
_other => Ok(()), // we don't create/store any other type of content in this test yet
Expand Down
4 changes: 2 additions & 2 deletions ant-node/tests/storage_payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
// let _upload_stats = uploader.start_upload().await?;

// let mut files_download = FilesDownload::new(files_api);
// let _ = files_download.download_file(file_addr, None).await?;
// let _ = files_download.file_download_public(file_addr, None).await?;

// Ok(())
// }
Expand Down Expand Up @@ -252,7 +252,7 @@
// let mut files_download = FilesDownload::new(files_api);
// assert!(
// matches!(
// files_download.download_file(content_addr, None).await,
// files_download.file_download_public(content_addr, None).await,
// Err(ClientError::Network(NetworkError::GetRecordError(
// GetRecordError::RecordNotFound
// )))
Expand Down
2 changes: 1 addition & 1 deletion ant-node/tests/verify_data_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ async fn store_chunks(

let random_bytes = Bytes::from(random_bytes);

client.data_put(random_bytes, wallet.into()).await?;
client.data_put_public(random_bytes, wallet.into()).await?;

uploaded_chunks_count += 1;

Expand Down
66 changes: 29 additions & 37 deletions autonomi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

// Put and fetch data.
let data_addr = client
.data_put(Bytes::from("Hello, World"), (&wallet).into())
.data_put_public(Bytes::from("Hello, World"), (&wallet).into())
.await?;
let _data_fetched = client.data_get(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_upload("files/to/upload".into(), &wallet).await?;
let dir_addr = client.dir_and_archive_upload_public("files/to/upload".into(), &wallet).await?;
client
.dir_download(dir_addr, "files/downloaded".into())
.dir_download_public(dir_addr, "files/downloaded".into())
.await?;

Ok(())
Expand All @@ -53,52 +53,44 @@ let wallet = Wallet::new_from_private_key(EvmNetwork::new_custom("<rpc URL>", "<

## Running tests

### Using a local EVM testnet
To run the tests, we can run a local network:

1. If you haven't, install Foundry, to be able to run Anvil
nodes: https://book.getfoundry.sh/getting-started/installation
2. Run a local EVM node:
1. Run a local EVM node:
> Note: To run the EVM node, Foundry is required to be installed: https://book.getfoundry.sh/getting-started/installation
```sh
cargo run --bin=evm-testnet
```

3. Run a local network with the `local` feature and use the local evm node.

```sh
cargo run --bin=antctl --features=local -- local run --build --clean --rewards-address=<ETHEREUM_ADDRESS> evm-local
```
```sh
cargo run --bin evm-testnet
```

4. Then run the tests with the `local` feature and pass the EVM params again:
2. Run a local network with the `local` feature and use the local EVM node.
```sh
cargo run --bin antctl --features local -- local run --build --clean --rewards-address <ETHEREUM_ADDRESS> evm-local
```

```sh
EVM_NETWORK=local cargo test --package autonomi --features=local
# Or with logs
RUST_LOG=autonomi EVM_NETWORK=local cargo test --package autonomi --features local -- --nocapture
```
3. Then run the tests with the `local` feature and pass the EVM params again:
```sh
EVM_NETWORK=local cargo test --features local --package autonomi
```

### Using a live testnet or mainnet

Using the hardcoded `Arbitrum One` option as an example, but you can also use the command flags of the steps above and
point it to a live network.
Using the hardcoded `Arbitrum One` option as an example, but you can also use the command flags of the steps above and point it to a live network.

1. Run a local network with the `local` feature:

```sh
cargo run --bin=antctl --features=local -- local run --build --clean --rewards-address=<ETHEREUM_ADDRESS> evm-arbitrum-one
cargo run --bin antctl --features local -- local run --build --clean --rewards-address <ETHEREUM_ADDRESS> evm-arbitrum-one
```

2. Then run the tests with the `local` feature. Make sure that the wallet of the private key you pass has enough gas and
payment tokens on the network (in this case Arbitrum One):
2. Then pass the private key of the wallet, and ensure it has enough gas and payment tokens on the network (in this case Arbitrum One):

```sh
EVM_NETWORK=arbitrum-one EVM_PRIVATE_KEY=<PRIVATE_KEY> cargo test --package=autonomi --features=local
EVM_NETWORK=arbitrum-one EVM_PRIVATE_KEY=<PRIVATE_KEY> cargo test --package autonomi --features local
```

## Using funds from the Deployer Wallet

You can use the `Deployer wallet private key` printed in the EVM node output to
initialise a wallet from with almost infinite gas and payment tokens. Example:
You can use the `Deployer wallet private key` printed in the EVM node output to initialise a wallet from with almost infinite gas and payment tokens. Example:

```rust
let rpc_url = "http://localhost:54370/";
Expand All @@ -107,25 +99,25 @@ let data_payments_address = "0x8464135c8F25Da09e49BC8782676a84730C318bC";
let private_key = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
let network = Network::Custom(CustomNetwork::new(
rpc_url,
payment_token_address,
data_payments_address,
rpc_url,
payment_token_address,
data_payments_address,
));
let deployer_wallet = Wallet::new_from_private_key(network, private_key).unwrap();
let receiving_wallet = Wallet::new_with_random_wallet(network);
// Send 10 payment tokens (atto)
let _ = deployer_wallet
.transfer_tokens(receiving_wallet.address(), Amount::from(10))
.await;
.transfer_tokens(receiving_wallet.address(), Amount::from(10))
.await;
```
Alternatively, you can provide the wallet address that should own all the gas and payment tokens to the EVM testnet
startup command using the `--genesis-wallet` flag:
```sh
cargo run --bin evm-testnet -- --genesis-wallet=<ETHEREUM_ADDRESS>
cargo run --bin evm-testnet -- --genesis-wallet <ETHEREUM_ADDRESS>
```
```shell
Expand Down
28 changes: 14 additions & 14 deletions autonomi/README_PYTHON.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ payment = PaymentOption.wallet(wallet)

# Upload data
data = b"Hello, Safe Network!"
addr = client.data_put(data, payment)
addr = client.data_put_public(data, payment)
print(f"Data uploaded to: {addr}")

# Download data
retrieved = client.data_get(addr)
retrieved = client.data_get_public(addr)
print(f"Retrieved: {retrieved.decode()}")
```

Expand All @@ -40,10 +40,10 @@ print(f"Retrieved: {retrieved.decode()}")

- `Client`: Main interface to the Autonomi network
- `connect(peers: List[str])`: Connect to network nodes
- `data_put(data: bytes, payment: PaymentOption)`: Upload data
- `data_get(addr: str)`: Download data
- `private_data_put(data: bytes, payment: PaymentOption)`: Store private data
- `private_data_get(access: PrivateDataAccess)`: Retrieve private data
- `data_put_public(data: bytes, payment: PaymentOption)`: Upload data
- `data_get_public(addr: str)`: Download data
- `data_put(data: bytes, payment: PaymentOption)`: Store private data
- `data_get(access: DataMapChunk)`: Retrieve private data
- `register_generate_key()`: Generate register key

- `Wallet`: Ethereum wallet management
Expand All @@ -56,16 +56,16 @@ print(f"Retrieved: {retrieved.decode()}")

#### Private Data

- `PrivateDataAccess`: Handle private data storage
- `DataMapChunk`: Handle private data storage
- `from_hex(hex: str)`: Create from hex string
- `to_hex()`: Convert to hex string
- `address()`: Get short reference address

```python
# Private data example
access = client.private_data_put(secret_data, payment)
access = client.data_put(secret_data, payment)
print(f"Private data stored at: {access.to_hex()}")
retrieved = client.private_data_get(access)
retrieved = client.data_get(access)
```

#### Registers
Expand Down Expand Up @@ -117,15 +117,15 @@ data, content_type = client.fetch_and_decrypt_vault(vault_key)
def handle_data_operations(client, payment):
# Upload text
text_data = b"Hello, Safe Network!"
text_addr = client.data_put(text_data, payment)
text_addr = client.data_put_public(text_data, payment)

# Upload binary data
with open("image.jpg", "rb") as f:
image_data = f.read()
image_addr = client.data_put(image_data, payment)
image_addr = client.data_put_public(image_data, payment)

# Download and verify
downloaded = client.data_get(text_addr)
downloaded = client.data_get_public(text_addr)
assert downloaded == text_data
```

Expand All @@ -138,11 +138,11 @@ def handle_private_data(client, payment):
data = json.dumps(secret).encode()

# Store privately
access = client.private_data_put(data, payment)
access = client.data_put(data, payment)
print(f"Access token: {access.to_hex()}")

# Retrieve
retrieved = client.private_data_get(access)
retrieved = client.data_get(access)
secret = json.loads(retrieved.decode())
```

Expand Down
4 changes: 2 additions & 2 deletions autonomi/examples/autonomi_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def connect_to_network(peers: list[str]) -> Client:

def upload_data(client: Client, data: bytes, payment: PaymentOption) -> str:
try:
addr = client.data_put(data, payment)
addr = client.data_put_public(data, payment)
print(f"Successfully uploaded data to: {addr}")
return addr
except Exception as e:
Expand All @@ -34,7 +34,7 @@ def upload_data(client: Client, data: bytes, payment: PaymentOption) -> str:

def download_data(client: Client, addr: str) -> bytes:
try:
data = client.data_get(addr)
data = client.data_get_public(addr)
print(f"Successfully downloaded {len(data)} bytes")
return data
except Exception as e:
Expand Down
8 changes: 4 additions & 4 deletions autonomi/examples/autonomi_data_registers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ def handle_data_operations(client: Client, payment: PaymentOption):

# Upload some text data
text_data = b"Hello, Safe Network!"
text_addr = client.data_put(text_data, payment)
text_addr = client.data_put_public(text_data, payment)
print(f"Text data uploaded to: {text_addr}")

# Upload binary data (like an image)
with open("example.jpg", "rb") as f:
image_data = f.read()
image_addr = client.data_put(image_data, payment)
image_addr = client.data_put_public(image_data, payment)
print(f"Image uploaded to: {image_addr}")

# Download and verify data
downloaded_text = client.data_get(text_addr)
downloaded_text = client.data_get_public(text_addr)
assert downloaded_text == text_data, "Text data verification failed!"
print("Text data verified successfully")

# Download and save image
downloaded_image = client.data_get(image_addr)
downloaded_image = client.data_get_public(image_addr)
with open("downloaded_example.jpg", "wb") as f:
f.write(downloaded_image)
print("Image downloaded successfully")
Expand Down
6 changes: 3 additions & 3 deletions autonomi/examples/autonomi_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ def main():

# Upload some data
data = b"Hello, Safe Network!"
addr = client.data_put(data, payment)
addr = client.data_put_public(data, payment)
print(f"Data uploaded to address: {addr}")

# Download the data back
downloaded = client.data_get(addr)
downloaded = client.data_get_public(addr)
print(f"Downloaded data: {downloaded.decode()}")

# You can also upload files
with open("example.txt", "rb") as f:
file_data = f.read()
file_addr = client.data_put(file_data, payment)
file_addr = client.data_put_public(file_data, payment)
print(f"File uploaded to address: {file_addr}")

if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 53cbf8d

Please sign in to comment.