Skip to content

Commit

Permalink
feat: fix docs and bad namings in API
Browse files Browse the repository at this point in the history
  • Loading branch information
grumbach committed Jan 10, 2025
1 parent a685123 commit db3723a
Show file tree
Hide file tree
Showing 18 changed files with 142 additions and 232 deletions.
22 changes: 10 additions & 12 deletions ant-protocol/src/storage/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct GraphEntry {
pub owner: PublicKey,
pub parents: Vec<PublicKey>,
pub content: GraphContent,
pub outputs: Option<Vec<(PublicKey, GraphContent)>>,
pub outputs: Vec<(PublicKey, GraphContent)>,
/// signs the above 4 fields with the owners key
pub signature: Signature,
}
Expand All @@ -33,7 +33,7 @@ impl GraphEntry {
owner: PublicKey,
parents: Vec<PublicKey>,
content: GraphContent,
outputs: Option<Vec<(PublicKey, GraphContent)>>,
outputs: Vec<(PublicKey, GraphContent)>,
signing_key: &SecretKey,
) -> Self {
let signature = signing_key.sign(Self::bytes_to_sign(&owner, &parents, &content, &outputs));
Expand All @@ -51,7 +51,7 @@ impl GraphEntry {
owner: PublicKey,
parents: Vec<PublicKey>,
content: GraphContent,
outputs: Option<Vec<(PublicKey, GraphContent)>>,
outputs: Vec<(PublicKey, GraphContent)>,
signature: Signature,
) -> Self {
Self {
Expand All @@ -68,7 +68,7 @@ impl GraphEntry {
owner: &PublicKey,
parents: &[PublicKey],
content: &[u8],
outputs: &Option<Vec<(PublicKey, GraphContent)>>,
outputs: &[(PublicKey, GraphContent)],
) -> Vec<u8> {
let mut bytes = Vec::new();
bytes.extend_from_slice(&owner.to_bytes());
Expand All @@ -83,14 +83,12 @@ impl GraphEntry {
bytes.extend_from_slice("content".as_bytes());
bytes.extend_from_slice(content);
bytes.extend_from_slice("outputs".as_bytes());
if let Some(outputs) = outputs {
bytes.extend_from_slice(
&outputs
.iter()
.flat_map(|(p, c)| [&p.to_bytes(), c.as_slice()].concat())
.collect::<Vec<_>>(),
);
}
bytes.extend_from_slice(
&outputs
.iter()
.flat_map(|(p, c)| [&p.to_bytes(), c.as_slice()].concat())
.collect::<Vec<_>>(),
);
bytes
}

Expand Down
8 changes: 4 additions & 4 deletions autonomi/nodejs/dist/linkedList.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { LinkedListOptions, PaymentOption } from './types';
export declare class LinkedList {
import { GarphEntryOptions, PaymentOption } from './types';
export declare class GarphEntry {
private nativeList;
private constructor();
static create(address: string): Promise<LinkedList>;
static create(address: string): Promise<GarphEntry>;
get(): Promise<any[]>;
put(options: LinkedListOptions, payment: PaymentOption): Promise<void>;
put(options: GarphEntryOptions, payment: PaymentOption): Promise<void>;
getCost(key: string): Promise<string>;
}
6 changes: 3 additions & 3 deletions autonomi/nodejs/dist/linkedList.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LinkedList = void 0;
class LinkedList {
exports.GarphEntry = void 0;
class GarphEntry {
constructor(nativeList) {
this.nativeList = nativeList;
}
Expand All @@ -22,4 +22,4 @@ class LinkedList {
throw new Error('Not implemented');
}
}
exports.LinkedList = LinkedList;
exports.GarphEntry = GarphEntry;
23 changes: 12 additions & 11 deletions autonomi/src/client/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ pub enum GraphError {
}

impl Client {
/// Fetches a Transaction from the network.
pub async fn transaction_get(
/// Fetches a GraphEntry from the network.
pub async fn graph_entry_get(
&self,
address: GraphEntryAddress,
) -> Result<Vec<GraphEntry>, GraphError> {
Expand All @@ -58,12 +58,13 @@ impl Client {
Ok(transactions)
}

pub async fn transaction_put(
/// Puts a GraphEntry to the network.
pub async fn graph_entry_put(
&self,
transaction: GraphEntry,
entry: GraphEntry,
wallet: &EvmWallet,
) -> Result<(), GraphError> {
let address = transaction.address();
let address = entry.address();

// pay for the transaction
let xor_name = address.xorname();
Expand All @@ -80,7 +81,7 @@ impl Client {
Some((proof, price)) => (proof, price),
None => {
// transaction was skipped, meaning it was already paid for
error!("Transaction at address: {address:?} was already paid for");
error!("GraphEntry at address: {address:?} was already paid for");
return Err(GraphError::AlreadyExists(address));
}
};
Expand All @@ -90,7 +91,7 @@ impl Client {
let record = Record {
key: NetworkAddress::from_graph_entry_address(address).to_record_key(),
value: try_serialize_record(
&(proof, &transaction),
&(proof, &entry),
RecordKind::DataWithPayment(DataTypes::GraphEntry),
)
.map_err(|_| GraphError::Serialization)?
Expand Down Expand Up @@ -136,10 +137,10 @@ impl Client {
Ok(())
}

/// Get the cost to create a transaction
pub async fn transaction_cost(&self, key: SecretKey) -> Result<AttoTokens, GraphError> {
/// Get the cost to create a GraphEntry
pub async fn graph_entry_cost(&self, key: SecretKey) -> Result<AttoTokens, GraphError> {
let pk = key.public_key();
trace!("Getting cost for transaction of {pk:?}");
trace!("Getting cost for GraphEntry of {pk:?}");

let address = GraphEntryAddress::from_owner(pk);
let xor = *address.xorname();
Expand All @@ -151,7 +152,7 @@ impl Client {
.map(|quote| quote.price())
.sum::<Amount>(),
);
debug!("Calculated the cost to create transaction of {pk:?} is {total_cost}");
debug!("Calculated the cost to create GraphEntry of {pk:?} is {total_cost}");
Ok(total_cost)
}
}
12 changes: 6 additions & 6 deletions autonomi/tests/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@ async fn transaction_put() -> Result<()> {

let key = bls::SecretKey::random();
let content = [0u8; 32];
let transaction = GraphEntry::new(key.public_key(), vec![], content, vec![].into(), &key);
let transaction = GraphEntry::new(key.public_key(), vec![], content, vec![], &key);

// estimate the cost of the transaction
let cost = client.transaction_cost(key.clone()).await?;
let cost = client.graph_entry_cost(key.clone()).await?;
println!("transaction cost: {cost}");

// put the transaction
client.transaction_put(transaction.clone(), &wallet).await?;
client.graph_entry_put(transaction.clone(), &wallet).await?;
println!("transaction put 1");

// wait for the transaction to be replicated
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;

// check that the transaction is stored
let txs = client.transaction_get(transaction.address()).await?;
let txs = client.graph_entry_get(transaction.address()).await?;
assert_eq!(txs, vec![transaction.clone()]);
println!("transaction got 1");

// try put another transaction with the same address
let content2 = [1u8; 32];
let transaction2 = GraphEntry::new(key.public_key(), vec![], content2, vec![].into(), &key);
let res = client.transaction_put(transaction2.clone(), &wallet).await;
let transaction2 = GraphEntry::new(key.public_key(), vec![], content2, vec![], &key);
let res = client.graph_entry_put(transaction2.clone(), &wallet).await;

assert!(matches!(
res,
Expand Down
76 changes: 31 additions & 45 deletions docs/online-documentation/api/autonomi-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,32 +186,32 @@ Mutable references with version tracking:
println!("Version: {}", metadata.version);
```

### 3. LinkedList
### 3. GraphEntry

Decentralized DAG structures for transaction chains:
Decentralized Graph structures for linked data:

=== "Node.js"
```typescript
import { LinkedList } from 'autonomi';
import { GraphEntry } from 'autonomi';

// Create a new linked list
const list = await client.createLinkedList();
// Create a new graph
const entry = await client.createGraphEntry();

// Append items
await client.appendToList(list.address, item1);
await client.appendToList(list.address, item2);
await client.appendToGraph(entry.address, item1);
await client.appendToGraph(entry.address, item2);

// Read list contents
const items = await client.getList(list.address);
// Read graph contents
const items = await client.getGraph(entry.address);

// Get list history
const history = await client.getListHistory(list.address);
// Get graph history
const history = await client.getGraphHistory(entry.address);
for (const entry of history) {
console.log(`Version ${entry.version}: ${entry.data}`);
}

// Check for forks
const forks = await client.detectForks(list.address);
const forks = await client.detectForks(entry.address);
if (!forks) {
console.log('No forks detected');
} else {
Expand All @@ -221,57 +221,43 @@ Decentralized DAG structures for transaction chains:

=== "Python"
```python
from autonomi import LinkedList
from autonomi import GraphEntry

# Create a new linked list
list = client.create_linked_list()
# Create a new graph
entry = client.create_graph_entry()

# Append items
client.append_to_list(list.address, item1)
client.append_to_list(list.address, item2)
client.append_to_graph(entry.address, item1)
client.append_to_graph(entry.address, item2)

# Read list contents
items = client.get_list(list.address)
items = client.get_graph(entry.address)

# Get list history
history = client.get_list_history(list.address)
# Get graph history
history = client.get_graph_history(entry.address)
for entry in history:
print(f"Version {entry.version}: {entry.data}")

# Check for forks
forks = client.detect_forks(list.address)
if not forks:
print("No forks detected")
else:
handle_forks(forks.branches)
```

=== "Rust"
```rust
use autonomi::LinkedList;
use autonomi::GraphEntry;

// Create a new linked list
let list = client.create_linked_list().await?;
// Create a new graph
let entry = client.create_graph_entry().await?;

// Append items
client.append_to_list(list.address(), item1).await?;
client.append_to_list(list.address(), item2).await?;
client.append_to_graph(entry.address(), item1).await?;
client.append_to_graph(entry.address(), item2).await?;

// Read list contents
let items = client.get_list(list.address()).await?;
// Read graph contents
let items = client.get_graph(entry.address()).await?;

// Get list history
let history = client.get_list_history(list.address()).await?;
// Get graph history
let history = client.get_graph_history(entry.address()).await?;
for entry in history {
println!("Version {}: {:?}", entry.version, entry.data);
}

// Check for forks
let forks = client.detect_forks(list.address()).await?;
match forks {
Fork::None => println!("No forks detected"),
Fork::Detected(branches) => handle_forks(branches),
}
```

### 4. ScratchPad
Expand Down Expand Up @@ -467,7 +453,7 @@ Each language provides appropriate error handling mechanisms:

=== "Rust"
```rust
use autonomi::error::{ChunkError, PointerError, ListError, ScratchPadError};
use autonomi::error::{ChunkError, PointerError, GraphError, ScratchPadError};

// Handle chunk operations
match client.get_chunk(address).await {
Expand Down Expand Up @@ -591,7 +577,7 @@ Each language provides appropriate error handling mechanisms:
1. **Data Type Selection**
- Use Chunks for immutable data
- Use Pointers for mutable references
- Use LinkedLists for ordered collections
- Use GraphEntrys for ordered collections
- Use ScratchPads for temporary data

2. **Error Handling**
Expand Down
12 changes: 6 additions & 6 deletions docs/online-documentation/api/autonomi-client/data_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ A mutable reference to data in the network.
}
```

## LinkedList
## GraphEntry

A decentralized DAG structure for ordered data.
A decentralized Graph structure for linked data.

=== "Node.js"
```typescript
interface LinkedList<T> {
interface GraphEntry<T> {
readonly address: Address;
readonly length: number;
append(item: T): void;
Expand All @@ -144,7 +144,7 @@ A decentralized DAG structure for ordered data.

=== "Python"
```python
class LinkedList(Generic[T]):
class GraphEntry(Generic[T]):
@property
def address(self) -> Address: ...
@property
Expand All @@ -156,12 +156,12 @@ A decentralized DAG structure for ordered data.

=== "Rust"
```rust
pub struct LinkedList<T> {
pub struct GraphEntry<T> {
pub address: Address,
pub length: usize,
}

impl<T> LinkedList<T> {
impl<T> GraphEntry<T> {
pub fn append(&mut self, item: T);
pub fn get(&self, index: usize) -> Option<&T>;
pub fn to_vec(&self) -> Vec<T>;
Expand Down
Loading

0 comments on commit db3723a

Please sign in to comment.