diff --git a/cmd/jsutils/README.md b/cmd/jsutils/README.md index d60fc0aca612..6c0ed5aa751c 100644 --- a/cmd/jsutils/README.md +++ b/cmd/jsutils/README.md @@ -35,4 +35,13 @@ output as following Get the performance between [ 19470 , 19670 ) txCountPerBlock = 3142.81 txCountTotal = 628562 BlockCount = 200 avgBlockTime = 3.005 inturnBlocksRatio = 0.975 txCountPerSecond = 1045.8602329450914 avgGasUsedPerBlock = 250.02062627 avgGasUsedPerSecond = 83.20153952412646 -``` \ No newline at end of file +``` + +### 4. Get validators slash count +```bash +use the latest block +node getslashcount.js --Rpc ${ArchiveRpc} +use a block number +node getslashcount.js --Rpc ${ArchiveRpc} --Num ${blockNum} +``` + diff --git a/cmd/jsutils/getslashcount.js b/cmd/jsutils/getslashcount.js new file mode 100644 index 000000000000..c057d282b0a9 --- /dev/null +++ b/cmd/jsutils/getslashcount.js @@ -0,0 +1,40 @@ +import { ethers } from "ethers"; +import program from "commander"; + +program.option("--Rpc ", "Rpc"); +program.option("--Num ", "num", 0) +program.parse(process.argv); + +const provider = new ethers.JsonRpcProvider(program.Rpc); + +const slashAbi = [ + "function getSlashIndicator(address validatorAddr) external view returns (uint256, uint256)" +] +const validatorSetAbi = [ + "function getLivingValidators() external view returns (address[], bytes[])" +] +const addrValidatorSet = '0x0000000000000000000000000000000000001000'; +const addrSlash = '0x0000000000000000000000000000000000001001'; +const validatorSet = new ethers.Contract(addrValidatorSet, validatorSetAbi, provider); +const slashIndicator = new ethers.Contract(addrSlash, slashAbi, provider) + + +const main = async () => { + let blockNum = ethers.getNumber(program.Num) + if (blockNum === 0) { + blockNum = await provider.getBlockNumber() + } + let block = await provider.getBlock(blockNum) + console.log("current block", blockNum, "time", block.date) + const data = await validatorSet.getLivingValidators({blockTag:blockNum}) + for (let i = 0; i < data[0].length; i++) { + let addr = data[0][i]; + let info = await slashIndicator.getSlashIndicator(addr, {blockTag:blockNum}) + console.log("index:", i, "address:", addr, "slashes:", ethers.toNumber(info[1])) + } +}; +main().then(() => process.exit(0)) + .catch((error) => { + console.error(error); + process.exit(1); + }); \ No newline at end of file