Skip to content

Commit

Permalink
feat: add script and ci to check if genesis code is generated from la…
Browse files Browse the repository at this point in the history
…test code
  • Loading branch information
cosinlink committed Aug 22, 2024
1 parent 7403b72 commit a9b5cd6
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/check-genesis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Check If Genesis Is Latest
on:
push:
branches:
- master
- develop
pull_request:
branches:
- master
- develop
permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read
jobs:
unit-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache@v1
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1

- name: Install Poetry
uses: snok/install-poetry@v1

- name: Install Project Dependencies
run: |
npm install ts-node -g
npm install
poetry install
forge install --no-git --no-commit foundry-rs/forge-std@v1.7.3
forge --version
- name: Build
run: |
forge build
- name: Check Genesis Bytecode
run: |
ts-node scripts/check-genesis-bytecode.ts
47 changes: 47 additions & 0 deletions scripts/check-genesis-bytecode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'dotenv/config';
import {execSync} from 'child_process';
import * as assert from "assert";
import * as fs from "fs";

const log = console.log;

const work = async () => {
log('compare current bytecode with latest mainnet contracts')
let str = (fs.readFileSync(__dirname + '/../genesis.json')).toString();
const currentGenesis = JSON.parse(str);
log('currentGenesis size:', JSON.stringify(currentGenesis, null, 2).length)

const result = execSync('poetry run python -m scripts.generate mainnet')
const resultStr = result.toString()
if (resultStr.indexOf('Generate genesis of mainnet successfully') === -1) {
throw Error(`generate mainnet genesis failed, error result: ${resultStr}`)
}
await sleep(5)
log('generated mainnet genesis')

str = (fs.readFileSync(__dirname + '/../genesis.json')).toString();
const generatedGenesis = JSON.parse(str);
log('generatedGenesis size:', JSON.stringify(generatedGenesis, null, 2).length)

log('try deepStrictEqual(currentGenesis, generatedGenesis)')
assert.deepStrictEqual(currentGenesis, generatedGenesis)

log('Success! genesis bytecode not changed')
};

const sleep = async (seconds: number) => {
console.log('sleep', seconds, 's');
await new Promise((resolve) => setTimeout(resolve, seconds * 1000));
};

const main = async () => {
await work();
};

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});

0 comments on commit a9b5cd6

Please sign in to comment.