Chunks

Chunks are native data types in the Autonomi Network:

  • Size: 4MB of raw bytes

  • Content-addressed: Address is the hash of its content

  • Immutable & self-verifiable: Once stored, data cannot be modified, and its integrity can be verified by computing the hash and comparing it to the address.

Client Methods

  • chunk_get Retrieves a chunk from the network by its address.

  • chunk_put Uploads a chunk to the network with payment handling. Returns the total cost and the chunk's address.

  • chunk_cost Estimates the storage cost for a chunk.

  • chunk_batch_upload Uploads multiple chunks efficiently with a single payment receipt.

Examples

Basic Chunk Operations

use autonomi::Client;
use autonomi::client::payment::PaymentOption;
use autonomi::client::chunk::{Chunk, Bytes};
use test_utils::evm::get_funded_wallet;
use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
    // Initialize a local client and test wallet
    let client = Client::init_local().await?;
    let wallet = get_funded_wallet();

    // Create a chunk with some data
    let chunk = Chunk::new(Bytes::from("Hello, world!"));
    println!("Created chunk with size: {} bytes", chunk.size());

    // Estimate cost before uploading
    let cost = client.chunk_cost(chunk.address()).await?;
    println!("Estimated storage cost: {cost}");

    // Upload chunk with payment
    let payment_option = PaymentOption::from(&wallet);
    let (put_cost, addr) = client.chunk_put(&chunk, payment_option).await?;
    assert_eq!(addr, *chunk.address());
    println!("Chunk uploaded for: {put_cost} at address: {}", addr.to_hex());

    // Wait for replication across the network
    tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;

    // Retrieve and verify the chunk
    let retrieved_chunk = client.chunk_get(&addr).await?;
    assert_eq!(retrieved_chunk, chunk);
    println!("Chunk retrieved successfully, data: {}", 
             String::from_utf8_lossy(retrieved_chunk.value()));
    
    Ok(())
}

Batch Upload Operations

For uploading multiple chunks efficiently:

Error Handling

circle-exclamation

Advanced Configuration

Performance Tuning

Streaming Data

The data_stream_public method returns an iterator that yields chunks of data as they arrive from the network:

Rust

Size Limits and Best Practices

  • Maximum chunk size: 4MB raw data (Chunk::MAX_RAW_SIZE)

  • Content addressing: Each chunk's address is the hash of its content

  • Immutability: Chunks cannot be modified once stored

  • Batch operations: Use chunk_batch_upload() for multiple chunks to reduce costs

  • Streaming: Use data_stream_public() or data_stream() (private data) for efficient large data transfers

  • Caching: Enable chunk caching for frequently accessed data

  • Error handling: Always handle network errors and size limit violations

Last updated