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

dapp: Gas snapshotting #850

Merged
merged 14 commits into from
Nov 11, 2021
28 changes: 28 additions & 0 deletions src/dapp-tests/integration/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,34 @@ test_custom_solc_json() {
assert "[[ -f out/dapp.sol.json ]]"
}

test_gas_snapshots() {
tmp=$(mktemp -d)

# copy source file
mkdir -p "$tmp/src"
cp "$CONTRACTS/factor.sol" "$tmp/src"

# init dapp project
cd "$tmp" || exit
export GIT_CONFIG_NOSYSTEM=1
export GIT_AUTHOR_NAME=dapp
export [email protected]
export GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME
export GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL
dapp init

# test with snapshots
dapp snapshot
assert "[[ -f .gas-snapshot ]]"

# test check snapshots
dapp check-snapshot || fail

# check that check snapshots fails if we change the snapshot
echo this_will_change_the_snapshot > .gas-snapshot
dapp check-snapshot && fail || echo "dapp success: snapshot diff detected"
}

test_nonce_1() {
local account
account=$(fresh_account)
Expand Down
2 changes: 2 additions & 0 deletions src/dapp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The output from `dapp build` now uses color to differentiate warnings and errors
- Support for solc 0.8.9/0.8.8

- `dapp snapshot` and `dapp check-snapshot` commands to snapshot test gas usage.

### Changed

- Dapp remappings ignores non-directories in `DAPP_LIB`
Expand Down
16 changes: 15 additions & 1 deletion src/dapp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,20 @@ it. For an example, see [this repo](https://github.com/dapp-org/radicle-contract

Updates a project submodule in the `lib` subdirectory.

### `dapp snapshot`

dapp-snapshot -- creates a snapshot of each test's gas usage
Usage: dapp snapshot

Saves a snapshot of each concrete test's gas usage in a `.gas-snapshot` file.

### `dapp check-snapshot`

dapp-check-snapshot -- check snapshot is up to date
Usage: dapp check-snapshot

Runs `dapp snapshot` and exits with an error code if its output does not match the current `.gas-snapshot` file.

### `dapp upgrade`

dapp-upgrade -- pull & commit all upstream lib changes
Expand All @@ -558,7 +572,7 @@ Spins up a geth testnet.

dapp-verify-contract -- verify contract source on etherscan
Usage: dapp verify-contract <path>:<contractname> <address> [constructorArgs]

Example: `dapp verify-contract src/auth/authorities/RolesAuthority.sol:RolesAuthority 0x9ed0e..`

Requires `ETHERSCAN_API_KEY` to be set.
Expand Down
13 changes: 13 additions & 0 deletions src/dapp/libexec/dapp/dapp-check-snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
### dapp-check-snapshot -- check snapshot is up to date
### Usage: dapp check-snapshot
set -e

# Uses the output of dapp test to compare with dapp snapshot
DIFF="$(diff .gas-snapshot <(dapp test --fuzz-runs 0 | grep 'gas:' | cut -d " " -f 2-4))"

if [ -n "$DIFF" ]
then
echo "$DIFF"
exit 1
fi
7 changes: 7 additions & 0 deletions src/dapp/libexec/dapp/dapp-snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
### dapp-snapshot -- creates a snapshot of each test's gas usage
### Usage: dapp snapshot
set -e

# Saves the output of dapp test to .gas-snapshot
dapp test --fuzz-runs 0 | grep 'gas:' | cut -d " " -f 2-4 > .gas-snapshot