Skip to content

Commit

Permalink
Merge branch 'main' of github.com:harismuzaffer/safe_network into use…
Browse files Browse the repository at this point in the history
…-default-nodes-dir-root-primary-mt-pt
  • Loading branch information
harismuzaffer committed Jan 1, 2025
2 parents 14f2f4f + cc90c70 commit 3f1d664
Show file tree
Hide file tree
Showing 66 changed files with 9,257 additions and 250 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Deploy Documentation
on:
push:
branches:
- main
- data_further_refactor
pull_request:
branches:
- main

permissions:
contents: write

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install mkdocs-material mkdocstrings mkdocstrings-python mkdocs-git-revision-date-localized-plugin
- name: Deploy Documentation
run: |
git config --global user.name "github-actions"
git config --global user.email "[email protected]"
mkdocs gh-deploy --force
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ uv.lock
*.swp

/vendor/


# Node.js
node_modules/

# MkDocs
site/
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 40 additions & 23 deletions ant-evm/src/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ impl FromStr for AttoTokens {
EvmError::FailedToParseAttoToken("Can't parse token units".to_string())
})?;

// Check if the units part is too large before multiplication
if units > Amount::from(u64::MAX) {
return Err(EvmError::ExcessiveValue);
}

units
.checked_mul(Amount::from(TOKEN_TO_RAW_CONVERSION))
.ok_or(EvmError::ExcessiveValue)?
Expand All @@ -114,6 +119,9 @@ impl FromStr for AttoTokens {
let remainder_conversion = TOKEN_TO_RAW_POWER_OF_10_CONVERSION
.checked_sub(remainder_str.len() as u64)
.ok_or(EvmError::LossOfPrecision)?;
if remainder_conversion > 32 {
return Err(EvmError::LossOfPrecision);
}
parsed_remainder * Amount::from(10).pow(Amount::from(remainder_conversion))
}
};
Expand All @@ -126,7 +134,7 @@ impl Display for AttoTokens {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
let unit = self.0 / Amount::from(TOKEN_TO_RAW_CONVERSION);
let remainder = self.0 % Amount::from(TOKEN_TO_RAW_CONVERSION);
write!(formatter, "{unit}.{remainder:09}")
write!(formatter, "{unit}.{remainder:032}")
}
}

Expand Down Expand Up @@ -160,24 +168,28 @@ mod tests {
AttoTokens::from_str("1.000000000000000001")?
);
assert_eq!(
AttoTokens::from_u64(1_100_000_000),
AttoTokens::from_u64(1_100_000_000_000_000_000),
AttoTokens::from_str("1.1")?
);
assert_eq!(
AttoTokens::from_u64(1_100_000_000_000_000_001),
AttoTokens::from_str("1.100000000000000001")?
);
assert_eq!(
AttoTokens::from_u128(4_294_967_295_000_000_000_000_000_000u128),
AttoTokens::from_str("4294967295")?
AttoTokens::from_u128(4_294_967_295_000_000_000_000_000u128),
AttoTokens::from_str("4294967.295")?
);
assert_eq!(
AttoTokens::from_u128(4_294_967_295_999_999_999_000_000u128),
AttoTokens::from_str("4294967.295999999999")?,
);
assert_eq!(
AttoTokens::from_u128(4_294_967_295_999_999_999_000_000_000_000_000u128),
AttoTokens::from_str("4294967295.999999999")?,
AttoTokens::from_u128(4_294_967_295_999_999_999_000_000u128),
AttoTokens::from_str("4294967.2959999999990000")?,
);
assert_eq!(
AttoTokens::from_u128(4_294_967_295_999_999_999_000_000_000_000_000u128),
AttoTokens::from_str("4294967295.9999999990000")?,
AttoTokens::from_u128(18_446_744_074_000_000_000_000_000_000u128),
AttoTokens::from_str("18446744074")?
);

assert_eq!(
Expand All @@ -200,30 +212,39 @@ mod tests {
);
assert_eq!(
Err(EvmError::LossOfPrecision),
AttoTokens::from_str("0.0000000009")
AttoTokens::from_str("0.0000000000000000001")
);
assert_eq!(
Err(EvmError::ExcessiveValue),
AttoTokens::from_str("18446744074")
AttoTokens::from_str("340282366920938463463374607431768211455")
);
Ok(())
}

