From 034d3457e3dc7fdb4c11bacf687e574c1f9387f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=A5=E5=9B=BD=E5=AE=87?= <841185308@qq.com> Date: Thu, 20 Jul 2023 16:45:37 +0800 Subject: [PATCH] chore: Add action to update assume valid target (#2773) * chore: Add action to update assume valid target * chore: Ignore scripts code style * fix: Use fetch to to replace node:http * chore: Use github context --- .github/workflows/check-code-style.yml | 2 +- .github/workflows/update_valid_target.yml | 87 +++++++++++++++++++++++ package.json | 3 +- scripts/update-valid-target.js | 34 +++++++++ 4 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/update_valid_target.yml create mode 100644 scripts/update-valid-target.js diff --git a/.github/workflows/check-code-style.yml b/.github/workflows/check-code-style.yml index 5ed1158ded..61aa93474a 100644 --- a/.github/workflows/check-code-style.yml +++ b/.github/workflows/check-code-style.yml @@ -33,7 +33,7 @@ jobs: id: changed-files uses: tj-actions/changed-files@v37 with: - files: "**/*.{js,cjs,mjs,jsx,ts,tsx,css,scss}" + files: "packages/**/*.{js,cjs,mjs,jsx,ts,tsx,css,scss}" - name: Prettier Check if: steps.changed-files.outputs.any_changed == 'true' diff --git a/.github/workflows/update_valid_target.yml b/.github/workflows/update_valid_target.yml new file mode 100644 index 0000000000..4b2cd85462 --- /dev/null +++ b/.github/workflows/update_valid_target.yml @@ -0,0 +1,87 @@ +name: Update ckb node assume valid target + +on: + pull_request: + types: [ready_for_review] + branches: + - master + +jobs: + ready-for-release: + name: Update ckb node assume valid target + runs-on: ubuntu-latest + steps: + - name: Create Branch + uses: peterjgrainger/action-create-branch@v2.2.0 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + branch: 'chore-update-valid-target/${{github.head_ref}}' + sha: '${{ github.event.pull_request.head.sha }}' + + - name: Checkout + uses: actions/checkout@v3 + with: + ref: 'chore-update-valid-target/${{github.head_ref}}' + + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version: 18.12.0 + + - name: Write env file + run: | + npm run update:valid-target + + - name: Commit env file + uses: actions/github-script@v6 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + BASE: ${{ github.head_ref }} + with: + script: | + const fs = require('node:fs') + const { BASE, HEAD } = process.env + const envFilePath = 'packages/neuron-wallet/.env' + const destinationBranch = `chore-update-valid-target/${BASE}` + const { data } = await github.rest.repos.getContent({ + owner: context.repo.owner, + repo: context.repo.repo, + path: envFilePath, + ref: destinationBranch, + }) + await github.rest.repos.createOrUpdateFileContents({ + owner: context.repo.owner, + repo: context.repo.repo, + path: envFilePath, + message: `chore: Update ckb node assume valid target for ${BASE}.`, + content: fs.readFileSync(envFilePath).toString('base64'), + sha: data.sha, + branch: destinationBranch, + }) + + - name: Create PR + uses: actions/github-script@v6 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + BASE: ${{github.head_ref}} + HEAD: chore-update-valid-target/${{github.head_ref}} + REPO: ${{github.repository}} + with: + script: | + const { BASE, HEAD, REPO } = process.env + const { data: pulls } = await github.rest.pulls.list({ + owner: context.repo.owner, + repo: context.repo.repo, + }) + if (pulls.some(pull => pull.head.ref === HEAD)) { + return + } + github.rest.pulls.create({ + owner: context.repo.owner, + repo: context.repo.repo, + head: HEAD, + base: BASE, + title: 'chore: Update ckb node assume valid target', + body: `This PR uses to update ckb node assume valid target for PR https://github.com/${REPO}/pull/${context.issue.number}`, + }) diff --git a/package.json b/package.json index e8577a2c72..26b658aa21 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,8 @@ "test:ci": "yarn build:main && yarn test", "lint": "lerna run --stream lint", "postinstall": "husky install", - "db:chain": "node ./node_modules/.bin/typeorm" + "db:chain": "node ./node_modules/.bin/typeorm", + "update:valid-target": "node ./scripts/update-valid-target.js" }, "devDependencies": { "@babel/core": "7.22.5", diff --git a/scripts/update-valid-target.js b/scripts/update-valid-target.js new file mode 100644 index 0000000000..78d2fbac0f --- /dev/null +++ b/scripts/update-valid-target.js @@ -0,0 +1,34 @@ +const fs = require('node:fs') +const path = require('node:path') + +async function rpcRequest(method, params = []) { + const result = await fetch( + 'https://mainnet.ckb.dev/rpc', + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + id: 0, + jsonrpc: '2.0', + method, + params, + }), + timeout: 10000, + } + ) + return result.json() +} + +const ESTIMATE_BLOCK_COUNT_PER_DAY = 8_000 +const envFilePath = path.resolve(__dirname, '../packages/neuron-wallet/.env') +const validTargetReg = /(CKB_NODE_ASSUME_VALID_TARGET=)[\S]*/ + +;(async function () { + const tipBlockNumber = (await rpcRequest('get_tip_block_number')).result + const validTargetBlockNumber = `0x${(BigInt(tipBlockNumber) - BigInt(ESTIMATE_BLOCK_COUNT_PER_DAY)).toString(16)}` + const blockHash = (await rpcRequest('get_block_hash', [validTargetBlockNumber])).result + const originEnvContent = fs.readFileSync(envFilePath).toString('utf-8') + fs.writeFileSync(envFilePath, originEnvContent.replace(validTargetReg, `CKB_NODE_ASSUME_VALID_TARGET='${blockHash}'`)) +})()