Skip to content

Commit

Permalink
ctb: Add echidna tests for Burn
Browse files Browse the repository at this point in the history
  • Loading branch information
maurelian committed Dec 7, 2022
1 parent 1594678 commit 0f8fc58
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/pretty-chicken-rest.md
@@ -0,0 +1,6 @@
---
'@eth-optimism/ci-builder': patch
'@eth-optimism/contracts-bedrock': patch
---

Add echidna tests for Burn
4 changes: 4 additions & 0 deletions .circleci/config.yml
Expand Up @@ -817,6 +817,10 @@ workflows:
echidna_target: aliasing
requires:
- bedrock-echidna-build
- bedrock-echidna-run:
echidna_target: burn
requires:
- bedrock-echidna-build
- op-bindings-build:
requires:
- yarn-monorepo
Expand Down
59 changes: 59 additions & 0 deletions packages/contracts-bedrock/contracts/echidna/FuzzBurn.sol
@@ -0,0 +1,59 @@
pragma solidity 0.8.15;

import { Burn } from "../libraries/Burn.sol";

contract EchidnaFuzzBurn {
bool failedEthBurn;
bool failedGasBurn;

/**
* @notice Takes an integer amount of eth to burn through the Burn library and
* updates the contract state if an incorrect amount of eth moved from the contract
*/
function testBurn(uint256 _value) public {
// cache the contract's eth balance
uint256 preBurnBalance = address(this).balance;

// execute a burn of _value eth
// (may way to add guardrails to this value rather than a truly unbounded uint256)
Burn.eth(_value);

// check that exactly _value eth was transfered from the contract
if (address(this).balance != preBurnBalance - _value) {
failedEthBurn = true;
}
}

/**
* @notice Takes an integer amount of gas to burn through the Burn library and
* updates the contract state if at least that amount of gas was not burned
* by the library
*/
function testGas(uint256 _value) public {
// cache the contract's current remaining gas
uint256 preBurnGas = gasleft();

// execute the gas burn
Burn.gas(_value);

// cache the remaining gas post burn
uint256 postBurnGas = gasleft();

// check that at least _value gas was burnt
if (postBurnGas > preBurnGas - _value) {
failedGasBurn = true;
}
}

function echidna_burn_eth() public view returns (bool) {
// ASSERTION: The amount burned should always match the amount passed exactly
return !failedEthBurn;
}

function echidna_burn_gas() public view returns (bool) {
// ASSERTION: The amount of gas burned should be strictly greater than the
// the amount passed as _value (minimum _value + whatever minor overhead to
// the value after the call)
return !failedGasBurn;
}
}

0 comments on commit 0f8fc58

Please sign in to comment.