#[test]
fn display() {
assert_eq!("0.000000000", format!("{}", AttoTokens::from_u64(0)));
assert_eq!("0.000000001", format!("{}", AttoTokens::from_u64(1)));
assert_eq!("0.000000010", format!("{}", AttoTokens::from_u64(10)));
assert_eq!(
"1.000000000",
"0.00000000000000000000000000000000",
format!("{}", AttoTokens::from_u64(0))
);
assert_eq!(
"0.00000000000000000000000000000001",
format!("{}", AttoTokens::from_u64(1))
);
assert_eq!(
"0.00000000000000000000000000000010",
format!("{}", AttoTokens::from_u64(10))
);
assert_eq!(
"1.00000000000000000000000000000000",
format!("{}", AttoTokens::from_u64(1_000_000_000_000_000_000))
);
assert_eq!(
"1.000000001",
"1.00000000000000000000000000000001",
format!("{}", AttoTokens::from_u64(1_000_000_000_000_000_001))
);
assert_eq!(
"4294967295.000000000",
"4.00000000000000294967295000000000",
format!("{}", AttoTokens::from_u64(4_294_967_295_000_000_000))
);
}
Expand All @@ -235,11 +256,11 @@ mod tests {
AttoTokens::from_u64(1).checked_add(AttoTokens::from_u64(2))
);
assert_eq!(
None,
Some(AttoTokens::from_u128(u64::MAX as u128 + 1)),
AttoTokens::from_u64(u64::MAX).checked_add(AttoTokens::from_u64(1))
);
assert_eq!(
None,
Some(AttoTokens::from_u128(u64::MAX as u128 * 2)),
AttoTokens::from_u64(u64::MAX).checked_add(AttoTokens::from_u64(u64::MAX))
);

Expand All @@ -249,11 +270,7 @@ mod tests {
);
assert_eq!(
None,
AttoTokens::from_u64(0).checked_sub(AttoTokens::from_u64(u64::MAX))
);
assert_eq!(
None,
AttoTokens::from_u64(10).checked_sub(AttoTokens::from_u64(11))
AttoTokens::from_u64(0).checked_sub(AttoTokens::from_u64(1))
);
}
}
1 change: 0 additions & 1 deletion ant-networking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ version = "0.3.1"

