Skip to content

Commit

Permalink
refactor: Transaction->LinkedList
Browse files Browse the repository at this point in the history
  • Loading branch information
dirvine committed Dec 20, 2024
1 parent 098d9d0 commit 29c0d6b
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 58 deletions.
4 changes: 2 additions & 2 deletions ant-networking/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use ant_protocol::storage::TransactionAddress;
use ant_protocol::storage::LinkedListAddress;
use ant_protocol::{messages::Response, storage::RecordKind, NetworkAddress, PrettyPrintRecordKey};
use libp2p::{
kad::{self, QueryId, Record},
Expand Down Expand Up @@ -135,7 +135,7 @@ pub enum NetworkError {

// ---------- Transaction Errors
#[error("Transaction not found: {0:?}")]
NoTransactionFoundInsideRecord(TransactionAddress),
NoTransactionFoundInsideRecord(LinkedListAddress),

// ---------- Store Error
#[error("No Store Cost Responses")]
Expand Down
4 changes: 2 additions & 2 deletions ant-networking/src/event/kad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
CLOSE_GROUP_SIZE,
};
use ant_protocol::{
storage::{try_serialize_record, RecordKind, Transaction},
storage::{try_serialize_record, LinkedList, RecordKind},
NetworkAddress, PrettyPrintRecordKey,
};
use itertools::Itertools;
Expand Down Expand Up @@ -412,7 +412,7 @@ impl SwarmDriver {
info!("For record {pretty_key:?} task {query_id:?}, found split record for a transaction, accumulated and sending them as a single record");
let accumulated_transactions = accumulated_transactions
.into_iter()
.collect::<Vec<Transaction>>();
.collect::<Vec<LinkedList>>();

let bytes = try_serialize_record(
&accumulated_transactions,
Expand Down
4 changes: 2 additions & 2 deletions ant-networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ use tokio::sync::{
};
use tokio::time::Duration;
use {
ant_protocol::storage::Transaction,
ant_protocol::storage::LinkedList,
ant_protocol::storage::{
try_deserialize_record, try_serialize_record, RecordHeader, RecordKind,
},
Expand Down Expand Up @@ -711,7 +711,7 @@ impl Network {
info!("For record {pretty_key:?} task found split record for a transaction, accumulated and sending them as a single record");
let accumulated_transactions = accumulated_transactions
.into_iter()
.collect::<Vec<Transaction>>();
.collect::<Vec<LinkedList>>();
let record = Record {
key: key.clone(),
value: try_serialize_record(&accumulated_transactions, RecordKind::Transaction)
Expand Down
8 changes: 4 additions & 4 deletions ant-networking/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{driver::GetRecordCfg, Network, NetworkError, Result};
use ant_protocol::storage::{Transaction, TransactionAddress};
use ant_protocol::storage::{LinkedList, LinkedListAddress};
use ant_protocol::{
storage::{try_deserialize_record, RecordHeader, RecordKind, RetryStrategy},
NetworkAddress, PrettyPrintRecordKey,
Expand All @@ -16,7 +16,7 @@ use libp2p::kad::{Quorum, Record};

impl Network {
/// Gets Transactions at TransactionAddress from the Network.
pub async fn get_transactions(&self, address: TransactionAddress) -> Result<Vec<Transaction>> {
pub async fn get_transactions(&self, address: LinkedListAddress) -> Result<Vec<LinkedList>> {
let key = NetworkAddress::from_transaction_address(address).to_record_key();
let get_cfg = GetRecordCfg {
get_quorum: Quorum::All,
Expand All @@ -35,10 +35,10 @@ impl Network {
}
}

pub fn get_transactions_from_record(record: &Record) -> Result<Vec<Transaction>> {
pub fn get_transactions_from_record(record: &Record) -> Result<Vec<LinkedList>> {
let header = RecordHeader::from_record(record)?;
if let RecordKind::Transaction = header.kind {
let transactions = try_deserialize_record::<Vec<Transaction>>(record)?;
let transactions = try_deserialize_record::<Vec<LinkedList>>(record)?;
Ok(transactions)
} else {
warn!(
Expand Down
20 changes: 10 additions & 10 deletions ant-node/src/put_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use crate::{node::Node, Error, Marker, Result};
use ant_evm::payment_vault::verify_data_payment;
use ant_evm::{AttoTokens, ProofOfPayment};
use ant_networking::NetworkError;
use ant_protocol::storage::Transaction;
use ant_protocol::storage::LinkedList;
use ant_protocol::{
storage::{
try_deserialize_record, try_serialize_record, Chunk, RecordHeader, RecordKind, RecordType,
Scratchpad, TransactionAddress,
Scratchpad, LinkedListAddress,
},
NetworkAddress, PrettyPrintRecordKey,
};
Expand Down Expand Up @@ -172,7 +172,7 @@ impl Node {
}
RecordKind::TransactionWithPayment => {
let (payment, transaction) =
try_deserialize_record::<(ProofOfPayment, Transaction)>(&record)?;
try_deserialize_record::<(ProofOfPayment, LinkedList)>(&record)?;

// check if the deserialized value's TransactionAddress matches the record's key
let net_addr = NetworkAddress::from_transaction_address(transaction.address());
Expand Down Expand Up @@ -354,7 +354,7 @@ impl Node {
}
RecordKind::Transaction => {
let record_key = record.key.clone();
let transactions = try_deserialize_record::<Vec<Transaction>>(&record)?;
let transactions = try_deserialize_record::<Vec<LinkedList>>(&record)?;
self.validate_merge_and_store_transactions(transactions, &record_key)
.await
}
Expand Down Expand Up @@ -559,14 +559,14 @@ impl Node {
/// If we already have a transaction at this address, the Vec is extended and stored.
pub(crate) async fn validate_merge_and_store_transactions(
&self,
transactions: Vec<Transaction>,
transactions: Vec<LinkedList>,
record_key: &RecordKey,
) -> Result<()> {
let pretty_key = PrettyPrintRecordKey::from(record_key);
debug!("Validating transactions before storage at {pretty_key:?}");

// only keep transactions that match the record key
let transactions_for_key: Vec<Transaction> = transactions
let transactions_for_key: Vec<LinkedList> = transactions
.into_iter()
.filter(|s| {
// get the record key for the transaction
Expand All @@ -591,7 +591,7 @@ impl Node {
}

// verify the transactions
let mut validated_transactions: BTreeSet<Transaction> = transactions_for_key
let mut validated_transactions: BTreeSet<LinkedList> = transactions_for_key
.into_iter()
.filter(|t| t.verify())
.collect();
Expand All @@ -608,7 +608,7 @@ impl Node {
// add local transactions to the validated transactions, turn to Vec
let local_txs = self.get_local_transactions(addr).await?;
validated_transactions.extend(local_txs.into_iter());
let validated_transactions: Vec<Transaction> = validated_transactions.into_iter().collect();
let validated_transactions: Vec<LinkedList> = validated_transactions.into_iter().collect();

// store the record into the local storage
let record = Record {
Expand Down Expand Up @@ -764,7 +764,7 @@ impl Node {

/// Get the local transactions for the provided `TransactionAddress`
/// This only fetches the transactions from the local store and does not perform any network operations.
async fn get_local_transactions(&self, addr: TransactionAddress) -> Result<Vec<Transaction>> {
async fn get_local_transactions(&self, addr: LinkedListAddress) -> Result<Vec<LinkedList>> {
// get the local transactions
let record_key = NetworkAddress::from_transaction_address(addr).to_record_key();
debug!("Checking for local transactions with key: {record_key:?}");
Expand All @@ -783,7 +783,7 @@ impl Node {
error!("Found a {record_kind} when expecting to find Spend at {addr:?}");
return Err(NetworkError::RecordKindMismatch(RecordKind::Transaction).into());
}
let local_transactions: Vec<Transaction> = try_deserialize_record(&local_record)?;
let local_transactions: Vec<LinkedList> = try_deserialize_record(&local_record)?;
Ok(local_transactions)
}
}
5 changes: 3 additions & 2 deletions ant-protocol/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{storage::RegisterAddress, NetworkAddress, PrettyPrintRecordKey};
use crate::{NetworkAddress, PrettyPrintRecordKey};
use ant_registers::RegisterAddress;
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// A specialised `Result` type for protocol crate.
pub type Result<T> = std::result::Result<T, Error>;

/// Main error types for the SAFE protocol.
#[derive(Error, Clone, PartialEq, Eq, Serialize, Deserialize, custom_debug::Debug)]
#[derive(Error, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Error {
// ---------- Misc errors
Expand Down
10 changes: 5 additions & 5 deletions ant-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub mod antnode_proto {
pub use error::Error;
use storage::ScratchpadAddress;

use self::storage::{ChunkAddress, RegisterAddress, TransactionAddress};
use self::storage::{ChunkAddress, LinkedListAddress, RegisterAddress};

/// Re-export of Bytes used throughout the protocol
pub use bytes::Bytes;
Expand Down Expand Up @@ -95,7 +95,7 @@ pub enum NetworkAddress {
/// The NetworkAddress is representing a ChunkAddress.
ChunkAddress(ChunkAddress),
/// The NetworkAddress is representing a TransactionAddress.
TransactionAddress(TransactionAddress),
TransactionAddress(LinkedListAddress),
/// The NetworkAddress is representing a ChunkAddress.
RegisterAddress(RegisterAddress),
/// The NetworkAddress is representing a RecordKey.
Expand All @@ -111,7 +111,7 @@ impl NetworkAddress {
}

/// Return a `NetworkAddress` representation of the `TransactionAddress`.
pub fn from_transaction_address(transaction_address: TransactionAddress) -> Self {
pub fn from_transaction_address(transaction_address: LinkedListAddress) -> Self {
NetworkAddress::TransactionAddress(transaction_address)
}
/// Return a `NetworkAddress` representation of the `TransactionAddress`.
Expand Down Expand Up @@ -413,14 +413,14 @@ impl std::fmt::Debug for PrettyPrintRecordKey<'_> {

#[cfg(test)]
mod tests {
use crate::storage::TransactionAddress;
use crate::storage::LinkedListAddress;
use crate::NetworkAddress;
use bls::rand::thread_rng;

#[test]
fn verify_transaction_addr_is_actionable() {
let xorname = xor_name::XorName::random(&mut thread_rng());
let transaction_addr = TransactionAddress::new(xorname);
let transaction_addr = LinkedListAddress::new(xorname);
let net_addr = NetworkAddress::from_transaction_address(transaction_addr);

let transaction_addr_hex = &transaction_addr.to_hex()[0..6]; // we only log the first 6 chars
Expand Down
4 changes: 2 additions & 2 deletions ant-protocol/src/storage/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

mod chunk;
mod scratchpad;
mod transaction;
mod linked_list;

pub use self::chunk::ChunkAddress;
pub use self::scratchpad::ScratchpadAddress;
pub use self::transaction::TransactionAddress;
pub use self::linked_list::LinkedListAddress;
pub use ant_registers::RegisterAddress;
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use xor_name::XorName;

/// Address of a transaction, is derived from the owner's public key
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub struct TransactionAddress(pub XorName);
pub struct LinkedListAddress(pub XorName);

impl TransactionAddress {
impl LinkedListAddress {
pub fn from_owner(owner: PublicKey) -> Self {
Self(XorName::from_content(&owner.to_bytes()))
}
Expand All @@ -32,7 +32,7 @@ impl TransactionAddress {
}
}

impl std::fmt::Debug for TransactionAddress {
impl std::fmt::Debug for LinkedListAddress {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "TransactionAddress({})", &self.to_hex()[0..6])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use super::address::TransactionAddress;
use super::address::LinkedListAddress;
use bls::SecretKey;
use serde::{Deserialize, Serialize};

// re-exports
pub use bls::{PublicKey, Signature};

/// Content of a transaction, limited to 32 bytes
pub type TransactionContent = [u8; 32];
pub type LinkedListContent = [u8; 32];

/// A generic Transaction on the Network
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash, Ord, PartialOrd)]
pub struct Transaction {
pub struct LinkedList {
pub owner: PublicKey,
pub parents: Vec<PublicKey>,
pub content: TransactionContent,
pub outputs: Vec<(PublicKey, TransactionContent)>,
pub content: LinkedListContent,
pub outputs: Vec<(PublicKey, LinkedListContent)>,
/// signs the above 4 fields with the owners key
pub signature: Signature,
}

impl Transaction {
impl LinkedList {
/// Create a new transaction, signing it with the provided secret key.
pub fn new(
owner: PublicKey,
parents: Vec<PublicKey>,
content: TransactionContent,
outputs: Vec<(PublicKey, TransactionContent)>,
content: LinkedListContent,
outputs: Vec<(PublicKey, LinkedListContent)>,
signing_key: &SecretKey,
) -> Self {
let signature = signing_key.sign(Self::bytes_to_sign(&owner, &parents, &content, &outputs));
Expand All @@ -50,8 +50,8 @@ impl Transaction {
pub fn new_with_signature(
owner: PublicKey,
parents: Vec<PublicKey>,
content: TransactionContent,
outputs: Vec<(PublicKey, TransactionContent)>,
content: LinkedListContent,
outputs: Vec<(PublicKey, LinkedListContent)>,
signature: Signature,
) -> Self {
Self {
Expand All @@ -68,7 +68,7 @@ impl Transaction {
owner: &PublicKey,
parents: &[PublicKey],
content: &[u8],
outputs: &[(PublicKey, TransactionContent)],
outputs: &[(PublicKey, LinkedListContent)],
) -> Vec<u8> {
let mut bytes = Vec::new();
bytes.extend_from_slice(&owner.to_bytes());
Expand All @@ -92,8 +92,8 @@ impl Transaction {
bytes
}

pub fn address(&self) -> TransactionAddress {
TransactionAddress::from_owner(self.owner)
pub fn address(&self) -> LinkedListAddress {
LinkedListAddress::from_owner(self.owner)
}

/// Get the bytes that the signature is calculated from.
Expand Down
6 changes: 3 additions & 3 deletions ant-protocol/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
mod address;
mod chunks;
mod header;
mod linked_list;
mod scratchpad;
mod transaction;

use core::fmt;
use exponential_backoff::Backoff;
use std::{num::NonZeroUsize, time::Duration};

pub use self::{
address::{ChunkAddress, RegisterAddress, ScratchpadAddress, TransactionAddress},
address::{ChunkAddress, LinkedListAddress, RegisterAddress, ScratchpadAddress},
chunks::Chunk,
header::{try_deserialize_record, try_serialize_record, RecordHeader, RecordKind, RecordType},
linked_list::LinkedList,
scratchpad::Scratchpad,
transaction::Transaction,
};

/// A strategy that translates into a configuration for exponential backoff.
Expand Down
Loading

0 comments on commit 29c0d6b

Please sign in to comment.