-
Notifications
You must be signed in to change notification settings - Fork 354
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
Fuzz testing #1255
Open
immrsd
wants to merge
16
commits into
OpenZeppelin:main
Choose a base branch
from
immrsd:fuzz-tests-erc20
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Fuzz testing #1255
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6b39ff6
Rename file with Math fuzz tests
immrsd b66d296
Turn on fuzz testing on CI
immrsd b6516d2
Introduce fuzzing feature to token and utils packages
immrsd fbc250e
Add overflow testing helpers
immrsd 8af84c4
Add fuzz tests for ERC20
immrsd 458332e
Add fuzz tests for ERC721Enumerable
immrsd e2e361d
Move fuzz helpers to test_common
immrsd c774c9e
Merge main
immrsd b9fa53d
Fix workflow file
immrsd 9f9ef60
Remove specifying empty default features
immrsd daac0fe
Lint files
immrsd 0f8bbf4
Update fuzz tests
immrsd e436ecb
Remove coverage command from CI test run
immrsd e147196
Improve ERC721Enum fuzz tests
immrsd 5a567ca
Reduce fuzzer runs from 10k to 1k
immrsd f5cddd2
Reduce fuzzer runs to 500
immrsd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use core::num::traits::ops::overflowing::{OverflowingAdd, OverflowingSub, OverflowingMul}; | ||
immrsd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
pub fn is_overflow_add<T, +OverflowingAdd<T>, +Drop<T>>(x: T, y: T) -> bool { | ||
let (_, does_overflow) = x.overflowing_add(y); | ||
does_overflow | ||
} | ||
|
||
pub fn is_overflow_mul<T, +OverflowingMul<T>, +Drop<T>>(x: T, y: T) -> bool { | ||
let (_, does_overflow) = x.overflowing_mul(y); | ||
does_overflow | ||
} | ||
|
||
pub fn is_overflow_sub<T, +OverflowingSub<T>, +Drop<T>>(x: T, y: T) -> bool { | ||
let (_, does_overflow) = x.overflowing_sub(y); | ||
does_overflow | ||
} | ||
immrsd marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
mod test_erc20; | ||
mod test_erc20_permit; | ||
|
||
#[cfg(feature: 'fuzzing')] | ||
mod test_fuzz_erc20; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
use ERC20Component::InternalTrait; | ||
use crate::erc20::ERC20Component::{ERC20CamelOnlyImpl, ERC20Impl}; | ||
use crate::erc20::ERC20Component::{ERC20MetadataImpl, InternalImpl}; | ||
use openzeppelin_test_common::mocks::erc20::DualCaseERC20Mock; | ||
use crate::erc20::ERC20Component; | ||
use openzeppelin_testing::constants::{ | ||
OWNER, SPENDER, RECIPIENT, NAME, SYMBOL | ||
}; | ||
use snforge_std::{test_address, start_cheat_caller_address}; | ||
use starknet::ContractAddress; | ||
use openzeppelin_testing::math::{is_overflow_add, is_overflow_sub}; | ||
|
||
// | ||
// Setup | ||
// | ||
|
||
type ComponentState = ERC20Component::ComponentState<DualCaseERC20Mock::ContractState>; | ||
|
||
fn COMPONENT_STATE() -> ComponentState { | ||
ERC20Component::component_state_for_testing() | ||
} | ||
|
||
fn setup(supply: u256) -> ComponentState { | ||
let mut state = COMPONENT_STATE(); | ||
state.initializer(NAME(), SYMBOL()); | ||
state.mint(OWNER(), supply); | ||
state | ||
} | ||
|
||
// | ||
// Tests | ||
// | ||
|
||
#[test] | ||
fn test_mint(supply: u256, mint_amount: u256) { | ||
if is_overflow_add(supply, mint_amount) { return; } | ||
let mut state = setup(supply); | ||
|
||
assert_total_supply(supply); | ||
assert_balance(OWNER(), supply); | ||
|
||
state.mint(RECIPIENT(), mint_amount); | ||
assert_total_supply(supply + mint_amount); | ||
assert_balance(RECIPIENT(), mint_amount); | ||
} | ||
|
||
#[test] | ||
fn test_burn(supply: u256, burn_amount: u256) { | ||
if is_overflow_sub(supply, burn_amount) { return; } | ||
let mut state = setup(supply); | ||
|
||
assert_total_supply(supply); | ||
assert_balance(OWNER(), supply); | ||
|
||
state.burn(OWNER(), burn_amount); | ||
assert_total_supply(supply - burn_amount); | ||
assert_balance(OWNER(), supply - burn_amount); | ||
} | ||
|
||
#[test] | ||
fn test_mint_burn(initial_supply: u256, mint_amount: u256, burn_amount: u256) { | ||
if is_overflow_add(initial_supply, mint_amount) { return; } | ||
if is_overflow_sub(mint_amount, burn_amount) { return; } | ||
let mut state = setup(initial_supply); | ||
let (owner, recipient) = (OWNER(), RECIPIENT()); | ||
|
||
// Mint | ||
state.mint(recipient, mint_amount); | ||
assert_total_supply(initial_supply + mint_amount); | ||
assert_balance(owner, initial_supply); | ||
assert_balance(recipient, mint_amount); | ||
|
||
// Burn | ||
state.burn(recipient, burn_amount); | ||
assert_total_supply(initial_supply + mint_amount - burn_amount); | ||
assert_balance(owner, initial_supply); | ||
assert_balance(recipient, mint_amount - burn_amount); | ||
} | ||
|
||
#[test] | ||
fn test_transfer(supply: u256, transfer_amount: u256) { | ||
if is_overflow_sub(supply, transfer_amount) { return; } | ||
let mut state = setup(supply); | ||
let (owner, recipient) = (OWNER(), RECIPIENT()); | ||
|
||
start_cheat_caller_address(test_address(), owner); | ||
state.transfer(recipient, transfer_amount); | ||
|
||
assert_balance(owner, supply - transfer_amount); | ||
assert_balance(recipient, transfer_amount); | ||
} | ||
|
||
#[test] | ||
fn test_transfer_from(supply: u256, transfer_amount: u256) { | ||
if is_overflow_sub(supply, transfer_amount) { return; } | ||
let mut state = setup(supply); | ||
let (owner, spender, recipient) = (OWNER(), SPENDER(), RECIPIENT()); | ||
let contract_address = test_address(); | ||
|
||
// Approve | ||
start_cheat_caller_address(contract_address, owner); | ||
state.approve(spender, transfer_amount); | ||
assert_balance(owner, supply); | ||
assert_allowance(owner, spender, transfer_amount); | ||
|
||
// Transfer from | ||
start_cheat_caller_address(contract_address, spender); | ||
state.transfer_from(owner, recipient, transfer_amount); | ||
assert_allowance(owner, spender, 0); | ||
assert_balance(owner, supply - transfer_amount); | ||
assert_balance(recipient, transfer_amount); | ||
assert_balance(spender, 0); | ||
} | ||
|
||
#[test] | ||
fn test__spend_allowance(supply: u256, spend_amount: u256) { | ||
if is_overflow_sub(supply, spend_amount) { return; } | ||
let mut state = setup(supply); | ||
let (owner, spender) = (OWNER(), SPENDER()); | ||
state._approve(owner, spender, supply); | ||
|
||
state._spend_allowance(owner, spender, spend_amount); | ||
assert_balance(owner, supply); | ||
assert_balance(spender, 0); | ||
assert_allowance(owner, spender, supply - spend_amount); | ||
immrsd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// | ||
// Helpers | ||
// | ||
|
||
fn assert_total_supply(expected: u256) { | ||
let state = COMPONENT_STATE(); | ||
assert_eq!(state.total_supply(), expected); | ||
} | ||
|
||
fn assert_allowance(owner: ContractAddress, spender: ContractAddress, expected: u256) { | ||
let state = COMPONENT_STATE(); | ||
assert_eq!(state.allowance(owner, spender), expected); | ||
} | ||
|
||
fn assert_balance(owner: ContractAddress, expected: u256) { | ||
let state = COMPONENT_STATE(); | ||
assert_eq!(state.balance_of(owner), expected); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
mod test_erc721; | ||
mod test_erc721_enumerable; | ||
mod test_erc721_receiver; | ||
|
||
#[cfg(feature: 'fuzzing')] | ||
mod test_fuzz_erc721_enumerable; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have the feeling that this will soon make tests run timeout in the action.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests are not running on the PR btw, when that's fixed I'm curious to see how much time will this take with this few examples.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reduced the number of fuzzer runs to 500 which seems a reasonable value to me, at least for now
With 10k runs the tests took 1h 55m to complete which is way too much, here are the run results: https://github.com/OpenZeppelin/cairo-contracts/actions/runs/12374506824/job/34537193923
With 1k runs it took 17m 27s to run all the tests. 10 minutes more than our usual test runs.
Results: https://github.com/OpenZeppelin/cairo-contracts/actions/runs/12379685661/job/34554383936
Although it's an acceptable duration, it can increase much when we add more fuzz test cases. So now with 500 runs the tests job takes 12 minutes (5 minutes more than it was without fuzz tests)