Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
cobite authored Jun 5, 2024
2 parents afd2921 + 4d27ec0 commit 28d077b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ jobs:
cargo test --release --package sn_client --lib
cargo test --release --package sn_client --doc
- name: Run node tests
timeout-minutes: 25
run: cargo test --release --package sn_node --lib

- name: Run network tests
timeout-minutes: 25
run: cargo test --release --package sn_networking
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ jobs:
cargo test --release --package sn_client --bins
cargo test --release --package sn_client --examples
- name: Run node tests
timeout-minutes: 25
run: cargo test --release --package sn_node --lib

- name: Run network tests
timeout-minutes: 25
run: cargo test --release -p sn_networking
Expand Down
53 changes: 46 additions & 7 deletions sn_node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,23 +902,36 @@ impl Node {
let balance_file_path = network.root_dir_path.join(FORWARDED_BALANCE_FILE_NAME);
let old_balance = read_forwarded_balance_value(&balance_file_path);
let updated_balance = old_balance + total_forwarded_amount;
if let Err(err) = std::fs::write(&balance_file_path, updated_balance.to_string()) {
error!(
"Failed to write the updated balance to the file {balance_file_path:?} with {err:?}"
);
}
trace!("Updating forwarded balance to {updated_balance}");
write_forwarded_balance_value(&balance_file_path, updated_balance)?;

Ok(updated_balance)
}
}

fn read_forwarded_balance_value(balance_file_path: &PathBuf) -> u64 {
trace!("Reading forwarded balance from file {balance_file_path:?}");
match std::fs::read_to_string(balance_file_path) {
Ok(balance) => balance.parse::<u64>().unwrap_or(0),
Err(_) => 0,
Ok(balance) => balance.parse::<u64>().unwrap_or_else(|_| {
trace!("The balance from file is not a valid number");
0
}),
Err(_) => {
trace!("Error while reading to string, setting the balance to 0. This can happen at node init.");
0
}
}
}

fn write_forwarded_balance_value(balance_file_path: &PathBuf, balance: u64) -> Result<()> {
if let Err(err) = std::fs::write(balance_file_path, balance.to_string()) {
error!(
"Failed to write the updated balance to the file {balance_file_path:?} with {err:?}"
);
}
Ok(())
}

async fn chunk_proof_verify_peer(
network: &Network,
peer_id: PeerId,
Expand Down Expand Up @@ -983,3 +996,29 @@ fn received_valid_chunk_proof(
None
}
}

#[cfg(test)]
mod tests {

use crate::node::{read_forwarded_balance_value, write_forwarded_balance_value};
use color_eyre::Result;
use tempfile::tempdir;
#[test]
fn read_and_write_reward_to_file() -> Result<()> {
let dir = tempdir()?;
let balance_file_path = dir.path().join("forwarded_balance");

let balance = read_forwarded_balance_value(&balance_file_path);
assert_eq!(balance, 0);

write_forwarded_balance_value(&balance_file_path, balance + 10)?;
let balance = read_forwarded_balance_value(&balance_file_path);
assert_eq!(balance, 10);

write_forwarded_balance_value(&balance_file_path, balance + 100)?;
let balance = read_forwarded_balance_value(&balance_file_path);
assert_eq!(balance, 110);

Ok(())
}
}

0 comments on commit 28d077b

Please sign in to comment.