From 4aa6e2c1c5361231905fc860caaa91e11d20abfc Mon Sep 17 00:00:00 2001 From: zzphust <1255950918@qq.com> Date: Tue, 5 Dec 2023 17:04:59 +0800 Subject: [PATCH] cmd/jsutils: add a tool for validator version --- cmd/jsutils/README.md | 16 ++++++++++++++++ cmd/jsutils/getvalidatorversion.js | 19 +++++++++++++++++++ cmd/jsutils/package.json | 14 ++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 cmd/jsutils/README.md create mode 100644 cmd/jsutils/getvalidatorversion.js create mode 100644 cmd/jsutils/package.json diff --git a/cmd/jsutils/README.md b/cmd/jsutils/README.md new file mode 100644 index 0000000000..1043f6c658 --- /dev/null +++ b/cmd/jsutils/README.md @@ -0,0 +1,16 @@ +## Requirement + +- nodejs: v20.10.0 +- npm: v10.2.3 + +## Prepare +Recommend use [nvm](https://github.com/nvm-sh/nvm) to manage node version. + +Install node.js dependency: +```shell script + npm install +``` +## Run +```bash + npm run start +``` \ No newline at end of file diff --git a/cmd/jsutils/getvalidatorversion.js b/cmd/jsutils/getvalidatorversion.js new file mode 100644 index 0000000000..408d2e2a8d --- /dev/null +++ b/cmd/jsutils/getvalidatorversion.js @@ -0,0 +1,19 @@ +import { ethers } from "ethers"; +const testNet = "https://bsc-prebsc-dataseed.bnbchain.org" +const mainNet = "https://bsc-dataseed.bnbchain.org" +const provider = new ethers.JsonRpcProvider(mainNet); +const valNum = 21; + +const main = async () => { + const blockNum = await provider.getBlockNumber(); + console.log(blockNum); + for (let i = 0; i < valNum; i++) { + let blockData = await provider.getBlock(blockNum - i); + console.log(blockData.miner); + let major = ethers.toNumber(ethers.dataSlice(blockData.extraData, 2, 3)) + let minor = ethers.toNumber(ethers.dataSlice(blockData.extraData, 3, 4)) + let patch = ethers.toNumber(ethers.dataSlice(blockData.extraData, 4, 5)) + console.log("version = ", major, ".", minor, ".", patch) + } +}; +main(); \ No newline at end of file diff --git a/cmd/jsutils/package.json b/cmd/jsutils/package.json new file mode 100644 index 0000000000..bf94abd27f --- /dev/null +++ b/cmd/jsutils/package.json @@ -0,0 +1,14 @@ +{ + "name": "jsutils", + "version": "1.0.0", + "type": "module", + "description": "jsUtils for bsc", + "main": "index.js", + "scripts": { + "start": "node getvalidatorversion.js" + }, + "dependencies": { + "ethers": "^6.2.3" + }, + "author": "BNB Chain" +}