Repository navigation
test: cover verify_prices, check contract artifacts against sources, keep secrets out of release.yml (V2-1750) #23
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
Open
Nic-dorman
wants to merge
6
commits into
main
Choose a base branch
from
nic/v2-1750-evmlib-ci-follow-ups
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d5fe55a
test: cover verify_prices, check contract artifacts against sources, …
Nic-dorman e1d4c44
fix: only strip a genuine solc metadata tail when comparing bytecode …
Nic-dorman 2a2722d
fix: strip only a tail shaped exactly like solc's metadata (V2-1750)
Nic-dorman 7290274
fix: strip solc metadata only when it names the pinned compiler (V2-1…
Nic-dorman 36f0ea1
fix: compare contract bytecode in full; check out submodules recursiv…
Nic-dorman 93bb12c
ci: pin Foundry to v1.8.7 for the contract artifacts check (V2-1750)
Nic-dorman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,77 @@ | ||
| #!/usr/bin/env python3 | ||
| """Fail if the committed PaymentVaultV2 artifacts have drifted from contracts/. | ||
|
|
||
| Usage: check_contract_artifacts.py <forge-out-dir> | ||
|
|
||
| Run `forge build --out <forge-out-dir>` first (contracts.yml does). The | ||
| committed files feed `sol!` and the Anvil deploys in the tests, so if a | ||
| contract source changes without them being regenerated, the tests keep | ||
| exercising the old contract. Checked: | ||
|
|
||
| artifacts/PaymentVaultV2.json ABI, bytecode and deployedBytecode | ||
| abi/IPaymentVault.json ABI (it is PaymentVaultV2's ABI) | ||
|
|
||
| Bytecode is compared in full, including solc's trailing CBOR metadata. That | ||
| metadata hashes the sources and the compiler settings, so it also catches a | ||
| drift the code alone wouldn't show. One of those settings is the remappings | ||
| Foundry auto-detects from lib/, including OpenZeppelin's nested submodules, | ||
| so the build only reproduces the committed artifact byte for byte when the | ||
| submodules are checked out recursively (contracts.yml does). Nothing is | ||
| stripped before comparing, so no executable code can be mistaken for metadata. | ||
|
|
||
| artifacts/AutonomiNetworkToken.json is not checked: it is a Hardhat artifact | ||
| whose source (contracts/AutonomiNetworkToken.sol) is not in this repo. | ||
| """ | ||
|
|
||
| import json | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| COMMITTED = Path("artifacts/PaymentVaultV2.json") | ||
| COMMITTED_ABI = Path("abi/IPaymentVault.json") | ||
|
|
||
|
|
||
| def canonical(value): | ||
| return json.dumps(value, sort_keys=True) | ||
|
|
||
|
|
||
| def code(entry): | ||
| """Lower-case hex code without 0x, from a Foundry bytecode entry or a hex string.""" | ||
| hex_code = entry["object"] if isinstance(entry, dict) else entry | ||
| return hex_code.removeprefix("0x").lower() | ||
|
|
||
|
|
||
| def main(): | ||
| if len(sys.argv) != 2: | ||
| sys.exit(__doc__) | ||
| built_path = Path(sys.argv[1]) / "PaymentVaultV2.sol" / "PaymentVaultV2.json" | ||
| built = json.loads(built_path.read_text()) | ||
| committed = json.loads(COMMITTED.read_text()) | ||
| committed_abi = json.loads(COMMITTED_ABI.read_text()) | ||
|
|
||
| failures = [] | ||
| if canonical(built["abi"]) != canonical(committed["abi"]): | ||
| failures.append(f"{COMMITTED}: ABI differs from contracts/") | ||
| if canonical(built["abi"]) != canonical(committed_abi): | ||
| failures.append(f"{COMMITTED_ABI}: ABI differs from contracts/") | ||
| for key in ("bytecode", "deployedBytecode"): | ||
| if code(built[key]) != code(committed[key]): | ||
| failures.append(f"{COMMITTED}: {key} differs from contracts/") | ||
|
|
||
| if failures: | ||
| for failure in failures: | ||
| print(f"::error::{failure}") | ||
| print( | ||
| "Regenerate with `forge build`, copy " | ||
| "artifacts/PaymentVaultV2.sol/PaymentVaultV2.json over " | ||
| f"{COMMITTED}, and write its `abi` array to {COMMITTED_ABI}.\n" | ||
| "If only the end of the bytecode (the metadata hash) differs, the " | ||
| "build probably saw different remappings: check out the submodules " | ||
| "recursively (`git submodule update --init --recursive`) and rebuild." | ||
| ) | ||
| sys.exit(1) | ||
| print(f"{COMMITTED} and {COMMITTED_ABI} match contracts/") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or 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,45 @@ | ||
| name: Contract artifacts | ||
|
|
||
| # The committed PaymentVaultV2 artifacts are what the tests deploy, so check | ||
| # they still match contracts/. Only runs when a PR touches the contracts, the | ||
| # artifacts or the Foundry config. | ||
| on: | ||
| pull_request: | ||
| paths: | ||
| - "contracts/**" | ||
| - "artifacts/**" | ||
| - "abi/**" | ||
| - "lib/**" | ||
| - "foundry.toml" | ||
| - "remappings.txt" | ||
| - ".github/scripts/check_contract_artifacts.py" | ||
| - ".github/workflows/contracts.yml" | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| artifacts: | ||
| name: Artifacts match sources | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| with: | ||
| # Recursive: Foundry auto-detects remappings from OpenZeppelin's nested | ||
| # submodules too, and they are hashed into the bytecode's metadata, | ||
| # which the check compares in full. | ||
| submodules: recursive | ||
|
|
||
| - name: Install Foundry | ||
| uses: foundry-rs/foundry-toolchain@v1 | ||
| with: | ||
| # Pinned so a Foundry release can't change the build (e.g. the | ||
| # remappings it auto-detects) under this byte-for-byte check. | ||
| # Bump deliberately, with the check passing on the new version. | ||
| version: v1.8.7 | ||
|
|
||
| - name: Build contracts | ||
| run: forge build --out "$RUNNER_TEMP/forge-out" --cache-path "$RUNNER_TEMP/forge-cache" | ||
|
|
||
| - name: Compare with committed artifacts | ||
| run: python3 .github/scripts/check_contract_artifacts.py "$RUNNER_TEMP/forge-out" |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: This assumes the final two bytes always encode a valid CBOR metadata length. With metadata absent (for example, if a later change sets
cbor_metadata = falseinfoundry.toml, which triggers this workflow), an oversized footer length can strip all executable code: I reproducedcode("0x600160005500") == code("0x600260005500") == ""although the bytecode differs. The drift check could then pass despite a code change. Please validate the footer length and CBOR structure; if no valid metadata tail exists, compare the full bytecode or fail explicitly. Add tests for metadata-free, truncated, and oversized-footer input.