[features]
default = []
encrypt-records = []
local = ["libp2p/mdns"]
loud = []
open-metrics = ["libp2p/metrics", "prometheus-client", "hyper", "sysinfo"]
Expand Down
4 changes: 2 additions & 2 deletions ant-networking/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,14 +664,14 @@ impl SwarmDriver {
RecordKind::Chunk => RecordType::Chunk,
RecordKind::Scratchpad => RecordType::Scratchpad,
RecordKind::Pointer => RecordType::Pointer,
RecordKind::LinkedList | RecordKind::Register => {
RecordKind::GraphEntry | RecordKind::Register => {
let content_hash = XorName::from_content(&record.value);
RecordType::NonChunk(content_hash)
}
RecordKind::ChunkWithPayment
| RecordKind::RegisterWithPayment
| RecordKind::PointerWithPayment
| RecordKind::LinkedListWithPayment
| RecordKind::GraphEntryWithPayment
| RecordKind::ScratchpadWithPayment => {
error!("Record {record_key:?} with payment shall not be stored locally.");
return Err(NetworkError::InCorrectRecordHeader);
Expand Down
9 changes: 4 additions & 5 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::LinkedListAddress;
use ant_protocol::storage::GraphEntryAddress;
use ant_protocol::{messages::Response, storage::RecordKind, NetworkAddress, PrettyPrintRecordKey};
use libp2p::{
kad::{self, QueryId, Record},
Expand Down Expand Up @@ -123,14 +123,13 @@ pub enum NetworkError {
#[error("Record header is incorrect")]
InCorrectRecordHeader,


// ---------- Chunk Errors
#[error("Failed to verify the ChunkProof with the provided quorum")]
FailedToVerifyChunkProof(NetworkAddress),

// ---------- LinkedList Errors
#[error("Linked list not found: {0:?}")]
NoLinkedListFoundInsideRecord(LinkedListAddress),
// ---------- Graph Errors
#[error("Graph entry not found: {0:?}")]
NoGraphEntryFoundInsideRecord(GraphEntryAddress),

// ---------- Store Error
#[error("No Store Cost Responses")]
Expand Down
10 changes: 5 additions & 5 deletions ant-networking/src/event/kad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{
driver::PendingGetClosestType, get_linked_list_from_record, get_quorum_value,
driver::PendingGetClosestType, get_graph_entry_from_record, get_quorum_value,
target_arch::Instant, GetRecordCfg, GetRecordError, NetworkError, Result, SwarmDriver,
CLOSE_GROUP_SIZE,
};
use ant_protocol::{
storage::{try_serialize_record, LinkedList, RecordKind},
storage::{try_serialize_record, GraphEntry, RecordKind},
NetworkAddress, PrettyPrintRecordKey,
};
use itertools::Itertools;
Expand Down Expand Up @@ -399,7 +399,7 @@ impl SwarmDriver {
debug!("For record {pretty_key:?} task {query_id:?}, fetch completed with split record");
let mut accumulated_transactions = BTreeSet::new();
for (record, _) in result_map.values() {
match get_linked_list_from_record(record) {
match get_graph_entry_from_record(record) {
Ok(transactions) => {
accumulated_transactions.extend(transactions);
}
Expand All @@ -412,11 +412,11 @@ 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<LinkedList>>();
.collect::<Vec<GraphEntry>>();

let bytes = try_serialize_record(
&accumulated_transactions,
RecordKind::LinkedList,
RecordKind::GraphEntry,
)?;

let new_accumulated_record = Record {
Expand Down
20 changes: 10 additions & 10 deletions ant-networking/src/linked_list.rs → ant-networking/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{driver::GetRecordCfg, Network, NetworkError, Result};
use ant_protocol::storage::{LinkedList, LinkedListAddress};
use ant_protocol::storage::{GraphEntry, GraphEntryAddress};
use ant_protocol::{
storage::{try_deserialize_record, RecordHeader, RecordKind, RetryStrategy},
NetworkAddress, PrettyPrintRecordKey,
};
use libp2p::kad::{Quorum, Record};

impl Network {
/// Gets LinkedList at LinkedListAddress from the Network.
pub async fn get_linked_list(&self, address: LinkedListAddress) -> Result<Vec<LinkedList>> {
let key = NetworkAddress::from_linked_list_address(address).to_record_key();
/// Gets GraphEntry at GraphEntryAddress from the Network.
pub async fn get_graph_entry(&self, address: GraphEntryAddress) -> Result<Vec<GraphEntry>> {
let key = NetworkAddress::from_graph_entry_address(address).to_record_key();
let get_cfg = GetRecordCfg {
get_quorum: Quorum::All,
retry_strategy: Some(RetryStrategy::Quick),
Expand All @@ -31,20 +31,20 @@ impl Network {
PrettyPrintRecordKey::from(&record.key)
);

get_linked_list_from_record(&record)
get_graph_entry_from_record(&record)
}
}

pub fn get_linked_list_from_record(record: &Record) -> Result<Vec<LinkedList>> {
pub fn get_graph_entry_from_record(record: &Record) -> Result<Vec<GraphEntry>> {
let header = RecordHeader::from_record(record)?;
if let RecordKind::LinkedList = header.kind {
let transactions = try_deserialize_record::<Vec<LinkedList>>(record)?;
if let RecordKind::GraphEntry = header.kind {
let transactions = try_deserialize_record::<Vec<GraphEntry>>(record)?;
Ok(transactions)
} else {
warn!(
"RecordKind mismatch while trying to retrieve linked_list from record {:?}",
"RecordKind mismatch while trying to retrieve graph_entry from record {:?}",
PrettyPrintRecordKey::from(&record.key)
);
Err(NetworkError::RecordKindMismatch(RecordKind::LinkedList))
Err(NetworkError::RecordKindMismatch(RecordKind::GraphEntry))
}
}
Loading

0 comments on commit 3f1d664

Please sign in to comment.