Skip to content

Commit

Permalink
chore: Add action to update assume valid target (#2773)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
yanguoyu authored Jul 20, 2023
1 parent 54c5f67 commit 034d345
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check-code-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
87 changes: 87 additions & 0 deletions .github/workflows/update_valid_target.yml
Original file line number Diff line number Diff line change
@@ -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}`,
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 34 additions & 0 deletions scripts/update-valid-target.js
Original file line number Diff line number Diff line change
@@ -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}'`))
})()

2 comments on commit 034d345

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 5608858445

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 5608859783

Please sign in to comment.