-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
4 changed files
with
124 additions
and
2 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
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,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}`, | ||
}) |
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,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}'`)) | ||
})() |
034d345
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.
Packaging for test is done in 5608858445
034d345
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.
Packaging for test is done in 5608859783