ファジングと不変条件テスト
固定されたユニットテストでは見逃すスマートコントラクトのエッジケースのバグを、Foundryを例にプロパティベースのファジングと不変条件テストで発見する方法を学びます。
「ファジングと不変条件テスト」はCoddyKit上の無料Blockchain Smart Contracts with Solidityレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはBlockchain Smart Contracts with Solidity学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Blockchain Smart Contracts with Solidityコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Limits of Example Tests
You have written unit tests with specific inputs. But attackers find the one input you did not try. Fixed examples cannot cover the huge space of possible values. This is where fuzzing helps.
What Is Fuzzing?
Fuzz testing runs a test many times with randomly generated inputs. Instead of asserting on one value, you assert a property that should hold for all inputs.
A Fuzz Test in Foundry
In Foundry, any test function parameter is automatically fuzzed. The framework feeds in many random values.
function testFuzz_DepositIncreasesBalance(uint256 amount) public {
vm.assume(amount > 0 && amount < 1e30);
vault.deposit(amount);
assertEq(vault.balanceOf(address(this)), amount);
}Bounding Inputs
Random values can be absurd (like the max uint256). Use vm.assume to discard bad inputs or bound to map a value into a valid range so tests stay meaningful.
function testFuzz_Transfer(uint256 amount) public {
amount = bound(amount, 1, token.balanceOf(address(this)));
token.transfer(bob, amount);
assertEq(token.balanceOf(bob), amount);
}Thinking in Properties
The shift is from 'with input X expect Y' to 'no matter the input, this rule holds'. Common properties:
- Total supply never changes on a transfer
- A user can never withdraw more than they deposited
- Balances never underflow
What Are Invariants?
An invariant is a property that must hold after any sequence of operations, not just one call. Invariant testing fires many random function calls in random order, then checks the invariant after each step.
Declaring an Invariant
In Foundry, functions prefixed with invariant_ are checked after each randomized call sequence.
function invariant_TotalSupplyEqualsSumOfBalances() public {
assertEq(token.totalSupply(), handler.sumOfBalances());
}The Handler Pattern
Raw random calls often revert or wander into useless states. A handler contract wraps the target with guided, valid actions and tracks expected totals (ghost variables) for the invariant to check.
contract Handler {
Token token;
uint256 public sumOfBalances;
function transfer(uint256 toSeed, uint256 amount) external {
// bounded, valid transfer logic that updates ghost totals
}
}Shrinking Failures
When a fuzzer finds a failing input, it shrinks it to the simplest counterexample. This makes the bug far easier to understand and reproduce than a random gigantic number.
Tuning Test Runs
More runs find deeper bugs but take longer. Configure run counts in foundry.toml for CI versus quick local checks.
[fuzz]
runs = 1000
[invariant]
runs = 256
depth = 50When to Use Each
Use fuzzing to harden individual functions against unexpected single inputs. Use invariant testing to verify system-wide rules survive any sequence of actions. Together they catch classes of bugs fixed tests never reach.
Quick Check
Test your understanding of property-based testing.
Recap
You learned fuzzing and invariant testing:
- Fuzzing runs functions with many random inputs against properties
vm.assumeandboundkeep inputs valid- Invariants check system rules across random call sequences
- Handlers guide actions; shrinking simplifies failures
These techniques uncover edge cases that example-based tests miss.
よくある質問
「ファジングと不変条件テスト」レッスンは無料ですか?
はい。「ファジングと不変条件テスト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Blockchain Smart Contracts with Solidityコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Blockchain Smart Contracts with Solidityコースには全4レッスンが含まれています。
「ファジングと不変条件テスト」で何を学びますか?
固定されたユニットテストでは見逃すスマートコントラクトのエッジケースのバグを、Foundryを例にプロパティベースのファジングと不変条件テストで発見する方法を学びます。 ブラウザで直接実行するハンズオンコードでBlockchain Smart Contracts with Solidityを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Blockchain Smart Contracts with Solidityを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのBlockchain Smart Contracts with Solidityは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「ファジングと不変条件テスト」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このBlockchain Smart Contracts with Solidityレッスンでコードを書いて実行できますか?
はい。すべてのBlockchain Smart Contracts with Solidityレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Foundry/Hardhatによる高度なテスト
- 形式手法による検証の基礎
- メインネットへのデプロイと監視
- ファジングと不変条件テスト