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

Tweak some file and register apis #2451

Closed
Closed
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion autonomi-cli/src/commands/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ 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(dir_path, &wallet)
.await
.wrap_err("Failed to upload file")?;
local_addr = addr_to_str(xor_name);
Expand Down
4 changes: 2 additions & 2 deletions autonomi-cli/src/commands/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub async fn create(name: &str, value: &str, public: bool, peers: Vec<Multiaddr>
let permissions = RegisterPermissions::new_anyone_can_write();
client
.register_create_with_permissions(
value.as_bytes().to_vec().into(),
Some(value.as_bytes().to_vec().into()),
name,
register_key,
permissions,
Expand All @@ -80,7 +80,7 @@ pub async fn create(name: &str, value: &str, public: bool, peers: Vec<Multiaddr>
info!("With private write access");
client
.register_create(
value.as_bytes().to_vec().into(),
Some(value.as_bytes().to_vec().into()),
name,
register_key,
&wallet,
Expand Down
1 change: 1 addition & 0 deletions autonomi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ sha2 = "0.10.6"
blst = "0.3.13"
blstrs = "0.7.1"
pyo3 = { version = "0.20", optional = true, features = ["extension-module", "abi3-py38"] }
crdts = "7.3.2"

[dev-dependencies]
alloy = { version = "0.5.3", default-features = false, features = ["std", "reqwest-rustls-tls", "provider-anvil-node", "sol-types", "json", "signers", "contract", "signer-local", "network"] }
Expand Down
31 changes: 25 additions & 6 deletions autonomi/src/client/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,24 @@ impl Client {

/// Upload a directory to the network. The directory is recursively walked.
/// Reads all files, splits into chunks, uploads chunks, uploads datamaps, uploads archive, returns ArchiveAddr (pointing to the archive)
pub async fn dir_upload(
pub async fn dir_and_archive_upload(
&self,
dir_path: PathBuf,
wallet: &EvmWallet,
) -> Result<ArchiveAddr, UploadError> {
match self.dir_upload(dir_path.clone(), wallet).await {
Ok(archive) => Ok(self.archive_upload(&archive, wallet).await?),
Err(e) => Err(e),
}
}

/// Upload a directory to the network. The directory is recursively walked.
/// Reads all files, splits into chunks, uploads chunks, uploads datamaps, uploads archive, returns Archive but does not upload that to the network.
pub async fn dir_upload(
&self,
dir_path: PathBuf,
wallet: &EvmWallet,
) -> Result<Archive, UploadError> {
info!("Uploading directory: {dir_path:?}");
let start = tokio::time::Instant::now();

Expand Down Expand Up @@ -152,14 +165,20 @@ impl Client {
}
}

// upload archive
let archive_serialized = archive.into_bytes()?;
let arch_addr = self.data_put(archive_serialized, wallet.into()).await?;

info!("Complete archive upload completed in {:?}", start.elapsed());
#[cfg(feature = "loud")]
println!("Upload completed in {:?}", start.elapsed());
Ok(arch_addr)
Ok(archive)
}

/// Upload a directory Archive to the network
pub async fn archive_upload(
&self,
archive: &Archive,
wallet: &EvmWallet,
) -> Result<ArchiveAddr, UploadError> {
let archive_serialized = archive.into_bytes()?;
Ok(self.data_put(archive_serialized, wallet.into()).await?)
}

/// Upload a file to the network.
Expand Down
9 changes: 5 additions & 4 deletions autonomi/src/client/registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/// Register Secret Key
pub use bls::SecretKey as RegisterSecretKey;

use sn_evm::Amount;
use sn_evm::AttoTokens;
use sn_evm::EvmWalletError;
Expand Down Expand Up @@ -260,12 +261,12 @@ impl Client {
RegisterAddress::new(name, pk)
}

/// Creates a new Register with a name and an initial value and uploads it to the network.
/// Creates a new Register with a name and optional initial value and uploads it to the network.
///
/// The Register is created with the owner as the only writer.
pub async fn register_create(
&self,
value: Bytes,
value: Option<Bytes>,
name: &str,
owner: RegisterSecretKey,
wallet: &EvmWallet,
Expand All @@ -282,7 +283,7 @@ impl Client {
/// Unlike `register_create`, this function allows you to specify the permissions for the register.
pub async fn register_create_with_permissions(
&self,
value: Bytes,
value: Option<Bytes>,
name: &str,
owner: RegisterSecretKey,
permissions: RegisterPermissions,
Expand All @@ -292,7 +293,7 @@ impl Client {
let name = XorName::from_content_parts(&[name.as_bytes()]);

// Owner can write to the register.
let register = Register::new(Some(value), name, owner, permissions)?;
let register = Register::new(value, name, owner, permissions)?;
let address = register.address();

let reg_xor = address.xorname();
Expand Down
4 changes: 2 additions & 2 deletions autonomi/tests/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async fn dir_upload_download() -> Result<()> {
let wallet = get_funded_wallet();

let addr = client
.dir_upload("tests/file/test_dir".into(), &wallet)
.dir_and_archive_upload("tests/file/test_dir".into(), &wallet)
.await?;

sleep(Duration::from_secs(10)).await;
Expand Down Expand Up @@ -86,7 +86,7 @@ async fn file_into_vault() -> Result<()> {
let client_sk = bls::SecretKey::random();

let addr = client
.dir_upload("tests/file/test_dir".into(), &wallet)
.dir_and_archive_upload("tests/file/test_dir".into(), &wallet)
.await?;
sleep(Duration::from_secs(2)).await;

Expand Down
7 changes: 6 additions & 1 deletion autonomi/tests/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ async fn register() -> Result<()> {
.map(char::from)
.collect();
let register = client
.register_create(vec![1, 2, 3, 4].into(), &rand_name, key.clone(), &wallet)
.register_create(
Some(vec![1, 2, 3, 4].into()),
&rand_name,
key.clone(),
&wallet,
)
.await
.unwrap();

Expand Down
7 changes: 6 additions & 1 deletion sn_node/tests/data_with_churn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,12 @@ fn create_registers_task(
let mut retries = 1;
loop {
match client
.register_create(random_data.clone(), &random_name, owner.clone(), &wallet)
.register_create(
Some(random_data.clone()),
&random_name,
owner.clone(),
&wallet,
)
.await
{
Ok(register) => {
Expand Down
7 changes: 6 additions & 1 deletion sn_node/tests/verify_data_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,12 @@ async fn store_registers(
.map(char::from)
.collect();
let register = client
.register_create(vec![1, 2, 3, 4].into(), &rand_name, key.clone(), wallet)
.register_create(
Some(vec![1, 2, 3, 4].into()),
&rand_name,
key.clone(),
wallet,
)
.await?;

println!("Created Register at {:?}", register.address());
Expand Down
Loading