Skip to content

Conversation

@petarvujovic98
Copy link
Collaborator

@petarvujovic98 petarvujovic98 commented Oct 20, 2025

This PR adds initial support for code fuzzing using cargo fuzz.
Currently the fuzz targets are working off of some assumed flows and should be improved for proper usage.
The limitations of existing fuzzing tooling require us to add some feature flag gating in order to have the code we want to fuzz compile in the fuzz target environment.
Additionally, some fields have been made public, but those could be reverted with preferred usage patterns without polluting our intended public API.


This change is Reviewable

@petarvujovic98 petarvujovic98 self-assigned this Oct 20, 2025
@github-actions
Copy link

Name Deployment
Registry gh-262.templar-in-training.testnet
Default market default-18652509223.gh-262.templar-in-training.testnet

Installed near-sandbox into /home/runner/work/contracts/contracts/target/debug/build/near-sandbox-utils-2ad3b5a56a37a3e2/out/.near/near-sandbox-2.8.0/near-sandbox

Gas Report

Snapshot Limits

harvest_yield

Iterations Gas
0 3.4 Tgas
391 14.9 Tgas

Estimated snapshot limit: 9635

apply_interest

Iterations Gas
0 3.6 Tgas
391 13.1 Tgas

Estimated snapshot limit: 11505

Action Gas Descriptors

Action Gas
collateralize 12.4 Tgas
withdraw_collateral 9.1 Tgas
borrow 14.9 Tgas
repay 13.7 Tgas
supply 8.3 Tgas
create_supply_withdrawal_request 3.1 Tgas
execute_next_supply_withdrawal_request 11.4 Tgas

Copy link
Collaborator

@peer2f00l peer2f00l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something that we should add to our Github workflows?

@peer2f00l peer2f00l requested a review from carrion256 October 23, 2025 18:29
Copy link
Collaborator

@peer2f00l peer2f00l left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see that the gas report is broken. Just that, I suppose.

}

// Test different operations based on selector
match op_selector % 8 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider making these their own tests and fuzzing the inputs for each one, in this case we're watering down potential fuzz paths

let _ = dec_a.fractional_part_as_u128_dividend();

// Test edge cases based on operation selector
match op_selector % 10 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above for these switchlike statements

Comment on lines +24 to +27
for precision in [0, 1, 5, 10, 20, 38] {
let fixed = decimal.to_fixed(precision);
let _ = Decimal::from_str(&fixed);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would fuzz everything up to a known fixed decimal here

Comment on lines +61 to +69
let edge_cases = [
"0",
"1",
"0.0",
"1.0",
"0.1",
"0.00000001",
"999999999999999",
];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think also here - fuzzing inputs should be the edge cases and countertests for them

Comment on lines +78 to +81
// Test malformed strings don't panic
let malformed = [
".", ".0", "0.", "abc", "1.2.3", "-1", "1e10", "NaN", "Infinity", "",
];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here, i think probably a few different fuzz tests for these multiple assertions, since finding a negative input would require running the entire suite again

Decimal::from(x) / Decimal::from(u64::MAX)
}

fuzz_target!(|data: (u64, u64, u64, u64, u64, u64, u64)| {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto here

Comment on lines +48 to +70
// Test 2: Compound interest calculation
// A = P(1 + r)^n
// For small rates, use binomial approximation to avoid overflow
// if rate_bps <= 1000 && periods <= 365 {
// let rate_per_period = rate_bps as f64 / 10000.0;
// let compound_multiplier = (1.0 + rate_per_period).powi(periods as i32);
//
// if compound_multiplier.is_finite() && compound_multiplier > 0.0 {
// let result = (principal as f64 * compound_multiplier) as u128;
//
// // Invariant: Result should always be >= principal
// assert!(
// result >= principal,
// "Compound interest resulted in less than principal"
// );
//
// // Invariant: Result shouldn't be absurdly large
// assert!(
// result <= principal.saturating_mul(100),
// "Compound interest grew unreasonably"
// );
// }
// }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still needed?

Copy link
Collaborator

@carrion256 carrion256 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider splitting up a bunch of the giant tests into more composable tests - right now if we meet any type of counter pattern from fuzzing we'd need to fuzz a lot to get to our desired state.

@petarvujovic98
Copy link
Collaborator Author

@carrion256 Thanks for the thorough review! I'll update this over the weekend to allow for more granular fuzzing and a faster iteration cycle.

@petarvujovic98 petarvujovic98 force-pushed the feature/eng-50-add-code-fuzzing-to-key-flows branch 2 times, most recently from 24c7c36 to 17134a2 Compare November 17, 2025 12:31
@petarvujovic98 petarvujovic98 force-pushed the feature/eng-50-add-code-fuzzing-to-key-flows branch from 17134a2 to 9d79b03 Compare November 17, 2025 12:57
@peer2f00l
Copy link
Collaborator

All tests pass locally; must be an issue with the runner again.

@peer2f00l peer2f00l merged commit c1e4587 into dev Nov 17, 2025
11 of 13 checks passed
@peer2f00l peer2f00l deleted the feature/eng-50-add-code-fuzzing-to-key-flows branch November 17, 2025 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants