fix: slither v2 #270
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
name: Task - Test Solidity | |
on: | |
workflow_call: | |
push: | |
paths: | |
- "solidity/**" | |
pull_request: | |
paths: | |
- "solidity/**" | |
env: | |
FOUNDRY_PROFILE: ci | |
jobs: | |
check: | |
env: | |
working-directory: ./solidity | |
name: Foundry project | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Check for Solidity changes | |
id: check_changes | |
run: | | |
git diff --name-only ${{ github.event.before }} ${{ github.sha }} > changes.txt | |
echo "::set-output name=solidity_changed::$(grep -q 'solidity/' changes.txt && echo 'true' || echo 'false')" | |
- name: Install Foundry | |
uses: foundry-rs/foundry-toolchain@v1 | |
with: | |
version: nightly | |
- name: Cache gas report | |
if: steps.check_changes.outputs.solidity_changed == 'true' | |
uses: actions/cache@v3 | |
with: | |
path: ${{ env.working-directory }}/previous_gas_report.txt | |
key: ${{ runner.os }}-gas-report-${{ github.sha }} | |
restore-keys: | | |
${{ runner.os }}-gas-report- | |
- name: Run Forge build | |
working-directory: ${{ env.working-directory }} | |
run: forge build --sizes | |
- name: Run Forge tests | |
working-directory: ${{ env.working-directory }} | |
run: forge test -vvv | |
- name: Initialize dummy previous gas report if not exists | |
if: steps.check_changes.outputs.solidity_changed == 'true' | |
working-directory: ${{ env.working-directory }} | |
run: | | |
if [ ! -f previous_gas_report.txt ]; then | |
echo "Initializing dummy previous_gas_report.txt with high values" | |
echo "SpotMedian,100000000000000000" > previous_gas_report.txt | |
echo "TWAP,100000000000000000" >> previous_gas_report.txt | |
echo "RealizedVolatility,100000000000000000" >> previous_gas_report.txt | |
echo "Options,100000000000000000" >> previous_gas_report.txt | |
echo "Perp,100000000000000000" >> previous_gas_report.txt | |
fi | |
- name: Run gas report | |
working-directory: ${{ env.working-directory }} | |
if: steps.check_changes.outputs.solidity_changed == 'true' | |
run: | | |
echo "Running Forge test..." | |
forge test --match-contract PragmaDecoderGasTest -vvv > full_output.txt | |
echo "Forge test completed. Contents of full_output.txt:" | |
cat full_output.txt | |
echo "Extracting gas report..." | |
grep "Gas used for" full_output.txt | sed -E 's/Gas used for ([A-Za-z]+) update: ([0-9]+)/\1,\2/' > current_gas_report.txt | |
echo "Extraction completed. Contents of current_gas_report.txt:" | |
cat current_gas_report.txt | |
- name: Compare gas results | |
if: steps.check_changes.outputs.solidity_changed == 'true' | |
id: compare-gas | |
working-directory: ${{ env.working-directory }} | |
run: | | |
echo "Starting gas comparison" | |
gas_summary="## Gas Report Summary\n\n" | |
if [ -f previous_gas_report.txt ]; then | |
echo "File exists, can process comparison" | |
while IFS=',' read -r current_test current_gas; do | |
previous_gas=$(grep "^$current_test," previous_gas_report.txt | cut -d',' -f2) | |
echo "Processing test: $current_test with current gas: $current_gas and previous gas: $previous_gas" | |
gas_summary+="### ${current_test}\n" | |
gas_summary+="- **Gas used**: ${current_gas}\n" | |
if [ -n "$previous_gas" ]; then | |
diff=$((current_gas - previous_gas)) | |
percent=$((diff * 100 / previous_gas)) | |
if [ $diff -gt 0 ]; then | |
gas_summary+="- **Change**: :red_circle: +${diff} (${percent}% increase)\n" | |
elif [ $diff -lt 0 ]; then | |
gas_summary+="- **Change**: :green_circle: ${diff} (${percent}% decrease)\n" | |
else | |
gas_summary+="- **Change**: :white_circle: No change\n" | |
fi | |
else | |
gas_summary+="- **Change**: N/A (New test)\n" | |
fi | |
gas_summary+="\n" | |
done < current_gas_report.txt | |
else | |
echo "No previous gas report found. This will be the baseline for future comparisons." | |
while IFS=',' read -r current_test current_gas; do | |
gas_summary+="### ${current_test}\n" | |
gas_summary+="- **Gas used**: ${current_gas}\n" | |
gas_summary+="- **Change**: N/A (First run)\n\n" | |
done < current_gas_report.txt | |
fi | |
echo "gas_summary<<EOF" >> $GITHUB_OUTPUT | |
echo "$gas_summary" >> $GITHUB_OUTPUT | |
echo "EOF" >> $GITHUB_OUTPUT | |
cp current_gas_report.txt previous_gas_report.txt | |
- name: Comment PR | |
uses: actions/github-script@v6 | |
if: github.event_name == 'pull_request' && steps.check_changes.outputs.solidity_changed == 'true' | |
env: | |
SUMMARY: ${{ steps.compare-gas.outputs.gas_summary }} | |
with: | |
github-token: ${{secrets.GITHUB_TOKEN}} | |
script: | | |
const summary = process.env.SUMMARY.replace(/\\n/g, '\n'); | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: summary | |
}) | |
- name: Upload gas report | |
if: steps.check_changes.outputs.solidity_changed == 'true' | |
uses: actions/upload-artifact@v3 | |
with: | |
name: gas-report | |
path: ${{ env.working-directory }}/previous_gas_report.txt | |
- name: Static analysis | |
if: steps.check_changes.outputs.solidity_changed == 'true' | |
uses: crytic/[email protected] | |
id: slither | |
with: | |
target: "solidity/" | |
slither-config: "solidity/slither.config.json" | |
slither-args: --exclude incorrect-exponentiation | |
sarif: results.sarif | |
fail-on: none | |
compile-command: | | |
forge build | |
ignore-compile: false | |
- name: Upload SARIF file | |
if: steps.check_changes.outputs.solidity_changed == 'true' | |
uses: github/codeql-action/upload-sarif@v3 | |
with: | |
sarif_file: ${{ steps.slither.outputs.sarif }} |