Skip to content

Commit

Permalink
cmd/jsutils: add a tool for validator version
Browse files Browse the repository at this point in the history
  • Loading branch information
blxdyx authored and zlacfzy committed Dec 7, 2023
1 parent 6c788d7 commit 15bebb3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/jsutils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## 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
mainnet
```bash
npm run startMainnet
```
testnet
```bash
npm run startTestnet
```
25 changes: 25 additions & 0 deletions cmd/jsutils/getvalidatorversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ethers } from "ethers";
import program from "commander";

program.option("--Rpc <Rpc>", "Rpc");
program.option("--Num <Num>", "validator num", 21)
program.parse(process.argv);

const provider = new ethers.JsonRpcProvider(program.Rpc);

const main = async () => {
const blockNum = await provider.getBlockNumber();
console.log(blockNum);
for (let i = 0; i < program.Num; i++) {
let blockData = await provider.getBlock(blockNum - i);
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(blockData.miner, "version =", major + "." + minor + "." + patch)
}
};
main().then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
16 changes: 16 additions & 0 deletions cmd/jsutils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "jsutils",
"version": "1.0.0",
"type": "module",
"description": "jsUtils for bsc",
"main": "index.js",
"scripts": {
"startMainnet": "node getvalidatorversion.js --Rpc https://bsc-dataseed.bnbchain.org --Num 21",
"startTestnet": "node getvalidatorversion.js --Rpc https://bsc-testnet-dataseed.bnbchain.org --Num 7"
},
"dependencies": {
"commander": "^3.0.1",
"ethers": "^6.2.3"
},
"author": "BNB Chain"
}

0 comments on commit 15bebb3

Please sign in to comment.