Skip to content

Commit

Permalink
cmd/jsutils: add a tool to get slash count (#2569)
Browse files Browse the repository at this point in the history
  • Loading branch information
zlacfzy authored Jul 8, 2024
1 parent bc970e5 commit e8456c2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
11 changes: 10 additions & 1 deletion cmd/jsutils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
```
### 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}
```
40 changes: 40 additions & 0 deletions cmd/jsutils/getslashcount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ethers } from "ethers";
import program from "commander";

program.option("--Rpc <Rpc>", "Rpc");
program.option("--Num <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);
});

0 comments on commit e8456c2

Please sign in to comment.