Skip to content

Commit e8456c2

Browse files
authored
cmd/jsutils: add a tool to get slash count (ethereum#2569)
1 parent bc970e5 commit e8456c2

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

cmd/jsutils/README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,13 @@ output as following
3535
Get the performance between [ 19470 , 19670 )
3636
txCountPerBlock = 3142.81 txCountTotal = 628562 BlockCount = 200 avgBlockTime = 3.005 inturnBlocksRatio = 0.975
3737
txCountPerSecond = 1045.8602329450914 avgGasUsedPerBlock = 250.02062627 avgGasUsedPerSecond = 83.20153952412646
38-
```
38+
```
39+
40+
### 4. Get validators slash count
41+
```bash
42+
use the latest block
43+
node getslashcount.js --Rpc ${ArchiveRpc}
44+
use a block number
45+
node getslashcount.js --Rpc ${ArchiveRpc} --Num ${blockNum}
46+
```
47+

cmd/jsutils/getslashcount.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { ethers } from "ethers";
2+
import program from "commander";
3+
4+
program.option("--Rpc <Rpc>", "Rpc");
5+
program.option("--Num <Num>", "num", 0)
6+
program.parse(process.argv);
7+
8+
const provider = new ethers.JsonRpcProvider(program.Rpc);
9+
10+
const slashAbi = [
11+
"function getSlashIndicator(address validatorAddr) external view returns (uint256, uint256)"
12+
]
13+
const validatorSetAbi = [
14+
"function getLivingValidators() external view returns (address[], bytes[])"
15+
]
16+
const addrValidatorSet = '0x0000000000000000000000000000000000001000';
17+
const addrSlash = '0x0000000000000000000000000000000000001001';
18+
const validatorSet = new ethers.Contract(addrValidatorSet, validatorSetAbi, provider);
19+
const slashIndicator = new ethers.Contract(addrSlash, slashAbi, provider)
20+
21+
22+
const main = async () => {
23+
let blockNum = ethers.getNumber(program.Num)
24+
if (blockNum === 0) {
25+
blockNum = await provider.getBlockNumber()
26+
}
27+
let block = await provider.getBlock(blockNum)
28+
console.log("current block", blockNum, "time", block.date)
29+
const data = await validatorSet.getLivingValidators({blockTag:blockNum})
30+
for (let i = 0; i < data[0].length; i++) {
31+
let addr = data[0][i];
32+
let info = await slashIndicator.getSlashIndicator(addr, {blockTag:blockNum})
33+
console.log("index:", i, "address:", addr, "slashes:", ethers.toNumber(info[1]))
34+
}
35+
};
36+
main().then(() => process.exit(0))
37+
.catch((error) => {
38+
console.error(error);
39+
process.exit(1);
40+
});

0 commit comments

Comments
 (0)