-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check storage layout consistency in PRs (#3967)
Co-authored-by: Francisco <frangio.1@gmail.com>
- Loading branch information
Showing
6 changed files
with
264 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
name: Compare storage layouts | ||
inputs: | ||
token: | ||
description: github token | ||
required: true | ||
buildinfo: | ||
description: compilation artifacts | ||
required: false | ||
default: artifacts/build-info/*.json | ||
layout: | ||
description: extracted storage layout | ||
required: false | ||
default: HEAD.layout.json | ||
out_layout: | ||
description: storage layout to upload | ||
required: false | ||
default: ${{ github.ref_name }}.layout.json | ||
ref_layout: | ||
description: storage layout for the reference branch | ||
required: false | ||
default: ${{ github.base_ref }}.layout.json | ||
|
||
runs: | ||
using: composite | ||
steps: | ||
- name: Extract layout | ||
run: | | ||
node scripts/checks/extract-layout.js ${{ inputs.buildinfo }} > ${{ inputs.layout }} | ||
shell: bash | ||
- name: Download reference | ||
if: github.event_name == 'pull_request' | ||
run: | | ||
RUN_ID=`gh run list --repo ${{ github.repository }} --branch ${{ github.base_ref }} --workflow ${{ github.workflow }} --limit 100 --json 'conclusion,databaseId,event' --jq 'map(select(.conclusion=="success" and .event!="pull_request"))[0].databaseId'` | ||
gh run download ${RUN_ID} --repo ${{ github.repository }} -n layout | ||
env: | ||
GITHUB_TOKEN: ${{ inputs.token }} | ||
shell: bash | ||
continue-on-error: true | ||
id: reference | ||
- name: Compare layouts | ||
if: steps.reference.outcome == 'success' && github.event_name == 'pull_request' | ||
run: | | ||
node scripts/checks/compare-layout.js --head ${{ inputs.layout }} --ref ${{ inputs.ref_layout }} >> $GITHUB_STEP_SUMMARY | ||
shell: bash | ||
- name: Rename artifacts for upload | ||
if: github.event_name != 'pull_request' | ||
run: | | ||
mv ${{ inputs.layout }} ${{ inputs.out_layout }} | ||
shell: bash | ||
- name: Save artifacts | ||
if: github.event_name != 'pull_request' | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: layout | ||
path: ${{ inputs.out_layout }} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,20 @@ | ||
const fs = require('fs'); | ||
const { getStorageUpgradeReport } = require('@openzeppelin/upgrades-core/dist/storage'); | ||
|
||
const { ref, head } = require('yargs').argv; | ||
|
||
const oldLayout = JSON.parse(fs.readFileSync(ref)); | ||
const newLayout = JSON.parse(fs.readFileSync(head)); | ||
|
||
for (const name in oldLayout) { | ||
if (name in newLayout) { | ||
const report = getStorageUpgradeReport(oldLayout[name], newLayout[name], {}); | ||
if (!report.ok) { | ||
console.log(`ERROR: Storage incompatibility in ${name}`); | ||
console.log(report.explain()); | ||
process.exitCode = 1; | ||
} | ||
} else { | ||
console.log(`WARNING: ${name} is missing from the current branch`); | ||
} | ||
} |
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,40 @@ | ||
const fs = require('fs'); | ||
const { findAll } = require('solidity-ast/utils'); | ||
const { astDereferencer } = require('@openzeppelin/upgrades-core/dist/ast-dereferencer'); | ||
const { solcInputOutputDecoder } = require('@openzeppelin/upgrades-core/dist/src-decoder'); | ||
const { extractStorageLayout } = require('@openzeppelin/upgrades-core/dist/storage/extract'); | ||
|
||
const { _ } = require('yargs').argv; | ||
|
||
const skipPath = ['contracts/mocks/', 'contracts-exposed/']; | ||
const skipKind = ['interface', 'library']; | ||
|
||
function extractLayouts(path) { | ||
const layout = {}; | ||
const { input, output } = JSON.parse(fs.readFileSync(path)); | ||
|
||
const decoder = solcInputOutputDecoder(input, output); | ||
const deref = astDereferencer(output); | ||
|
||
for (const src in output.contracts) { | ||
if (skipPath.some(prefix => src.startsWith(prefix))) { | ||
continue; | ||
} | ||
|
||
for (const contractDef of findAll('ContractDefinition', output.sources[src].ast)) { | ||
if (skipKind.includes(contractDef.contractKind)) { | ||
continue; | ||
} | ||
|
||
layout[contractDef.name] = extractStorageLayout( | ||
contractDef, | ||
decoder, | ||
deref, | ||
output.contracts[src][contractDef.name].storageLayout, | ||
); | ||
} | ||
} | ||
return layout; | ||
} | ||
|
||
console.log(JSON.stringify(Object.assign(..._.map(extractLayouts)))); |