Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(node): add unit test for forwarded reward tracking cache logic #1847

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(())
}
}
Loading